Skip to content

Commit 086db34

Browse files
committed
improvement(Windows,UI): Change the Window title basing on the App theme
1 parent 0a36057 commit 086db34

File tree

5 files changed

+128
-38
lines changed

5 files changed

+128
-38
lines changed

common/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ kotlin {
136136
api(libs.devsrsouza.feather)
137137
api(libs.mm2d.touchicon)
138138
api(libs.ksoup.html)
139+
api(libs.kdroidfilter.platformtools.darkmodedetector)
139140
}
140141
}
141142
commonTest.dependencies {

common/src/commonMain/kotlin/com/artemchep/keyguard/ui/theme/Theme.kt

Lines changed: 85 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import androidx.compose.material3.lightColorScheme
1111
import androidx.compose.runtime.Composable
1212
import androidx.compose.runtime.CompositionLocalProvider
1313
import androidx.compose.runtime.ReadOnlyComposable
14+
import androidx.compose.runtime.State
1415
import androidx.compose.runtime.collectAsState
1516
import androidx.compose.runtime.getValue
1617
import androidx.compose.runtime.remember
18+
import androidx.compose.runtime.rememberUpdatedState
1719
import androidx.compose.ui.graphics.Color
1820
import androidx.compose.ui.graphics.compositeOver
1921
import androidx.compose.ui.graphics.luminance
@@ -31,8 +33,10 @@ import com.artemchep.keyguard.platform.theme.hasDarkThemeEnabled
3133
import com.artemchep.keyguard.res.Res
3234
import com.artemchep.keyguard.res.*
3335
import com.artemchep.keyguard.ui.theme.m3.dynamicColorScheme
36+
import kotlinx.coroutines.flow.combine
3437
import org.jetbrains.compose.resources.Font
3538
import org.kodein.di.compose.rememberInstance
39+
import kotlin.Boolean
3640

3741
val ColorScheme.selectedContainer
3842
@ReadOnlyComposable
@@ -259,32 +263,33 @@ val robotoSansFontFamily: FontFamily
259263
return FontFamily(Font(res))
260264
}
261265

266+
267+
@Composable
268+
fun KeyguardTheme(
269+
content: @Composable () -> Unit,
270+
) {
271+
val theme = rememberThemeConfigState()
272+
val dark = rememberThemeDarkState(
273+
config = theme.value,
274+
)
275+
KeyguardTheme(
276+
config = theme.value,
277+
dark = dark.value,
278+
content = content,
279+
)
280+
}
281+
262282
@OptIn(ExperimentalMaterial3ExpressiveApi::class)
263283
@Composable
264284
fun KeyguardTheme(
285+
config: ThemeConfig,
286+
dark: Boolean,
265287
content: @Composable () -> Unit,
266288
) {
267-
val getTheme by rememberInstance<GetTheme>()
268-
val getThemeUseAmoledDark by rememberInstance<GetThemeUseAmoledDark>()
269-
val getThemeExpressive by rememberInstance<GetThemeExpressive>()
270-
val getColors by rememberInstance<GetColors>()
271-
val expressive by remember(getThemeExpressive) {
272-
getThemeExpressive()
273-
}.collectAsState(true)
274-
val theme by remember(getTheme) {
275-
getTheme()
276-
}.collectAsState(initial = null)
277-
val themeBlack by remember(getThemeUseAmoledDark) {
278-
getThemeUseAmoledDark()
279-
}.collectAsState(initial = false)
280-
val colors by remember(getColors) {
281-
getColors()
282-
}.collectAsState(initial = null)
283-
val isDarkColorScheme = when (theme) {
284-
AppTheme.DARK -> true
285-
AppTheme.LIGHT -> false
286-
null -> CurrentPlatform.hasDarkThemeEnabled()
287-
}
289+
val expressive = config.expressive
290+
val themeBlack = config.useAmoledBlack
291+
val colors = config.colors
292+
val isDarkColorScheme = dark
288293

289294
val scheme = kotlin.run {
290295
val scheme = appColorScheme(
@@ -360,6 +365,65 @@ fun KeyguardTheme(
360365
}
361366
}
362367

368+
data class ThemeConfig(
369+
val expressive: Boolean,
370+
val colors: AppColors?,
371+
val mode: AppTheme?,
372+
val useAmoledBlack: Boolean,
373+
)
374+
375+
@Composable
376+
fun rememberThemeConfigState(): State<ThemeConfig> {
377+
val getTheme by rememberInstance<GetTheme>()
378+
val getThemeUseAmoledDark by rememberInstance<GetThemeUseAmoledDark>()
379+
val getThemeExpressive by rememberInstance<GetThemeExpressive>()
380+
val getColors by rememberInstance<GetColors>()
381+
return remember(
382+
getTheme,
383+
getThemeUseAmoledDark,
384+
getThemeExpressive,
385+
getColors,
386+
) {
387+
val themeExpressiveFlow = getThemeExpressive()
388+
val themeColorsFlow = getColors()
389+
val themeModeFlow = getTheme()
390+
val themeModeAmoledBlackFlow = getThemeUseAmoledDark()
391+
combine(
392+
themeExpressiveFlow,
393+
themeColorsFlow,
394+
themeModeFlow,
395+
themeModeAmoledBlackFlow,
396+
) { expressive, colors, mode, modeAmoledBlack ->
397+
ThemeConfig(
398+
expressive = expressive,
399+
colors = colors,
400+
mode = mode,
401+
useAmoledBlack = modeAmoledBlack,
402+
)
403+
}
404+
}.collectAsState(
405+
// use sane defaults
406+
initial = ThemeConfig(
407+
expressive = true,
408+
colors = null,
409+
mode = null,
410+
useAmoledBlack = false,
411+
)
412+
)
413+
}
414+
415+
@Composable
416+
fun rememberThemeDarkState(
417+
config: ThemeConfig,
418+
): State<Boolean> {
419+
val dark = when (config.mode) {
420+
AppTheme.DARK -> true
421+
AppTheme.LIGHT -> false
422+
null -> CurrentPlatform.hasDarkThemeEnabled()
423+
}
424+
return rememberUpdatedState(dark)
425+
}
426+
363427
@Composable
364428
private fun appColorScheme(
365429
colors: AppColors?,

common/src/desktopMain/kotlin/com/artemchep/keyguard/ui/theme/Theme.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
package com.artemchep.keyguard.ui.theme
44

55
import androidx.compose.material3.ColorScheme
6+
import androidx.compose.material3.MaterialTheme
67
import androidx.compose.runtime.Composable
78
import androidx.compose.runtime.NonRestartableComposable
9+
import com.artemchep.keyguard.ui.LocalComposeWindow
10+
import io.github.kdroidfilter.platformtools.darkmodedetector.windows.setWindowsAdaptiveTitleBar
811

912
@Composable
1013
@NonRestartableComposable
@@ -16,5 +19,6 @@ actual fun appDynamicLightColorScheme(): ColorScheme = plainLightColorScheme()
1619

1720
@Composable
1821
actual fun SystemUiThemeEffect() {
19-
// Do nothing.
22+
val dark = MaterialTheme.colorScheme.isDark
23+
LocalComposeWindow.current.setWindowsAdaptiveTitleBar(dark)
2024
}

desktopApp/src/jvmMain/kotlin/com/artemchep/keyguard/Main.kt

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import androidx.compose.runtime.remember
1212
import androidx.compose.ui.Modifier
1313
import androidx.compose.ui.semantics.semantics
1414
import androidx.compose.ui.window.ApplicationScope
15+
import androidx.compose.ui.window.FrameWindowScope
1516
import androidx.compose.ui.window.Tray
1617
import androidx.compose.ui.window.Window
1718
import androidx.compose.ui.window.application
@@ -50,7 +51,6 @@ import com.artemchep.keyguard.desktop.util.navigateToEmail
5051
import com.artemchep.keyguard.desktop.util.navigateToFile
5152
import com.artemchep.keyguard.desktop.util.navigateToFileInFileManager
5253
import com.artemchep.keyguard.feature.favicon.Favicon
53-
import com.artemchep.keyguard.feature.favicon.FaviconUrl
5454
import com.artemchep.keyguard.feature.keyguard.AppRoute
5555
import com.artemchep.keyguard.feature.navigation.LocalNavigationBackHandler
5656
import com.artemchep.keyguard.feature.navigation.NavigationController
@@ -69,7 +69,6 @@ import com.artemchep.keyguard.ui.LocalComposeWindow
6969
import com.artemchep.keyguard.ui.surface.LocalBackgroundManager
7070
import com.artemchep.keyguard.ui.surface.LocalSurfaceColor
7171
import com.artemchep.keyguard.ui.theme.KeyguardTheme
72-
import io.ktor.http.Url
7372
import kotlinx.coroutines.GlobalScope
7473
import kotlinx.coroutines.coroutineScope
7574
import kotlinx.coroutines.flow.MutableStateFlow
@@ -310,21 +309,11 @@ private fun ApplicationScope.KeyguardWindow(
310309
windowStateManager: WindowStateManager,
311310
onCloseRequest: () -> Unit,
312311
) {
313-
val windowState = windowStateManager.rememberWindowState()
314-
val keyboardShortcutsService by rememberInstance<KeyboardShortcutsService>()
315-
Window(
312+
KeyguardWindow(
313+
processLifecycleProvider = processLifecycleProvider,
314+
windowStateManager = windowStateManager,
316315
onCloseRequest = onCloseRequest,
317-
icon = painterResource(Res.drawable.ic_keyguard),
318-
state = windowState,
319-
title = "Keyguard",
320-
onKeyEvent = { event ->
321-
keyboardShortcutsService.handle(event)
322-
},
323316
) {
324-
LaunchLifecycleProviderEffect(
325-
processLifecycleProvider = processLifecycleProvider,
326-
)
327-
328317
KeyguardTheme {
329318
val containerColor = LocalBackgroundManager.current.colorHighest
330319
val containerColorAnimatedState = animateColorAsState(containerColor)
@@ -341,7 +330,6 @@ private fun ApplicationScope.KeyguardWindow(
341330
) {
342331
CompositionLocalProvider(
343332
LocalSurfaceColor provides containerColor,
344-
LocalComposeWindow provides this.window,
345333
) {
346334
Navigation(
347335
exitApplication = ::exitApplication,
@@ -354,6 +342,36 @@ private fun ApplicationScope.KeyguardWindow(
354342
}
355343
}
356344

345+
@Composable
346+
private fun ApplicationScope.KeyguardWindow(
347+
processLifecycleProvider: LePlatformLifecycleProvider,
348+
windowStateManager: WindowStateManager,
349+
onCloseRequest: () -> Unit,
350+
content: @Composable FrameWindowScope.() -> Unit,
351+
) {
352+
val windowState = windowStateManager.rememberWindowState()
353+
val keyboardShortcutsService by rememberInstance<KeyboardShortcutsService>()
354+
Window(
355+
onCloseRequest = onCloseRequest,
356+
icon = painterResource(Res.drawable.ic_keyguard),
357+
state = windowState,
358+
title = "Keyguard",
359+
onKeyEvent = { event ->
360+
keyboardShortcutsService.handle(event)
361+
},
362+
) {
363+
CompositionLocalProvider(
364+
LocalComposeWindow provides this.window,
365+
) {
366+
LaunchLifecycleProviderEffect(
367+
processLifecycleProvider = processLifecycleProvider,
368+
)
369+
370+
content()
371+
}
372+
}
373+
}
374+
357375
@Composable
358376
private fun Content() {
359377
CompositionLocalProvider(

gradle/libs.versions.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ kotpass = "0.13.0"
108108
ksoup = "0.6.0"
109109
# https://github.com/java-native-access/jna
110110
jna = "5.18.1"
111+
# https://github.com/kdroidFilter/Platform-Tool
112+
platformtools = "0.7.4"
111113
# https://github.com/osipxd/encrypted-datastore
112114
osipxdCryptoDatastore = "1.1.1-beta03"
113115
# https://github.com/cashapp/licensee
@@ -276,6 +278,7 @@ ktor-ktor-serialization-kotlinx = { module = "io.ktor:ktor-serialization-kotlinx
276278
keemobile-kotpass = { module = "app.keemobile:kotpass", version.ref = "kotpass" }
277279
ksoup-html = { module = "com.mohamedrejeb.ksoup:ksoup-html", version.ref = "ksoup" }
278280
java-jna = { module = "net.java.dev.jna:jna", version.ref = "jna" }
281+
kdroidfilter-platformtools-darkmodedetector = { module = "io.github.kdroidfilter:platformtools.darkmodedetector", version.ref = "platformtools" }
279282
osipxd-security-crypto-datastore-preferences = { module = "io.github.osipxd:security-crypto-datastore-preferences", version.ref = "osipxdCryptoDatastore" }
280283
bouncycastle-bcpkix = { module = "org.bouncycastle:bcpkix-jdk18on", version.ref = "bouncycastle" }
281284
bouncycastle-bcprov = { module = "org.bouncycastle:bcprov-jdk18on", version.ref = "bouncycastle" }

0 commit comments

Comments
 (0)