| /* |
| * Copyright 2016-2017 JetBrains s.r.o. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| // This file was automatically generated from coroutines-guide-ui.md by Knit tool. Do not edit. |
| package guide.ui.blocking.example03 |
| |
| import kotlinx.coroutines.experimental.* |
| import kotlinx.coroutines.experimental.channels.* |
| import kotlinx.coroutines.experimental.javafx.JavaFx as UI |
| import javafx.application.Application |
| import javafx.event.EventHandler |
| import javafx.geometry.* |
| import javafx.scene.* |
| import javafx.scene.input.MouseEvent |
| import javafx.scene.layout.StackPane |
| import javafx.scene.paint.Color |
| import javafx.scene.shape.Circle |
| import javafx.scene.text.Text |
| import javafx.stage.Stage |
| |
| fun main(args: Array<String>) { |
| Application.launch(ExampleApp::class.java, *args) |
| } |
| |
| class ExampleApp : Application() { |
| val hello = Text("Hello World!").apply { |
| fill = Color.valueOf("#C0C0C0") |
| } |
| |
| val fab = Circle(20.0, Color.valueOf("#FF4081")) |
| |
| val root = StackPane().apply { |
| children += hello |
| children += fab |
| StackPane.setAlignment(hello, Pos.CENTER) |
| StackPane.setAlignment(fab, Pos.BOTTOM_RIGHT) |
| StackPane.setMargin(fab, Insets(15.0)) |
| } |
| |
| val scene = Scene(root, 240.0, 380.0).apply { |
| fill = Color.valueOf("#303030") |
| } |
| |
| override fun start(stage: Stage) { |
| stage.title = "Example" |
| stage.scene = scene |
| stage.show() |
| setup(hello, fab) |
| } |
| } |
| |
| fun Node.onClick(action: suspend (MouseEvent) -> Unit) { |
| val eventActor = actor<MouseEvent>(UI, capacity = Channel.CONFLATED) { |
| for (event in channel) action(event) // pass event to action |
| } |
| onMouseClicked = EventHandler { event -> |
| eventActor.offer(event) |
| } |
| } |
| |
| fun setup(hello: Text, fab: Circle) { |
| var result = "none" // the last result |
| // counting animation |
| launch(UI) { |
| var counter = 0 |
| while (true) { |
| hello.text = "${++counter}: $result" |
| delay(100) // update the text every 100ms |
| } |
| } |
| // compute next fibonacci number of each click |
| var x = 1 |
| fab.onClick { |
| result = "fib($x) = ${fib(x)}" |
| x++ |
| } |
| } |
| |
| suspend fun fib(x: Int): Int = run(CommonPool) { |
| fibBlocking(x) |
| } |
| |
| fun fibBlocking(x: Int): Int = |
| if (x <= 1) 1 else fibBlocking(x - 1) + fibBlocking(x - 2) |