diff --git a/app/src/main/java/com/droiddeck/launcher/ui/FocusGlide.kt b/app/src/main/java/com/droiddeck/launcher/ui/FocusGlide.kt index b1d55104ad..8949c082d8 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/FocusGlide.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/FocusGlide.kt @@ -25,7 +25,6 @@ import androidx.compose.ui.draw.drawWithContent import androidx.compose.ui.geometry.CornerRadius import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Rect -import androidx.compose.ui.geometry.RoundRect import androidx.compose.ui.geometry.Size import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Outline @@ -64,14 +63,8 @@ internal class FocusGlide(val view: View) { val hot = mutableStateListOf() /** Bumped when the current control moves (a scroll, a relayout). */ var moves by mutableIntStateOf(0) - /** - * A box the ring rides instead of the focused control, in host coordinates: the edge of a page - * blooming out of the control that opened it (PageBloom). Cleared, the ring springs from there - * onto whatever has focus. - */ - var transit by mutableStateOf(null) - /** Whether the ring is up (a controller is driving): if not, a bloom draws its own edge. */ - var ringShown: () -> Boolean = { false } + /** Off while a page floods in or out (PageFlood); back on, it appears on whatever has focus. */ + var hidden by mutableStateOf(false) val current: GlideSource? get() = hot.lastOrNull() @@ -160,11 +153,8 @@ internal fun FocusGlideHost(modifier: Modifier = Modifier, content: @Composable var width by remember { mutableStateOf(2.dp) } if (cur != null) width = cur.width - glide.ringShown = { alpha.targetValue > 0f } - LaunchedEffect(glide) { var shown: GlideSource? = null - var rode = false var movedAt = 0L var dirSpecs: List> = List(4) { follow() } var delays = LongArray(4) @@ -181,18 +171,11 @@ internal fun FocusGlideHost(modifier: Modifier = Modifier, content: @Composable jobs[4]?.cancel() jobs[4] = scope.launch { if (snap) corner.snapTo(r) else corner.animateTo(r, Motion.sp(0.8f, 500f)) } } - snapshotFlow { Triple(glide.current, glide.moves, glide.transit != null) }.collectLatest { (src, _, riding) -> - if (riding) { - // Ride the bloom's edge, frame by frame, for as long as it runs. - if (alpha.targetValue <= 0f) return@collectLatest + snapshotFlow { Triple(glide.current, glide.moves, glide.hidden) }.collectLatest { (src, _, hidden) -> + if (hidden) { jobs.forEach { it?.cancel() } - rode = true - while (true) { - val t = glide.transit ?: break - edges[0].snapTo(t.left); edges[1].snapTo(t.top); edges[2].snapTo(t.right); edges[3].snapTo(t.bottom) - corner.snapTo(t.topLeftCornerRadius.x) - withFrameNanos { } - } + alpha.snapTo(0f) + shown = null return@collectLatest } if (src == null) { @@ -203,9 +186,7 @@ internal fun FocusGlideHost(modifier: Modifier = Modifier, content: @Composable shown = null return@collectLatest } - // Off a bloom, it springs in onto the control as it would from another one. - var first = src !== shown || rode - rode = false + var first = src !== shown val start = System.nanoTime() var last: Rect? = null while (true) { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndContent.kt b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndContent.kt index e64b468dfa..13c08ee32a 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndContent.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndContent.kt @@ -4,13 +4,11 @@ import androidx.compose.ui.res.pluralStringResource import androidx.compose.runtime.CompositionLocalProvider import android.os.Build import androidx.compose.animation.AnimatedContent -import androidx.compose.runtime.snapshotFlow -import androidx.compose.ui.layout.onSizeChanged -import androidx.compose.ui.geometry.Size import androidx.compose.animation.EnterExitState import androidx.compose.animation.ExitTransition -import androidx.compose.animation.core.FastOutSlowInEasing import androidx.compose.animation.core.animateFloat +import androidx.compose.animation.core.snap +import androidx.compose.animation.core.tween import androidx.compose.animation.scaleIn import androidx.compose.animation.scaleOut import androidx.compose.runtime.DisposableEffect @@ -122,7 +120,6 @@ internal fun Pane( val pages = remember { HashMap Unit>() } // Pages opened from a control (the cog), with where that control sat, in this pane's coordinates. val origins = remember { HashMap() } - val glide = LocalFocusGlide.current val paneAt = remember { arrayOf(Offset.Zero) } val livePage by rememberUpdatedState(s.pageKey) // Picking another game changes the detail beside the list, not the whole page. @@ -136,11 +133,11 @@ internal fun Pane( modifier = Modifier.onGloballyPositioned { paneAt[0] = it.positionInRoot() }, transitionSpec = { when { - // Into a page from its control: the page blooms over a pane that sinks back. + // Into a page from its control: the control floods the pane (PageFlood) as it sinks back. targetState in origins -> (fadeIn(Motion.tw(120))) .togetherWith(fadeOut(Motion.tw(320, 80)) + scaleOut(Motion.tw(480), targetScale = 0.95f)) .apply { targetContentZIndex = 1f } - // Back out of it: the page folds into the control as the pane comes forward again. + // Back out of it: the flood draws back into the control as the pane comes forward again. initialState in origins -> (fadeIn(Motion.tw(300, 100)) + scaleIn(Motion.tw(460, 40), initialScale = 0.95f)) .togetherWith(ExitTransition.None) .apply { targetContentZIndex = -1f } @@ -157,27 +154,12 @@ internal fun Pane( val from = origins[key] if (from == null) shown() else { - val open by transition.animateFloat( - // An even ease, not Motion.Ease: that one covers most of the pane in its first tenth. - transitionSpec = { if (targetState == EnterExitState.Visible) Motion.tw(620, easing = FastOutSlowInEasing) else Motion.tw(440, easing = FastOutSlowInEasing) }, - label = "bloom", - ) { if (it == EnterExitState.Visible) 1f else 0f } - var boxSize by remember { mutableStateOf(Size.Zero) } - if (glide != null) { - // A controller's focus ring rides the bloom's edge while it runs. - LaunchedEffect(Unit) { - snapshotFlow { open to (transition.currentState == transition.targetState) }.collect { (p, settled) -> - val hostAt = glide.host?.takeIf { it.isAttached }?.positionInRoot() ?: Offset.Zero - glide.transit = if (settled || boxSize == Size.Zero || !glide.ringShown()) null - else bloomBox(boxSize, from, p, paneAt[0] - hostAt) - } - } - DisposableEffect(Unit) { onDispose { glide.transit = null } } - } - Box( - Modifier.fillMaxSize().onSizeChanged { boxSize = Size(it.width.toFloat(), it.height.toFloat()) } - .bloom({ open }, from, pal.background) { if (glide?.ringShown() == true) null else pal.signal }, - ) { shown() } + // Keeps a leaving page on screen while its flood draws back into the control. + transition.animateFloat( + transitionSpec = { if (targetState == EnterExitState.PostExit) tween(PAGE_RETURN_MS) else snap() }, + label = "pageReturn", + ) { if (it == EnterExitState.PostExit) 1f else 0f } + PageFlood(from, leaving = transition.targetState == EnterExitState.PostExit) { shown() } } } else Content(s, if (key == "games") selected else key, a, Modifier.fillMaxSize(), onSelect, onAndroidAppClick, onOpenDeveloperOptions, onRequestWirelessAdb) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/PageBloom.kt b/app/src/main/java/com/droiddeck/launcher/ui/PageBloom.kt deleted file mode 100644 index 99eaa60baa..0000000000 --- a/app/src/main/java/com/droiddeck/launcher/ui/PageBloom.kt +++ /dev/null @@ -1,87 +0,0 @@ -package com.droiddeck.launcher.ui - -import android.os.SystemClock -import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.drawWithContent -import androidx.compose.ui.geometry.CornerRadius -import androidx.compose.ui.geometry.Offset -import androidx.compose.ui.geometry.Rect -import androidx.compose.ui.geometry.RoundRect -import androidx.compose.ui.geometry.Size -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.graphics.Outline -import androidx.compose.ui.graphics.Shape -import androidx.compose.ui.graphics.drawscope.Stroke -import androidx.compose.ui.graphics.graphicsLayer -import androidx.compose.ui.unit.Density -import androidx.compose.ui.unit.LayoutDirection -import androidx.compose.ui.unit.dp - -// A page that opens from a control (the cog beside Play) blooms out of it: the control's own -// rounded box stretches out until it is the pane, and shrinks back into the control on the way -// out. With a controller the focus ring rides that edge (FocusGlide.transit); by touch the bloom -// draws the same edge itself. - -/** Where a page opened from: the control's bounds and corner radius, in px. */ -internal class Origin(val bounds: Rect, val corner: Float) { - fun translate(by: Offset) = Origin(bounds.translate(by), corner) -} - -internal object PageOrigin { - private var origin: Origin? = null - private var at = 0L - - /** The control a page is about to open from, in root coordinates. */ - fun mark(o: Origin) { - origin = o - at = SystemClock.uptimeMillis() - } - - /** The control just pressed, once: stale after a second, so a page opened another way never blooms. */ - fun take(): Origin? = origin.takeIf { SystemClock.uptimeMillis() - at < 1_000 }.also { origin = null } -} - -/** The bloom's box at [p] (0 = the control, 1 = the whole of [size]), corner and all, moved by [shift]. */ -internal fun bloomBox(size: Size, from: Origin, p: Float, shift: Offset = Offset.Zero): RoundRect { - val b = from.bounds - fun mix(a: Float, z: Float) = a + (z - a) * p - return RoundRect( - Rect(mix(b.left, 0f), mix(b.top, 0f), mix(b.right, size.width), mix(b.bottom, size.height)).translate(shift), - CornerRadius(mix(from.corner, 0f)), - ) -} - -/** - * Clips to the bloom's box at [progress], filled with [fill] so it reads even over a page that - * draws no ground of its own, and traced with [rim] when the focus ring is not there to do it. - * At 1 nothing is clipped or drawn. - */ -internal fun Modifier.bloom(progress: () -> Float, from: Origin, fill: Color, rim: () -> Color?): Modifier = - graphicsLayer { - val p = progress() - if (p >= 1f) { - clip = false - return@graphicsLayer - } - shape = BoxAt(bloomBox(size, from, p)) - clip = true - }.drawWithContent { - val p = progress() - // Solid while it grows; it gives way to the pane's own backdrop over the last stretch, by - // when the page it covered has faded out underneath. - if (p < 1f) drawRect(fill, alpha = ((1f - p) / 0.3f).coerceIn(0f, 1f)) - drawContent() - val r = rim() - if (p < 1f && r != null) { - val w = 2.dp.toPx() - val box = bloomBox(size, from, p) - drawRoundRect( - r.copy(alpha = r.alpha * (1f - p * p)), Offset(box.left + w / 2f, box.top + w / 2f), Size(box.width - w, box.height - w), - CornerRadius((box.topLeftCornerRadius.x - w / 2f).coerceAtLeast(0f)), style = Stroke(w), - ) - } - } - -private class BoxAt(private val box: RoundRect) : Shape { - override fun createOutline(size: Size, layoutDirection: LayoutDirection, density: Density) = Outline.Rounded(box) -} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/PageFlood.kt b/app/src/main/java/com/droiddeck/launcher/ui/PageFlood.kt new file mode 100644 index 0000000000..9a0cfa8949 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/PageFlood.kt @@ -0,0 +1,145 @@ +package com.droiddeck.launcher.ui + +import android.os.SystemClock +import androidx.compose.animation.core.Animatable +import androidx.compose.animation.core.spring +import androidx.compose.foundation.Canvas +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.runtime.snapshotFlow +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clipToBounds +import androidx.compose.ui.geometry.CornerRadius +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.geometry.Rect +import androidx.compose.ui.geometry.Size +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.drawscope.DrawScope +import androidx.compose.ui.graphics.drawscope.Stroke +import androidx.compose.ui.graphics.graphicsLayer +import androidx.compose.ui.layout.onSizeChanged +import androidx.compose.ui.unit.dp +import androidx.compose.ui.util.lerp +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.launch +import kotlin.math.PI +import kotlin.math.sin + +// A page opened from a control (the cog beside Play) grows out of it as a session grows out of +// Play (LaunchFlood): the control stretches out over the pane on loose springs, the far edges +// first, and the page rises into it once it is covered. Back draws it down into the control again. + +/** Where a page opened from: the control's bounds and corner radius, in px. */ +internal class Origin(val bounds: Rect, val corner: Float) { + fun translate(by: Offset) = Origin(bounds.translate(by), corner) +} + +internal object PageOrigin { + private var origin: Origin? = null + private var at = 0L + + /** The control a page is about to open from, in root coordinates. */ + fun mark(o: Origin) { + origin = o + at = SystemClock.uptimeMillis() + } + + /** The control just pressed, once: stale after a second, so a page opened another way never floods. */ + fun take(): Origin? = origin.takeIf { SystemClock.uptimeMillis() - at < 1_000 }.also { origin = null } +} + +/** How long a leaving page is kept for its flood to draw back into the control. */ +internal const val PAGE_RETURN_MS = 900 + +/** + * [content], a page that opened from the control at [from] (in this element's coordinates): the + * control's tile stretching over it until covered, then the page rising in as the tile fades. + * Once [leaving], the reverse: the tile comes back over the page and draws down into the control. + * The focus ring stays out of it until it is done. + */ +@Composable +internal fun PageFlood(from: Origin, leaving: Boolean, content: @Composable () -> Unit) { + val pal = LocalPalette.current + val glide = LocalFocusGlide.current + // Left, top, right, bottom: 0 on the control, 1 on the pane's edge. + val edges = remember { List(4) { Animatable(0f) } } + val shown = remember { Animatable(0f) } + val tile = remember { Animatable(1f) } + var size by remember { mutableStateOf(Size.Zero) } + LaunchedEffect(size != Size.Zero, leaving) { + if (size == Size.Zero) return@LaunchedEffect + glide?.hidden = true + val b = from.bounds + val travel = listOf(b.left, b.top, size.width - b.right, size.height - b.bottom).map { it.coerceAtLeast(0f) } + val far = travel.max().coerceAtLeast(1f) + coroutineScope { + if (!leaving) { + val runs = edges.mapIndexed { i, edge -> + val lead = travel[i] / far + launch { + delay(Motion.ms((150 * (1f - lead)).toInt()).toLong()) + edge.animateTo(1f, Motion.sp(0.5f, lerp(150f, 260f, lead))) + } + } + snapshotFlow { edges.all { it.value >= 0.995f } }.first { it } + launch { tile.animateTo(0f, Motion.tw(360, 60)) } + shown.animateTo(1f, Motion.tw(320)) + runs.forEach { it.join() } + } else { + launch { shown.animateTo(0f, Motion.tw(140)) } + tile.animateTo(1f, Motion.tw(140)) + edges.mapIndexed { i, edge -> + val lead = travel[i] / far + launch { + delay(Motion.ms((120 * (1f - lead)).toInt()).toLong()) + edge.animateTo(0f, if (Motion.scale == 0f) Motion.sp() else spring(dampingRatio = 1f, stiffness = lerp(170f, 260f, lead))) + } + }.forEach { it.join() } + tile.animateTo(0f, Motion.tw(120)) + } + } + glide?.hidden = false + } + DisposableEffect(Unit) { onDispose { glide?.hidden = false } } + Box(Modifier.fillMaxSize().clipToBounds().onSizeChanged { size = Size(it.width.toFloat(), it.height.toFloat()) }) { + Box(Modifier.fillMaxSize().graphicsLayer { alpha = shown.value; translationY = (1f - shown.value) * 14.dp.toPx() }) { content() } + Canvas(Modifier.fillMaxSize()) { + if (tile.value > 0.01f) drawTile(from, edges.map { it.value }, pal.surface, pal.line2, tile.value) + } + } +} + +/** + * The control's tile with its edges [v] of the way to the pane's (left, top, right, bottom): + * sinking with the page behind, blobby in flight, square once it fills the pane. + */ +private fun DrawScope.drawTile(from: Origin, v: List, fill: Color, line: Color, alpha: Float) { + val (l, t, r, b) = v + val mean = v.sumOf { it.coerceIn(0f, 1f).toDouble() }.toFloat() / 4f + // The control sinks with the page behind it (scaled to 0.95 about the pane's centre). + val k = 1f - 0.05f * mean + val cx = size.width / 2f + val cy = size.height / 2f + val f = from.bounds + val left = lerp(cx + (f.left - cx) * k, 0f, l) + val top = lerp(cy + (f.top - cy) * k, 0f, t) + val right = lerp(cx + (f.right - cx) * k, size.width, r) + val bottom = lerp(cy + (f.bottom - cy) * k, size.height, b) + val w = (right - left).coerceAtLeast(0f) + val h = (bottom - top).coerceAtLeast(0f) + val blob = sin(PI * mean).toFloat().coerceAtLeast(0f) + val corner = lerp(from.corner * (1f - mean), minOf(w, h) * 0.42f, blob) + drawRoundRect(fill, Offset(left, top), Size(w, h), CornerRadius(corner), alpha = alpha) + // The control's outline, kept while it is still the size of one. + val edge = (1f - mean * 3f).coerceIn(0f, 1f) + if (edge > 0f) drawRoundRect(line, Offset(left, top), Size(w, h), CornerRadius(corner), alpha = alpha * edge, style = Stroke(1.dp.toPx())) +}