Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // This file was automatically generated from coroutines-guide-ui.md by Knit tool. Do not edit. |
| 18 | package guide.ui.blocking.example03 |
| 19 | |
| 20 | import kotlinx.coroutines.experimental.* |
| 21 | import kotlinx.coroutines.experimental.channels.* |
| 22 | import kotlinx.coroutines.experimental.javafx.JavaFx as UI |
| 23 | import javafx.application.Application |
| 24 | import javafx.event.EventHandler |
| 25 | import javafx.geometry.* |
| 26 | import javafx.scene.* |
| 27 | import javafx.scene.input.MouseEvent |
| 28 | import javafx.scene.layout.StackPane |
| 29 | import javafx.scene.paint.Color |
| 30 | import javafx.scene.shape.Circle |
| 31 | import javafx.scene.text.Text |
| 32 | import javafx.stage.Stage |
| 33 | |
| 34 | fun main(args: Array<String>) { |
| 35 | Application.launch(ExampleApp::class.java, *args) |
| 36 | } |
| 37 | |
| 38 | class ExampleApp : Application() { |
| 39 | val hello = Text("Hello World!").apply { |
| 40 | fill = Color.valueOf("#C0C0C0") |
| 41 | } |
| 42 | |
| 43 | val fab = Circle(20.0, Color.valueOf("#FF4081")) |
| 44 | |
| 45 | val root = StackPane().apply { |
| 46 | children += hello |
| 47 | children += fab |
| 48 | StackPane.setAlignment(hello, Pos.CENTER) |
| 49 | StackPane.setAlignment(fab, Pos.BOTTOM_RIGHT) |
| 50 | StackPane.setMargin(fab, Insets(15.0)) |
| 51 | } |
| 52 | |
| 53 | val scene = Scene(root, 240.0, 380.0).apply { |
| 54 | fill = Color.valueOf("#303030") |
| 55 | } |
| 56 | |
| 57 | override fun start(stage: Stage) { |
| 58 | stage.title = "Example" |
| 59 | stage.scene = scene |
| 60 | stage.show() |
| 61 | setup(hello, fab) |
| 62 | } |
| 63 | } |
| 64 | |
Roman Elizarov | bf6630f | 2017-03-14 20:06:42 +0300 | [diff] [blame] | 65 | fun Node.onClick(action: suspend (MouseEvent) -> Unit) { |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 66 | val eventActor = actor<MouseEvent>(UI, capacity = Channel.CONFLATED) { |
Roman Elizarov | bf6630f | 2017-03-14 20:06:42 +0300 | [diff] [blame] | 67 | for (event in channel) action(event) // pass event to action |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 68 | } |
| 69 | onMouseClicked = EventHandler { event -> |
| 70 | eventActor.offer(event) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | fun setup(hello: Text, fab: Circle) { |
| 75 | var result = "none" // the last result |
| 76 | // counting animation |
| 77 | launch(UI) { |
| 78 | var counter = 0 |
| 79 | while (true) { |
| 80 | hello.text = "${++counter}: $result" |
| 81 | delay(100) // update the text every 100ms |
| 82 | } |
| 83 | } |
| 84 | // compute next fibonacci number of each click |
| 85 | var x = 1 |
| 86 | fab.onClick { |
| 87 | result = "fib($x) = ${fib(x)}" |
| 88 | x++ |
| 89 | } |
| 90 | } |
| 91 | |
Roman Elizarov | f9e13f5 | 2017-12-21 12:23:15 +0300 | [diff] [blame] | 92 | suspend fun fib(x: Int): Int = withContext(CommonPool) { |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 93 | fibBlocking(x) |
| 94 | } |
| 95 | |
| 96 | fun fibBlocking(x: Int): Int = |
Vixb | f742bc8 | 2018-01-11 16:42:24 +0800 | [diff] [blame^] | 97 | if (x <= 1) x else fibBlocking(x - 1) + fibBlocking(x - 2) |