forked from androidx/androidx
/
Application.desktop.kt
275 lines (260 loc) · 9.48 KB
/
Application.desktop.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* Copyright 2021 The Android Open Source Project
*
* 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.
*/
@file:OptIn(ExperimentalComposeUiApi::class)
package androidx.compose.ui.window
import androidx.compose.runtime.Applier
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Composition
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MonotonicFrameClock
import androidx.compose.runtime.Recomposer
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.runtime.withFrameNanos
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.awt.ComposePanel
import androidx.compose.ui.configureSwingGlobalsForCompose
import androidx.compose.ui.platform.GlobalSnapshotManager
import androidx.compose.ui.platform.LocalDensity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.coroutines.yield
import org.jetbrains.skiko.MainUIDispatcher
import kotlin.system.exitProcess
/**
* An entry point for the Compose application. See [awaitApplication] for more information.
*
* Usually this entry point is used inside `main()` function:
* ```
* fun main() = application {
*
* }
* ```
*
* After all windows are closed and all operations are completed, the application will end.
* Set [exitProcessOnExit] to `false`, if you need to execute some code after [application] block, otherwise the code after it
* won't be executed, as [application] will exit the process.
*
* This entry point is a blocking operation (it blocks the current thread until application
* finishes) and can't be called inside UI thread. To launch new application from UI thread (for
* example, from some event listener), use `GlobalScope.launchApplication` instead.
*
* Application can launch background tasks using [LaunchedEffect]
* or create [Window], [Dialog], or [Tray] in a declarative Compose way:
*
* ```
* fun main() = application {
* val isSplashScreenShowing by remember { mutableStateOf(true) }
*
* LaunchedEffect(Unit) {
* delay(2000)
* isSplashScreenShowing = false
* }
*
* if (isSplashScreenShowing) {
* Window(title = "Splash") {}
* } else {
* Window(title = "App") {}
* }
* }
* ```
*
* When there is no any active compositions, this function will end.
* Active composition is a composition that have active coroutine (for example, launched in
* [LaunchedEffect]) or that have child composition created inside [Window], [Dialog], or [Tray].
*
* Don't use any animation in this function
* (for example, [withFrameNanos] or [androidx.compose.animation.core.animateFloatAsState]),
* because underlying [MonotonicFrameClock] hasn't synchronized with any display, and produces
* frames as fast as possible.
*
* All animation's should be created inside Composable content of the
* [Window] / [Dialog] / [ComposePanel].
*
* @param exitProcessOnExit should `exitProcess(0)` be called after the application is closed.
* exitProcess speedup process exit (instant instead of 1-4sec).
* If `false`, the execution of the function will be unblocked after application is exited
* (when the last window is closed, and all [LaunchedEffect] are complete).
* @see [awaitApplication]
*/
fun application(
exitProcessOnExit: Boolean = true,
content: @Composable ApplicationScope.() -> Unit
) {
if (System.getProperty("compose.application.configure.swing.globals") == "true") {
configureSwingGlobalsForCompose()
}
runBlocking {
awaitApplication {
content()
}
}
if (exitProcessOnExit) {
exitProcess(0)
}
}
/**
* Short variant of launching application inside [CoroutineScope].
*
* This function is equivalent of:
* ```
* CoroutineScope.launch {
* awaitApplication {
*
* }
* }
* ```
*
* Don't use `GlobalScope.launchApplication {}` to launch application inside `main()` function
* without waiting it to end.
* As it will not block the main thread, and application process will stop
* (because global coroutines are daemon threads, daemon threads don't keep process alive:
* https://kotlinlang.org/docs/coroutines-basics.html#global-coroutines-are-like-daemon-threads)
*
* @see [awaitApplication]
*/
fun CoroutineScope.launchApplication(
content: @Composable ApplicationScope.() -> Unit
): Job {
if (System.getProperty("compose.application.configure.swing.globals") == "true") {
configureSwingGlobalsForCompose()
}
return launch {
awaitApplication(content = content)
}
}
/**
* An entry point for the Compose application.
*
* Application can launch background tasks using [LaunchedEffect]
* or create [Window], [Dialog], or [Tray] in a declarative Compose way:
*
* ```
* fun main() = runBlocking {
* awaitApplication {
* val isSplashScreenShowing by remember { mutableStateOf(true) }
*
* LaunchedEffect(Unit) {
* delay(2000)
* isSplashScreenShowing = false
* }
*
* if (isSplashScreenShowing) {
* Window(title = "Splash") {}
* } else {
* Window(title = "App") {}
* }
* }
* }
* ```
*
* When there is no any active compositions, this function will end.
* Active composition is a composition that have active coroutine (for example, launched in
* [LaunchedEffect]) or that have child composition created inside [Window], [Dialog], or [Tray].
*
* Don't use any animation in this function
* (for example, [withFrameNanos] or [androidx.compose.animation.core.animateFloatAsState]),
* because underlying [MonotonicFrameClock] hasn't synchronized with any display, and produces
* frames as fast as possible.
*
* All animation's should be created inside Composable content of the
* [Window] / [Dialog] / [ComposePanel].
*/
suspend fun awaitApplication(
content: @Composable ApplicationScope.() -> Unit
) {
if (System.getProperty("compose.application.configure.swing.globals") == "true") {
configureSwingGlobalsForCompose()
}
withContext(MainUIDispatcher) {
withContext(YieldFrameClock) {
GlobalSnapshotManager.ensureStarted()
val recomposer = Recomposer(coroutineContext)
var isOpen by mutableStateOf(true)
val applicationScope = object : ApplicationScope {
override fun exitApplication() {
isOpen = false
}
}
launch {
recomposer.runRecomposeAndApplyChanges()
}
launch {
val applier = ApplicationApplier()
val composition = Composition(applier, recomposer)
try {
composition.setContent {
if (isOpen) {
CompositionLocalProvider(
// Resources which are defined at the application level can use
// density to calculate intrinsicSize
LocalDensity provides GlobalDensity
) {
applicationScope.content()
}
}
}
recomposer.close()
recomposer.join()
} finally {
composition.dispose()
}
}
}
}
}
/**
* Scope used by [application], [awaitApplication], [launchApplication]
*/
@Stable
interface ApplicationScope {
/**
* Close all windows created inside the application and cancel all launched effects
* (they launch via [LaunchedEffect] adn [rememberCoroutineScope].
*/
fun exitApplication()
}
private object YieldFrameClock : MonotonicFrameClock {
override suspend fun <R> withFrameNanos(
onFrame: (frameTimeNanos: Long) -> R
): R {
// We call `yield` to avoid blocking UI thread. If we don't call this then application
// can be frozen for the user in some cases as it will not receive any input events.
//
// Swing dispatcher will process all pending events and resume after `yield`.
yield()
return onFrame(System.nanoTime())
}
}
private class ApplicationApplier : Applier<Unit> {
override val current: Unit = Unit
override fun down(node: Unit) = Unit
override fun up() = Unit
override fun insertTopDown(index: Int, instance: Unit) = Unit
override fun insertBottomUp(index: Int, instance: Unit) = Unit
override fun remove(index: Int, count: Int) = Unit
override fun move(from: Int, to: Int, count: Int) = Unit
override fun clear() = Unit
override fun onEndChanges() = Unit
}