Просмотр исходного кода

Merge pull request #1509 from fansangg/fix/theme-switch-delay

fix:菜单切换深浅色一些按钮变化延迟的问题
Kudomaga 4 месяцев назад
Родитель
Сommit
8090637770

+ 4 - 3
app/src/main/java/io/legado/app/ui/theme/LegadoTheme.kt

@@ -5,6 +5,7 @@ import androidx.compose.material3.lightColorScheme
 import androidx.compose.runtime.Composable
 import androidx.compose.runtime.ReadOnlyComposable
 import androidx.compose.runtime.compositionLocalOf
+import androidx.compose.runtime.staticCompositionLocalOf
 import androidx.compose.ui.graphics.Color
 import androidx.compose.ui.text.TextStyle
 import com.materialkolor.PaletteStyle
@@ -109,15 +110,15 @@ data class LegadoTypography(
 
     )
 
-val LocalLegadoColorScheme = compositionLocalOf<LegadoColorScheme> {
+val LocalLegadoColorScheme = staticCompositionLocalOf<LegadoColorScheme> {
     error("No ColorScheme provided")
 }
 
-val LocalLegadoTypography = compositionLocalOf<LegadoTypography> {
+val LocalLegadoTypography = staticCompositionLocalOf<LegadoTypography> {
     error("No Typography provided")
 }
 
-val LocalLegadoThemeColors = compositionLocalOf {
+val LocalLegadoThemeColors = staticCompositionLocalOf {
     LegadoThemeMode(
         colorScheme = lightColorScheme(),
         isDark = false,

+ 8 - 18
app/src/main/java/io/legado/app/ui/theme/ThemeColorSchemeOverride.kt

@@ -95,23 +95,13 @@ fun ProvideColorSchemeOverride(
     overrideIsDark: Boolean? = null,
     content: @Composable () -> Unit,
 ) {
-    val themeAnimationSpec = tween<Color>(
-        durationMillis = 700,
-        easing = FastOutSlowInEasing
-    )
     val baseThemeMode = LocalLegadoThemeColors.current
-    val animatedColorScheme = colorScheme.animateColorSchemeAsState(themeAnimationSpec)
-    val animatedSeedColor = animateColorAsState(
-        targetValue = seedColor,
-        animationSpec = themeAnimationSpec,
-        label = "theme_seed_animation"
-    ).value
-    val legadoColorScheme = remember(animatedColorScheme) { animatedColorScheme.toLegadoColorScheme() }
+    val legadoColorScheme = remember(colorScheme) { colorScheme.toLegadoColorScheme() }
     val resolvedIsDark = overrideIsDark ?: baseThemeMode.isDark
-    val overrideThemeMode = remember(baseThemeMode, animatedColorScheme, animatedSeedColor, resolvedIsDark) {
+    val overrideThemeMode = remember(baseThemeMode, colorScheme, seedColor, resolvedIsDark) {
         baseThemeMode.copy(
-            colorScheme = animatedColorScheme,
-            seedColor = animatedSeedColor,
+            colorScheme = colorScheme,
+            seedColor = seedColor,
             isDark = resolvedIsDark,
         )
     }
@@ -131,7 +121,7 @@ fun ProvideColorSchemeOverride(
         isMiuixEngine,
         miuixColorSchemeMode,
         overrideThemeMode.isDark,
-        animatedSeedColor,
+        seedColor,
         miuixPaletteStyle,
         miuixColorSpec
     ) {
@@ -140,7 +130,7 @@ fun ProvideColorSchemeOverride(
         } else {
             ThemeController(
                 colorSchemeMode = miuixColorSchemeMode,
-                keyColor = animatedSeedColor,
+                keyColor = seedColor,
                 paletteStyle = miuixPaletteStyle,
                 colorSpec = miuixColorSpec,
                 isDark = overrideThemeMode.isDark
@@ -155,7 +145,7 @@ fun ProvideColorSchemeOverride(
         if (miuixController != null) {
             MiuixTheme(controller = miuixController) {
                 MaterialTheme(
-                    colorScheme = animatedColorScheme,
+                    colorScheme = colorScheme,
                     typography = materialTypography,
                     shapes = materialShapes
                 ) {
@@ -164,7 +154,7 @@ fun ProvideColorSchemeOverride(
             }
         } else {
             MaterialTheme(
-                colorScheme = animatedColorScheme,
+                colorScheme = colorScheme,
                 typography = materialTypography,
                 shapes = materialShapes
             ) {

+ 17 - 2
app/src/main/java/io/legado/app/ui/widget/components/button/series/SeriesIconButton.kt

@@ -19,9 +19,13 @@ import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
 import androidx.compose.material3.Icon
 import androidx.compose.material3.IconButtonDefaults
 import androidx.compose.material3.ripple
+import androidx.compose.animation.core.snap
 import androidx.compose.runtime.Composable
 import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
 import androidx.compose.runtime.remember
+import androidx.compose.runtime.setValue
+import androidx.compose.runtime.SideEffect
 import androidx.compose.ui.Alignment
 import androidx.compose.ui.Modifier
 import androidx.compose.ui.draw.clip
@@ -69,13 +73,24 @@ internal fun SeriesButton(
     selectedContentColor: Color = LegadoTheme.colorScheme.onPrimaryContainer,
     content: @Composable (Color) -> Unit
 ) {
+    var lastEnabled by remember { mutableStateOf(enabled) }
+    var lastSelected by remember { mutableStateOf(selected) }
+    val isStateChanged = enabled != lastEnabled || selected != lastSelected
+
+    SideEffect {
+        lastEnabled = enabled
+        lastSelected = selected
+    }
+
+    val animSpec = if (isStateChanged) tween<Color>(150) else snap()
+
     val containerColor by animateColorAsState(
         targetValue = when {
             !enabled -> disabledContainerColor(style)
             selected -> selectedContainerColor
             else -> containerColor ?: containerColor(style)
         },
-        animationSpec = tween(150),
+        animationSpec = animSpec,
         label = "SeriesIconContainerColor"
     )
     val resolvedContentColor by animateColorAsState(
@@ -84,7 +99,7 @@ internal fun SeriesButton(
             selected -> selectedContentColor
             else -> contentColor
         },
-        animationSpec = tween(150),
+        animationSpec = animSpec,
         label = "SeriesIconContentColor"
     )
     val shape = IconButtonDefaults.extraSmallRoundShape

+ 18 - 2
app/src/main/java/io/legado/app/ui/widget/components/tabRow/CardTabRow.kt

@@ -8,8 +8,15 @@ import androidx.compose.foundation.layout.fillMaxWidth
 import androidx.compose.foundation.layout.padding
 import androidx.compose.material3.MaterialTheme
 import androidx.compose.material3.Text
+import androidx.compose.animation.core.snap
+import androidx.compose.ui.graphics.Color
 import androidx.compose.runtime.Composable
 import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.setValue
+import androidx.compose.runtime.SideEffect
+import androidx.compose.runtime.mutableIntStateOf
 import androidx.compose.ui.Alignment
 import androidx.compose.ui.Modifier
 import androidx.compose.ui.text.font.FontWeight
@@ -26,6 +33,15 @@ fun CardTabRow(
     onTabSelected: (Int) -> Unit,
     modifier: Modifier = Modifier,
 ) {
+    var lastSelectedTabIndex by remember { mutableIntStateOf(selectedTabIndex) }
+    val isSelectionChanged = selectedTabIndex != lastSelectedTabIndex
+
+    SideEffect {
+        lastSelectedTabIndex = selectedTabIndex
+    }
+
+    val animSpec = if (isSelectionChanged) tween<Color>(durationMillis = 200) else snap()
+
     Row(
         modifier = modifier.fillMaxWidth(),
         horizontalArrangement = Arrangement.spacedBy(8.dp),
@@ -38,7 +54,7 @@ fun CardTabRow(
                 } else {
                     LegadoTheme.colorScheme.surfaceContainerLow
                 },
-                animationSpec = tween(durationMillis = 200),
+                animationSpec = animSpec,
                 label = "tabColor",
             )
             val contentColor by animateColorAsState(
@@ -47,7 +63,7 @@ fun CardTabRow(
                 } else {
                     LegadoTheme.colorScheme.onSurfaceVariant
                 },
-                animationSpec = tween(durationMillis = 200),
+                animationSpec = animSpec,
                 label = "tabContentColor",
             )