blob: 3ff5d7d5d036f3c36e3769f99fdc1729fe85a694 [file] [log] [blame]
Roman Elizarov23f864e2017-03-03 19:57:47 +03001/*
Roman Elizarov660c2d72020-02-14 13:18:37 +03002 * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarov23f864e2017-03-03 19:57:47 +03003 */
4
5// This file was automatically generated from coroutines-guide-ui.md by Knit tool. Do not edit.
Roman Elizarov660c2d72020-02-14 13:18:37 +03006package kotlinx.coroutines.javafx.guide.exampleUiBlocking03
Roman Elizarov23f864e2017-03-03 19:57:47 +03007
Roman Elizarov0950dfa2018-07-13 10:33:25 +03008import kotlinx.coroutines.*
9import kotlinx.coroutines.channels.*
10import kotlinx.coroutines.javafx.JavaFx as Main
Roman Elizarov23f864e2017-03-03 19:57:47 +030011import javafx.application.Application
12import javafx.event.EventHandler
13import javafx.geometry.*
14import javafx.scene.*
15import javafx.scene.input.MouseEvent
16import javafx.scene.layout.StackPane
17import javafx.scene.paint.Color
18import javafx.scene.shape.Circle
19import javafx.scene.text.Text
20import javafx.stage.Stage
21
22fun main(args: Array<String>) {
23 Application.launch(ExampleApp::class.java, *args)
24}
25
26class 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 Elizarovbf6630f2017-03-14 20:06:42 +030053fun Node.onClick(action: suspend (MouseEvent) -> Unit) {
Vsevolod Tolstopyatov1f7b2d82018-10-09 15:57:51 +030054 val eventActor = GlobalScope.actor<MouseEvent>(Dispatchers.Main, capacity = Channel.CONFLATED) {
Roman Elizarovbf6630f2017-03-14 20:06:42 +030055 for (event in channel) action(event) // pass event to action
Roman Elizarov23f864e2017-03-03 19:57:47 +030056 }
57 onMouseClicked = EventHandler { event ->
Vsevolod Tolstopyatov3c83c0c2021-04-13 16:47:55 +030058 eventActor.trySend(event)
Roman Elizarov23f864e2017-03-03 19:57:47 +030059 }
60}
61
62fun setup(hello: Text, fab: Circle) {
63 var result = "none" // the last result
64 // counting animation
Roman Elizarovdc29b072018-09-11 18:42:03 +030065 GlobalScope.launch(Dispatchers.Main) {
Roman Elizarov23f864e2017-03-03 19:57:47 +030066 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 Elizarovdc29b072018-09-11 18:42:03 +030080suspend fun fib(x: Int): Int = withContext(Dispatchers.Default) {
Roman Elizarov23f864e2017-03-03 19:57:47 +030081 fibBlocking(x)
82}
83
84fun fibBlocking(x: Int): Int =
Vixbf742bc82018-01-11 16:42:24 +080085 if (x <= 1) x else fibBlocking(x - 1) + fibBlocking(x - 2)