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.

Restore full Liquid Glass shader backdrop rendering with blur, lens refraction, and inner shadows to LiquidSlider.kt

Chethan61679ae1c217457
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 7e662ab..d4654a2 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
@@ -1,7 +1,6 @@
package com.chethan616.clearpdf.ui.components
import androidx.compose.foundation.background
-import androidx.compose.foundation.border
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
@@ -14,8 +13,8 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
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
@@ -44,7 +43,7 @@ import com.kyant.backdrop.highlight.Highlight
import com.kyant.backdrop.shadow.InnerShadow
import com.kyant.backdrop.shadow.Shadow
import com.kyant.shapes.Capsule
-import kotlinx.coroutines.flow.collectLatest
+import kotlin.math.abs
@Composable
fun LiquidSlider(
@@ -63,47 +62,61 @@ fun LiquidSlider(
val trackBackdrop = rememberLayerBackdrop()
BoxWithConstraints(
- modifier.fillMaxWidth(),
+ modifier = modifier.fillMaxWidth(),
contentAlignment = Alignment.CenterStart
) {
- val trackWidth = constraints.maxWidth
-
+ val trackWidth = constraints.maxWidth.coerceAtLeast(1)
val isLtr = LocalLayoutDirection.current == LayoutDirection.Ltr
val animationScope = rememberCoroutineScope()
+ val currentValue = value().coerceIn(valueRange.start, valueRange.endInclusive)
+ val onValueChangeState = rememberUpdatedState(onValueChange)
var didDrag by remember { mutableStateOf(false) }
- val dampedDragAnimation = remember(animationScope) {
+ var isDragging by remember { mutableStateOf(false) }
+
+ val dampedDragAnimation = remember(
+ animationScope,
+ isLtr,
+ trackWidth,
+ valueRange.start,
+ valueRange.endInclusive,
+ visibilityThreshold
+ ) {
DampedDragAnimation(
animationScope = animationScope,
- initialValue = value(),
+ initialValue = currentValue,
valueRange = valueRange,
visibilityThreshold = visibilityThreshold,
initialScale = 1f,
pressedScale = 1.5f,
- onDragStarted = {},
+ onDragStarted = {
+ didDrag = false
+ isDragging = true
+ },
onDragStopped = {
+ isDragging = false
if (didDrag) {
- onValueChange(targetValue)
+ onValueChangeState.value(targetValue.coerceIn(valueRange.start, valueRange.endInclusive))
}
+ didDrag = false
},
onDrag = { _, dragAmount ->
- if (!didDrag) {
- didDrag = dragAmount.x != 0f
+ val delta = (valueRange.endInclusive - valueRange.start) * (dragAmount.x / trackWidth.toFloat())
+ if (delta != 0f) {
+ didDrag = true
+ val nextValue =
+ if (isLtr) (targetValue + delta).coerceIn(valueRange.start, valueRange.endInclusive)
+ else (targetValue - delta).coerceIn(valueRange.start, valueRange.endInclusive)
+ updateValue(nextValue)
+ onValueChangeState.value(nextValue)
}
- val delta = (valueRange.endInclusive - valueRange.start) * (dragAmount.x / trackWidth)
- onValueChange(
- if (isLtr) (targetValue + delta).coerceIn(valueRange)
- else (targetValue - delta).coerceIn(valueRange)
- )
}
)
}
- LaunchedEffect(dampedDragAnimation) {
- snapshotFlow { value() }
- .collectLatest { value ->
- if (dampedDragAnimation.targetValue != value) {
- dampedDragAnimation.updateValue(value)
- }
- }
+
+ LaunchedEffect(currentValue, isDragging, dampedDragAnimation) {
+ if (!isDragging && abs(dampedDragAnimation.targetValue - currentValue) > visibilityThreshold) {
+ dampedDragAnimation.updateValue(currentValue)
+ }
}
Box(Modifier.layerBackdrop(trackBackdrop)) {
@@ -111,15 +124,16 @@ fun LiquidSlider(
Modifier
.clip(Capsule)
.background(trackColor)
- .pointerInput(animationScope) {
+ .pointerInput(trackWidth, isLtr, valueRange.start, valueRange.endInclusive) {
detectTapGestures { position ->
- val delta = (valueRange.endInclusive - valueRange.start) * (position.x / trackWidth)
+ val delta =
+ (valueRange.endInclusive - valueRange.start) * (position.x / trackWidth.toFloat())
val targetValue =
(if (isLtr) valueRange.start + delta
else valueRange.endInclusive - delta)
.coerceIn(valueRange)
dampedDragAnimation.animateToValue(targetValue)
- onValueChange(targetValue)
+ onValueChangeState.value(targetValue)
}
}
.height(6f.dp)
@@ -147,19 +161,56 @@ fun LiquidSlider(
translationX =
(-size.width / 2f + trackWidth * dampedDragAnimation.progress)
.fastCoerceIn(-size.width / 4f, trackWidth - size.width * 3f / 4f) * if (isLtr) 1f else -1f
- 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)
- shadowElevation = 8f.dp.toPx()
- shape = Capsule
- clip = true
}
.then(dampedDragAnimation.modifier)
- .background(Color.White)
- .border(1.5.dp, accentColor, Capsule)
- .size(36.dp, 24.dp)
+ .drawBackdrop(
+ backdrop = rememberCombinedBackdrop(
+ backdrop,
+ rememberBackdrop(trackBackdrop) { drawBackdrop ->
+ val progress = dampedDragAnimation.pressProgress
+ val scaleX = lerp(2f / 3f, 1f, progress)
+ val scaleY = lerp(0f, 1f, progress)
+ scale(scaleX, scaleY) { drawBackdrop() }
+ }
+ ),
+ shape = { Capsule },
+ effects = {
+ val progress = dampedDragAnimation.pressProgress
+ blur(8f.dp.toPx() * (1f - progress))
+ lens(
+ 10f.dp.toPx() * progress,
+ 14f.dp.toPx() * progress,
+ chromaticAberration = true
+ )
+ },
+ highlight = {
+ val progress = dampedDragAnimation.pressProgress
+ Highlight.Ambient.copy(
+ width = Highlight.Ambient.width / 1.5f,
+ blurRadius = Highlight.Ambient.blurRadius / 1.5f,
+ alpha = progress
+ )
+ },
+ shadow = {
+ Shadow(radius = 4f.dp, color = Color.Black.copy(alpha = 0.05f))
+ },
+ innerShadow = {
+ val progress = dampedDragAnimation.pressProgress
+ InnerShadow(radius = 4f.dp * progress, alpha = progress)
+ },
+ 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
+ drawRect(Color.White.copy(alpha = 1f - progress))
+ }
+ )
+ .size(40f.dp, 24f.dp)
)
}
}