Skip to content

Commit 9290fe9

Browse files
authored
Refactor splash screen launch logic in Start.kt (#1357)
Simplifies the splash screen logic by removing coroutine delays and instead using a LaunchedEffect to trigger Base.main after the splash animation completes. The splash window now closes automatically when a new window is opened, improving startup flow and reliability.
1 parent 26c53c9 commit 9290fe9

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

app/src/processing/app/ui/Start.kt

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ import androidx.compose.ui.Modifier
1414
import androidx.compose.ui.draw.clip
1515
import androidx.compose.ui.res.painterResource
1616
import androidx.compose.ui.unit.dp
17-
import androidx.compose.ui.window.*
18-
import kotlinx.coroutines.delay
19-
import kotlinx.coroutines.launch
17+
import androidx.compose.ui.window.Window
18+
import androidx.compose.ui.window.WindowPosition
19+
import androidx.compose.ui.window.application
20+
import androidx.compose.ui.window.rememberWindowState
2021
import processing.app.Base
22+
import java.awt.AWTEvent
23+
import java.awt.event.WindowEvent
24+
2125

2226
/**
2327
* Show a splash screen window. A rewrite of Splash.java
@@ -27,8 +31,6 @@ class Start {
2731
@JvmStatic
2832
fun main(args: Array<String>) {
2933
val duration = 200
30-
val timeMargin = 50
31-
3234
application {
3335
var starting by remember { mutableStateOf(true) }
3436
Window(
@@ -44,24 +46,10 @@ class Start {
4446
)
4547
) {
4648
var visible by remember { mutableStateOf(false) }
47-
val composition = rememberCoroutineScope()
49+
var launched by remember { mutableStateOf(false) }
4850
LaunchedEffect(Unit) {
4951
Toolkit.setIcon(window)
50-
5152
visible = true
52-
composition.launch {
53-
delay(duration.toLong() + timeMargin)
54-
try {
55-
Base.main(args)
56-
} catch (e: Exception) {
57-
throw InternalError("Failed to invoke main method", e)
58-
}
59-
composition.launch {
60-
visible = false
61-
delay(duration.toLong() + timeMargin)
62-
starting = false
63-
}
64-
}
6553
}
6654
AnimatedVisibility(
6755
visible = visible,
@@ -76,8 +64,23 @@ class Start {
7664
durationMillis = duration,
7765
easing = LinearEasing
7866
)
79-
)
67+
),
8068
) {
69+
LaunchedEffect(visible, transition.currentState) {
70+
if (launched) return@LaunchedEffect
71+
if (!visible) return@LaunchedEffect
72+
// Wait until the view is no longer transitioning
73+
if (transition.targetState != transition.currentState) return@LaunchedEffect
74+
launched = true
75+
Base.main(args)
76+
// List for any new windows opening, and close the splash when one does
77+
java.awt.Toolkit.getDefaultToolkit()
78+
.addAWTEventListener({ event ->
79+
if (event.id != WindowEvent.WINDOW_OPENED) return@addAWTEventListener
80+
81+
visible = false
82+
}, AWTEvent.WINDOW_EVENT_MASK);
83+
}
8184
Image(
8285
painter = painterResource("about-processing.svg"),
8386
contentDescription = "About",

0 commit comments

Comments
 (0)