camera: zoom-bar swipe is relative, no landing jump
- Commit
3a5ccd94c7dcd50e71415f7975c3a153fddfe9c0- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-24 17:21 UTC (11 days ago)
- Parents
e70179d
The scrub emitted the finger's absolute bar position mapped across the whole range, so landing near the right end jumped straight to ~max. The router now anchors at the current ratio on drag start and scales multiplicatively from drag distance (full log range over two bar widths), emitting only past touch slop; touch-down alone emits nothing. Tap-to-chip selection, clamping, and ZoomLockPolicy composition unchanged. Verified: ZoomScrubTest 15 + ZoomBarScrubComposeTest 5 green incl. the far-end small-drag regression; compile green.
Diff
camera/src/main/java/com/vayunmathur/camera/domain/ZoomScrub.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/domain/ZoomScrub.kt b/camera/src/main/java/com/vayunmathur/camera/domain/ZoomScrub.ktindex 418039b96a..db4a4e0da9 100644--- a/camera/src/main/java/com/vayunmathur/camera/domain/ZoomScrub.kt+++ b/camera/src/main/java/com/vayunmathur/camera/domain/ZoomScrub.kt@@ -4,8 +4,9 @@ import kotlin.math.exp import kotlin.math.ln /**- * Pure zoom-scrub seam for the ZoomBar: maps a horizontal position (or a- * cumulative drag delta) across the bar to a continuous zoom ratio.+ * Pure zoom-scrub seam for the ZoomBar: maps a cumulative drag delta+ * (relative to the ratio anchored at drag start) to a continuous zoom+ * ratio. * * Zoom ratios are multiplicative (2x is as far from 1x as 1x is from * 0.5x), so the mapping interpolates in log space: the bar's midpoint@@ -13,6 +14,13 @@ import kotlin.math.ln * entry point clamps to the device range and sanitises degenerate or * non-finite ranges the same way [PreviewGestureRouter.updateZoomRange] * does, so a scrub can never emit a ratio the session would reject.+ *+ * Scrubbing is RELATIVE, never absolute: the finger's touchdown+ * position on the bar carries no zoom meaning. [ZoomScrubRouter]+ * anchors on the current ratio at drag start and scales it by the+ * drag delta only (see [zoomRatioForDragDelta]), so a small movement+ * produces a small ratio change wherever the finger lands, and+ * touch-down alone emits nothing. */ /** Sanitised [minZoom]..[maxZoom]; invalid/non-finite collapses to 1x..1x. */@@ -44,6 +52,11 @@ fun zoomRatioForFraction(fraction: Float, minZoom: Float, maxZoom: Float): Float * Ratio at horizontal position [xPx] within a bar [widthPx] wide. * A non-positive/non-finite width has no position to map, so it returns * the range minimum (the scrub simply has not started meaningfully).+ *+ * Absolute position mapping — NOT used for scrubbing (a scrub is+ * relative to its anchor, see [zoomRatioForDragDelta]); retained as+ * the pure position/fraction mapping the inverse helpers round-trip+ * against. */ fun zoomRatioForPosition(xPx: Float, widthPx: Float, minZoom: Float, maxZoom: Float): Float { val (min, max) = sanitizedZoomRange(minZoom, maxZoom)@@ -61,10 +74,29 @@ fun fractionForZoomRatio(ratio: Float, minZoom: Float, maxZoom: Float): Float { (ln(max.toDouble()) - ln(min.toDouble()))).toFloat().coerceIn(0f, 1f) } +/**+ * Number of bar widths of drag that traverses the whole log zoom range+ * (min -> max as a multiplicative factor).+ *+ * Sensitivity choice: 2 bar widths for the full range. One bar width+ * therefore multiplies the anchored ratio by `sqrt(max / min)` (e.g.+ * ~4.5x over a 0.5x..10x range), and a touch-slop-sized movement moves+ * the ratio by only a few percent. One width for the full range (the+ * previous feel) made short scrubs twitchy: a quarter-bar flick from+ * 1x already landed past 2x on a 0.5x..10x device. Two widths keeps a+ * single confident swipe able to cross most of the usable range while+ * fine adjustment near the anchor stays fine.+ */+internal const val ZOOM_SCRUB_FULL_RANGE_WIDTHS = 2f+ /** * Ratio after dragging [cumulativeDeltaPx] from [startRatio] across a bar- * [widthPx] wide: one full bar-width of drag traverses the whole log- * range. A non-positive/non-finite width leaves [startRatio] (clamped).+ * [widthPx] wide: [ZOOM_SCRUB_FULL_RANGE_WIDTHS] bar-widths of drag+ * traverse the whole log range, i.e.+ * `ratio = startRatio * exp(ln(max/min) * dx / (widths * widthPx))`,+ * clamped to the range. The result depends only on the delta and the+ * anchored [startRatio] — never on where on the bar the drag began.+ * A non-positive/non-finite width leaves [startRatio] (clamped). */ fun zoomRatioForDragDelta( startRatio: Float,@@ -78,7 +110,8 @@ fun zoomRatioForDragDelta( val start = if (startRatio.isFinite()) startRatio.coerceIn(min, max) else min if (!widthPx.isFinite() || widthPx <= 0f || !cumulativeDeltaPx.isFinite()) return start val logSpan = ln(max.toDouble()) - ln(min.toDouble())- val target = exp(ln(start.toDouble()) + (cumulativeDeltaPx / widthPx).toDouble() * logSpan)+ val widths = (cumulativeDeltaPx / widthPx).toDouble() / ZOOM_SCRUB_FULL_RANGE_WIDTHS+ val target = exp(ln(start.toDouble()) + widths * logSpan) return target.toFloat().coerceIn(min, max) } @@ -90,11 +123,15 @@ fun zoomRatioForDragDelta( * the panorama/photosphere zoom lock, so locked modes ignore scrubs * exactly like pinch — the bar is hidden there regardless). *- * The bar width is frozen at drag start: the live-ratio chip the ZoomBar- * inserts mid-scrub changes the row's width, and re-deriving the fraction- * from the new width would make the ratio jump under a stationary finger.- * A gesture only becomes a scrub once it moves past the touch slop; the- * handler uses [dragEnd]'s result to tell a scrub from a chip tap.+ * The bar width and the current ratio are frozen (anchored) at drag+ * start: the live-ratio chip the ZoomBar inserts mid-scrub changes the+ * row's width, and re-deriving anything from the new width or the+ * finger's absolute position would make the ratio jump under a+ * stationary finger. Every emission is [zoomRatioForDragDelta] from+ * the anchor, driven by `x - downX` only. A gesture only becomes a+ * scrub once it moves past the touch slop (touch-down and sub-slop+ * movement emit nothing); the handler uses [dragEnd]'s result to tell+ * a scrub from a chip tap. */ class ZoomScrubRouter( private val onZoom: (Float) -> Unit,@@ -106,6 +143,7 @@ class ZoomScrubRouter( private var widthAtDragStartPx = 0f private var downXPx = 0f+ private var anchorRatio = 1f private var scrubbing = false val isScrubbing: Boolean get() = scrubbing@@ -117,16 +155,23 @@ class ZoomScrubRouter( maxZoomRatio = max } - /** A pointer went down at [xPx] on a bar [widthPx] wide. */- fun dragStart(xPx: Float, widthPx: Float) {+ /**+ * A pointer went down at [xPx] on a bar [widthPx] wide, with+ * [currentRatio] the live zoom ratio to anchor the scrub on+ * (clamped into the range at emission time). Emits nothing.+ */+ fun dragStart(xPx: Float, widthPx: Float, currentRatio: Float) { downXPx = xPx widthAtDragStartPx = widthPx+ anchorRatio = currentRatio scrubbing = false } /** * The pointer moved to [xPx]. Past [touchSlopPx] from the down point- * the gesture is a scrub and every further move emits its ratio live.+ * the gesture is a scrub and every further move emits its ratio live,+ * computed from the delta `xPx - downX` against the anchored ratio —+ * never from the absolute position. */ fun dragTo(xPx: Float, touchSlopPx: Float = 0f) { if (!scrubbing) {@@ -134,7 +179,15 @@ class ZoomScrubRouter( if (dx * dx <= touchSlopPx * touchSlopPx) return scrubbing = true }- onZoom(zoomRatioForPosition(xPx, widthAtDragStartPx, minZoomRatio, maxZoomRatio))+ onZoom(+ zoomRatioForDragDelta(+ anchorRatio,+ xPx - downXPx,+ widthAtDragStartPx,+ minZoomRatio,+ maxZoomRatio,+ )+ ) } /** All pointers up: true when the gesture was a scrub (not a tap). */@@ -150,5 +203,6 @@ class ZoomScrubRouter( private fun reset() { scrubbing = false widthAtDragStartPx = 0f+ anchorRatio = 1f } }
camera/src/main/java/com/vayunmathur/camera/ui/ZoomBar.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/ZoomBar.kt b/camera/src/main/java/com/vayunmathur/camera/ui/ZoomBar.ktindex 067bea0ca2..bc34f5b29a 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/ZoomBar.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/ZoomBar.kt@@ -71,6 +71,7 @@ internal fun ZoomBar( // Latest values for the long-lived gesture handler below. val currentOnZoomSelected by rememberUpdatedState(onZoomSelected) val currentZoomRange by rememberUpdatedState(zoomRange)+ val currentZoomState by rememberUpdatedState(currentZoom) var chipBounds by remember { mutableStateOf<List<ZoomChipBounds>>(emptyList()) } val currentChipBounds by rememberUpdatedState(chipBounds) Row(@@ -78,8 +79,10 @@ internal fun ZoomBar( .background(Color(0x99000000), androidx.compose.foundation.shape.RoundedCornerShape(24.dp)) // One gesture handler for the whole bar (the PreviewGestureRouter // lesson: two competing detectors race). Past the touch slop a- // horizontal drag is a continuous scrub — position across the- // bar maps log-scale across the device zoom range and is applied+ // horizontal drag is a continuous RELATIVE scrub — the ratio+ // anchored at drag start is scaled log-scale by the drag delta+ // (never by the finger's absolute position, so touch-down alone+ // emits nothing and a small move never jumps) and is applied // live through onZoomSelected (CameraViewModel.setZoomRatio, so // clamping and the panorama/photosphere zoom lock compose // exactly as for pinch); a release without scrubbing selects@@ -92,7 +95,7 @@ internal fun ZoomBar( val down = awaitFirstDown(requireUnconsumed = false) val (minZoom, maxZoom) = currentZoomRange router.updateZoomRange(minZoom, maxZoom)- router.dragStart(down.position.x, size.width.toFloat())+ router.dragStart(down.position.x, size.width.toFloat(), currentZoomState) try { while (true) { val event = awaitPointerEvent()
camera/src/test/java/com/vayunmathur/camera/domain/ZoomScrubTest.kt
diff --git a/camera/src/test/java/com/vayunmathur/camera/domain/ZoomScrubTest.kt b/camera/src/test/java/com/vayunmathur/camera/domain/ZoomScrubTest.ktindex 16799d394a..9590a31dea 100644--- a/camera/src/test/java/com/vayunmathur/camera/domain/ZoomScrubTest.kt+++ b/camera/src/test/java/com/vayunmathur/camera/domain/ZoomScrubTest.kt@@ -6,7 +6,7 @@ import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue -/** Log-scale position->ratio mapping and scrub routing for the ZoomBar. */+/** Log-scale drag-delta->ratio mapping and relative scrub routing for the ZoomBar. */ class ZoomScrubTest { @Test@@ -65,11 +65,14 @@ class ZoomScrubTest { } @Test- fun dragDeltaTraversesWholeRangeOverOneBarWidth() {- // One full width from the minimum reaches the maximum.- assertEquals(8f, zoomRatioForDragDelta(0.5f, 200f, 200f, 0.5f, 8f), 0.001f)- // Half a width from 1x (quarter-way in log space) reaches 3/4-way: 4x.- assertEquals(4f, zoomRatioForDragDelta(1f, 100f, 200f, 0.5f, 8f), 0.001f)+ fun dragDeltaTraversesWholeRangeOverTwoBarWidths() {+ // Sensitivity: full log range over ZOOM_SCRUB_FULL_RANGE_WIDTHS (2)+ // bar widths. Two full widths from the minimum reaches the maximum.+ assertEquals(8f, zoomRatioForDragDelta(0.5f, 400f, 200f, 0.5f, 8f), 0.001f)+ // One width multiplies by sqrt(max/min) = 4: 0.5x -> 2x.+ assertEquals(2f, zoomRatioForDragDelta(0.5f, 200f, 200f, 0.5f, 8f), 0.001f)+ // Half a width from 1x multiplies by 16^0.25 = 2.+ assertEquals(2f, zoomRatioForDragDelta(1f, 100f, 200f, 0.5f, 8f), 0.001f) // Overshoot clamps; a degenerate width leaves the clamped start. assertEquals(8f, zoomRatioForDragDelta(1f, 10_000f, 200f, 0.5f, 8f), 0.001f) assertEquals(0.5f, zoomRatioForDragDelta(1f, -10_000f, 200f, 0.5f, 8f), 0.001f)@@ -81,7 +84,7 @@ class ZoomScrubTest { val emitted = mutableListOf<Float>() val router = ZoomScrubRouter(onZoom = { emitted.add(it) }) router.updateZoomRange(0.5f, 8f)- router.dragStart(100f, 200f)+ router.dragStart(100f, 200f, currentRatio = 1f) router.dragTo(105f, touchSlopPx = 10f) assertTrue(emitted.isEmpty(), "movement within slop must not zoom") assertFalse(router.isScrubbing)@@ -89,43 +92,108 @@ class ZoomScrubTest { router.dragTo(200f, touchSlopPx = 10f) assertTrue(router.isScrubbing) assertEquals(2, emitted.size)- // 150/200 -> fraction .75, 200/200 -> max, both in range and rising.- assertTrue(emitted[0] in 0.5f..8f && emitted[1] == 8f && emitted[0] < emitted[1])+ // Relative from the 1x anchor: dx=50 -> 16^(50/400) = 1.414x,+ // dx=100 -> 16^(100/400) = 2x. Position on the bar is irrelevant.+ assertEquals(1.4142f, emitted[0], 0.001f)+ assertEquals(2f, emitted[1], 0.001f) assertTrue(router.dragEnd(), "a scrubbed gesture must not read as a tap") } + @Test+ fun routerTouchdownAnywhereEmitsNothingUntilMovement() {+ // Touch-down (and sub-slop movement) at the left edge, middle and+ // right edge of the bar must all emit nothing: position is not zoom.+ for (downX in listOf(0f, 100f, 200f)) {+ val emitted = mutableListOf<Float>()+ val router = ZoomScrubRouter(onZoom = { emitted.add(it) })+ router.updateZoomRange(0.5f, 8f)+ router.dragStart(downX, 200f, currentRatio = 1f)+ assertTrue(emitted.isEmpty(), "touch-down at $downX must not zoom")+ router.dragTo(downX + 5f, touchSlopPx = 10f)+ assertTrue(emitted.isEmpty(), "sub-slop move at $downX must not zoom")+ assertFalse(router.dragEnd(), "no scrub past slop means tap at $downX")+ }+ }++ @Test+ fun routerEqualDeltasFromDifferentStartsProduceEqualMultiplicativeChanges() {+ fun scrub(downX: Float, startRatio: Float, dx: Float): Float {+ val emitted = mutableListOf<Float>()+ val router = ZoomScrubRouter(onZoom = { emitted.add(it) })+ router.updateZoomRange(0.5f, 8f)+ router.dragStart(downX, 200f, currentRatio = startRatio)+ router.dragTo(downX + dx, touchSlopPx = 1f)+ return emitted.single()+ }+ // Same delta + same anchor, different touchdown: identical result.+ assertEquals(scrub(10f, 1f, 40f), scrub(150f, 1f, 40f), 0.0001f)+ // Same delta, different anchors: equal multiplicative change.+ val from1x = scrub(20f, 1f, 40f)+ val from2x = scrub(180f, 2f, 40f)+ assertEquals(from1x / 1f, from2x / 2f, 0.001f)+ }++ @Test+ fun routerSmallDragFromOneXNeverJumpsTowardMax() {+ // Finger lands at the far (positional-max) end of the bar at 1x and+ // moves a little: the old absolute mapping emitted ~max; the+ // relative scrub must stay near 1x. dx=20/200 -> 16^(20/400) = 1.15x.+ val emitted = mutableListOf<Float>()+ val router = ZoomScrubRouter(onZoom = { emitted.add(it) })+ router.updateZoomRange(0.5f, 8f)+ router.dragStart(195f, 200f, currentRatio = 1f)+ router.dragTo(215f, touchSlopPx = 1f)+ assertEquals(1, emitted.size)+ assertEquals(1.1487f, emitted[0], 0.001f)+ assertTrue(emitted[0] < 1.3f, "small drag must stay near anchor, got $emitted")+ }++ @Test+ fun routerLongDragHitsClamp() {+ val emitted = mutableListOf<Float>()+ val router = ZoomScrubRouter(onZoom = { emitted.add(it) })+ router.updateZoomRange(0.5f, 8f)+ router.dragStart(100f, 200f, currentRatio = 2f)+ router.dragTo(100f + 10_000f, touchSlopPx = 1f)+ router.dragTo(100f - 10_000f, touchSlopPx = 1f)+ assertEquals(listOf(8f, 0.5f), emitted)+ }+ @Test fun routerTapAndCancelEmitNothing() { val emitted = mutableListOf<Float>() val router = ZoomScrubRouter(onZoom = { emitted.add(it) }) router.updateZoomRange(0.5f, 8f)- router.dragStart(100f, 200f)+ router.dragStart(100f, 200f, currentRatio = 1f) router.dragTo(102f, touchSlopPx = 10f) assertFalse(router.dragEnd(), "a plain press-release is a tap") assertTrue(emitted.isEmpty()) - router.dragStart(100f, 200f)+ router.dragStart(100f, 200f, currentRatio = 1f) router.dragTo(190f, touchSlopPx = 10f) router.dragCancel() assertFalse(router.isScrubbing) // The pre-cancel live emission stands (it was applied live), but // the cancel itself adds nothing and leaves the router reusable. assertEquals(1, emitted.size)- router.dragStart(100f, 200f)+ router.dragStart(100f, 200f, currentRatio = 1f) assertFalse(router.dragEnd()) } @Test- fun routerFreezesWidthCapturedAtDragStart() {+ fun routerFreezesWidthAndAnchorCapturedAtDragStart() { val emitted = mutableListOf<Float>() val router = ZoomScrubRouter(onZoom = { emitted.add(it) }) router.updateZoomRange(1f, 4f)- router.dragStart(50f, 100f)- // Same x always yields the same ratio within one gesture, even- // though the live-chip insertion would widen the real bar.+ router.dragStart(50f, 100f, currentRatio = 2f)+ // Same delta always yields the same ratio within one gesture, even+ // though the live-chip insertion would widen the real bar and the+ // live ratio has since moved: dx=50 -> 2x * 4^(50/200) = 2.83x. router.dragTo(100f, touchSlopPx = 1f) router.dragTo(100f, touchSlopPx = 1f)- assertEquals(listOf(4f, 4f), emitted)+ assertEquals(2, emitted.size)+ assertEquals(2.8284f, emitted[0], 0.001f)+ assertEquals(emitted[0], emitted[1], 0.0001f) } @Test@@ -133,7 +201,7 @@ class ZoomScrubTest { val emitted = mutableListOf<Float>() val router = ZoomScrubRouter(onZoom = { emitted.add(it) }) router.updateZoomRange(Float.NaN, -1f)- router.dragStart(0f, 100f)+ router.dragStart(0f, 100f, currentRatio = 1f) router.dragTo(100f, touchSlopPx = 1f) assertEquals(listOf(1f), emitted) }
camera/src/test/java/com/vayunmathur/camera/ui/ZoomBarScrubComposeTest.kt
diff --git a/camera/src/test/java/com/vayunmathur/camera/ui/ZoomBarScrubComposeTest.kt b/camera/src/test/java/com/vayunmathur/camera/ui/ZoomBarScrubComposeTest.ktindex 466ae99142..3dbd144397 100644--- a/camera/src/test/java/com/vayunmathur/camera/ui/ZoomBarScrubComposeTest.kt+++ b/camera/src/test/java/com/vayunmathur/camera/ui/ZoomBarScrubComposeTest.kt@@ -20,10 +20,12 @@ import org.junit.Rule import org.junit.runner.RunWith /**- * ZoomBar scrub: a horizontal drag across the bar emits continuous zoom- * through the same callback the chips use (CameraViewModel.setZoomRatio- * in production, so clamping and the zoom lock compose); a tap still- * selects the chip; locked modes render no bar to scrub.+ * ZoomBar scrub: a horizontal drag emits continuous RELATIVE zoom (the+ * ratio anchored at drag start, scaled by the drag delta — never the+ * finger's absolute position) through the same callback the chips use+ * (CameraViewModel.setZoomRatio in production, so clamping and the+ * zoom lock compose); a tap still selects the chip; locked modes+ * render no bar to scrub. */ @RunWith(AndroidJUnit4::class) class ZoomBarScrubComposeTest {@@ -82,6 +84,32 @@ class ZoomBarScrubComposeTest { assertTrue(emitted.last() < 5f, "dragging left must zoom out: $emitted") } + @Test fun smallDragFromFarRightAtOneXDoesNotJumpTowardMax() {+ // Regression for the absolute-position scrub: at 1x, landing on+ // the far (5x) end of the bar and moving a little emitted ~5x.+ // A relative scrub anchors at 1x, so a short drag stays near 1x.+ val emitted = mutableListOf<Float>()+ rule.setContent {+ ZoomBar(+ currentZoom = 1f,+ zoomLevels = levels,+ onZoomSelected = { emitted.add(it) },+ zoomRange = 0.5f to 5f,+ )+ }+ rule.onRoot().performTouchInput {+ down(Offset(width - 4f, center.y))+ moveBy(Offset(48f, 0f))+ up()+ }+ rule.waitForIdle()+ assertTrue(emitted.isNotEmpty(), "past-slop drag must emit a scrub: $emitted")+ assertTrue(+ emitted.all { it in 0.9f..2f },+ "small drag from 1x must stay near 1x wherever it lands, got $emitted",+ )+ }+ @Test fun tapStillSelectsChip() { var selected: Float? = null rule.setContent {