library: add travelledOffset one-shot motion helper (#owner-report)
- Commit
d8fc1d2401076e09b84583d6e50e6deb987bcf89- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-12 20:35 UTC (23 days ago)
- Parents
d97faf9
Context: Solitaire needs several cards flying at once (tap-to-move,
drag drops, a rejected drag returning, auto-complete hops), each
landing on its own clock without restarting or jumping the others.
The app modules may not reach for compose animation directly, and
library/ui had only target-following helpers (animatedDp & co), which
re-aim one shared value rather than run independent keyed trips.
What: travelledOffset(key, from, to, spec = Motion.drop(), onArrived)
returns a State<Offset> that travels once from `from` to `to` and
then calls onArrived. Each key is its own trip, so sibling travellers
are independent and recomposition never restarts one in the air; a
moved `to` re-aims from the current position. Returned as State so
callers can defer the read to layout (Modifier.offset { }) and a trip
does not recompose its caller every frame.
User-visible effect: none by itself; used by the Solitaire card
motion layer in the following commits.
Verification: :library:ui:compileDebugKotlin and :library:ui:detekt
in the container build (see branch report).Diff
library/ui/src/main/java/com/vayunmathur/library/ui/TravelledOffset.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/TravelledOffset.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/TravelledOffset.ktnew file mode 100644index 0000000000..d40dbd7022--- /dev/null+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/TravelledOffset.kt@@ -0,0 +1,42 @@+package com.vayunmathur.library.ui++import androidx.compose.animation.core.Animatable+import androidx.compose.animation.core.FiniteAnimationSpec+import androidx.compose.animation.core.VectorConverter+import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.State+import androidx.compose.runtime.remember+import androidx.compose.runtime.rememberUpdatedState+import androidx.compose.ui.geometry.Offset++/**+ * An [Offset] that travels once from [from] to [to] and then calls [onArrived]: a dropped item+ * flying from the finger to its cell, a card returning to the pile it was picked up from.+ *+ * One-shot and keyed rather than target-following like [animatedDp]: the caller owns each trip's+ * identity through [key], so several travellers can be in the air at once, each on its own clock,+ * and recomposing the caller (or adding a sibling traveller) never restarts or jumps one that is+ * already moving. A new [key] starts a new trip from [from]. If [to] moves mid-trip (the+ * destination was re-laid out), the traveller re-aims from wherever it has got to.+ *+ * Returned as a [State] so the reader can defer the read to layout (`Modifier.offset { }`) and a+ * trip does not recompose its caller every frame. Defaults to [Motion.drop], the platform's+ * finger-to-cell landing.+ */+@Composable+fun travelledOffset(+ key: Any,+ from: Offset,+ to: Offset,+ spec: FiniteAnimationSpec<Offset> = Motion.drop(),+ onArrived: () -> Unit = {},+): State<Offset> {+ val position = remember(key) { Animatable(from, Offset.VectorConverter) }+ val arrived = rememberUpdatedState(onArrived)+ LaunchedEffect(key, to) {+ position.animateTo(to, spec)+ arrived.value()+ }+ return position.asState()+}