Mirror the Android pane close animation
Give every pane one shared slide spec, a fixed 300 ms duration and a symmetric cubic-bezier easing, so a close is the open curve run backwards at the same duration; several levels that pop in one tap now stagger so each reverses in order from the top. The stack mounts a level while it is open or still sliding out and calls the pane only then, and rememberPaneContent retains each closing pane's full parent identity (item/version/section, stage key, visit). A closing pane therefore keeps rendering the content that opened it instead of vanishing or falling back to an empty node, and a fully closed level composes nothing. Geometry, tokens and insets are unchanged. Adds three pure unit cases for the symmetric easing and the stagger rule; documents the change and its emulator verification in android/README.md.
This commit is contained in:
@@ -49,6 +49,7 @@ import net.awan.pipelines.ui.components.ConnectionBanner
|
||||
import net.awan.pipelines.ui.components.SlidingPaneStack
|
||||
import net.awan.pipelines.ui.components.clampTabIndex
|
||||
import net.awan.pipelines.ui.components.pipelineDetailTabs
|
||||
import net.awan.pipelines.ui.components.rememberPaneContent
|
||||
import net.awan.pipelines.ui.components.runDetailTabs
|
||||
import net.awan.pipelines.ui.graph.PipelineStagePane
|
||||
import net.awan.pipelines.ui.graph.RunStagePane
|
||||
@@ -222,14 +223,18 @@ private fun PipelineDestination(vm: AppViewModel, wide: Boolean, onOpenRun: (Lon
|
||||
selectedId = selection.itemId.takeIf { depth >= 1 },
|
||||
covered = covered,
|
||||
)
|
||||
1 -> if (selection.itemId != 0L) {
|
||||
1 -> rememberPaneContent(
|
||||
selection.itemId.takeIf { it != 0L }?.let {
|
||||
Triple(it, selection.itemVersion, selection.sectionIndex)
|
||||
},
|
||||
)?.let { (itemId, version, sectionIndex) ->
|
||||
PipelineDetailScreen(
|
||||
vm = vm,
|
||||
pipelineId = selection.itemId,
|
||||
version = selection.itemVersion,
|
||||
pipelineId = itemId,
|
||||
version = version,
|
||||
onBack = onBack,
|
||||
onOpenRun = onOpenRun,
|
||||
selectedSection = clampTabIndex(selection.sectionIndex, pipelineDetailTabs.size),
|
||||
selectedSection = clampTabIndex(sectionIndex, pipelineDetailTabs.size),
|
||||
onSelectSection = { selection = selection.copy(sectionIndex = it) },
|
||||
modifier = paneModifier,
|
||||
wide = wide,
|
||||
@@ -239,11 +244,15 @@ private fun PipelineDestination(vm: AppViewModel, wide: Boolean, onOpenRun: (Lon
|
||||
covered = covered,
|
||||
)
|
||||
}
|
||||
2 -> selection.stageKey?.let { key ->
|
||||
2 -> rememberPaneContent(
|
||||
selection.stageKey?.let {
|
||||
Triple(selection.itemId, selection.itemVersion, it)
|
||||
},
|
||||
)?.let { (itemId, version, key) ->
|
||||
PipelineStagePane(
|
||||
vm = vm,
|
||||
pipelineId = selection.itemId,
|
||||
version = selection.itemVersion,
|
||||
pipelineId = itemId,
|
||||
version = version,
|
||||
stageKey = key,
|
||||
onBack = onBack,
|
||||
wide = wide,
|
||||
@@ -290,12 +299,14 @@ private fun RunsDestination(
|
||||
selectedRunId = selection.itemId.takeIf { depth >= 1 },
|
||||
covered = covered,
|
||||
)
|
||||
1 -> if (selection.itemId != 0L) {
|
||||
1 -> rememberPaneContent(
|
||||
selection.itemId.takeIf { it != 0L }?.let { it to selection.sectionIndex },
|
||||
)?.let { (itemId, sectionIndex) ->
|
||||
RunDetailScreen(
|
||||
vm = vm,
|
||||
runId = selection.itemId,
|
||||
runId = itemId,
|
||||
onBack = onBack,
|
||||
selectedSection = clampTabIndex(selection.sectionIndex, runDetailTabs.size),
|
||||
selectedSection = clampTabIndex(sectionIndex, runDetailTabs.size),
|
||||
onSelectSection = { onSelectionChange(selection.copy(sectionIndex = it)) },
|
||||
modifier = paneModifier,
|
||||
wide = wide,
|
||||
@@ -307,12 +318,16 @@ private fun RunsDestination(
|
||||
covered = covered,
|
||||
)
|
||||
}
|
||||
2 -> selection.stageKey?.let { key ->
|
||||
2 -> rememberPaneContent(
|
||||
selection.stageKey?.let {
|
||||
Triple(selection.itemId, it, selection.stageVisit)
|
||||
},
|
||||
)?.let { (itemId, key, visit) ->
|
||||
RunStagePane(
|
||||
vm = vm,
|
||||
runId = selection.itemId,
|
||||
runId = itemId,
|
||||
stageKey = key,
|
||||
visitIndex = selection.stageVisit,
|
||||
visitIndex = visit,
|
||||
onBack = onBack,
|
||||
wide = wide,
|
||||
modifier = paneModifier,
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package net.awan.pipelines.ui.components
|
||||
|
||||
import androidx.compose.animation.core.CubicBezierEasing
|
||||
import androidx.compose.animation.core.Easing
|
||||
import androidx.compose.animation.core.TweenSpec
|
||||
import androidx.compose.animation.core.animateDpAsState
|
||||
import androidx.compose.animation.core.spring
|
||||
import androidx.compose.animation.core.tween
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.BoxWithConstraints
|
||||
@@ -18,6 +21,7 @@ import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.material3.LocalMinimumInteractiveComponentSize
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.input.pointer.PointerEventPass
|
||||
@@ -52,6 +56,43 @@ internal val PEEK_BUDGET_DP = 224.dp
|
||||
internal val MIN_ACTIVE_PANE_WIDTH_DP = 320.dp
|
||||
internal val MIN_COVERED_PANE_WIDTH_DP = 112.dp
|
||||
|
||||
/**
|
||||
* The one slide timing every pane shares. [PANE_SLIDE_EASING] is a symmetric
|
||||
* ease (its control points mirror about the centre), so the same curve run from
|
||||
* the open offset back to the closed one is exactly the open curve travelled
|
||||
* backwards: closing a pane is the time-reverse of opening it, at the same
|
||||
* [PANE_SLIDE_MILLIS] duration. When several levels close in one tap the levels
|
||||
* below the topmost wait [PANE_POP_STEP_MILLIS] each, so they reverse in order.
|
||||
*/
|
||||
internal const val PANE_SLIDE_MILLIS = 300
|
||||
internal const val PANE_POP_STEP_MILLIS = 60
|
||||
internal val PANE_SLIDE_EASING: Easing = CubicBezierEasing(0.42f, 0f, 0.58f, 1f)
|
||||
|
||||
/**
|
||||
* The extra delay, in milliseconds, before the pane at [level] starts reversing
|
||||
* for a stack whose visible depth just moved from [previousDepth] to [depth]. A
|
||||
* single closing level is the exact reverse of opening it and starts at once;
|
||||
* when more than one level was open and several are leaving, the topmost leaves
|
||||
* first and each lower level waits one more [PANE_POP_STEP_MILLIS] so the
|
||||
* cascade reads in order. A level that is open or opening never waits. Pure, so
|
||||
* the mirror/stagger rule needs no device.
|
||||
*/
|
||||
internal fun paneSlideDelayMillis(level: Int, depth: Int, previousDepth: Int): Int {
|
||||
if (level <= depth) return 0
|
||||
val closing = previousDepth - depth
|
||||
return if (closing > 1) (previousDepth - level) * PANE_POP_STEP_MILLIS else 0
|
||||
}
|
||||
|
||||
/**
|
||||
* The slide spec for the pane at [level]: one shared duration and easing in both
|
||||
* directions, plus [paneSlideDelayMillis] when several levels pop together.
|
||||
*/
|
||||
internal fun paneSlideSpec(level: Int, depth: Int, previousDepth: Int): TweenSpec<Dp> = tween(
|
||||
durationMillis = PANE_SLIDE_MILLIS,
|
||||
delayMillis = paneSlideDelayMillis(level, depth, previousDepth),
|
||||
easing = PANE_SLIDE_EASING,
|
||||
)
|
||||
|
||||
/**
|
||||
* The peek budget actually spent on a wide window: the fixed [PEEK_BUDGET_DP]
|
||||
* token, reduced only when that would leave the active pane narrower than
|
||||
@@ -156,21 +197,24 @@ internal fun peekStripsDp(
|
||||
* squeezed.
|
||||
*
|
||||
* [depth] is the number of visible panes minus one (0..[MAX_PANE_DEPTH]);
|
||||
* [pane] composes the level's content and is expected to render nothing for a
|
||||
* level whose selection is empty (so a closing pane simply slides back out). On
|
||||
* a wide window, tapping a covered pane's peek calls [onPopTo] with that pane's
|
||||
* level, closing every pane to its right; [backContentDescription] names each
|
||||
* strip. On a narrow window there is no strip and Back/gesture/the pane's own
|
||||
* back control pop the level.
|
||||
* [pane] composes the level's content. The stack mounts a level while it is open
|
||||
* or still sliding out, and calls [pane] only then, so the caller can name the
|
||||
* level's content with [rememberPaneContent]: the level keeps rendering the
|
||||
* content that opened it until it is fully closed, and a fully closed level is
|
||||
* not composed at all (no work, no stale content). On a wide window, tapping a
|
||||
* covered pane's peek calls [onPopTo] with that pane's level, closing every pane
|
||||
* to its right; [backContentDescription] names each strip. On a narrow window
|
||||
* there is no strip and Back/gesture/the pane's own back control pop the level.
|
||||
*
|
||||
* Every pane is always composed and each level has its own long-lived
|
||||
* [animateDpAsState], so opening slides in and closing slides back out with no
|
||||
* first-frame hack, and a rapid run of taps only retargets an existing animator
|
||||
* (never half-slid). Opening and closing are exact mirrors: the same offsets and
|
||||
* [spring] retarget in reverse. A covered pane is hidden from accessibility and
|
||||
* made non-interactive so TalkBack focus follows the top pane and a tap can never
|
||||
* leak through to a covered one; on a wide window only its peek strip stays
|
||||
* tappable.
|
||||
* Each level has its own long-lived [animateDpAsState], so opening slides in and
|
||||
* closing slides back out with no first-frame hack, and a rapid run of taps only
|
||||
* retargets an existing animator (never half-slid). Opening and closing are exact
|
||||
* mirrors: the same offsets and the one shared [paneSlideSpec] (a fixed duration
|
||||
* and a symmetric easing, so running it backwards is the same curve reversed)
|
||||
* are retargeted in reverse, and several levels closing in one tap reverse in
|
||||
* order. A covered pane is hidden from accessibility and made non-interactive so
|
||||
* TalkBack focus follows the top pane and a tap can never leak through to a
|
||||
* covered one; on a wide window only its peek strip stays tappable.
|
||||
*/
|
||||
@Composable
|
||||
fun SlidingPaneStack(
|
||||
@@ -206,6 +250,11 @@ fun SlidingPaneStack(
|
||||
val folded = strips.firstOrNull()?.takeIf { it.folded > 1 }?.folded ?: 0
|
||||
// The active pane always opens at the whole budget (or 0 when narrow).
|
||||
val activeStart = strips.fold(0.dp) { acc, strip -> acc + strip.width }
|
||||
// The depth just before the current one, so a pop knows how many levels
|
||||
// were open (to stagger their reversal). Held stable for a given depth so
|
||||
// the slide spec never changes part-way through a slide.
|
||||
val depthTrack = remember { PaneDepthTrack(depth) }
|
||||
val closingFrom = remember(depth) { depthTrack.previous.also { depthTrack.previous = depth } }
|
||||
|
||||
// The folded older levels as one edge indicator with a count, clear of the
|
||||
// status/nav bars and of the left gesture area; tapping it jumps back to
|
||||
@@ -232,8 +281,11 @@ fun SlidingPaneStack(
|
||||
}
|
||||
}
|
||||
|
||||
// Panes 0..MAX are always composed in order (the animator stays warm); a
|
||||
// level beyond the depth sits off-screen and the caller renders nothing.
|
||||
// Panes 0..MAX are always given their own long-lived animator, in order,
|
||||
// even while closed, so a retarget slides rather than jumps and the
|
||||
// animator stays warm; a level beyond the depth waits exactly off the
|
||||
// right edge. The one shared spec (see [paneSlideSpec]) makes closing the
|
||||
// exact time-reverse of opening.
|
||||
for (level in 0..MAX_PANE_DEPTH) {
|
||||
val covered = level < depth
|
||||
// A level folded into the edge indicator is not drawn on its own.
|
||||
@@ -252,32 +304,41 @@ fun SlidingPaneStack(
|
||||
val openWidth = paneWidthDp(hostWidth, if (level <= depth) openOffset else budget)
|
||||
val offset by animateDpAsState(
|
||||
targetValue = openOffset,
|
||||
animationSpec = spring(),
|
||||
animationSpec = paneSlideSpec(level, depth, closingFrom),
|
||||
label = "pane-slide-$level",
|
||||
)
|
||||
Box(
|
||||
Modifier
|
||||
.offset(x = offset)
|
||||
.width(openWidth)
|
||||
.fillMaxHeight()
|
||||
.then(if (level < depth) Modifier.blockPointerInput() else Modifier),
|
||||
) {
|
||||
if (strip != null) {
|
||||
// Constrain the compact identity to the visible strip so it is
|
||||
// centred in the space the covered pane actually has. It rides
|
||||
// with the pane's own offset and so slides in and out with it.
|
||||
val stripInset = if (level == 0) startInset else 0.dp
|
||||
Box(
|
||||
Modifier
|
||||
.align(Alignment.TopStart)
|
||||
.offset(x = stripInset, y = topInset)
|
||||
.width(maxOf(0.dp, strip.width - stripInset))
|
||||
.height(stripHeight),
|
||||
) {
|
||||
pane(level, true, Modifier.fillMaxSize())
|
||||
// A level is mounted while it is open or has not yet finished sliding
|
||||
// out. An unmounted level keeps its animator warm but composes no
|
||||
// content, so a fully closed pane does no work; a closing pane keeps
|
||||
// rendering its content (retained by the caller) until it is off the
|
||||
// screen, so it slides back out instead of vanishing.
|
||||
val mounted = level <= depth || offset.value < hostWidth.value
|
||||
if (mounted) {
|
||||
Box(
|
||||
Modifier
|
||||
.offset(x = offset)
|
||||
.width(openWidth)
|
||||
.fillMaxHeight()
|
||||
.then(if (level < depth) Modifier.blockPointerInput() else Modifier),
|
||||
) {
|
||||
if (strip != null) {
|
||||
// Constrain the compact identity to the visible strip so it
|
||||
// is centred in the space the covered pane actually has. It
|
||||
// rides with the pane's own offset and slides in so it also
|
||||
// slides back out with it.
|
||||
val stripInset = if (level == 0) startInset else 0.dp
|
||||
Box(
|
||||
Modifier
|
||||
.align(Alignment.TopStart)
|
||||
.offset(x = stripInset, y = topInset)
|
||||
.width(maxOf(0.dp, strip.width - stripInset))
|
||||
.height(stripHeight),
|
||||
) {
|
||||
pane(level, true, Modifier.fillMaxSize())
|
||||
}
|
||||
} else {
|
||||
pane(level, false, Modifier.fillMaxSize())
|
||||
}
|
||||
} else {
|
||||
pane(level, false, Modifier.fillMaxSize())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -325,3 +386,28 @@ private fun Modifier.blockPointerInput(): Modifier = this
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** A tiny non-observable cell, so remembering a value never schedules recomposition. */
|
||||
private class PaneMemory<T> {
|
||||
var value: T? = null
|
||||
}
|
||||
|
||||
/** The depth before the current one, tracked without scheduling recomposition. */
|
||||
private class PaneDepthTrack(initial: Int) {
|
||||
var previous: Int = initial
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps the last non-null [value] a pane was given, so a pane that is sliding
|
||||
* out still renders the content that opened it after the selection naming it
|
||||
* has been cleared. It is only called while the stack has the level mounted
|
||||
* (open or still finishing its exit), so once the level is fully closed the
|
||||
* remembered value is discarded and a closed pane does no work. A pane that
|
||||
* opens later sets a fresh value, so nothing stale is ever shown.
|
||||
*/
|
||||
@Composable
|
||||
internal fun <T : Any> rememberPaneContent(value: T?): T? {
|
||||
val memory = remember { PaneMemory<T>() }
|
||||
if (value != null) memory.value = value
|
||||
return memory.value
|
||||
}
|
||||
|
||||
@@ -131,4 +131,43 @@ class SlidingPaneStackTest {
|
||||
assertDp(355.dp, paneWidthDp(411.dp, 56.dp))
|
||||
assertDp(0.dp, paneWidthDp(100.dp, 200.dp))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun theSlideEasingIsItsOwnReverse() {
|
||||
// A symmetric easing means running the one shared spec from the open
|
||||
// offset back to the closed one is the open curve travelled backwards,
|
||||
// so closing mirrors opening exactly rather than merely looking similar.
|
||||
for (i in 0..100) {
|
||||
val t = i / 100f
|
||||
assertEquals(
|
||||
"easing is not symmetric at $t",
|
||||
1f,
|
||||
PANE_SLIDE_EASING.transform(t) + PANE_SLIDE_EASING.transform(1f - t),
|
||||
1e-4f,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aSingleLevelCloseIsTheExactMirrorAndStartsAtOnce() {
|
||||
// Opening never waits; closing one level is its reverse, so it never
|
||||
// waits either.
|
||||
assertEquals(0, paneSlideDelayMillis(level = 1, depth = 0, previousDepth = 1))
|
||||
assertEquals(0, paneSlideDelayMillis(level = 2, depth = 1, previousDepth = 2))
|
||||
// A level at or below the depth is open/opening: no stagger.
|
||||
assertEquals(0, paneSlideDelayMillis(level = 1, depth = 1, previousDepth = 0))
|
||||
assertEquals(0, paneSlideDelayMillis(level = 0, depth = 2, previousDepth = 1))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun severalClosingLevelsReverseInOrderFromTheTop() {
|
||||
// Popping the whole stack to the list: the top pane leaves first and the
|
||||
// level beneath waits one step, so the cascade reads in order.
|
||||
assertEquals(0, paneSlideDelayMillis(level = 2, depth = 0, previousDepth = 2))
|
||||
assertEquals(
|
||||
PANE_POP_STEP_MILLIS,
|
||||
paneSlideDelayMillis(level = 1, depth = 0, previousDepth = 2),
|
||||
)
|
||||
assertTrue(PANE_SLIDE_MILLIS > 0)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user