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.

The tab bar folds as one shape, and the header blur costs less

Lelonio659cf8c1f41b
diff --git a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
index ed3a7c3..6c1e8f7 100644
--- a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
@@ -64,6 +64,7 @@ import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.text.style.TextOverflow
import com.adamglin.phosphoricons.fill.XCircle
import dev.lelonio.square.ui.glass.floatingtabbar.FloatingTabBar
+import dev.lelonio.square.ui.glass.floatingtabbar.sharedSearchTab
import dev.lelonio.square.ui.glass.floatingtabbar.FloatingTabBarDefaults
import dev.lelonio.square.ui.glass.floatingtabbar.rememberFloatingTabBarScrollConnection
import androidx.compose.ui.unit.sp
@@ -313,6 +314,12 @@ private fun androidx.compose.foundation.layout.RowScope.BarTab(
modifier: Modifier = Modifier,
onClick: () -> Unit,
) {
+ // The geometric bar draws the selected control once, outside the fixed
+ // tab strip, so it can travel without leaving a second icon behind.
+ if (dev.lelonio.square.ui.glass.floatingtabbar.LocalGeometricFloatingTabBar.current && selected) {
+ Spacer(Modifier.weight(1f))
+ return
+ }
LiquidBottomTab(onClick = onClick, modifier = modifier) {
Icon(
icon,
@@ -2118,7 +2125,8 @@ fun SquareApp(
}
},
),
- inline = tabBarScroll.isInline,
+ inline = dev.lelonio.square.ui.glass.floatingtabbar.LocalFloatingTabBarInline.current,
+ collapseProgress = dev.lelonio.square.ui.glass.floatingtabbar.LocalFloatingTabBarCollapseProgress.current?.invoke(),
onClick = {
scope.launch { expand.animateTo(1f, expandSpec) }
},
@@ -2395,6 +2403,7 @@ fun SquareApp(
// own.
indicatorVisible = activeTab in TAB_ROUTES,
modifier = tabsModifier,
+ drawContainer = !dev.lelonio.square.ui.glass.floatingtabbar.LocalGeometricFloatingTabBar.current,
) {
BarTab(
R.string.home,
@@ -2436,34 +2445,30 @@ fun SquareApp(
viewModel.clearPageHistory()
navController.switchTab(Routes.LIBRARY)
}
- BarTab(
- R.string.search,
- // Heavy rather than solid: a filled
- // magnifier reads as a blob at this size,
- // and it is the one glyph here whose shape
- // is the whole of its meaning.
- PhosphorIcons.Bold.MagnifyingGlass,
- activeTab == Routes.SEARCH,
- tabAccent,
- barInk,
- // This slot is the folded bar's search
- // circle standing in the row. Sharing its
- // bounds is what keeps it at the right edge
- // while the pill collapses to the left,
- // instead of shrinking away with the pill
- // and a circle fading in where it was — which
- // is what the reference does: the first and
- // last of the row survive the fold, the
- // three in between go.
- modifier = Modifier.sharedElement(
- sharedContentState = rememberSharedContentState("standaloneTab"),
- animatedVisibilityScope = tabsVisibility,
- zIndexInOverlay = 2f,
- ),
- ) {
- viewModel.clearPageHistory()
- searchOpen = true
- navController.switchTab(Routes.SEARCH)
+ if (dev.lelonio.square.ui.glass.floatingtabbar.LocalGeometricFloatingTabBar.current) {
+ Spacer(Modifier.weight(1f))
+ } else {
+ BarTab(
+ R.string.search,
+ // Heavy rather than solid: a filled
+ // magnifier reads as a blob at this size,
+ // and it is the one glyph here whose shape
+ // is the whole of its meaning.
+ PhosphorIcons.Bold.MagnifyingGlass,
+ activeTab == Routes.SEARCH,
+ tabAccent,
+ barInk,
+ // The circle and labelled tab share bounds,
+ // retaining their surfaces during the fold.
+ // The incoming tab inherits the capsule clip.
+ modifier = if (dev.lelonio.square.ui.glass.LocalLiquidBottomTabsReplica.current) {
+ Modifier
+ } else sharedSearchTab(tabsVisibility),
+ ) {
+ viewModel.clearPageHistory()
+ searchOpen = true
+ navController.switchTab(Routes.SEARCH)
+ }
}
}
}
diff --git a/app/src/main/java/dev/lelonio/square/ui/components/HeroBackdrop.kt b/app/src/main/java/dev/lelonio/square/ui/components/HeroBackdrop.kt
index d3655b2..a4547b5 100644
--- a/app/src/main/java/dev/lelonio/square/ui/components/HeroBackdrop.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/components/HeroBackdrop.kt
@@ -7,6 +7,9 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.ui.draw.clipToBounds
+import androidx.compose.ui.draw.drawWithCache
+import androidx.compose.ui.graphics.ShaderBrush
+import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.graphics.TransformOrigin
import androidx.compose.ui.graphics.FilterQuality
@@ -248,53 +251,32 @@ fun HeroBackdrop(
Box(
modifier = Modifier
.fillMaxSize()
- // Its own layer, so the mask below erases this copy alone
- // and not the sharp one underneath it.
- .graphicsLayer { compositingStrategy = CompositingStrategy.Offscreen }
- .drawWithContent {
- // The moving cover's own frame when it is playing, so
- // the blur is of what is on screen rather than of the
- // still it started from.
+ // Compose the bitmap and its alpha mask in one shader:
+ // no header-sized offscreen render target on each redraw.
+ .drawWithCache {
val frame = motion.latest.value
- drawCropped(frame?.soft ?: blur, shown.zoom)
- drawRect(
- // Eased rather than straight, and started well
- // before the colour does.
- //
- // A linear ramp between two stops is a band: the eye
- // finds both ends of it, and what should read as a
- // picture dissolving reads as a picture with a strip
- // over it. These are the same two ends with the
- // middle bent — slow to leave, quick through the
- // middle, slow to arrive — which is the shape a fade
- // has to have before it stops looking like a shape.
- brush = Brush.verticalGradient(
- // Ahead of the slot's own fade where the
- // picture ends on itself: the copy that
- // dissolves has to be blurred *before* it
- // starts going, or what dissolves is a sharp
- // photograph and the eye follows it down.
- *if (fadeToPage) {
- softening(softenFrom, softenTo, Color.Black)
- } else if (extendPicture) {
- // Just behind the fade rather than a third
- // of the way up: the picture stays sharp
- // down to where it gives way.
- softening(
- 1f - SLOT_FADE - SLOT_BLUR_LEAD,
- 1f - SLOT_FADE + SLOT_BLUR_TAIL,
- Color.Black,
- )
- } else {
- softening(
- (softenFrom - 0.16f).coerceAtLeast(0f),
- softenFrom + 0.03f,
- Color.Black,
- )
- },
- ),
- blendMode = BlendMode.DstIn,
+ val stops = if (fadeToPage) {
+ softening(softenFrom, softenTo, Color.Black)
+ } else if (extendPicture) {
+ softening(
+ 1f - SLOT_FADE - SLOT_BLUR_LEAD,
+ 1f - SLOT_FADE + SLOT_BLUR_TAIL,
+ Color.Black,
+ )
+ } else {
+ softening(
+ (softenFrom - 0.16f).coerceAtLeast(0f),
+ softenFrom + 0.03f,
+ Color.Black,
+ )
+ }
+ val brush = maskedPictureBrush(
+ frame?.soft ?: blur,
+ shown.zoom,
+ size,
+ stops,
)
+ onDrawBehind { drawRect(brush = brush) }
},
)
}
@@ -849,6 +831,39 @@ private const val FRAME_SOFTEN = 2
/** How far each copy of a frame moves [MotionFrames]' blend towards itself. */
private const val FRAME_FOLLOW = 0.2f
+/** The same cropped picture and DstIn fade, without an intermediate surface. */
+private fun maskedPictureBrush(
+ image: ImageBitmap,
+ zoom: Float,
+ size: androidx.compose.ui.geometry.Size,
+ stops: Array<Pair<Float, Color>>,
+): androidx.compose.ui.graphics.Brush {
+ val crop = croppedTo(image.width, image.height, size.width / size.height, zoom)
+ val picture = android.graphics.BitmapShader(
+ image.asAndroidBitmap(),
+ android.graphics.Shader.TileMode.CLAMP,
+ android.graphics.Shader.TileMode.CLAMP,
+ ).apply {
+ setLocalMatrix(android.graphics.Matrix().apply {
+ setRectToRect(
+ android.graphics.RectF(
+ crop.left.toFloat(), crop.top.toFloat(),
+ crop.right.toFloat(), crop.bottom.toFloat(),
+ ),
+ android.graphics.RectF(0f, 0f, size.width, size.height),
+ android.graphics.Matrix.ScaleToFit.FILL,
+ )
+ })
+ }
+ val mask = android.graphics.LinearGradient(
+ 0f, 0f, 0f, size.height,
+ stops.map { it.second.toArgb() }.toIntArray(),
+ stops.map { it.first }.toFloatArray(),
+ android.graphics.Shader.TileMode.CLAMP,
+ )
+ return ShaderBrush(android.graphics.ComposeShader(picture, mask, android.graphics.PorterDuff.Mode.DST_IN))
+}
+
/**
* Draws [image] filling this area, cropped at the sides or the top and bottom
* to its shape and centred, the way ContentScale.Crop lays out the still.
diff --git a/app/src/main/java/dev/lelonio/square/ui/glass/LiquidBottomTabs.kt b/app/src/main/java/dev/lelonio/square/ui/glass/LiquidBottomTabs.kt
index 27bc469..fdd1160 100644
--- a/app/src/main/java/dev/lelonio/square/ui/glass/LiquidBottomTabs.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/glass/LiquidBottomTabs.kt
@@ -22,6 +22,7 @@ import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
+import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
@@ -69,6 +70,9 @@ import kotlinx.coroutines.launch
import kotlin.math.abs
import kotlin.math.sign
+// The tinted copy is a drawing source, not another shared-transition endpoint.
+internal val LocalLiquidBottomTabsReplica = staticCompositionLocalOf { false }
+
@Composable
fun LiquidBottomTabs(
selectedTabIndex: () -> Int,
@@ -109,6 +113,7 @@ fun LiquidBottomTabs(
* keeps upstream's.
*/
indicatorColor: Color? = null,
+ drawContainer: Boolean = true,
content: @Composable RowScope.() -> Unit
) {
val isLightTheme = !isSystemInDarkTheme()
@@ -234,7 +239,7 @@ fun LiquidBottomTabs(
// other pane. The puck below keeps its own recipe on purpose —
// what it does is press-driven, a bend and a rim that only exist
// while a finger is on it, which is not a material.
- .liquidGlass(
+ .then(if (drawContainer) Modifier.liquidGlass(
config = LocalGlassEffectConfig.current,
shape = ContinuousCapsule(),
// Follows the setting like everything else; the shell of a
@@ -247,7 +252,7 @@ fun LiquidBottomTabs(
scaleX = scale
scaleY = scale
},
- )
+ ) else Modifier)
.then(interactiveHighlight.modifier)
.height(height)
.fillMaxWidth()
@@ -257,6 +262,7 @@ fun LiquidBottomTabs(
)
CompositionLocalProvider(
+ LocalLiquidBottomTabsReplica provides true,
LocalLiquidBottomTabScale provides {
lerp(1f, 1.2f, dampedDragAnimation.pressProgress)
}
diff --git a/app/src/main/java/dev/lelonio/square/ui/glass/floatingtabbar/FloatingTabBar.kt b/app/src/main/java/dev/lelonio/square/ui/glass/floatingtabbar/FloatingTabBar.kt
index 0c0f9f6..8e5cb31 100644
--- a/app/src/main/java/dev/lelonio/square/ui/glass/floatingtabbar/FloatingTabBar.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/glass/floatingtabbar/FloatingTabBar.kt
@@ -10,12 +10,11 @@
* SharedTransitionScope.sharedElement. Compiling the source here keeps it
* binary-compatible.
*
- * Kept as close to upstream as it can be. The library animates the fold itself
- * — shared bounds on a spring, with the crossfade only clearing away what has
- * no counterpart in the other state — and every local attempt to describe that
- * movement by hand made it worse. What this app adds goes through the seams the
- * library already has: the glass through tabBarContentModifier, the selection
- * shape through colors.selectedTabBackgroundColor.
+ * Shared elements carry the tab group, search and player between layouts.
+ * A single bounds animation moves and resizes each surface continuously.
+ * Search crossfades its circle and tab within shared bounds; the incoming
+ * tab stays clipped to the growing group's surface.
+ * Glass styling remains supplied through tabBarContentModifier.
*/
@file:OptIn(ExperimentalSharedTransitionApi::class)
@@ -23,6 +22,8 @@
package dev.lelonio.square.ui.glass.floatingtabbar
import androidx.compose.animation.AnimatedContent
+import androidx.compose.animation.BoundsTransform
+import androidx.compose.animation.SizeTransform
import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.EnterExitState
import androidx.compose.animation.ExperimentalSharedTransitionApi
@@ -30,8 +31,11 @@ import androidx.compose.animation.SharedTransitionLayout
import androidx.compose.animation.SharedTransitionScope
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.animateContentSize
+import androidx.compose.animation.core.LinearEasing
+import androidx.compose.animation.core.CubicBezierEasing
import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.animateFloatAsState
+import androidx.compose.animation.core.tween
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.keyframes
@@ -61,6 +65,8 @@ import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
+import androidx.compose.runtime.CompositionLocalProvider
+import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.LaunchedEffect
@@ -295,6 +301,27 @@ fun FloatingTabBar(
) {
val scope = remember(contentKey) { FloatingTabBarScopeImpl().apply { content() } }
+ if (expandedTabs != null && !standaloneInExpanded && expandedTabsHeight != Dp.Unspecified) {
+ SharedTransitionLayout(modifier = modifier) {
+ GeometricFloatingTabBar(
+ scope = scope,
+ selectedTabKey = selectedTabKey,
+ isInline = scrollConnection.isInline,
+ searchMode = searchMode && searchBarContent != null,
+ onExpand = scrollConnection::expand,
+ expandedTabs = expandedTabs,
+ accessory = expandedAccessory ?: inlineAccessory,
+ searchBarContent = searchBarContent,
+ rowHeight = expandedTabsHeight,
+ tabBarContentModifier = tabBarContentModifier,
+ colors = colors,
+ shapes = shapes,
+ sizes = sizes,
+ )
+ }
+ return
+ }
+
val isAccessoryShared = inlineAccessory != null && expandedAccessory != null
// Three shapes rather than two; see searchMode.
@@ -305,29 +332,15 @@ fun FloatingTabBar(
}
SharedTransitionLayout(modifier = modifier) {
- // LOCAL CHANGE: the fold as two drops of water rather than two layouts.
- //
- // Held as a transition of its own so the merge can be timed against it:
- // the shapes travel through the middle of the fold, and it is only there
- // that the blur-and-threshold pass has anything to join. The radius runs
- // 0 at both ends and peaks halfway, which is what draws the neck between
- // the circles, thins it, and pinches it off. See gooey.
val fold = updateTransition(targetState = visual, label = "fold")
- val gooeyRadius by fold.animateFloat(
+ fold.AnimatedContent(
transitionSpec = {
- keyframes {
- durationMillis = GooeyDurationMs
- 0f at 0
- GooeyPeakBlur at GooeyDurationMs / 2 using FastOutSlowInEasing
- 0f at GooeyDurationMs
- }
+ (fadeIn(tween(FoldDurationMs, easing = FoldEasing)) togetherWith
+ fadeOut(tween(FoldDurationMs, easing = FoldEasing)))
+ .using(SizeTransform(clip = false) { _, _ ->
+ tween(FoldDurationMs, easing = FoldEasing)
+ })
},
- label = "gooey",
- ) { 0f }
-
- Box(Modifier.gooey { gooeyRadius }) {
- fold.AnimatedContent(
- transitionSpec = { fadeIn() togetherWith fadeOut() },
contentAlignment = Alignment.BottomStart
) { target ->
if (target == FloatingTabBarVisual.SEARCH) {
@@ -379,21 +392,33 @@ fun FloatingTabBar(
)
}
}
- }
}
}
-/**
- * The merge, and how long it lasts.
- *
- * Blur enough for the two circles' edges to reach each other and no more: at a
- * larger radius the icons inside them smear, and the pass covers the whole bar.
- * The window is the fold's own — the shapes are between places for about that
- * long — and the radius is zero at both ends, so at rest this costs nothing: no
- * offscreen layer, no filter. See gooey.
- */
-private const val GooeyPeakBlur = 14f
-private const val GooeyDurationMs = 260
+// Measured against the supplied clip: one continuous movement, with a quick
+// departure and a soft settle. All bounds use the same progress curve.
+internal const val FoldDurationMs = 320
+private val FoldEasing = CubicBezierEasing(0.2f, 0f, 0f, 1f)
+internal val LocalFloatingTabBarInline = staticCompositionLocalOf { false }
+
+// Search changes its content (glass circle -> labelled tab), so retain both
+// versions during the morph. sharedElement would draw only the incoming tab,
+// dropping the circle before the growing bar has reached it.
+@Composable
+internal fun SharedTransitionScope.sharedSearchTab(
+ animatedVisibilityScope: AnimatedVisibilityScope,
+): Modifier = Modifier.sharedBounds(
+ sharedContentState = rememberSharedContentState("standaloneTab"),
+ animatedVisibilityScope = animatedVisibilityScope,
+ enter = fadeIn(tween(FoldDurationMs, easing = LinearEasing)),
+ exit = fadeOut(tween(FoldDurationMs, easing = LinearEasing)),
+ boundsTransform = FoldBoundsTransform,
+ zIndexInOverlay = 2f,
+)
+
+internal val FoldBoundsTransform = BoundsTransform { _, _ ->
+ tween(FoldDurationMs, easing = FoldEasing)
+}
/** LOCAL CHANGE: the three shapes the bar can take; see searchMode. */
private enum class FloatingTabBarVisual { INLINE, EXPANDED, SEARCH }
@@ -472,11 +497,7 @@ private fun SharedTransitionScope.SearchExpandedBar(
Modifier
.weight(1f)
.fillMaxHeight()
- .sharedElement(
- sharedContentState = rememberSharedContentState("standaloneTab"),
- animatedVisibilityScope = animatedVisibilityScope,
- zIndexInOverlay = 1f
- )
+ .then(sharedSearchTab(animatedVisibilityScope))
.shadow(shape = shapes.tabBarShape, elevation = elevations.expandedElevation)
.background(color = colors.backgroundColor, shape = shapes.tabBarShape)
.clip(shapes.tabBarShape)
@@ -748,6 +769,7 @@ private fun SharedTransitionScope.InlineTab(
.sharedElement(
sharedContentState = rememberSharedContentState("tabGroup"),
animatedVisibilityScope = animatedVisibilityScope,
+ boundsTransform = FoldBoundsTransform,
zIndexInOverlay = 1f
)
.shadow(
@@ -773,6 +795,7 @@ private fun SharedTransitionScope.InlineTab(
Modifier.sharedElement(
sharedContentState = rememberSharedContentState("tab#${inlineTab.key}-icon"),
animatedVisibilityScope = animatedVisibilityScope,
+ boundsTransform = FoldBoundsTransform,
zIndexInOverlay = 1f
)
) {
@@ -802,11 +825,7 @@ private fun SharedTransitionScope.InlineStandaloneTab(
isInline = true,
isStandalone = true,
modifier = modifier
- .sharedElement(
- sharedContentState = rememberSharedContentState("standaloneTab"),
- animatedVisibilityScope = animatedVisibilityScope,
- zIndexInOverlay = 1f
- )
+ .then(sharedSearchTab(animatedVisibilityScope))
.shadow(
shape = shapes.standaloneTabShape,
elevation = elevations.inlineElevation
@@ -843,27 +862,31 @@ private fun SharedTransitionScope.InlineAccessory(
if (isAccessoryShared) {
Modifier.sharedElement(
sharedContentState = rememberSharedContentState("accessory"),
- animatedVisibilityScope = animatedVisibilityScope
+ animatedVisibilityScope = animatedVisibilityScope,
+ boundsTransform = FoldBoundsTransform,
+ zIndexInOverlay = 3f,
)
} else {
Modifier.animateEnterExitAccessory(
sharedTransitionScope = this,
- animatedVisibilityScope = animatedVisibilityScope
+ animatedVisibilityScope = animatedVisibilityScope,
)
}
)
) {
- accessory(
- Modifier
- .fillMaxSize()
- .shadow(
- shape = shapes.accessoryShape,
- elevation = elevations.inlineElevation
- )
- .background(color = colors.accessoryBackgroundColor, shapes.accessoryShape)
- .clip(shapes.accessoryShape),
- animatedVisibilityScope
- )
+ CompositionLocalProvider(LocalFloatingTabBarInline provides true) {
+ accessory(
+ Modifier
+ .fillMaxSize()
+ .shadow(
+ shape = shapes.accessoryShape,
+ elevation = elevations.inlineElevation
+ )
+ .background(color = colors.accessoryBackgroundColor, shapes.accessoryShape)
+ .clip(shapes.accessoryShape),
+ animatedVisibilityScope
+ )
+ }
}
}
}
@@ -950,6 +973,8 @@ private fun SharedTransitionScope.ExpandedBar(
.sharedElement(
sharedContentState = rememberSharedContentState("tabGroup"),
animatedVisibilityScope = animatedVisibilityScope,
+ boundsTransform = FoldBoundsTransform,
+ clipInOverlayDuringTransition = OverlayClip(shapes.tabBarShape),
zIndexInOverlay = 1f
)
) {
@@ -1019,26 +1044,30 @@ private fun SharedTransitionScope.ExpandedAccessory(
if (isAccessoryShared) {
Modifier.sharedElement(
sharedContentState = rememberSharedContentState("accessory"),
- animatedVisibilityScope = animatedVisibilityScope
+ animatedVisibilityScope = animatedVisibilityScope,
+ boundsTransform = FoldBoundsTransform,
+ zIndexInOverlay = 3f,
)
} else {
Modifier.animateEnterExitAccessory(
sharedTransitionScope = this,
- animatedVisibilityScope = animatedVisibilityScope
+ animatedVisibilityScope = animatedVisibilityScope,
)
}
)
) {
- accessory(
- Modifier
- .shadow(
- shape = shapes.accessoryShape,
- elevation = elevations.expandedElevation
- )
- .background(color = colors.accessoryBackgroundColor, shapes.accessoryShape)
- .clip(shapes.accessoryShape),
- animatedVisibilityScope
- )
+ CompositionLocalProvider(LocalFloatingTabBarInline provides false) {
+ accessory(
+ Modifier
+ .shadow(
+ shape = shapes.accessoryShape,
+ elevation = elevations.expandedElevation
+ )
+ .background(color = colors.accessoryBackgroundColor, shapes.accessoryShape)
+ .clip(shapes.accessoryShape),
+ animatedVisibilityScope
+ )
+ }
}
}
@Composable
@@ -1078,6 +1107,7 @@ private fun SharedTransitionScope.ExpandedTabs(
.sharedElement(
sharedContentState = rememberSharedContentState("tabGroup"),
animatedVisibilityScope = animatedVisibilityScope,
+ boundsTransform = FoldBoundsTransform,
zIndexInOverlay = 1f
)
.shadow(
@@ -1132,12 +1162,13 @@ private fun SharedTransitionScope.ExpandedTabs(
Modifier.sharedElement(
sharedContentState = rememberSharedContentState("tab#${tab.key}-icon"),
animatedVisibilityScope = animatedVisibilityScope,
+ boundsTransform = FoldBoundsTransform,
zIndexInOverlay = 1f
)
} else {
Modifier.animateEnterExitTab(
sharedTransitionScope = this@ExpandedTabs,
- animatedVisibilityScope = animatedVisibilityScope
+ animatedVisibilityScope = animatedVisibilityScope,
)
}
) {
@@ -1148,7 +1179,7 @@ private fun SharedTransitionScope.ExpandedTabs(
Box(
Modifier.animateEnterExitTab(
sharedTransitionScope = this@ExpandedTabs,
- animatedVisibilityScope = animatedVisibilityScope
+ animatedVisibilityScope = animatedVisibilityScope,
)
) {
tab.title()
@@ -1197,11 +1228,7 @@ private fun SharedTransitionScope.ExpandedStandaloneTab(
isInline = false,
isStandalone = true,
modifier = modifier
- .sharedElement(
- sharedContentState = rememberSharedContentState("standaloneTab"),
- animatedVisibilityScope = animatedVisibilityScope,
- zIndexInOverlay = 1f
- )
+ .then(sharedSearchTab(animatedVisibilityScope))
.shadow(
shape = shapes.standaloneTabShape,
elevation = elevations.expandedElevation
@@ -1276,9 +1303,9 @@ private fun Modifier.animateEnterExitTab(
animatedVisibilityScope: AnimatedVisibilityScope
): Modifier = with(sharedTransitionScope) {
with(animatedVisibilityScope) {
- val enterStartFraction = 0.5f
- val enterEndFraction = 0.8f
- val durationMs = 150
+ val enterStartFraction = 0.15f
+ val enterEndFraction = 0.85f
+ val durationMs = FoldDurationMs
val animatedAlpha by transition.animateFloat(
transitionSpec = {
@@ -1297,7 +1324,7 @@ private fun Modifier.animateEnterExitTab(
}
}
- val blurRadius = with(LocalDensity.current) { 50.dp.toPx() }
+ val blurRadius = with(LocalDensity.current) { 8.dp.toPx() }
val animatedBlur by transition.animateFloat(
transitionSpec = {
keyframes {
@@ -1335,7 +1362,7 @@ private fun Modifier.animateEnterExitTab(
}
}
-private class FloatingTabBarScopeImpl : FloatingTabBarScope {
+internal class FloatingTabBarScopeImpl : FloatingTabBarScope {
val tabs = mutableStateListOf<FloatingTabBarTab>()
var standaloneTab: FloatingTabBarTab? by mutableStateOf(null)
private set
@@ -1389,7 +1416,7 @@ private class FloatingTabBarScopeImpl : FloatingTabBarScope {
}
}
-private data class FloatingTabBarTab(
+internal data class FloatingTabBarTab(
val key: Any,
val title: @Composable () -> Unit,
val icon: @Composable () -> Unit,
@@ -1524,4 +1551,4 @@ object FloatingTabBarDefaults {
inlineElevation = inlineElevation,
expandedElevation = expandedElevation,
)
-}
\ No newline at end of file
+}
diff --git a/app/src/main/java/dev/lelonio/square/ui/glass/floatingtabbar/FoldGeometry.kt b/app/src/main/java/dev/lelonio/square/ui/glass/floatingtabbar/FoldGeometry.kt
new file mode 100644
index 0000000..05fc845
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/ui/glass/floatingtabbar/FoldGeometry.kt
@@ -0,0 +1,41 @@
+package dev.lelonio.square.ui.glass.floatingtabbar
+
+import kotlin.math.roundToInt
+
+internal data class FoldGeometry(
+ val height: Int,
+ val rowY: Int,
+ val navWidth: Int,
+ val searchWidth: Int,
+ val searchX: Int,
+ val selectedX: Int,
+ val playerX: Int,
+ val playerY: Int,
+)
+
+/** Pure coordinates: reversing a fold retraces exactly the same geometry. */
+internal fun foldGeometry(
+ width: Int, expandedRowHeight: Int, spacing: Int, playerHeight: Int, hasPlayer: Boolean,
+ fold: Float, search: Float, tabsCount: Int, selectedIndex: Int, inset: Float, collapsedRowHeight: Int,
+): FoldGeometry {
+ fun mix(a: Float, b: Float, progress: Float) = a + (b - a) * progress
+ val p = fold.coerceIn(0f, 1f)
+ val s = search.coerceIn(0f, 1f)
+ val rowHeight = mix(expandedRowHeight.toFloat(), collapsedRowHeight.toFloat(), p).roundToInt()
+ val circle = rowHeight.coerceAtMost(width)
+ val rowY = if (hasPlayer) ((playerHeight + spacing) * (1f - p)).roundToInt() else 0
+ val tabWidth = (width - 2 * inset) / tabsCount.coerceAtLeast(1)
+ val searchCenter = inset + tabWidth * (tabsCount - 0.5f)
+ val searchWidth = mix(circle.toFloat(), (width - circle - spacing).coerceAtLeast(circle).toFloat(), s * (1f - p)).roundToInt()
+ val detachedSearchX = mix(searchCenter - circle / 2f, (width - circle).toFloat(), p)
+ return FoldGeometry(
+ height = rowHeight + rowY,
+ rowY = rowY,
+ navWidth = mix(mix(width.toFloat(), circle.toFloat(), s), circle.toFloat(), p).roundToInt(),
+ searchWidth = searchWidth,
+ searchX = mix(detachedSearchX, (width - searchWidth).toFloat(), s).roundToInt(),
+ selectedX = mix(inset + tabWidth * (selectedIndex + 0.5f) - circle / 2f, 0f, maxOf(p, s)).roundToInt(),
+ playerX = mix(0f, (circle + spacing).toFloat(), p).roundToInt(),
+ playerY = (rowY - mix((playerHeight + spacing).toFloat(), (playerHeight - rowHeight) / 2f, p)).roundToInt(),
+ )
+}
diff --git a/app/src/main/java/dev/lelonio/square/ui/glass/floatingtabbar/GeometricFloatingTabBar.kt b/app/src/main/java/dev/lelonio/square/ui/glass/floatingtabbar/GeometricFloatingTabBar.kt
new file mode 100644
index 0000000..207fbc5
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/ui/glass/floatingtabbar/GeometricFloatingTabBar.kt
@@ -0,0 +1,207 @@
+@file:OptIn(androidx.compose.animation.ExperimentalSharedTransitionApi::class)
+
+package dev.lelonio.square.ui.glass.floatingtabbar
+
+import androidx.compose.animation.AnimatedVisibility
+import androidx.compose.animation.SharedTransitionScope
+import androidx.compose.animation.core.CubicBezierEasing
+import androidx.compose.animation.core.animateFloatAsState
+import androidx.compose.animation.core.tween
+import androidx.compose.foundation.background
+import androidx.compose.foundation.clickable
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.height
+import androidx.compose.foundation.layout.size
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.CompositionLocalProvider
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.staticCompositionLocalOf
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.draw.clip
+import androidx.compose.ui.draw.drawWithContent
+import androidx.compose.ui.graphics.drawscope.clipRect
+import androidx.compose.ui.graphics.graphicsLayer
+import androidx.compose.ui.layout.Layout
+import androidx.compose.ui.semantics.clearAndSetSemantics
+import androidx.compose.ui.unit.Constraints
+import androidx.compose.ui.unit.Dp
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.util.lerp
+import kotlin.math.roundToInt
+
+internal val LocalGeometricFloatingTabBar = staticCompositionLocalOf { false }
+internal val LocalFloatingTabBarCollapseProgress = staticCompositionLocalOf<(() -> Float)?> { null }
+
+/**
+ * One coordinate system and one clock for the fold. The tab content is measured
+ * at its resting width; only its surface shrinks. Search and the player are
+ * single persistent nodes, so neither gets a new destination from a moving
+ * shared-element parent. All coordinates below are relative to the bottom row.
+ */
+@Composable
+internal fun SharedTransitionScope.GeometricFloatingTabBar(
+ scope: FloatingTabBarScopeImpl,
+ selectedTabKey: Any?,
+ isInline: Boolean,
+ searchMode: Boolean,
+ onExpand: () -> Unit,
+ expandedTabs: @Composable SharedTransitionScope.(Modifier, androidx.compose.animation.AnimatedVisibilityScope) -> Unit,
+ accessory: (@Composable SharedTransitionScope.(Modifier, androidx.compose.animation.AnimatedVisibilityScope) -> Unit)?,
+ searchBarContent: (@Composable (Modifier) -> Unit)?,
+ rowHeight: Dp,
+ tabBarContentModifier: Modifier,
+ colors: FloatingTabBarColors,
+ shapes: FloatingTabBarShapes,
+ sizes: FloatingTabBarSizes,
+) {
+ val fold = animateFloatAsState(
+ if (isInline) 1f else 0f,
+ tween(320, easing = CubicBezierEasing(0.2f, 0f, 0f, 1f)),
+ label = "barGeometryFold",
+ )
+ val search = animateFloatAsState(
+ if (searchMode) 1f else 0f,
+ tween(320, easing = CubicBezierEasing(0.2f, 0f, 0f, 1f)),
+ label = "barGeometrySearch",
+ )
+ val collapseProgress = remember(fold) { { fold.value } }
+ val selected = scope.getInlineTab(selectedTabKey)
+ val searchTab = scope.standaloneTab ?: scope.tabs.lastOrNull()
+ val searchTitle = scope.tabs.lastOrNull()?.title
+ val selectedIndex = scope.tabs.indexOf(selected).coerceAtLeast(0)
+ val tabsCount = scope.tabs.size.coerceAtLeast(1)
+ val ownsSelectedSlot = selected?.key == selectedTabKey
+ val sharedScope = this
+
+ // This visibility scope only satisfies the existing content contract. It
+ // never changes visibility and does not participate in the fold.
+ AnimatedVisibility(visible = true) {
+ val visibility = this
+ Layout(
+ modifier = Modifier.fillMaxWidth(),
+ content = {
+ // 0: the single navigation surface.
+ Box(Modifier.background(colors.backgroundColor, shapes.tabBarShape)
+ .clip(shapes.tabBarShape).then(tabBarContentModifier))
+ // 1: resting-width tabs, fading into the selected icon.
+ Box(Modifier.graphicsLayer {
+ alpha = (1f - fold.value * 3f).coerceIn(0f, 1f) * (1f - search.value)
+ }.drawWithContent {
+ val p = fold.value
+ val width = lerp(lerp(size.width, size.height, search.value), size.height, p)
+ val contentScope = this
+ clipRect(right = width) { contentScope.drawContent() }
+ }.then(if (isInline || searchMode) Modifier.clearAndSetSemantics {} else Modifier)) {
+ CompositionLocalProvider(LocalGeometricFloatingTabBar provides true) {
+ sharedScope.expandedTabs(Modifier.fillMaxWidth(), visibility)
+ }
+ }
+ // 2: one selected control, including its label at rest.
+ Box(Modifier.graphicsLayer {
+ alpha = if (ownsSelectedSlot) 1f else maxOf(fold.value, search.value)
+ }.then(if (isInline || searchMode) Modifier.clickable {
+ if (isInline) onExpand() else selected?.onClick?.invoke()
+ } else Modifier).then(if (!ownsSelectedSlot && !isInline && !searchMode) Modifier.clearAndSetSemantics {} else Modifier)) {
+ GeometricTabContent(
+ icon = { selected?.icon?.invoke() },
+ title = { selected?.title?.invoke() },
+ titleFraction = { (1f - fold.value) * (1f - search.value) },
+ )
+ }
+ // 3: search's glass exists only while detached from navigation.
+ Box(Modifier.graphicsLayer { alpha = maxOf(fold.value, search.value) }
+ .background(colors.backgroundColor, shapes.standaloneTabShape)
+ .clip(shapes.standaloneTabShape).then(tabBarContentModifier))
+ // 4: one magnifier and label, sharing the surface's geometry.
+ Box(Modifier.graphicsLayer { alpha = 1f - search.value * (1f - fold.value) }
+ .clickable(enabled = !searchMode || isInline) { searchTab?.onClick?.invoke() }
+ .then(if (searchMode && !isInline) Modifier.clearAndSetSemantics {} else Modifier)) {
+ GeometricTabContent(
+ icon = { searchTab?.icon?.invoke() },
+ title = { searchTitle?.invoke() },
+ titleFraction = { 1f - fold.value },
+ )
+ }
+ // 5: search field replaces the magnifier when entering search.
+ Box(Modifier.graphicsLayer { alpha = search.value * (1f - fold.value) }
+ .then(if (!searchMode || isInline) Modifier.clearAndSetSemantics {} else Modifier)) {
+ if (searchMode || search.value > 0f) {
+ searchBarContent?.invoke(Modifier.fillMaxWidth().height(rowHeight))
+ }
+ }
+ // 6: one player whose width, height and content all use fold.
+ Box {
+ if (accessory != null) {
+ CompositionLocalProvider(LocalFloatingTabBarCollapseProgress provides collapseProgress) {
+ sharedScope.accessory(
+ Modifier.fillMaxWidth().background(colors.accessoryBackgroundColor, shapes.accessoryShape)
+ .clip(shapes.accessoryShape), visibility,
+ )
+ }
+ }
+ }
+ },
+ ) { children, constraints ->
+ val width = constraints.maxWidth
+ val expandedHeight = rowHeight.roundToPx()
+ // 36dp artwork plus 6dp padding above and below in the compact player.
+ val compactHeight = if (accessory != null) 48.dp.roundToPx() else expandedHeight
+ val spacing = sizes.componentSpacing.roundToPx()
+ val p = fold.value
+ val s = search.value
+ val height = lerp(expandedHeight.toFloat(), compactHeight.toFloat(), p).roundToInt()
+ val circle = height.coerceAtMost(width)
+ val playerWidth = lerp(width.toFloat(), (width - 2 * (compactHeight + spacing)).coerceAtLeast(0).toFloat(), p).roundToInt()
+ val player = children[6].measure(Constraints(minWidth = playerWidth, maxWidth = playerWidth))
+ val geometry = foldGeometry(width, expandedHeight, spacing, player.height, accessory != null,
+ p, s, tabsCount, selectedIndex, 4.dp.toPx(), compactHeight)
+ val totalHeight = geometry.height
+ val rowY = geometry.rowY
+ val navWidth = geometry.navWidth
+ val surface = children[0].measure(Constraints.fixed(navWidth, height))
+ val tabs = children[1].measure(Constraints.fixed(width, height))
+ val selectedButton = children[2].measure(Constraints.fixed(circle, height))
+ val searchWidth = geometry.searchWidth
+ val searchX = geometry.searchX
+ val searchSurface = children[3].measure(Constraints.fixed(searchWidth, height))
+ val searchControl = children[4].measure(Constraints.fixed(circle, height))
+ val field = children[5].measure(Constraints.fixed(searchWidth, height))
+ val selectedX = geometry.selectedX
+ val playerX = geometry.playerX
+ val playerY = geometry.playerY
+ layout(width, totalHeight) {
+ surface.placeRelative(0, rowY)
+ tabs.placeRelative(0, rowY)
+ selectedButton.placeRelative(selectedX, rowY)
+ searchSurface.placeRelative(searchX, rowY)
+ searchControl.placeRelative(searchX, rowY)
+ field.placeRelative(searchX, rowY)
+ player.placeRelative(playerX, playerY)
+ }
+ }
+ }
+}
+
+@Composable
+private fun GeometricTabContent(
+ icon: @Composable () -> Unit,
+ title: @Composable () -> Unit,
+ titleFraction: () -> Float,
+) {
+ Layout(content = {
+ Box(Modifier.size(26.dp), contentAlignment = Alignment.Center) { icon() }
+ Box(Modifier.graphicsLayer { alpha = titleFraction() }) { title() }
+ }) { children, constraints ->
+ val iconPlaceable = children[0].measure(Constraints.fixed(26.dp.roundToPx(), 26.dp.roundToPx()))
+ val titlePlaceable = children[1].measure(Constraints(maxWidth = constraints.maxWidth))
+ val gap = 2.dp.roundToPx()
+ val fullTop = (constraints.maxHeight - iconPlaceable.height - gap - titlePlaceable.height) / 2f
+ val top = lerp((constraints.maxHeight - iconPlaceable.height) / 2f, fullTop, titleFraction()).roundToInt()
+ layout(constraints.maxWidth, constraints.maxHeight) {
+ iconPlaceable.placeRelative((constraints.maxWidth - iconPlaceable.width) / 2, top)
+ titlePlaceable.placeRelative((constraints.maxWidth - titlePlaceable.width) / 2, top + iconPlaceable.height + gap)
+ }
+ }
+}
diff --git a/app/src/main/java/dev/lelonio/square/ui/player/FloatingMiniPlayer.kt b/app/src/main/java/dev/lelonio/square/ui/player/FloatingMiniPlayer.kt
index e49d3aa..50cc04c 100644
--- a/app/src/main/java/dev/lelonio/square/ui/player/FloatingMiniPlayer.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/player/FloatingMiniPlayer.kt
@@ -94,12 +94,16 @@ fun FloatingMiniPlayer(
remoteLabel: String? = null,
/** Folded: the strip has room for the song and one button, nothing else. */
inline: Boolean = false,
+ collapseProgress: Float? = null,
onClick: () -> Unit,
onTogglePlay: () -> Unit,
onNext: () -> Unit,
onPrevious: () -> Unit,
onSeek: (Long) -> Unit,
) {
+ val fold = (collapseProgress ?: if (inline) 1f else 0f).coerceIn(0f, 1f)
+ val artSize = androidx.compose.ui.unit.lerp(ArtSize, 36.dp, fold)
+ val controlSize = androidx.compose.ui.unit.lerp(ControlSize, 36.dp, fold)
val scope = rememberCoroutineScope()
val layoutDirection = LocalLayoutDirection.current
@@ -181,28 +185,28 @@ fun FloatingMiniPlayer(
onClick = onClick,
)
.padding(
- horizontal = if (inline) 10.dp else 12.dp,
- vertical = if (inline) 6.dp else 8.dp,
+ horizontal = androidx.compose.ui.unit.lerp(12.dp, 10.dp, fold),
+ vertical = androidx.compose.ui.unit.lerp(8.dp, 6.dp, fold),
),
) {
Artwork(
url = state.artworkUrl,
title = state.title,
- modifier = Modifier.size(if (inline) 36.dp else ArtSize),
+ modifier = Modifier.size(artSize),
corner = 9.dp,
decodeSize = ArtSize,
)
- Spacer(Modifier.width(if (inline) 8.dp else 10.dp))
+ Spacer(Modifier.width(androidx.compose.ui.unit.lerp(10.dp, 8.dp, fold)))
Column(Modifier.weight(1f)) {
Text(
text = state.title,
- style = if (inline) {
- MaterialTheme.typography.bodySmall
- } else {
- MaterialTheme.typography.bodyMedium
- },
+ style = androidx.compose.ui.text.lerp(
+ MaterialTheme.typography.bodyMedium,
+ MaterialTheme.typography.bodySmall,
+ fold,
+ ),
color = contentColor,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
@@ -214,17 +218,17 @@ fun FloatingMiniPlayer(
contentDescription = null,
tint = MiniPlayerAccent,
modifier = Modifier
- .size(if (inline) 10.dp else 12.dp)
+ .size(androidx.compose.ui.unit.lerp(12.dp, 10.dp, fold))
.padding(end = 0.dp),
)
}
Text(
text = remoteLabel ?: state.artist,
- style = if (inline) {
- MaterialTheme.typography.bodySmall.copy(fontSize = 10.sp)
- } else {
- MaterialTheme.typography.bodySmall
- },
+ style = androidx.compose.ui.text.lerp(
+ MaterialTheme.typography.bodySmall,
+ MaterialTheme.typography.bodySmall.copy(fontSize = 10.sp),
+ fold,
+ ),
color = if (remoteLabel != null) {
MiniPlayerAccent
} else {
@@ -239,8 +243,10 @@ fun FloatingMiniPlayer(
// Skip first and play second, which is the order asked for: the
// button that starts the music is the one nearest the thumb.
- if (!inline) {
- IconButton(onClick = onNext, modifier = Modifier.size(ControlSize)) {
+ if (fold < 1f) {
+ IconButton(onClick = onNext, enabled = fold < 0.5f,
+ modifier = Modifier.width(ControlSize * (1f - fold)).height(controlSize)
+ .graphicsLayer { alpha = 1f - fold }.clipToBounds()) {
Icon(
PhosphorIcons.Fill.SkipForward,
contentDescription = stringResource(R.string.next),
@@ -252,7 +258,7 @@ fun FloatingMiniPlayer(
IconButton(
onClick = onTogglePlay,
- modifier = Modifier.size(if (inline) 36.dp else ControlSize),
+ modifier = Modifier.size(controlSize),
) {
// The same ring the full player draws, for the same reason: a
// track being fetched can take long enough that a tap on play
@@ -263,7 +269,7 @@ fun FloatingMiniPlayer(
CircularProgressIndicator(
color = contentColor.copy(alpha = 0.5f),
strokeWidth = 1.5.dp,
- modifier = Modifier.size(if (inline) 30.dp else 34.dp),
+ modifier = Modifier.size(androidx.compose.ui.unit.lerp(34.dp, 30.dp, fold)),
)
}
Icon(
@@ -272,7 +278,7 @@ fun FloatingMiniPlayer(
if (state.wantsPlay) R.string.pause else R.string.play,
),
tint = contentColor,
- modifier = Modifier.size(if (inline) 20.dp else 24.dp),
+ modifier = Modifier.size(androidx.compose.ui.unit.lerp(24.dp, 20.dp, fold)),
)
}
}