@@ -5,17 +5,78 @@ package com.artemchep.keyguard.ui.theme
55import androidx.compose.material3.ColorScheme
66import androidx.compose.material3.MaterialTheme
77import androidx.compose.runtime.Composable
8- import androidx.compose.runtime.NonRestartableComposable
8+ import androidx.compose.runtime.LaunchedEffect
9+ import androidx.compose.runtime.getValue
10+ import androidx.compose.runtime.mutableStateOf
11+ import androidx.compose.runtime.remember
12+ import androidx.compose.runtime.rememberUpdatedState
13+ import androidx.compose.runtime.setValue
14+ import androidx.compose.ui.graphics.Color
15+ import androidx.lifecycle.Lifecycle
16+ import androidx.lifecycle.compose.LocalLifecycleOwner
17+ import com.artemchep.autotype.getSystemAccentColor as getNativeSystemAccentColor
918import com.artemchep.keyguard.ui.LocalComposeWindow
19+ import com.artemchep.keyguard.ui.theme.m3.dynamicColorScheme
1020import io.github.kdroidfilter.platformtools.darkmodedetector.windows.setWindowsAdaptiveTitleBar
21+ import kotlinx.coroutines.Dispatchers
22+ import kotlinx.coroutines.delay
23+ import kotlinx.coroutines.ensureActive
24+ import kotlinx.coroutines.isActive
25+ import kotlinx.coroutines.withContext
1126
1227@Composable
13- @NonRestartableComposable
14- actual fun appDynamicDarkColorScheme (): ColorScheme = plainDarkColorScheme()
28+ actual fun appDynamicDarkColorScheme (): ColorScheme {
29+ val accentColor = rememberSystemAccentColor()
30+ return remember(accentColor) {
31+ if (accentColor != 0 ) {
32+ dynamicColorScheme(
33+ keyColor = Color (accentColor),
34+ isDark = true ,
35+ )
36+ } else {
37+ plainDarkColorScheme()
38+ }
39+ }
40+ }
41+
42+ @Composable
43+ actual fun appDynamicLightColorScheme (): ColorScheme {
44+ val accentColor = rememberSystemAccentColor()
45+ return remember(accentColor) {
46+ if (accentColor != 0 ) {
47+ dynamicColorScheme(
48+ keyColor = Color (accentColor),
49+ isDark = false ,
50+ )
51+ } else {
52+ plainLightColorScheme()
53+ }
54+ }
55+ }
1556
1657@Composable
17- @NonRestartableComposable
18- actual fun appDynamicLightColorScheme (): ColorScheme = plainLightColorScheme()
58+ private fun rememberSystemAccentColor (): Int {
59+ var accentColor by remember {
60+ mutableStateOf(0 )
61+ }
62+
63+ val updatedLifecycle by rememberUpdatedState(LocalLifecycleOwner .current.lifecycle)
64+ LaunchedEffect (Unit ) {
65+ while (isActive) {
66+ accentColor = withContext(Dispatchers .IO ) {
67+ getNativeSystemAccentColor()
68+ }
69+
70+ // We are potentially spawning processes to check the actual
71+ // accent color, so let's be conservative with the refresh rate.
72+ val delayMs = if (updatedLifecycle.currentState >= Lifecycle .State .RESUMED ) {
73+ 4000L
74+ } else 8000L
75+ delay(delayMs)
76+ }
77+ }
78+ return accentColor
79+ }
1980
2081@Composable
2182actual fun SystemUiThemeEffect () {
0 commit comments