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.

Cut the cost of expanding the player

Leloniod125d4b23135
diff --git a/.claude/settings.local.json b/.claude/settings.local.json
index e664eb6..779832c 100644
--- a/.claude/settings.local.json
+++ b/.claude/settings.local.json
@@ -84,7 +84,8 @@
"Bash(cd \"/Users/emanuele/Progetto *)",
"Bash(git -c user.name=Emanuele -c [email protected] commit -qm 'Play through Spotify Connect *)",
"Bash(./gradlew :app:compileDevKotlin)",
- "Bash(git -c user.name=Emanuele -c [email protected] commit -qm 'Add the Connect device picker to the player *)"
+ "Bash(git -c user.name=Emanuele -c [email protected] commit -qm 'Add the Connect device picker to the player *)",
+ "Bash(git -c user.name=Emanuele -c [email protected] commit -qm 'Cut the cost of expanding the player *)"
]
}
}
diff --git a/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.bin b/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.bin
index cc8ae6d..61c29f9 100644
--- a/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.bin
+++ b/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.bin
@@ -2,27 +2,27 @@ C/C++ Structured Logl
j
h/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/additional_project_files.txtC
A
-?com.android.build.gradle.internal.cxx.io.EncodedFileFingerPrint  �����3  ����3i
+?com.android.build.gradle.internal.cxx.io.EncodedFileFingerPrint  �����3  ����3i
g
-e/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/android_gradle_build.json  �����3� ����3n
+e/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/android_gradle_build.json  �����3� ����3n
l
-j/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/android_gradle_build_mini.json  �����3� �����3[
+j/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/android_gradle_build_mini.json  �����3� �����3[
Y
-W/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build.ninja  �����3�� ����3_
+W/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build.ninja  �����3�� ����3_
]
-[/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build.ninja.txt  �����3d
+[/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build.ninja.txt  �����3d
b
-`/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build_file_index.txt  �����3 @ ����3e
+`/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build_file_index.txt  �����3 @ ����3e
c
-a/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/compile_commands.json  �����3�� ����3i
+a/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/compile_commands.json  �����3�� ����3i
g
-e/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/compile_commands.json.bin  �����3 �" ����3o
+e/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/compile_commands.json.bin  �����3 �" ����3o
m
-k/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/metadata_generation_command.txt  �����3
+k/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/metadata_generation_command.txt  �����3
� ����3b
`
-^/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/prefab_config.json  �����3  ( ����3g
+^/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/prefab_config.json  �����3  ( ����3g
e
-c/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/symbol_folder_index.txt  �����3  b ����3D
+c/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/symbol_folder_index.txt  �����3  b ����3D
B
-@/Users/emanuele/Progetto Spotify/app/src/main/cpp/CMakeLists.txt  �����3 � �Ȅ��3
\ No newline at end of file
+@/Users/emanuele/Progetto Spotify/app/src/main/cpp/CMakeLists.txt  �����3 � �Ȅ��3
\ No newline at end of file
diff --git a/app/src/main/java/dev/emanuele/spot/ui/SpotApp.kt b/app/src/main/java/dev/emanuele/spot/ui/SpotApp.kt
index 3e32546..aec5006 100644
--- a/app/src/main/java/dev/emanuele/spot/ui/SpotApp.kt
+++ b/app/src/main/java/dev/emanuele/spot/ui/SpotApp.kt
@@ -46,7 +46,6 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalDensity
-import androidx.compose.ui.draw.blur
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
@@ -67,7 +66,9 @@ import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
+import androidx.compose.ui.platform.LocalContext
import coil.compose.AsyncImage
+import coil.request.ImageRequest
import com.kyant.backdrop.Backdrop
import com.kyant.backdrop.backdrops.layerBackdrop
import com.kyant.backdrop.backdrops.rememberLayerBackdrop
@@ -113,6 +114,13 @@ private val expandSpec = spring<Float>(
private val BottomBarHeight = 62.dp
+/**
+ * Decode size of the page backdrop, in pixels.
+ *
+ * Small enough that stretching it across the window *is* the blur.
+ */
+private const val BACKDROP_DECODE_PX = 32
+
@UnstableApi
@Composable
fun SpotApp(
@@ -462,16 +470,21 @@ private fun AppBackdrop(artworkUrl: String?) {
.background(Color(0xFF0A0A0C)),
) {
if (artworkUrl != null) {
+ // Softened by decoding it tiny and letting the upscale do the work,
+ // not by `Modifier.blur`. An 80dp blur over the whole window is a
+ // RenderEffect on a full-screen layer, re-run whenever that layer
+ // changes — which during the player's expansion is every frame, and
+ // was a large part of why it stuttered. A 32px bitmap stretched to
+ // the window is the same picture for nothing.
AsyncImage(
- model = artworkUrl,
+ model = ImageRequest.Builder(LocalContext.current)
+ .data(artworkUrl)
+ .size(BACKDROP_DECODE_PX)
+ .crossfade(true)
+ .build(),
contentDescription = null,
contentScale = ContentScale.Crop,
- modifier = Modifier
- .fillMaxSize()
- // Well past the point where the cover is recognisable: this
- // is meant to read as light coming off the artwork, not as
- // an out-of-focus photograph.
- .blur(80.dp),
+ modifier = Modifier.fillMaxSize(),
)
}
diff --git a/app/src/main/java/dev/emanuele/spot/ui/player/GlassSurface.kt b/app/src/main/java/dev/emanuele/spot/ui/player/GlassSurface.kt
index 0bdbc2b..32461bc 100644
--- a/app/src/main/java/dev/emanuele/spot/ui/player/GlassSurface.kt
+++ b/app/src/main/java/dev/emanuele/spot/ui/player/GlassSurface.kt
@@ -1,5 +1,6 @@
package dev.emanuele.spot.ui.player
+import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
@@ -33,6 +34,17 @@ import com.kyant.backdrop.effects.vibrancy
* Android 12 the blur goes too. What is left is a plain translucent surface, so
* the screen stays usable rather than breaking on older phones.
*/
+/**
+ * Whether panes actually refract, or fall back to a plain film.
+ *
+ * False while the player is in motion. A pane's blur and lens are RuntimeShaders
+ * sampling a recorded layer, and during the expansion that layer changes every
+ * frame — several full-screen shader passes per frame, which is most of what
+ * made the animation stutter. Nobody can see refraction through a surface that
+ * is travelling anyway.
+ */
+val LocalGlassEnabled = androidx.compose.runtime.staticCompositionLocalOf { true }
+
@Composable
fun GlassSurface(
backdrop: Backdrop,
@@ -53,6 +65,18 @@ fun GlassSurface(
surfaceColor: Color = Color.Unspecified,
content: @Composable () -> Unit,
) {
+ if (!LocalGlassEnabled.current) {
+ Box(
+ modifier.background(
+ color = if (surfaceColor.isSpecified) surfaceColor else FallbackFilm,
+ shape = shape,
+ ),
+ ) {
+ content()
+ }
+ return
+ }
+
Box(
modifier.drawBackdrop(
backdrop = backdrop,
@@ -77,3 +101,6 @@ fun GlassSurface(
content()
}
}
+
+/** What a pane looks like with the refraction switched off. */
+private val FallbackFilm = Color.White.copy(alpha = 0.12f)
diff --git a/app/src/main/java/dev/emanuele/spot/ui/player/NowPlayingSheet.kt b/app/src/main/java/dev/emanuele/spot/ui/player/NowPlayingSheet.kt
index 7c31f2f..e6dd177 100644
--- a/app/src/main/java/dev/emanuele/spot/ui/player/NowPlayingSheet.kt
+++ b/app/src/main/java/dev/emanuele/spot/ui/player/NowPlayingSheet.kt
@@ -15,17 +15,18 @@ 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.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
-import androidx.compose.ui.draw.clip
+import androidx.compose.ui.graphics.CompositingStrategy
import androidx.compose.ui.graphics.graphicsLayer
+import androidx.compose.ui.layout.layout
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
-import androidx.compose.ui.unit.lerp
import kotlinx.coroutines.launch
import kotlin.math.abs
@@ -69,10 +70,25 @@ fun NowPlayingSheet(
) {
val scope = rememberCoroutineScope()
val density = LocalDensity.current
- // Clamped, because the settle spring is under-damped on purpose: it passes
- // 1 on the way in, and an interpolated padding past that end is negative,
- // which Compose rejects outright.
- val fraction = progress.value.coerceIn(0f, 1f)
+
+ // Nothing here reads `progress.value` during composition, and that is the
+ // whole performance story of this file.
+ //
+ // Reading it in the composable body recomposed the sheet — and with it the
+ // entire player and the bar — on every frame of the travel, which measured
+ // at a 97ms median frame and a 250ms worst case. Read from inside a layout
+ // or a graphicsLayer block instead, the same value only re-measures or
+ // re-draws, which is two phases cheaper.
+ //
+ // These two are derived rather than read directly so a recomposition
+ // happens when the answer *changes*, not when the number does.
+ val expandedVisible by remember { derivedStateOf { progress.value > 0.001f } }
+
+ // True only at the two ends of the travel; see LocalGlassEnabled.
+ val settled by remember {
+ derivedStateOf { progress.value <= 0.001f || progress.value >= 0.999f }
+ }
+ val collapsedVisible by remember { derivedStateOf { progress.value < 0.999f } }
BoxWithConstraints(Modifier.fillMaxSize()) {
val fullHeight = maxHeight
@@ -91,7 +107,7 @@ fun NowPlayingSheet(
}
}
- BackHandler(enabled = fraction > 0.5f) { settle(0f) }
+ BackHandler(enabled = expandedVisible) { settle(0f) }
val draggable = rememberDraggableState { delta ->
scope.launch {
@@ -100,16 +116,41 @@ fun NowPlayingSheet(
}
}
+ val marginPx = with(density) { SIDE_MARGIN.roundToPx() }
+ val insetPx = with(density) { bottomInset.roundToPx() }
+ val collapsedPx = with(density) { collapsedHeight.roundToPx() }
+ val fullPx = with(density) { fullHeight.roundToPx() }
+
Box(
Modifier
.align(Alignment.BottomCenter)
.fillMaxWidth()
- .padding(horizontal = lerp(16.dp, 0.dp, fraction))
- // The bar floats above the tab bar; the player owns the whole
- // window.
- .padding(bottom = lerp(bottomInset, 0.dp, fraction))
- .height(lerp(collapsedHeight, fullHeight, fraction))
- .clip(RoundedCornerShape(lerp(22.dp, 0.dp, fraction)))
+ // Size, margins and inset in one measure pass, all read here
+ // rather than in composition.
+ .layout { measurable, constraints ->
+ val f = progress.value.coerceIn(0f, 1f)
+ val margin = ((1f - f) * marginPx).toInt()
+ val inset = ((1f - f) * insetPx).toInt()
+ val height = collapsedPx + ((fullPx - collapsedPx) * f).toInt()
+
+ val width = (constraints.maxWidth - margin * 2).coerceAtLeast(0)
+ val placeable = measurable.measure(
+ constraints.copy(
+ minWidth = width,
+ maxWidth = width,
+ minHeight = height,
+ maxHeight = height,
+ ),
+ )
+ layout(constraints.maxWidth, height + inset) {
+ placeable.place(margin, 0)
+ }
+ }
+ .graphicsLayer {
+ val f = progress.value.coerceIn(0f, 1f)
+ shape = RoundedCornerShape(SHEET_CORNER * (1f - f))
+ clip = true
+ }
.draggable(
state = draggable,
orientation = Orientation.Vertical,
@@ -120,14 +161,14 @@ fun NowPlayingSheet(
// which reads as the gesture having been ignored.
val target = when {
abs(velocity) > FLING_VELOCITY -> if (velocity < 0) 1f else 0f
- fraction > 0.5f -> 1f
+ progress.value > 0.5f -> 1f
else -> 0f
}
settle(target)
},
),
) {
- if (fraction > 0.001f) {
+ if (expandedVisible) {
Box(
Modifier
.align(Alignment.BottomCenter)
@@ -136,7 +177,9 @@ fun NowPlayingSheet(
// Opaque before the player's own content arrives, or the
// page behind would read through it at the start of
// every expansion.
- .graphicsLayer { alpha = (fraction / 0.25f).coerceIn(0f, 1f) },
+ .graphicsLayer {
+ alpha = (progress.value / 0.25f).coerceIn(0f, 1f)
+ },
) {
background()
}
@@ -145,25 +188,38 @@ fun NowPlayingSheet(
// Laid out at its final size from the start and simply uncovered.
// Re-measuring a screen this complex on every frame of the travel is
// what would make it stutter.
- if (fraction > 0.001f) {
+ if (expandedVisible) {
Box(
Modifier
.align(Alignment.BottomCenter)
.height(fullHeight)
.fillMaxWidth()
- .graphicsLayer { alpha = expandedAlpha(fraction) },
+ .graphicsLayer {
+ alpha = expandedAlpha(progress.value)
+ // Modulated rather than composited: the default
+ // draws the whole subtree into an offscreen buffer
+ // the size of the window before applying alpha,
+ // every frame. Modulating is per-draw-call and looks
+ // identical here because nothing in the player
+ // overlaps itself.
+ compositingStrategy = CompositingStrategy.ModulateAlpha
+ },
) {
- expandedContent()
+ androidx.compose.runtime.CompositionLocalProvider(
+ LocalGlassEnabled provides settled,
+ ) {
+ expandedContent()
+ }
}
}
- if (fraction < 0.999f) {
+ if (collapsedVisible) {
Box(
Modifier
.align(Alignment.TopCenter)
.height(collapsedHeight)
.fillMaxWidth()
- .graphicsLayer { alpha = collapsedAlpha(fraction) },
+ .graphicsLayer { alpha = collapsedAlpha(progress.value) },
) {
collapsedContent()
}
@@ -185,5 +241,10 @@ private fun collapsedAlpha(fraction: Float): Float =
private fun expandedAlpha(fraction: Float): Float =
((fraction - 0.15f) / 0.5f).coerceIn(0f, 1f)
+/** Margin the collapsed bar keeps from the edges of the window. */
+private val SIDE_MARGIN = 16.dp
+
+private val SHEET_CORNER = 22.dp
+
/** Past this, a drag is a flick and its direction decides where it lands. */
private const val FLING_VELOCITY = 900f