Skip to content

Commit 5218cf3

Browse files
committed
delete zip-preview.md; refine color transition animations; add theme persistence in localStorage; enhance theme toggle design with animated visuals.
1 parent 7524310 commit 5218cf3

File tree

4 files changed

+194
-277
lines changed

4 files changed

+194
-277
lines changed

.junie/zip-preview.md

Lines changed: 0 additions & 230 deletions
This file was deleted.

composeApp/src/wasmJsMain/kotlin/love/forte/simbot/codegen/App.kt

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,61 @@ import androidx.compose.ui.Modifier
99
import androidx.compose.ui.graphics.Color
1010
import androidx.compose.ui.text.font.FontFamily
1111
import androidx.compose.ui.unit.dp
12+
import kotlinx.browser.localStorage
1213
import love.forte.simbot.codegen.gen.view.GradleSettingsView
1314
import org.jetbrains.compose.resources.ExperimentalResourceApi
1415
import org.jetbrains.compose.resources.preloadFont
1516
import simbot_codegen.composeapp.generated.resources.LXGWNeoXiHeiScreen
1617
import simbot_codegen.composeapp.generated.resources.Res
18+
import org.w3c.dom.get
19+
import org.w3c.dom.set
20+
import web.console.console
21+
22+
private const val THEME_PREFERENCE_KEY = "simbot_codegen_theme_preference"
23+
24+
/**
25+
* 从本地存储加载主题偏好设置
26+
*/
27+
private fun loadThemePreference(): ColorMode {
28+
return try {
29+
val savedTheme = localStorage[THEME_PREFERENCE_KEY]
30+
when (savedTheme) {
31+
"DARK" -> ColorMode.DARK
32+
"LIGHT" -> ColorMode.LIGHT
33+
else -> ColorMode.LIGHT // 默认亮色主题
34+
}
35+
} catch (e: Exception) {
36+
ColorMode.LIGHT // 发生错误时使用默认主题
37+
}
38+
}
39+
40+
/**
41+
* 保存主题偏好设置到本地存储
42+
*/
43+
private fun saveThemePreference(colorMode: ColorMode) {
44+
try {
45+
localStorage[THEME_PREFERENCE_KEY] = colorMode.name
46+
} catch (e: Exception) {
47+
// 保存失败时静默处理,不影响用户体验
48+
}
49+
}
1750

1851
@Composable
1952
fun App() {
2053
@OptIn(ExperimentalResourceApi::class)
2154
val lxgwNeo by preloadFont(resource = Res.font.LXGWNeoXiHeiScreen)
2255
val fm = lxgwNeo?.let { FontFamily(it) }
2356

24-
// 创建主题状态管理
25-
var colorMode by remember { mutableStateOf(ColorMode.LIGHT) }
57+
// 创建主题状态管理,从本地存储加载保存的主题偏好
58+
var colorMode by remember { mutableStateOf(loadThemePreference()) }
2659
val appContext = remember(colorMode) {
2760
AppContext(
2861
colorMode = colorMode,
29-
toggleColorMode = { colorMode = colorMode.toggle() }
62+
toggleColorMode = {
63+
val newColorMode = colorMode.toggle()
64+
colorMode = newColorMode
65+
saveThemePreference(newColorMode)
66+
}
3067
)
3168
}
3269

composeApp/src/wasmJsMain/kotlin/love/forte/simbot/codegen/AppContext.kt

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package love.forte.simbot.codegen
22

33
import androidx.compose.animation.animateColorAsState
4-
import androidx.compose.animation.core.tween
4+
import androidx.compose.animation.core.spring
5+
import androidx.compose.animation.core.Spring
56
import androidx.compose.material3.*
67
import androidx.compose.runtime.*
78
import androidx.compose.ui.graphics.Color
@@ -81,9 +82,11 @@ fun rememberAnimatedColorScheme(colorMode: ColorMode): ColorScheme {
8182
ColorMode.DARK -> darkColors
8283
}
8384

84-
// 为每个颜色添加动画过渡
85-
val animationDuration = 300
86-
val animationSpec = tween<Color>(durationMillis = animationDuration)
85+
// 为每个颜色添加动画过渡 - 使用弹性动画实现更自然的过渡效果
86+
val animationSpec = spring<Color>(
87+
dampingRatio = Spring.DampingRatioMediumBouncy,
88+
stiffness = Spring.StiffnessMedium
89+
)
8790

8891
val animatedPrimary by animateColorAsState(targetColors.primary, animationSpec, label = "primary")
8992
val animatedSecondary by animateColorAsState(targetColors.secondary, animationSpec, label = "secondary")
@@ -100,36 +103,53 @@ fun rememberAnimatedColorScheme(colorMode: ColorMode): ColorScheme {
100103
val animatedPrimaryContainer by animateColorAsState(targetColors.primaryContainer, animationSpec, label = "primaryContainer")
101104
val animatedSecondaryContainer by animateColorAsState(targetColors.secondaryContainer, animationSpec, label = "secondaryContainer")
102105

106+
// 为剩余的颜色属性添加动画
107+
val animatedOnPrimaryContainer by animateColorAsState(targetColors.onPrimaryContainer, animationSpec, label = "onPrimaryContainer")
108+
val animatedInversePrimary by animateColorAsState(targetColors.inversePrimary, animationSpec, label = "inversePrimary")
109+
val animatedOnSecondaryContainer by animateColorAsState(targetColors.onSecondaryContainer, animationSpec, label = "onSecondaryContainer")
110+
val animatedTertiaryContainer by animateColorAsState(targetColors.tertiaryContainer, animationSpec, label = "tertiaryContainer")
111+
val animatedOnTertiaryContainer by animateColorAsState(targetColors.onTertiaryContainer, animationSpec, label = "onTertiaryContainer")
112+
val animatedOnSurfaceVariant by animateColorAsState(targetColors.onSurfaceVariant, animationSpec, label = "onSurfaceVariant")
113+
val animatedSurfaceTint by animateColorAsState(targetColors.surfaceTint, animationSpec, label = "surfaceTint")
114+
val animatedInverseSurface by animateColorAsState(targetColors.inverseSurface, animationSpec, label = "inverseSurface")
115+
val animatedInverseOnSurface by animateColorAsState(targetColors.inverseOnSurface, animationSpec, label = "inverseOnSurface")
116+
val animatedOnError by animateColorAsState(targetColors.onError, animationSpec, label = "onError")
117+
val animatedErrorContainer by animateColorAsState(targetColors.errorContainer, animationSpec, label = "errorContainer")
118+
val animatedOnErrorContainer by animateColorAsState(targetColors.onErrorContainer, animationSpec, label = "onErrorContainer")
119+
val animatedOutline by animateColorAsState(targetColors.outline, animationSpec, label = "outline")
120+
val animatedOutlineVariant by animateColorAsState(targetColors.outlineVariant, animationSpec, label = "outlineVariant")
121+
val animatedScrim by animateColorAsState(targetColors.scrim, animationSpec, label = "scrim")
122+
103123
return ColorScheme(
104124
primary = animatedPrimary,
105125
onPrimary = animatedOnPrimary,
106126
primaryContainer = animatedPrimaryContainer,
107-
onPrimaryContainer = targetColors.onPrimaryContainer,
108-
inversePrimary = targetColors.inversePrimary,
127+
onPrimaryContainer = animatedOnPrimaryContainer,
128+
inversePrimary = animatedInversePrimary,
109129
secondary = animatedSecondary,
110130
onSecondary = animatedOnSecondary,
111131
secondaryContainer = animatedSecondaryContainer,
112-
onSecondaryContainer = targetColors.onSecondaryContainer,
132+
onSecondaryContainer = animatedOnSecondaryContainer,
113133
tertiary = animatedTertiary,
114134
onTertiary = animatedOnTertiary,
115-
tertiaryContainer = targetColors.tertiaryContainer,
116-
onTertiaryContainer = targetColors.onTertiaryContainer,
135+
tertiaryContainer = animatedTertiaryContainer,
136+
onTertiaryContainer = animatedOnTertiaryContainer,
117137
background = animatedBackground,
118138
onBackground = animatedOnBackground,
119139
surface = animatedSurface,
120140
onSurface = animatedOnSurface,
121141
surfaceVariant = animatedSurfaceVariant,
122-
onSurfaceVariant = targetColors.onSurfaceVariant,
123-
surfaceTint = targetColors.surfaceTint,
124-
inverseSurface = targetColors.inverseSurface,
125-
inverseOnSurface = targetColors.inverseOnSurface,
142+
onSurfaceVariant = animatedOnSurfaceVariant,
143+
surfaceTint = animatedSurfaceTint,
144+
inverseSurface = animatedInverseSurface,
145+
inverseOnSurface = animatedInverseOnSurface,
126146
error = animatedError,
127-
onError = targetColors.onError,
128-
errorContainer = targetColors.errorContainer,
129-
onErrorContainer = targetColors.onErrorContainer,
130-
outline = targetColors.outline,
131-
outlineVariant = targetColors.outlineVariant,
132-
scrim = targetColors.scrim,
147+
onError = animatedOnError,
148+
errorContainer = animatedErrorContainer,
149+
onErrorContainer = animatedOnErrorContainer,
150+
outline = animatedOutline,
151+
outlineVariant = animatedOutlineVariant,
152+
scrim = animatedScrim,
133153
surfaceBright = targetColors.surfaceBright,
134154
surfaceDim = targetColors.surfaceDim,
135155
surfaceContainer = targetColors.surfaceContainer,

0 commit comments

Comments
 (0)