|
| 1 | +package love.forte.simbot.codegen |
| 2 | + |
| 3 | +import androidx.compose.animation.animateColorAsState |
| 4 | +import androidx.compose.animation.core.tween |
| 5 | +import androidx.compose.material3.* |
| 6 | +import androidx.compose.runtime.* |
| 7 | +import androidx.compose.ui.graphics.Color |
| 8 | + |
| 9 | +/** |
| 10 | + * 应用上下文数据类,包含全局应用状态 |
| 11 | + */ |
| 12 | +data class AppContext( |
| 13 | + val colorMode: ColorMode, |
| 14 | + val toggleColorMode: () -> Unit |
| 15 | +) |
| 16 | + |
| 17 | +/** |
| 18 | + * 用于提供全局应用上下文的 CompositionLocal |
| 19 | + */ |
| 20 | +val LocalAppContext = staticCompositionLocalOf<AppContext> { |
| 21 | + error("LocalAppContext not provided") |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * 颜色模式枚举 |
| 26 | + */ |
| 27 | +enum class ColorMode { |
| 28 | + DARK, |
| 29 | + LIGHT; |
| 30 | + |
| 31 | + fun toggle(): ColorMode = when (this) { |
| 32 | + DARK -> LIGHT |
| 33 | + LIGHT -> DARK |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// 定义基础颜色 |
| 38 | +val lightColors = lightColorScheme( |
| 39 | + primary = Color(0xFF3B7DD8), // 优雅的蓝色 |
| 40 | + secondary = Color(0xFF4DB6A5), // 平衡的绿松石色 |
| 41 | + tertiary = Color(0xFFEF9265), // 温和的橙色 |
| 42 | + background = Color(0xFFF8F8F8), // 温和的白色背景 |
| 43 | + surface = Color(0xFFFFFFFF), // 纯白色表面 |
| 44 | + error = Color(0xFFD32F2F), // 标准错误色 |
| 45 | + onPrimary = Color(0xFFFFFFFF), // 主色上的文本 |
| 46 | + onSecondary = Color(0xFFFFFFFF), // 次要色上的文本 |
| 47 | + onTertiary = Color(0xFFFFFFFF), // 第三色上的文本 |
| 48 | + onBackground = Color(0xFF212121), // 背景上的文本 |
| 49 | + onSurface = Color(0xFF212121), // 表面上的文本 |
| 50 | + surfaceVariant = Color(0xFFE6E6E6), // 表面变体 |
| 51 | + primaryContainer = Color(0xFFD4E3F8), // 主色容器 |
| 52 | + secondaryContainer = Color(0xFFD4F0EB) // 次要色容器 |
| 53 | +) |
| 54 | + |
| 55 | +val darkColors = darkColorScheme( |
| 56 | + primary = Color(0xFF5B9EE1), // 柔和的蓝色 |
| 57 | + secondary = Color(0xFF80CBBF), // 淡绿蓝色 |
| 58 | + tertiary = Color(0xFFEFB196), // 淡橙色 |
| 59 | + background = Color(0xFF1A1A1A), // 稍微柔和的黑色 |
| 60 | + surface = Color(0xFF252525), // 柔和的暗色表面 |
| 61 | + error = Color(0xFFE57373), // 淡红色错误提示 |
| 62 | + onPrimary = Color(0xFFF5F5F5), // 主色上的文本 |
| 63 | + onSecondary = Color(0xFF121212), // 次要色上的文本 |
| 64 | + onTertiary = Color(0xFF121212), // 第三色上的文本 |
| 65 | + onBackground = Color(0xFFE0E0E0), // 背景上的文本 |
| 66 | + onSurface = Color(0xFFE0E0E0), // 表面上的文本 |
| 67 | + surfaceVariant = Color(0xFF3A3A3A), // 表面变体 |
| 68 | + primaryContainer = Color(0xFF2F4D6F), // 主色容器 |
| 69 | + secondaryContainer = Color(0xFF396458) // 次要色容器 |
| 70 | +) |
| 71 | + |
| 72 | +/** |
| 73 | + * 创建带动画过渡的颜色方案 |
| 74 | + */ |
| 75 | +@Composable |
| 76 | +fun rememberAnimatedColorScheme(colorMode: ColorMode): ColorScheme { |
| 77 | + |
| 78 | + |
| 79 | + val targetColors = when (colorMode) { |
| 80 | + ColorMode.LIGHT -> lightColors |
| 81 | + ColorMode.DARK -> darkColors |
| 82 | + } |
| 83 | + |
| 84 | + // 为每个颜色添加动画过渡 |
| 85 | + val animationDuration = 300 |
| 86 | + val animationSpec = tween<Color>(durationMillis = animationDuration) |
| 87 | + |
| 88 | + val animatedPrimary by animateColorAsState(targetColors.primary, animationSpec, label = "primary") |
| 89 | + val animatedSecondary by animateColorAsState(targetColors.secondary, animationSpec, label = "secondary") |
| 90 | + val animatedTertiary by animateColorAsState(targetColors.tertiary, animationSpec, label = "tertiary") |
| 91 | + val animatedBackground by animateColorAsState(targetColors.background, animationSpec, label = "background") |
| 92 | + val animatedSurface by animateColorAsState(targetColors.surface, animationSpec, label = "surface") |
| 93 | + val animatedError by animateColorAsState(targetColors.error, animationSpec, label = "error") |
| 94 | + val animatedOnPrimary by animateColorAsState(targetColors.onPrimary, animationSpec, label = "onPrimary") |
| 95 | + val animatedOnSecondary by animateColorAsState(targetColors.onSecondary, animationSpec, label = "onSecondary") |
| 96 | + val animatedOnTertiary by animateColorAsState(targetColors.onTertiary, animationSpec, label = "onTertiary") |
| 97 | + val animatedOnBackground by animateColorAsState(targetColors.onBackground, animationSpec, label = "onBackground") |
| 98 | + val animatedOnSurface by animateColorAsState(targetColors.onSurface, animationSpec, label = "onSurface") |
| 99 | + val animatedSurfaceVariant by animateColorAsState(targetColors.surfaceVariant, animationSpec, label = "surfaceVariant") |
| 100 | + val animatedPrimaryContainer by animateColorAsState(targetColors.primaryContainer, animationSpec, label = "primaryContainer") |
| 101 | + val animatedSecondaryContainer by animateColorAsState(targetColors.secondaryContainer, animationSpec, label = "secondaryContainer") |
| 102 | + |
| 103 | + return ColorScheme( |
| 104 | + primary = animatedPrimary, |
| 105 | + onPrimary = animatedOnPrimary, |
| 106 | + primaryContainer = animatedPrimaryContainer, |
| 107 | + onPrimaryContainer = targetColors.onPrimaryContainer, |
| 108 | + inversePrimary = targetColors.inversePrimary, |
| 109 | + secondary = animatedSecondary, |
| 110 | + onSecondary = animatedOnSecondary, |
| 111 | + secondaryContainer = animatedSecondaryContainer, |
| 112 | + onSecondaryContainer = targetColors.onSecondaryContainer, |
| 113 | + tertiary = animatedTertiary, |
| 114 | + onTertiary = animatedOnTertiary, |
| 115 | + tertiaryContainer = targetColors.tertiaryContainer, |
| 116 | + onTertiaryContainer = targetColors.onTertiaryContainer, |
| 117 | + background = animatedBackground, |
| 118 | + onBackground = animatedOnBackground, |
| 119 | + surface = animatedSurface, |
| 120 | + onSurface = animatedOnSurface, |
| 121 | + surfaceVariant = animatedSurfaceVariant, |
| 122 | + onSurfaceVariant = targetColors.onSurfaceVariant, |
| 123 | + surfaceTint = targetColors.surfaceTint, |
| 124 | + inverseSurface = targetColors.inverseSurface, |
| 125 | + inverseOnSurface = targetColors.inverseOnSurface, |
| 126 | + 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, |
| 133 | + surfaceBright = targetColors.surfaceBright, |
| 134 | + surfaceDim = targetColors.surfaceDim, |
| 135 | + surfaceContainer = targetColors.surfaceContainer, |
| 136 | + surfaceContainerHigh = targetColors.surfaceContainerHigh, |
| 137 | + surfaceContainerHighest = targetColors.surfaceContainerHighest, |
| 138 | + surfaceContainerLow = targetColors.surfaceContainerLow, |
| 139 | + surfaceContainerLowest = targetColors.surfaceContainerLowest |
| 140 | + ) |
| 141 | +} |
0 commit comments