Skip to content

Provide a simple redux-like framework for the JavaFx WebView component

License

Notifications You must be signed in to change notification settings

sgdan/webview-redux

Repository files navigation

webview-redux Build Status Download

Provides a simple redux-like framework for the JavaFx WebView component.

See CounterExample.kt which demonstrates basic usage.

Get started

Available from the jcenter repository.

Maven:

<dependency>
  <groupId>com.github.sgdan</groupId>
  <artifactId>webview-redux</artifactId>
  <version>0.0.1</version>
</dependency>

Gradle:

compile 'com.github.sgdan:webview-redux:0.0.1'

Redux class

The Redux.kt class can be instantiated by providing:

  • a WebView component to use as display
  • the initial state: State
  • a view function: (State) -> View
  • an update function: (State, Action) -> State

View function

The view function can use the kotlinx.html DSL to build the view from the state:

fun view(state: Int): Node = createDoc().create.html {
        body {
            h1 { +"Counter" }
            +"Current value: $state"
        }
    }

An Animation timer is used so that the view function won't be used more than once per screen refresh cycle.

Events

The Redux class adds a performAction function to the JavaScript context of the WebView. This allows code in the view function to create named action events with optional parameters:

button {
    +"Clear"
    onClick = "performAction('CLEAR');"
}
button {
    +"Enter"
    onClick = "performAction('ENTER', document.getElementById('input1').value, 'arg');"
}

Events can also be triggered from a background thread or co-routine:

launch {
    while (true) {
        delay(1000)
        redux.perform(Action.TICK)
    }
}

About

Provide a simple redux-like framework for the JavaFx WebView component

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published