Skip to content

Commit

Permalink
feat:使用compositionlocal管理app设置
Browse files Browse the repository at this point in the history
  • Loading branch information
Yricky committed Aug 30, 2024
1 parent 1542c6d commit f2c7b1b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.coroutines.launch
import me.yricky.abcde.desktop.DesktopUtils
import me.yricky.abcde.ui.ABCDEWindow
import me.yricky.abcde.ui.Icons
import me.yricky.abcde.ui.LocalAppConfig
import me.yricky.abcde.ui.isDarkTheme

@Composable
Expand All @@ -27,6 +28,7 @@ fun SettingsPanel(show:Boolean,onDismiss:()->Unit){
){
Column(Modifier.fillMaxSize().padding(8.dp)) {
OutlinedCard() {
val cfg = LocalAppConfig.current
var newDensity by remember { mutableStateOf(cfg.density) }
val scope = rememberCoroutineScope()
Text("DPI缩放:${String.format("%.02f",newDensity)}", modifier = Modifier.padding(horizontal = 8.dp))
Expand Down
4 changes: 1 addition & 3 deletions abcdecoder/src/jvmMain/kotlin/me/yricky/abcde/ui/theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,7 @@ object Icons{

@Composable
fun isDarkTheme():Boolean{
val cfg by DesktopUtils.AppConfig.flow.collectAsState()
val darkTheme by remember { derivedStateOf { cfg.darkTheme } }
return darkTheme ?: isSystemInDarkTheme()
return LocalAppConfig.current.darkTheme ?: isSystemInDarkTheme()
}


Expand Down
37 changes: 18 additions & 19 deletions abcdecoder/src/jvmMain/kotlin/me/yricky/abcde/ui/windowFrame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ import androidx.compose.ui.window.WindowState
import androidx.compose.ui.window.rememberWindowState
import me.yricky.abcde.desktop.DesktopUtils

val LocalAppConfig = staticCompositionLocalOf { DesktopUtils.AppConfig.flow.value }

class ABCDEWindowScope(
private val frameWindowScope: FrameWindowScope,
private val _cfg:State<DesktopUtils.AppConfig>
):FrameWindowScope by frameWindowScope{
val cfg by _cfg
}
@Composable
fun ABCDEWindow(
Expand Down Expand Up @@ -58,24 +57,24 @@ fun ABCDEWindow(
onPreviewKeyEvent,
onKeyEvent
){
val cfg = DesktopUtils.AppConfig.flow.collectAsState()
val windowScope = remember(this) { ABCDEWindowScope(this,cfg) }
val density by remember { derivedStateOf { cfg.value.density } }
Crossfade(isDarkTheme()) { b ->
MaterialTheme(
colorScheme = if (b) darkColorScheme() else lightColorScheme(),
) {
val bgColor = MaterialTheme.colorScheme.background
LaunchedEffect(null){
window.background = java.awt.Color(bgColor.value.toInt())
}
CompositionLocalProvider(
LocalScrollbarStyle provides LocalScrollbarStyle.current.copy(
unhoverColor = MaterialTheme.colorScheme.tertiary.copy(alpha = 0.2f),
hoverColor = MaterialTheme.colorScheme.tertiary
),
LocalDensity provides Density(density,1f)
val cfg by DesktopUtils.AppConfig.flow.collectAsState()
val windowScope = remember(this) { ABCDEWindowScope(this) }
CompositionLocalProvider(
LocalScrollbarStyle provides LocalScrollbarStyle.current.copy(
unhoverColor = MaterialTheme.colorScheme.tertiary.copy(alpha = 0.2f),
hoverColor = MaterialTheme.colorScheme.tertiary
),
LocalDensity provides Density(cfg.density,1f),
LocalAppConfig provides cfg
) {
Crossfade(isDarkTheme()) { b ->
MaterialTheme(
colorScheme = if (b) darkColorScheme() else lightColorScheme(),
) {
val bgColor = MaterialTheme.colorScheme.background
LaunchedEffect(null){
window.background = java.awt.Color(bgColor.value.toInt())
}
Box(Modifier.background(MaterialTheme.colorScheme.background)) {
windowScope.content()
}
Expand Down

0 comments on commit f2c7b1b

Please sign in to comment.