Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

Swipe the cover to change song again, on a liquid glass progress bar

Leloniod785c3191b92
diff --git a/app/src/main/java/dev/lelonio/square/ui/glass/LiquidSlider.kt b/app/src/main/java/dev/lelonio/square/ui/glass/LiquidSlider.kt
index e07f2df..ff37747 100644
--- a/app/src/main/java/dev/lelonio/square/ui/glass/LiquidSlider.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/glass/LiquidSlider.kt
@@ -19,7 +19,7 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
-import androidx.compose.foundation.layout.size
+import androidx.compose.foundation.layout.requiredSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
@@ -41,6 +41,7 @@ import androidx.compose.ui.layout.layout
import androidx.compose.ui.zIndex
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.LayoutDirection
+import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.util.fastCoerceIn
import androidx.compose.ui.util.fastRoundToInt
@@ -71,7 +72,19 @@ fun LiquidSlider(
// current cover, so the colours are parameters with the upstream values as
// defaults.
accentColor: Color = Color(0xFF0091FF),
- trackColor: Color = Color(0xFF787880).copy(0.36f)
+ trackColor: Color = Color(0xFF787880).copy(0.36f),
+ // LOCAL CHANGE: told once, when the finger lifts. For a slider whose every
+ // value is expensive to apply — a seek — and that wants only the last one.
+ onValueChangeFinished: () -> Unit = {},
+ // LOCAL CHANGE: upstream's thumb is a 40 by 24 capsule. Equal sides make it
+ // a circle, which is what the playback position wants.
+ thumbSize: DpSize = DpSize(40f.dp, 24f.dp),
+ // LOCAL CHANGE: the thumb's scale at rest and while held, upstream's as
+ // defaults. A thumb meant to sit small is drawn at its held size and scaled
+ // down, rather than drawn small: the lens is sized in dp, and a lens near
+ // half the pane it sits in is what hangs the refraction shader.
+ thumbRestScale: Float = 1f,
+ thumbPressedScale: Float = 1.5f,
) {
val trackBackdrop = rememberLayerBackdrop()
@@ -91,6 +104,12 @@ fun LiquidSlider(
// one sent the other as it was when the panel opened, and a pitch set
// a moment ago sprang back to a value nobody had chosen.
val latestOnValueChange by rememberUpdatedState(onValueChange)
+ val latestOnFinished by rememberUpdatedState(onValueChangeFinished)
+ // LOCAL CHANGE: the value too, for the same reason. The effect below
+ // is started once, and it went on reading the first lambda it was
+ // given: a progress bar whose song turned into its video kept drawing
+ // the thumb against the song's length, well past where the video was.
+ val latestValue by rememberUpdatedState(value)
val animationScope = rememberCoroutineScope()
var didDrag by remember { mutableStateOf(false) }
val dampedDragAnimation = remember(animationScope) {
@@ -99,8 +118,8 @@ fun LiquidSlider(
initialValue = value(),
valueRange = valueRange,
visibilityThreshold = visibilityThreshold,
- initialScale = 1f,
- pressedScale = 1.5f,
+ initialScale = thumbRestScale,
+ pressedScale = thumbPressedScale,
onDragStarted = {},
onDragStopped = {
if (didDrag) {
@@ -121,7 +140,7 @@ fun LiquidSlider(
)
}
LaunchedEffect(dampedDragAnimation) {
- snapshotFlow { value() }
+ snapshotFlow { latestValue() }
.collectLatest { value ->
if (dampedDragAnimation.targetValue != value) {
dampedDragAnimation.updateValue(value)
@@ -191,6 +210,7 @@ fun LiquidSlider(
// and lensed after the finger is gone reads as the
// control being stuck mid-drag.
dampedDragAnimation.release()
+ latestOnFinished()
}
}
}
@@ -213,6 +233,7 @@ fun LiquidSlider(
.coerceIn(valueRange)
dampedDragAnimation.animateToValue(targetValue)
latestOnValueChange(targetValue)
+ latestOnFinished()
}
}
.height(6f.dp)
@@ -297,7 +318,9 @@ fun LiquidSlider(
drawRect(Color.White.copy(alpha = 1f - progress))
}
)
- .size(40f.dp, 24f.dp)
+ // Required rather than asked for: a thumb taller than the
+ // slider it sits on keeps its size and overhangs the track.
+ .requiredSize(thumbSize)
)
}
}
diff --git a/app/src/main/java/dev/lelonio/square/ui/player/GlassProgressBar.kt b/app/src/main/java/dev/lelonio/square/ui/player/GlassProgressBar.kt
index 5fdd726..ebcad3f 100644
--- a/app/src/main/java/dev/lelonio/square/ui/player/GlassProgressBar.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/player/GlassProgressBar.kt
@@ -10,19 +10,40 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
+import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
+import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.layout
+import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
+import dev.lelonio.square.ui.glass.LiquidSlider
+import dev.lelonio.square.ui.glass.backdrop.Backdrop
+import kotlinx.coroutines.flow.first
+import kotlinx.coroutines.withTimeoutOrNull
+import kotlin.math.abs
import kotlin.math.roundToInt
+/** The thumb's size while held, which is the size it is drawn at. */
+private val THUMB_HELD = 36.dp
+
+/** The height the bar takes in the column, the touch target's. */
+private val BAR_ROOM = 28.dp
+
+/** How long the thumb waits at a seek's target for playback to reach it. */
+private const val SETTLE_MS = 2_000L
+
+/** Close enough to the target, as a share of the song, to let go of it. */
+private const val SETTLE_CLOSE = 0.01f
+
/**
* The playback position, as a thin capsule.
*
@@ -36,6 +57,10 @@ import kotlin.math.roundToInt
* So the position is scrubbed locally and committed once, on release. While a
* drag is in progress the incoming position is ignored, or each update would
* yank the bar back to where playback still is.
+ *
+ * Given a [backdrop], it is the liquid slider after all — the same glass thumb
+ * as the effects panel — with the seek still made once, when the finger lifts;
+ * see LiquidSlider's onValueChangeFinished.
*/
@Composable
fun GlassProgressBar(
@@ -45,9 +70,78 @@ fun GlassProgressBar(
accentColor: Color,
trackColor: Color,
modifier: Modifier = Modifier,
+ backdrop: Backdrop? = null,
+ /**
+ * Where the finger is, in milliseconds, while it holds the bar; null once
+ * the bar is showing playback again. For the time beside it, which would
+ * otherwise say where the song is rather than where it is being taken.
+ */
+ onScrub: (Long?) -> Unit = {},
) {
var scrubbing by remember { mutableStateOf<Float?>(null) }
var width by remember { mutableStateOf(1f) }
+ val latestOnScrub by rememberUpdatedState(onScrub)
+ LaunchedEffect(scrubbing, durationMs) {
+ latestOnScrub(scrubbing?.let { (it * durationMs).toLong() })
+ }
+
+ if (backdrop != null) {
+ // Where the finger left the thumb, held until playback gets there. Let
+ // go straight away and the thumb slid back to the old position, then
+ // forward again once the seek landed: a bounce on every scrub.
+ var settling by remember { mutableStateOf<Float?>(null) }
+ LaunchedEffect(settling) {
+ val target = settling ?: return@LaunchedEffect
+ withTimeoutOrNull(SETTLE_MS) {
+ snapshotFlow { progressOf(positionMs.value, durationMs) }
+ .first { abs(it - target) < SETTLE_CLOSE }
+ }
+ if (settling == target) {
+ scrubbing = null
+ settling = null
+ }
+ }
+ LiquidSlider(
+ value = { scrubbing ?: progressOf(positionMs.value, durationMs) },
+ onValueChange = {
+ settling = null
+ scrubbing = it
+ },
+ onValueChangeFinished = {
+ val target = scrubbing
+ if (target != null && durationMs > 0) {
+ onSeek((target * durationMs).toLong())
+ settling = target
+ }
+ },
+ valueRange = 0f..1f,
+ // A second of a long song is still a step the thumb should take.
+ visibilityThreshold = 0.0005f,
+ backdrop = backdrop,
+ accentColor = accentColor,
+ trackColor = trackColor,
+ // Small at rest, and grows under the finger to the size it is
+ // drawn at.
+ thumbSize = DpSize(THUMB_HELD, THUMB_HELD),
+ thumbRestScale = 14f / 36f,
+ thumbPressedScale = 1f,
+ // As tall as the held thumb, or the part of it past the slider's
+ // edge was cut off and it came out flattened underneath; but
+ // taking the old bar's 28dp in the column, so nothing around it
+ // moves.
+ modifier = modifier.layout { measurable, constraints ->
+ val tall = THUMB_HELD.roundToPx()
+ val placeable = measurable.measure(
+ constraints.copy(minHeight = tall, maxHeight = tall),
+ )
+ val room = BAR_ROOM.roundToPx()
+ layout(placeable.width, room) {
+ placeable.place(0, (room - placeable.height) / 2)
+ }
+ },
+ )
+ return
+ }
val progress = scrubbing ?: progressOf(positionMs.value, durationMs)
diff --git a/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt b/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
index 32df6de..4c65e60 100644
--- a/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
@@ -62,6 +62,7 @@ import androidx.compose.foundation.Canvas
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State
+import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.mutableFloatStateOf
@@ -153,6 +154,9 @@ import kotlin.math.abs
*/
private const val CANVAS_SWIPE_FOLLOW = 0.35f
+/** How far the cover fades under a swipe that has crossed the whole screen. */
+private const val COVER_SWIPE_DIM = 0.7f
+
/**
* How far a Canvas is darkened while playback is paused.
*
@@ -505,6 +509,10 @@ fun PlayerScreen(
val canvas = canvas?.takeIf { !videoOn }
val canvasShift = remember { mutableFloatStateOf(0f) }
+ // The same gesture over the cover. Not a shift: the picture is not moved,
+ // only dimmed as the finger goes, and the next one comes in through the
+ // cover's own crossfade, the way it does on a tap of next.
+ val coverSwipe = remember { mutableFloatStateOf(0f) }
// Whether the clip has put a frame on screen yet.
//
@@ -722,6 +730,10 @@ fun PlayerScreen(
// above it and moving it one to one drags the whole screen's
// refraction with it.
.graphicsLayer {
+ val dim = coverSwipe.floatValue
+ if (dim != 0f) {
+ alpha = 1f - (abs(dim) / size.width).coerceIn(0f, 1f) * COVER_SWIPE_DIM
+ }
val shift = canvasShift.floatValue
if (shift == 0f) return@graphicsLayer
translationX = shift * CANVAS_SWIPE_FOLLOW
@@ -1336,11 +1348,23 @@ fun PlayerScreen(
.graphicsLayer { alpha = panelAlpha },
) { stage ->
when (stage) {
- // Empty: the cover above this AnimatedContent
- // is what fills the slot when no panel is open.
- // Empty: the cover above this AnimatedContent
- // is what fills the slot when no panel is open.
- Stage.COVER -> Box(Modifier.fillMaxSize())
+ // Nothing to draw: the cover is the picture
+ // behind the player now. But the slot keeps the
+ // track-change swipe, which went with the square
+ // sleeve when that was taken off this screen.
+ // The picture fades as the finger goes rather
+ // than sliding; see coverSwipe.
+ Stage.COVER -> CoverGestures(
+ onNext = onNext,
+ onPrevious = onPrevious,
+ canGoNext = state.hasNext,
+ canGoPrevious = state.hasPrevious,
+ followFinger = false,
+ onDrag = { coverSwipe.floatValue = it },
+ modifier = Modifier.fillMaxSize(),
+ ) {
+ Box(Modifier.fillMaxSize())
+ }
Stage.VIDEO -> Column(
Modifier.fillMaxSize(),
@@ -1753,14 +1777,26 @@ fun PlayerScreen(
// Tighter on its side, where the column is short.
Spacer(Modifier.height(if (sideways) 10.dp else 18.dp))
+ // Where the bar is being dragged to, while it is.
+ var scrubMs by remember { mutableStateOf<Long?>(null) }
+ val shownPosition = remember(positionMs) {
+ derivedStateOf { scrubMs ?: positionMs.value }
+ }
GlassProgressBar(
positionMs = positionMs,
durationMs = state.durationMs,
onSeek = onSeek,
+ onScrub = { scrubMs = it },
accentColor = GlassInk,
trackColor = GlassInk.copy(alpha = 0.28f),
+ // The cover's own layer, which carries its
+ // colours down to here: the ground alone is
+ // the app behind the player, and the thumb,
+ // clear glass while it is dragged, came
+ // out grey over a brown page.
+ backdrop = glassBackdrop,
)
- TimeRow(positionMs, state.durationMs)
+ TimeRow(shownPosition, state.durationMs)
Spacer(Modifier.height(if (sideways) 6.dp else 14.dp))
}