Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | // This file was automatically generated from coroutines-guide-ui.md by Knit tool. Do not edit. |
Roman Elizarov | a9687a3 | 2018-06-29 17:28:38 +0300 | [diff] [blame] | 6 | package kotlinx.coroutines.experimental.javafx.guide.blocking03 |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 7 | |
| 8 | import kotlinx.coroutines.experimental.* |
| 9 | import kotlinx.coroutines.experimental.channels.* |
| 10 | import kotlinx.coroutines.experimental.javafx.JavaFx as UI |
| 11 | import javafx.application.Application |
| 12 | import javafx.event.EventHandler |
| 13 | import javafx.geometry.* |
| 14 | import javafx.scene.* |
| 15 | import javafx.scene.input.MouseEvent |
| 16 | import javafx.scene.layout.StackPane |
| 17 | import javafx.scene.paint.Color |
| 18 | import javafx.scene.shape.Circle |
| 19 | import javafx.scene.text.Text |
| 20 | import javafx.stage.Stage |
| 21 | |
| 22 | fun main(args: Array<String>) { |
| 23 | Application.launch(ExampleApp::class.java, *args) |
| 24 | } |
| 25 | |
| 26 | class ExampleApp : Application() { |
| 27 | val hello = Text("Hello World!").apply { |
| 28 | fill = Color.valueOf("#C0C0C0") |
| 29 | } |
| 30 | |
| 31 | val fab = Circle(20.0, Color.valueOf("#FF4081")) |
| 32 | |
| 33 | val root = StackPane().apply { |
| 34 | children += hello |
| 35 | children += fab |
| 36 | StackPane.setAlignment(hello, Pos.CENTER) |
| 37 | StackPane.setAlignment(fab, Pos.BOTTOM_RIGHT) |
| 38 | StackPane.setMargin(fab, Insets(15.0)) |
| 39 | } |
| 40 | |
| 41 | val scene = Scene(root, 240.0, 380.0).apply { |
| 42 | fill = Color.valueOf("#303030") |
| 43 | } |
| 44 | |
| 45 | override fun start(stage: Stage) { |
| 46 | stage.title = "Example" |
| 47 | stage.scene = scene |
| 48 | stage.show() |
| 49 | setup(hello, fab) |
| 50 | } |
| 51 | } |
| 52 | |
Roman Elizarov | bf6630f | 2017-03-14 20:06:42 +0300 | [diff] [blame] | 53 | fun Node.onClick(action: suspend (MouseEvent) -> Unit) { |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 54 | val eventActor = actor<MouseEvent>(UI, capacity = Channel.CONFLATED) { |
Roman Elizarov | bf6630f | 2017-03-14 20:06:42 +0300 | [diff] [blame] | 55 | for (event in channel) action(event) // pass event to action |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 56 | } |
| 57 | onMouseClicked = EventHandler { event -> |
| 58 | eventActor.offer(event) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | fun setup(hello: Text, fab: Circle) { |
| 63 | var result = "none" // the last result |
| 64 | // counting animation |
| 65 | launch(UI) { |
| 66 | var counter = 0 |
| 67 | while (true) { |
| 68 | hello.text = "${++counter}: $result" |
| 69 | delay(100) // update the text every 100ms |
| 70 | } |
| 71 | } |
| 72 | // compute next fibonacci number of each click |
| 73 | var x = 1 |
| 74 | fab.onClick { |
| 75 | result = "fib($x) = ${fib(x)}" |
| 76 | x++ |
| 77 | } |
| 78 | } |
| 79 | |
Roman Elizarov | f9e13f5 | 2017-12-21 12:23:15 +0300 | [diff] [blame] | 80 | suspend fun fib(x: Int): Int = withContext(CommonPool) { |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 81 | fibBlocking(x) |
| 82 | } |
| 83 | |
| 84 | fun fibBlocking(x: Int): Int = |
Vixb | f742bc8 | 2018-01-11 16:42:24 +0800 | [diff] [blame] | 85 | if (x <= 1) x else fibBlocking(x - 1) + fibBlocking(x - 2) |