Skip to content

Commit 6fa2e4e

Browse files
committed
⚗️ Add lag simulation feature with toggle switch
1 parent 0d4a4b1 commit 6fa2e4e

File tree

1 file changed

+31
-0
lines changed
  • sample/composeApp/src/commonMain/kotlin/io/github/vinceglb/confettikit/sample

1 file changed

+31
-0
lines changed

sample/composeApp/src/commonMain/kotlin/io/github/vinceglb/confettikit/sample/App.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,43 @@ import androidx.compose.foundation.layout.Arrangement
44
import androidx.compose.foundation.layout.Box
55
import androidx.compose.foundation.layout.Column
66
import androidx.compose.foundation.layout.fillMaxSize
7+
import androidx.compose.foundation.layout.Row
78
import androidx.compose.material3.Button
89
import androidx.compose.material3.Scaffold
10+
import androidx.compose.material3.Switch
911
import androidx.compose.material3.Text
1012
import androidx.compose.runtime.Composable
13+
import androidx.compose.runtime.LaunchedEffect
1114
import androidx.compose.runtime.getValue
1215
import androidx.compose.runtime.mutableStateOf
1316
import androidx.compose.runtime.remember
1417
import androidx.compose.runtime.setValue
1518
import androidx.compose.ui.Alignment
1619
import androidx.compose.ui.Modifier
1720
import androidx.compose.ui.unit.dp
21+
import androidx.compose.animation.core.withInfiniteAnimationFrameMillis
1822
import io.github.vinceglb.confettikit.compose.ConfettiKit
23+
import kotlin.time.Duration.Companion.milliseconds
24+
import kotlin.time.TimeSource
1925

2026
@Composable
2127
fun App() {
2228
var isAnimating by remember { mutableStateOf(false) }
2329
var index by remember { mutableStateOf(0) }
30+
var simulateLag by remember { mutableStateOf(false) }
31+
32+
if (simulateLag) {
33+
LaunchedEffect(Unit) {
34+
while (true) {
35+
withInfiniteAnimationFrameMillis {
36+
val start = TimeSource.Monotonic.markNow()
37+
while (start.elapsedNow() < 40.milliseconds) {
38+
// Busy-loop to simulate main-thread jank
39+
}
40+
}
41+
}
42+
}
43+
}
2444

2545
Scaffold {
2646
Box(
@@ -39,6 +59,17 @@ fun App() {
3959
) {
4060
Text("Fiesta 🥳")
4161
}
62+
63+
Row(
64+
verticalAlignment = Alignment.CenterVertically,
65+
horizontalArrangement = Arrangement.spacedBy(8.dp),
66+
) {
67+
Text("Simulate lag")
68+
Switch(
69+
checked = simulateLag,
70+
onCheckedChange = { simulateLag = it },
71+
)
72+
}
4273
}
4374

4475
if (isAnimating) {

0 commit comments

Comments
 (0)