Skip to content

Commit bf6321d

Browse files
authored
Use rememberUpdatedState to prevent stale lambda capture (#30)
* Attempting to fix dragging lag on Linux #19 * Use rememberUpdatedState for lambdas that are captured to ensure it uses the latest value when calling it. * Fix issue with resizing of color wheel * Fix lint
1 parent 2adfe69 commit bf6321d

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

color-picker/src/commonMain/kotlin/com/godaddy/android/colorpicker/ClassicColorPicker.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import androidx.compose.foundation.layout.Spacer
66
import androidx.compose.foundation.layout.height
77
import androidx.compose.foundation.layout.width
88
import androidx.compose.runtime.Composable
9+
import androidx.compose.runtime.getValue
910
import androidx.compose.runtime.mutableStateOf
11+
import androidx.compose.runtime.rememberUpdatedState
1012
import androidx.compose.runtime.saveable.rememberSaveable
1113
import androidx.compose.ui.Modifier
1214
import androidx.compose.ui.graphics.Color
@@ -35,14 +37,15 @@ fun ClassicColorPicker(
3537
Row(modifier = modifier) {
3638
val barThickness = 32.dp
3739
val paddingBetweenBars = 8.dp
40+
val updatedOnColorChanged by rememberUpdatedState(onColorChanged)
3841
Column(modifier = Modifier.weight(0.8f)) {
3942
SaturationValueArea(
4043
modifier = Modifier.weight(0.8f),
4144
currentColor = colorPickerValueState.value,
4245
onSaturationValueChanged = { saturation, value ->
4346
colorPickerValueState.value =
4447
colorPickerValueState.value.copy(saturation = saturation, value = value)
45-
onColorChanged(colorPickerValueState.value)
48+
updatedOnColorChanged(colorPickerValueState.value)
4649
}
4750
)
4851
if (showAlphaBar) {
@@ -52,7 +55,7 @@ fun ClassicColorPicker(
5255
currentColor = colorPickerValueState.value,
5356
onAlphaChanged = { alpha ->
5457
colorPickerValueState.value = colorPickerValueState.value.copy(alpha = alpha)
55-
onColorChanged(colorPickerValueState.value)
58+
updatedOnColorChanged(colorPickerValueState.value)
5659
}
5760
)
5861
}
@@ -63,7 +66,7 @@ fun ClassicColorPicker(
6366
currentColor = colorPickerValueState.value,
6467
onHueChanged = { newHue ->
6568
colorPickerValueState.value = colorPickerValueState.value.copy(hue = newHue)
66-
onColorChanged(colorPickerValueState.value)
69+
updatedOnColorChanged(colorPickerValueState.value)
6770
}
6871
)
6972
}

color-picker/src/commonMain/kotlin/com/godaddy/android/colorpicker/harmony/HarmonyColorPicker.kt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import androidx.compose.foundation.layout.padding
1515
import androidx.compose.foundation.layout.wrapContentSize
1616
import androidx.compose.runtime.Composable
1717
import androidx.compose.runtime.State
18-
import androidx.compose.runtime.derivedStateOf
1918
import androidx.compose.runtime.getValue
2019
import androidx.compose.runtime.mutableStateOf
2120
import androidx.compose.runtime.remember
21+
import androidx.compose.runtime.rememberUpdatedState
2222
import androidx.compose.runtime.setValue
2323
import androidx.compose.ui.Modifier
2424
import androidx.compose.ui.geometry.Offset
@@ -48,6 +48,7 @@ fun HarmonyColorPicker(
4848
.fillMaxWidth()
4949
) {
5050
val hsvColor = remember { mutableStateOf(HsvColor.from(color)) }
51+
val updatedOnColorChanged by rememberUpdatedState(onColorChanged)
5152

5253
HarmonyColorPickerWithMagnifiers(
5354
modifier = Modifier
@@ -56,7 +57,7 @@ fun HarmonyColorPicker(
5657
hsvColor = hsvColor,
5758
onColorChanged = {
5859
hsvColor.value = it
59-
onColorChanged(it)
60+
updatedOnColorChanged(it)
6061
},
6162
harmonyMode = harmonyMode
6263
)
@@ -68,7 +69,7 @@ fun HarmonyColorPicker(
6869
.weight(0.2f),
6970
onValueChange = { value ->
7071
hsvColor.value = hsvColor.value.copy(value = value)
71-
onColorChanged(hsvColor.value)
72+
updatedOnColorChanged(hsvColor.value)
7273
},
7374
currentColor = hsvColor.value
7475
)
@@ -90,14 +91,11 @@ private fun HarmonyColorPickerWithMagnifiers(
9091
.aspectRatio(1f, matchHeightConstraintsFirst = true)
9192

9293
) {
94+
val updatedOnColorChanged by rememberUpdatedState(onColorChanged)
9395
val diameterPx = remember(constraints.maxWidth) {
9496
mutableStateOf(constraints.maxWidth)
9597
}
96-
val magnifierSize = remember {
97-
derivedStateOf {
98-
IntSize(diameterPx.value, diameterPx.value)
99-
}
100-
}
98+
10199
var animateChanges by remember {
102100
mutableStateOf(false)
103101
}
@@ -108,10 +106,10 @@ private fun HarmonyColorPickerWithMagnifiers(
108106
fun updateColorWheel(newPosition: Offset, animate: Boolean) {
109107
// Work out if the new position is inside the circle we are drawing, and has a
110108
// valid color associated to it. If not, keep the current position
111-
val newColor = colorForPosition(newPosition, magnifierSize.value, hsvColor.value.value)
109+
val newColor = colorForPosition(newPosition, IntSize(diameterPx.value, diameterPx.value), hsvColor.value.value)
112110
if (newColor != null) {
113111
animateChanges = animate
114-
onColorChanged(newColor)
112+
updatedOnColorChanged(newColor)
115113
}
116114
}
117115

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Sun Sep 25 18:37:16 BST 2022
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
4-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-all.zip
4+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
55
zipStoreBase=GRADLE_USER_HOME
66
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)