ui: the focus ring carries the page bloom, focus returns to its opener

The bloom is now the opening control's own rounded box stretching out to the pane, not a
circle. With a controller the focus ring rides its edge out to the pane's frame and springs
in onto the page's first control; on Back it rides the box back into the control, which
takes focus again. By touch, with no ring up, the bloom traces the same edge itself.

Closing a page used to leave focus on the page while it animated out, so nothing took it
back afterwards.
This commit is contained in:
xXJSONDeruloXx committed 2026-10-01 22:37:47 -04:00
1 parent ed57ce963c
commit af0f64b3de
5 files changed
+105 -41

No files matched your search

@@ -25,6 +25,7 @@ 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
@@ -63,6 +64,14 @@ internal class FocusGlide(val view: View) {
val hot = mutableStateListOf<GlideSource>()
/** 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<RoundRect?>(null)
/** Whether the ring is up (a controller is driving): if not, a bloom draws its own edge. */
var ringShown: () -> Boolean = { false }
val current: GlideSource? get() = hot.lastOrNull()
@@ -151,8 +160,11 @@ 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<AnimationSpec<Float>> = List(4) { follow() }
var delays = LongArray(4)
@@ -169,7 +181,20 @@ 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 { glide.current to glide.moves }.collectLatest { (src, _) ->
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
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 { }
}
return@collectLatest
}
if (src == null) {
// Focus passing between two controls can let go of one a moment before the next
// takes it: wait a beat, so that reads as a move and not a fade out and back in.
@@ -178,7 +203,9 @@ internal fun FocusGlideHost(modifier: Modifier = Modifier, content: @Composable
shown = null
return@collectLatest
}
var first = src !== shown
// Off a bloom, it springs in onto the control as it would from another one.
var first = src !== shown || rode
rode = false
val start = System.nanoTime()
var last: Rect? = null
while (true) {
@@ -4,6 +4,9 @@ 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
@@ -118,7 +121,8 @@ internal fun Pane(
// Pages keep drawing while they leave, so one can fold away instead of blinking out.
val pages = remember { HashMap<String, @Composable () -> Unit>() }
// Pages opened from a control (the cog), with where that control sat, in this pane's coordinates.
val origins = remember { HashMap<String, Rect>() }
val origins = remember { HashMap<String, Origin>() }
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.
@@ -158,7 +162,22 @@ internal fun Pane(
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 }
Box(Modifier.fillMaxSize().bloom({ open }, from.center, from.minDimension / 2f, pal.background, pal.signal)) { shown() }
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() }
}
}
else Content(s, if (key == "games") selected else key, a, Modifier.fillMaxSize(), onSelect, onAndroidAppClick, onOpenDeveloperOptions, onRequestWirelessAdb)
@@ -50,6 +50,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshotFlow
import androidx.compose.runtime.withFrameNanos
import kotlinx.coroutines.flow.first
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
@@ -418,6 +419,16 @@ private fun FrontEndScreenBody(s: FrontEndState, a: FrontEndActions, page: (@Com
// A tile or button that opens a page goes away with the page it was on, and focus with it;
// the pad then had nothing to move from (a press landed back on the rail's first item). So
// once the new page is in, a controller lands on its main button.
// Closing a page: it stays on screen while it leaves (and holds focus there), so hand focus
// back now to the control it was opened from, and the ring folds back onto that.
var openPage by remember { mutableStateOf<String?>(null) }
LaunchedEffect(s.pageKey) {
val closed = openPage != null && s.pageKey == null
openPage = s.pageKey
if (!closed || processSettingsPageVisible || inputModeManager.inputMode != InputMode.Keyboard) return@LaunchedEffect
withFrameNanos { }
runCatching { frontFocus.paneEntry().requestFocus() }
}
LaunchedEffect(selected, s.pageKey, processSettingsPageVisible) {
if (processSettingsPageVisible) return@LaunchedEffect
// Past the old page's exit (170ms), then the first frame the new page takes focus.
@@ -235,11 +235,12 @@ internal fun Cog(size: androidx.compose.ui.unit.Dp = 48.dp, onClick: () -> Unit)
val rot by animateFloatAsState((if (hot) 90f else 0f) + spins * 180f, Motion.sp(0.55f), label = "cog")
val pal = LocalPalette.current
val placed = remember { arrayOfNulls<androidx.compose.ui.layout.LayoutCoordinates>(1) }
val cornerPx = with(androidx.compose.ui.platform.LocalDensity.current) { (if (size > 48.dp) 14.dp else 12.dp).toPx() }
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.paneItem("cog").downToFirstTile().size(size).onGloballyPositioned { placed[0] = it }.clip(if (size > 48.dp) Shape14 else Shape12).background(Color.White.copy(alpha = 0.03f)).glideBorder(hot, if (size > 48.dp) Shape14 else Shape12, pal.signal, pal.line2)
.hoverable(src).clickable(interactionSource = src, indication = LocalIndication.current) {
placed[0]?.takeIf { it.isAttached }?.let { PageOrigin.mark(it.boundsInRoot()) }
placed[0]?.takeIf { it.isAttached }?.let { PageOrigin.mark(Origin(it.boundsInRoot(), cornerPx)) }
spins++
onClick()
},
@@ -2,80 +2,86 @@ 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.draw.drawWithContent
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Outline
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.TransformOrigin
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
import kotlin.math.hypot
import kotlin.math.max
// A page that opens from a control (the cog beside Play) blooms out of it: a circle growing from
// the control until it fills the pane, and folding back into it on the way out.
// 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 bounds: Rect? = null
private var origin: Origin? = null
private var at = 0L
/** The control a page is about to open from, in root coordinates. */
fun mark(r: Rect) {
bounds = r
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(): Rect? = bounds.takeIf { SystemClock.uptimeMillis() - at < 1_000 }.also { bounds = null }
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 a circle around [origin] (in this element's own coordinates) that grows from [from] px
* to past the farthest corner as [progress] goes 0 to 1, filled with [fill] so the disc reads even
* over a page that draws no ground of its own, a [rim] tracing its edge, and a small zoom in from
* the same point. At 1 nothing is clipped.
* 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, origin: Offset, from: Float, fill: Color, rim: Color): Modifier =
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 = CircleAt(origin, radius(size, origin, from, p))
shape = BoxAt(bloomBox(size, from, p))
clip = true
val zoom = 0.94f + 0.06f * p
scaleX = zoom
scaleY = zoom
if (size.width > 0f && size.height > 0f) transformOrigin = TransformOrigin(origin.x / size.width, origin.y / size.height)
}.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()
if (p < 1f) {
val width = 2.dp.toPx()
drawCircle(rim.copy(alpha = rim.alpha * (1f - p)), radius(size, origin, from, p) - width / 2f, origin, style = Stroke(width))
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 fun radius(size: Size, origin: Offset, from: Float, p: Float): Float {
val far = max(
max(hypot(origin.x, origin.y), hypot(size.width - origin.x, origin.y)),
max(hypot(origin.x, size.height - origin.y), hypot(size.width - origin.x, size.height - origin.y)),
)
return from + (far - from) * p
}
private class CircleAt(private val centre: Offset, private val radius: Float) : Shape {
override fun createOutline(size: Size, layoutDirection: LayoutDirection, density: Density) =
Outline.Generic(Path().apply { addOval(Rect(centre, radius)) })
private class BoxAt(private val box: RoundRect) : Shape {
override fun createOutline(size: Size, layoutDirection: LayoutDirection, density: Density) = Outline.Rounded(box)
}