Răsfoiți Sursa

stable memory chart colors

Milan Jurkulak 3 luni în urmă
părinte
comite
9b734731ca

+ 5 - 13
shared/src/commonMain/kotlin/org/mjdev/desktop/components/chart/DonutChart.kt

@@ -32,19 +32,11 @@ fun DonutChart(
 ) {
     var needRefresh by remember { mutableStateOf(0L) }
     val textStyle = TextStyle(color = textColor)
-    val data by remember(
-        title,
-        textColor,
-        outerCircularColor,
-        innerCircularColor,
-        ratioLineColor,
-        animationDuration,
-        refreshTimeout,
-        animation,
-        legendPosition,
-        dataHandler,
-        needRefresh,
-    ) { mutableStateOf(dataHandler()) }
+    // derivedStateOf tracks the compose state read INSIDE dataHandler (e.g. the wallpaper
+    // palette colors of MemoryChart slices). With plain remember(keys) the slices only
+    // recoloured on the next refreshTimeout tick, so for seconds the chart mixed OLD slice
+    // colors with NEW ring/text colors — the "randomly different widget color" bug.
+    val data by remember(dataHandler, needRefresh) { derivedStateOf { dataHandler() } }
     DonutChart(
         modifier = modifier,
         pieChartData = data,

+ 7 - 2
shared/src/commonMain/kotlin/org/mjdev/desktop/managers/palette/Palette.kt

@@ -10,6 +10,7 @@ package org.mjdev.desktop.managers.palette
 
 import androidx.compose.runtime.MutableState
 import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.snapshots.Snapshot
 import androidx.compose.ui.graphics.Color
 import androidx.compose.ui.graphics.lerp
 import kotlinx.coroutines.CoroutineScope
@@ -134,8 +135,12 @@ class Palette(
                     } else {
                         background.lighter(textFactor)
                     }
-                backgroundColorState.value = background
-                textColorState.value = text
+                // One atomic snapshot: writing the two states separately let the UI recompose
+                // between them (new background + stale text) — a transient color mismatch.
+                Snapshot.withMutableSnapshot {
+                    backgroundColorState.value = background
+                    textColorState.value = text
+                }
             }
             createFromPalette()
         }