Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.
Kotlin alternativeandroidandroid-applicationclientglassmorphisminnertubelibrespotliquid-glassspotifyspotify-connectspotifyapiyoutube-musicyoutube-playerytdlnis
A bar that folds without rebuilding the page under it
Lelonio3d74ff0d1ee4
diff --git a/app/src/main/java/dev/lelonio/square/ui/MainActivity.kt b/app/src/main/java/dev/lelonio/square/ui/MainActivity.ktindex efab7f0..9111149 100644--- a/app/src/main/java/dev/lelonio/square/ui/MainActivity.kt+++ b/app/src/main/java/dev/lelonio/square/ui/MainActivity.kt@@ -61,6 +61,14 @@ class MainActivity : ComponentActivity() {*/private var link by mutableStateOf<LinkRequest?>(null)+ /** The screen's fastest mode while a finger is on the app; see the class. */+ private val touchRefreshRate = TouchRefreshRate(this)++ override fun dispatchTouchEvent(event: android.view.MotionEvent): Boolean {+ touchRefreshRate.onTouch(event)+ return super.dispatchTouchEvent(event)+ }+override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)// Transparent bars; SquareTheme sets the icon colour, because it is the@@ -266,6 +274,7 @@ class MainActivity : ComponentActivity() {}override fun onStop() {+ touchRefreshRate.reset()runCatching { unregisterReceiver(listenRequest) }controller?.release()controller = nulldiff --git a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt b/app/src/main/java/dev/lelonio/square/ui/SquareApp.ktindex 8712494..1ff8148 100644--- a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt+++ b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt@@ -175,6 +175,7 @@ import androidx.compose.animation.togetherWithimport dev.lelonio.square.ui.theme.SquareThemeimport dev.lelonio.square.ui.theme.rememberArtworkColorimport dev.lelonio.square.ui.theme.rememberArtworkFootColor+import kotlinx.coroutines.flow.collectLatestimport kotlinx.coroutines.flow.firstimport kotlinx.coroutines.launchimport com.adamglin.PhosphorIcons@@ -227,6 +228,9 @@ private val expandSpec = spring<Float>(private val BottomBarHeight = 62.dp+/** How long the bar's height has to hold still before the lists follow it. */+private const val BAR_SETTLE_MS = 120L+/*** White, carrying a colour's hue.*@@ -908,7 +912,22 @@ fun SquareApp(// Measured rather than assumed. The constant this replaced was 62dp while// the bar is a 64dp capsule with padding around it and the navigation inset// under that, so the sheet sat about twenty over it.+ //+ // The open bar's height, taken once it has settled. The lists read this as+ // their bottom padding, and a height that followed the fold recomposed the+ // page under the bar and laid it out again on each frame of the animation,+ // while that page was being scrolled: on a mid-range phone that was most of+ // the frame, and the fold stuttered. Nor does the folded height count: a+ // page that reached its end while the bar was folding would have dropped by+ // the difference once it settled. Lists end above the open bar either way.var barHeight by remember { mutableStateOf(BottomBarHeight) }+ val measuredBarHeight = remember { kotlinx.coroutines.flow.MutableStateFlow(BottomBarHeight) }+ LaunchedEffect(measuredBarHeight) {+ measuredBarHeight.collectLatest { height ->+ kotlinx.coroutines.delay(BAR_SETTLE_MS)+ barHeight = height+ }+ }val density = LocalDensity.current// Recorded here rather than at the tap: this fires for auto-advance and for@@ -1965,7 +1984,11 @@ fun SquareApp(.graphicsLayer {alpha = (1f - expand.value * 3f).coerceIn(0f, 1f) * chrome}- .onSizeChanged { barHeight = with(density) { it.height.toDp() } },+ .onSizeChanged {+ if (!tabBarScroll.isInline) {+ measuredBarHeight.value = with(density) { it.height.toDp() }+ }+ },) {// The material comes from the settings now, provided once at// the top of the app; what is still local here is whichdiff --git a/app/src/main/java/dev/lelonio/square/ui/TouchRefreshRate.kt b/app/src/main/java/dev/lelonio/square/ui/TouchRefreshRate.ktnew file mode 100644index 0000000..294dc56--- /dev/null+++ b/app/src/main/java/dev/lelonio/square/ui/TouchRefreshRate.kt@@ -0,0 +1,94 @@+package dev.lelonio.square.ui++import android.app.Activity+import android.os.Build+import android.os.Handler+import android.os.Looper+import android.view.Display+import android.view.MotionEvent++/**+ * Asks for the screen's fastest mode while a finger is on the app, and gives+ * it back shortly after.+ *+ * Some phones decide the rate app by app. The Nothing Phone (3a) held this app+ * at 90 Hz while other apps scrolled at 120: it gives an app that asks only+ * for "fast" its middle rate, and a frame rate requested from a view is+ * clamped to that too. What it does honour is a window naming the display mode+ * it wants. Named for good, though, that mode keeps the panel at its top rate+ * over a picture that is not moving, which is battery spent on nothing. So it+ * is named when a finger comes down and released a little after the last one+ * lifts, which leaves time for a fling to settle and for the bar to finish+ * folding.+ *+ * Where the phone already runs at its top rate, or has only one, asking+ * changes nothing.+ */+internal class TouchRefreshRate(private val activity: Activity) {++ private val handler = Handler(Looper.getMainLooper())+ private val release = Runnable { request(NO_PREFERENCE) }++ /** The mode last written to the window, so a tap does not rewrite it. */+ private var requested = NO_PREFERENCE++ fun onTouch(event: MotionEvent) {+ when (event.actionMasked) {+ MotionEvent.ACTION_DOWN -> {+ handler.removeCallbacks(release)+ request(fastestMode())+ }+ MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {+ handler.removeCallbacks(release)+ handler.postDelayed(release, HOLD_MS)+ }+ }+ }++ /** Lets the mode go at once, for an activity that is leaving the screen. */+ fun reset() {+ handler.removeCallbacks(release)+ request(NO_PREFERENCE)+ }++ /**+ * The fastest mode at the resolution the screen is using now: a mode at+ * another resolution would be a resolution change, not a faster one.+ */+ private fun fastestMode(): Int {+ val display = display() ?: return NO_PREFERENCE+ val current = display.mode+ val fastest = display.supportedModes+ .filter {+ it.physicalWidth == current.physicalWidth &&+ it.physicalHeight == current.physicalHeight+ }+ .maxByOrNull { it.refreshRate }+ ?: return NO_PREFERENCE+ return fastest.modeId+ }++ @Suppress("DEPRECATION")+ private fun display(): Display? =+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {+ activity.display+ } else {+ activity.windowManager.defaultDisplay+ }++ private fun request(modeId: Int) {+ if (modeId == requested) return+ requested = modeId+ val window = activity.window+ val attributes = window.attributes+ attributes.preferredDisplayModeId = modeId+ window.attributes = attributes+ }++ private companion object {+ const val NO_PREFERENCE = 0++ /** Long enough for a fling and the bar's fold after the finger lifts. */+ const val HOLD_MS = 2_000L+ }+}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.ktindex 62de602..27bc469 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@@ -17,7 +17,6 @@ import androidx.compose.animation.core.animateFloatAsStateimport androidx.compose.animation.core.tweenimport androidx.compose.foundation.isSystemInDarkThemeimport androidx.compose.foundation.layout.Box-import androidx.compose.foundation.layout.BoxWithConstraintsimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.RowScopeimport androidx.compose.foundation.layout.fillMaxWidth@@ -28,6 +27,7 @@ import androidx.compose.runtime.CompositionLocalProviderimport androidx.compose.runtime.LaunchedEffectimport androidx.compose.runtime.derivedStateOfimport androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableFloatStateOfimport androidx.compose.runtime.mutableIntStateOfimport androidx.compose.runtime.rememberimport androidx.compose.runtime.rememberCoroutineScope@@ -40,6 +40,7 @@ import androidx.compose.ui.geometry.Offsetimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.graphics.ColorFilterimport androidx.compose.ui.graphics.graphicsLayer+import androidx.compose.ui.layout.onSizeChangedimport dev.lelonio.square.ui.glass.backdrop.BackdropEffectScopeimport androidx.compose.ui.graphics.GraphicsLayerScopeimport androidx.compose.ui.platform.LocalDensity@@ -114,19 +115,36 @@ fun LiquidBottomTabs(val tabsBackdrop = rememberLayerBackdrop()- BoxWithConstraints(- modifier,+ // LOCAL CHANGE: the width is read where it is used, not in composition.+ //+ // Upstream measures itself with BoxWithConstraints, which composes its+ // content again for every width it is given. That is free for a bar that+ // stands still and ruinous for one that changes shape: as the bottom bar+ // folds, this group loses a little width on every frame, and every frame+ // recomposed all five tabs twice over (the row and its tinted copy), with+ // the page scrolling underneath. Nothing here needs the width before+ // layout: the drag, the lit slot and the highlight all ask for it when+ // they run.+ val widthPx = remember { mutableFloatStateOf(0f) }++ Box(+ modifier.onSizeChanged { widthPx.floatValue = it.width.toFloat() },contentAlignment = Alignment.CenterStart) {val density = LocalDensity.current- val tabWidth = with(density) {- (constraints.maxWidth.toFloat() - 8f.dp.toPx()) / tabsCount+ val tabWidth: () -> Float = remember(density, tabsCount) {+ { with(density) { (widthPx.floatValue - 8f.dp.toPx()) / tabsCount } }}val offsetAnimation = remember { Animatable(0f) }val panelOffset by remember(density) {derivedStateOf {- val fraction = (offsetAnimation.value / constraints.maxWidth).fastCoerceIn(-1f, 1f)+ val width = widthPx.floatValue+ val fraction = if (width > 0f) {+ (offsetAnimation.value / width).fastCoerceIn(-1f, 1f)+ } else {+ 0f+ }with(density) {4f.dp.toPx() * fraction.sign * EaseOut.transform(abs(fraction))}@@ -170,7 +188,7 @@ fun LiquidBottomTabs(},onDrag = { _, dragAmount ->updateValue(- (targetValue + dragAmount.x / tabWidth * if (isLtr) 1f else -1f)+ (targetValue + dragAmount.x / tabWidth() * if (isLtr) 1f else -1f).fastCoerceIn(0f, (tabsCount - 1).toFloat()))animationScope.launch {@@ -199,8 +217,8 @@ fun LiquidBottomTabs(animationScope = animationScope,position = { size, offset ->Offset(- if (isLtr) (dampedDragAnimation.value + 0.5f) * tabWidth + panelOffset- else size.width - (dampedDragAnimation.value + 0.5f) * tabWidth + panelOffset,+ if (isLtr) (dampedDragAnimation.value + 0.5f) * tabWidth() + panelOffset+ else size.width - (dampedDragAnimation.value + 0.5f) * tabWidth() + panelOffset,size.height / 2f)}@@ -283,8 +301,8 @@ fun LiquidBottomTabs(.graphicsLayer {alpha = indicatorAlphatranslationX =- if (isLtr) dampedDragAnimation.value * tabWidth + panelOffset- else size.width - (dampedDragAnimation.value + 1f) * tabWidth + panelOffset+ if (isLtr) dampedDragAnimation.value * tabWidth() + panelOffset+ else size.width - (dampedDragAnimation.value + 1f) * tabWidth() + panelOffset}val slotLayer: GraphicsLayerScope.() -> Unit = {scaleX = dampedDragAnimation.scaleX