Giter VIP home page Giter VIP logo

swingflow's Introduction

SwingFlow

Maven Central Apache license

Makes binding Kotlin Flows to Java Swing components easier!

To bind a simple String Flow to the text property of a JLabel we need to collect the latest value. Unfortunately, collecting a flow suspends execution. So we have to launch a coroutine as long as we want to update the label. For example as long it's attached to a parent or as long it's visible.

SwingFlow allows to bind your observable data in a syntax, which looks like the normal (non-reactive) calls to set the desired properties with the ability to specify a custom lifecycle.

val greeting = flowOf("Hello SwingFlow!")

val header = JLabel() binds {
    ::setText { greeting }
}

Calling methods this way, results in a one-directional binding. To bind a property ( typically bi-directional ).

val header = JLabel() binds {
    ::text { greeting }
}

There is also a way to bind the property in a one-directional manner ( the recommended Section - Syntax ). Currently only set is supported.

val header = JLabel() binds {
    ::text::
    set { greeting }
}

You can use all the intermediate flow operations inside these bindings.

val countDown =
    (10 downTo 0)
    .asFlow()
    .onEach { delay(1000) }

val number = JLabel() binds {
    ::text::
    set {
        countDown.map(Int::toString)
    }
}

As soon as get is supported, one can do the same in the other direction as well.

val name = MutableStateFlow("SwingFlow")

val header = JLabel() binds {
    ::text::
    set {
        name.map { "Hello $it!" }
    }
}

val yourName = JTextField() binds {
    ::text::
    get { name }
}

If you don't specify any lifecycle when calling binds, the ParentLifecycle is used. Lifecycle and binding-logic are now decoupled from each other. So when changing the lifecycle later on, there's only this one place to consider. This way, a Swing component is not responsible for things related to the bindings lifecycle management.

Lifecycles

Predefined lifecycles

  • ParentLifecycle

swingflow's People

Contributors

merlinths avatar

Stargazers

Zane XiaoYe avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.