Skip to content

Open-source Android PDF and document toolkit with PDF tools, Office viewing, XLSX editing, scanning, and image editing. Built with Kotlin and Jetpack Compose.

Fix LiquidToggle double-fire; liquid stretch on toggle/slider thumbs

Chethan616909e26581790
diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/components/LiquidSlider.kt b/app/src/main/java/com/chethan616/clearpdf/ui/components/LiquidSlider.kt
index d4654a2..9d543e3 100644
--- a/app/src/main/java/com/chethan616/clearpdf/ui/components/LiquidSlider.kt
+++ b/app/src/main/java/com/chethan616/clearpdf/ui/components/LiquidSlider.kt
@@ -15,6 +15,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
+import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
@@ -113,6 +114,13 @@ fun LiquidSlider(
)
}
+ // Squash-and-stretch of the thumb along its travel (LiquidTracker technique).
+ val stretch = rememberLiquidStretchState(stretchFactor = 0.4f, minScale = 0.85f, maxScale = 1.2f)
+ LaunchedEffect(dampedDragAnimation, stretch, trackWidth) {
+ snapshotFlow { dampedDragAnimation.progress }
+ .collect { p -> stretch.onPositionSample(p * trackWidth) }
+ }
+
LaunchedEffect(currentValue, isDragging, dampedDragAnimation) {
if (!isDragging && abs(dampedDragAnimation.targetValue - currentValue) > visibilityThreshold) {
dampedDragAnimation.updateValue(currentValue)
@@ -163,6 +171,7 @@ fun LiquidSlider(
.fastCoerceIn(-size.width / 4f, trackWidth - size.width * 3f / 4f) * if (isLtr) 1f else -1f
}
.then(dampedDragAnimation.modifier)
+ .liquidStretch(stretch)
.drawBackdrop(
backdrop = rememberCombinedBackdrop(
backdrop,
@@ -201,9 +210,6 @@ fun LiquidSlider(
layerBlock = {
scaleX = dampedDragAnimation.scaleX
scaleY = dampedDragAnimation.scaleY
- val velocity = dampedDragAnimation.velocity / 10f
- scaleX /= 1f - (velocity * 0.75f).fastCoerceIn(-0.2f, 0.2f)
- scaleY *= 1f - (velocity * 0.25f).fastCoerceIn(-0.2f, 0.2f)
},
onDrawSurface = {
val progress = dampedDragAnimation.pressProgress
diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/components/LiquidToggle.kt b/app/src/main/java/com/chethan616/clearpdf/ui/components/LiquidToggle.kt
index 5d9ecb3..59405b5 100644
--- a/app/src/main/java/com/chethan616/clearpdf/ui/components/LiquidToggle.kt
+++ b/app/src/main/java/com/chethan616/clearpdf/ui/components/LiquidToggle.kt
@@ -1,6 +1,5 @@
package com.chethan616.clearpdf.ui.components
-import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
@@ -25,6 +24,9 @@ import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.role
import androidx.compose.ui.semantics.semantics
+import androidx.compose.ui.semantics.onClick
+import androidx.compose.ui.semantics.toggleableState
+import androidx.compose.ui.state.ToggleableState
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.compose.ui.util.fastCoerceIn
@@ -116,11 +118,27 @@ fun LiquidToggle(
val trackBackdrop = rememberLayerBackdrop()
+ // Squash-and-stretch of the thumb along its travel (LiquidTracker technique).
+ val stretch = rememberLiquidStretchState(stretchFactor = 0.4f, minScale = 0.85f, maxScale = 1.2f)
+ LaunchedEffect(dampedDragAnimation, stretch) {
+ snapshotFlow { dampedDragAnimation.value }
+ .collect { v -> stretch.onPositionSample(v * dragWidth) }
+ }
+
+ // The whole switch is the gesture surface: DampedDragAnimation reports a tap through
+ // onDragStopped (didDrag == false), so no extra `clickable` - stacking one here made a tap
+ // fire onSelect twice (toggle on, then straight back off).
Box(
modifier
.clip(Capsule)
- .clickable {
- onSelect(!selected())
+ .then(dampedDragAnimation.modifier)
+ .semantics {
+ role = Role.Switch
+ toggleableState = ToggleableState(selected())
+ onClick {
+ onSelect(!selected())
+ true
+ }
},
contentAlignment = Alignment.CenterStart
) {
@@ -144,10 +162,7 @@ fun LiquidToggle(
if (isLtr) lerp(padding, padding + dragWidth, fraction)
else lerp(-padding, -(padding + dragWidth), fraction)
}
- .semantics {
- role = Role.Switch
- }
- .then(dampedDragAnimation.modifier)
+ .liquidStretch(stretch)
.drawBackdrop(
backdrop = rememberCombinedBackdrop(
backdrop,
@@ -194,9 +209,6 @@ fun LiquidToggle(
layerBlock = {
scaleX = dampedDragAnimation.scaleX
scaleY = dampedDragAnimation.scaleY
- val velocity = dampedDragAnimation.velocity / 50f
- scaleX /= 1f - (velocity * 0.75f).fastCoerceIn(-0.2f, 0.2f)
- scaleY *= 1f - (velocity * 0.25f).fastCoerceIn(-0.2f, 0.2f)
},
onDrawSurface = {
val progress = dampedDragAnimation.pressProgress