camera: drag along the zoom bar scrubs zoom; split bars file
- Commit
e70179d6da3356c7186acefc8ad59792e2ffddf1- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-24 17:06 UTC (11 days ago)
- Parents
3175e1f
ZoomBar gains a continuous horizontal scrub: one pointerInput handler maps bar position to a zoom ratio by log interpolation across the session zoom range (midpoint = geometric mean), frozen width/range at drag-start so the live-ratio chip can't shift the scale mid-gesture; taps still select chips (semantics click preserved). Same setZoomRatio path as chips and pinch, so ZoomLockPolicy composes. Pure mapping in domain/ZoomScrub.kt (11 tests) + compose tests. CameraSettingsBars.kt split at 378 lines over the 350-line ui limit: ZoomBar (151 lines) into ZoomBar.kt. Verified: lintVitalRelease + compile + targeted zoom tests 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.ktnew file mode 100644index 0000000000..418039b96a--- /dev/null+++ b/camera/src/main/java/com/vayunmathur/camera/domain/ZoomScrub.kt@@ -0,0 +1,154 @@+package com.vayunmathur.camera.domain++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.+ *+ * 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+ * is the geometric mean of the range, not its arithmetic mean. Every+ * 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.+ */++/** Sanitised [minZoom]..[maxZoom]; invalid/non-finite collapses to 1x..1x. */+internal fun sanitizedZoomRange(minZoom: Float, maxZoom: Float): Pair<Float, Float> =+ if (!minZoom.isFinite() || !maxZoom.isFinite() || minZoom <= 0f || maxZoom < minZoom) {+ 1f to 1f+ } else {+ minZoom to maxZoom+ }++private fun logRatioForFraction(fraction: Double, minZoom: Double, maxZoom: Double): Float =+ exp(ln(minZoom) + fraction * (ln(maxZoom) - ln(minZoom))).toFloat()++/**+ * Ratio at [fraction] (0 = bar start, 1 = bar end) of [minZoom]..[maxZoom]+ * on a log scale. Endpoints return [minZoom]/[maxZoom] exactly; a fraction+ * outside 0..1 (or non-finite) clamps to the nearer end. A degenerate+ * range (min == max) returns that ratio for every fraction.+ */+fun zoomRatioForFraction(fraction: Float, minZoom: Float, maxZoom: Float): Float {+ val (min, max) = sanitizedZoomRange(minZoom, maxZoom)+ if (min == max) return min+ val f = if (fraction.isFinite()) fraction.coerceIn(0f, 1f).toDouble() else 0.0+ return logRatioForFraction(f, min.toDouble(), max.toDouble())+ .coerceIn(min, max)+}++/**+ * 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).+ */+fun zoomRatioForPosition(xPx: Float, widthPx: Float, minZoom: Float, maxZoom: Float): Float {+ val (min, max) = sanitizedZoomRange(minZoom, maxZoom)+ if (min == max) return min+ if (!widthPx.isFinite() || widthPx <= 0f || !xPx.isFinite()) return min+ return zoomRatioForFraction(xPx / widthPx, min, max)+}++/** Inverse of [zoomRatioForFraction]: where [ratio] sits in the range, 0..1. */+fun fractionForZoomRatio(ratio: Float, minZoom: Float, maxZoom: Float): Float {+ val (min, max) = sanitizedZoomRange(minZoom, maxZoom)+ if (min == max) return 0f+ val clamped = if (ratio.isFinite()) ratio.coerceIn(min, max) else min+ return ((ln(clamped.toDouble()) - ln(min.toDouble())) /+ (ln(max.toDouble()) - ln(min.toDouble()))).toFloat().coerceIn(0f, 1f)+}++/**+ * 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).+ */+fun zoomRatioForDragDelta(+ startRatio: Float,+ cumulativeDeltaPx: Float,+ widthPx: Float,+ minZoom: Float,+ maxZoom: Float,+): Float {+ val (min, max) = sanitizedZoomRange(minZoom, maxZoom)+ if (min == max) return min+ 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)+ return target.toFloat().coerceIn(min, max)+}++/**+ * Stateful scrub router for the ZoomBar's single gesture handler, in the+ * style of [PreviewGestureRouter]: the UI feeds it pointer positions and+ * it emits live ratios through [onZoom] (wired to+ * `CameraViewModel.setZoomRatio`, which clamps again and composes with+ * 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.+ */+class ZoomScrubRouter(+ private val onZoom: (Float) -> Unit,+) {+ var minZoomRatio: Float = 1f+ private set+ var maxZoomRatio: Float = 1f+ private set++ private var widthAtDragStartPx = 0f+ private var downXPx = 0f+ private var scrubbing = false++ val isScrubbing: Boolean get() = scrubbing++ /** Sets the clamp range; a degenerate/non-finite range collapses to 1x. */+ fun updateZoomRange(minZoom: Float, maxZoom: Float) {+ val (min, max) = sanitizedZoomRange(minZoom, maxZoom)+ minZoomRatio = min+ maxZoomRatio = max+ }++ /** A pointer went down at [xPx] on a bar [widthPx] wide. */+ fun dragStart(xPx: Float, widthPx: Float) {+ downXPx = xPx+ widthAtDragStartPx = widthPx+ 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.+ */+ fun dragTo(xPx: Float, touchSlopPx: Float = 0f) {+ if (!scrubbing) {+ val dx = xPx - downXPx+ if (dx * dx <= touchSlopPx * touchSlopPx) return+ scrubbing = true+ }+ onZoom(zoomRatioForPosition(xPx, widthAtDragStartPx, minZoomRatio, maxZoomRatio))+ }++ /** All pointers up: true when the gesture was a scrub (not a tap). */+ fun dragEnd(): Boolean {+ val wasScrubbing = scrubbing+ reset()+ return wasScrubbing+ }++ /** The gesture was cancelled: no tap must be inferred from it. */+ fun dragCancel() = reset()++ private fun reset() {+ scrubbing = false+ widthAtDragStartPx = 0f+ }+}
camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewBox.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewBox.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewBox.ktindex ee0429ce74..be4ae157d1 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewBox.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewBox.kt@@ -227,6 +227,7 @@ internal fun BoxWithConstraintsScope.CameraPreviewBox( zoomRatio = state.zoomRatio, availableZoomLevels = state.availableZoomLevels, onZoomSelected = { viewModel.setZoomRatio(it) },+ zoomRange = viewModel.currentZoomRange(), exposureComp = state.exposureComp, onExposureComp = { viewModel.setExposureCompensation(it) }, shadows = state.shadows,
camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsBars.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsBars.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsBars.ktindex e6470542ef..f949474639 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsBars.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsBars.kt@@ -13,7 +13,6 @@ import androidx.compose.foundation.layout.width import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.CircleShape import androidx.compose.runtime.Composable-import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip@@ -26,7 +25,6 @@ import androidx.compose.ui.unit.sp import com.vayunmathur.camera.R import com.vayunmathur.camera.platform.CameraMode import com.vayunmathur.camera.platform.CameraViewModel-import com.vayunmathur.camera.platform.formatZoomLabel import com.vayunmathur.library.ui.IconBlur import com.vayunmathur.library.ui.IconContrast import com.vayunmathur.library.ui.IconIso@@ -139,53 +137,6 @@ internal fun SettingsButtonRow( } } -@Composable-internal fun ZoomBar(- currentZoom: Float,- zoomLevels: List<Pair<String, Float>>,- onZoomSelected: (Float) -> Unit,- modifier: Modifier = Modifier-) {- // Insert the live zoom ratio between the two fixed levels it falls between,- // unless it already matches one of the listed options.- val displayLevels = remember(zoomLevels, currentZoom) {- if (zoomLevels.any { kotlin.math.abs(it.second - currentZoom) < 0.05f }) {- zoomLevels- } else {- val entry = formatZoomLabel(currentZoom) to currentZoom- val insertAt = zoomLevels.indexOfFirst { it.second > currentZoom }- if (insertAt < 0) zoomLevels + entry- else zoomLevels.toMutableList().apply { add(insertAt, entry) }- }- }- Row(- modifier = modifier- .background(Color(0x99000000), androidx.compose.foundation.shape.RoundedCornerShape(24.dp))- .padding(horizontal = 6.dp, vertical = 4.dp),- horizontalArrangement = Arrangement.spacedBy(2.dp),- verticalAlignment = Alignment.CenterVertically- ) {- displayLevels.forEach { (label, ratio) ->- val isSelected = kotlin.math.abs(currentZoom - ratio) < 0.05f- Box(- modifier = Modifier- .size(36.dp)- .selectedPill(isSelected, CircleShape)- .clip(CircleShape)- .clickable { onZoomSelected(ratio) },- contentAlignment = Alignment.Center- ) {- Text(- label,- color = if (isSelected) Color.White else Color(0xFFBBBBBB),- fontSize = 13.sp,- fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Normal- )- }- }- }-}- @Composable internal fun ExposureTimeBar( selectedIndex: Int,
camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsColumn.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsColumn.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsColumn.ktindex 9f69fe7ec4..1f7369d491 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsColumn.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraSettingsColumn.kt@@ -30,6 +30,9 @@ internal fun CameraSettingsColumn( zoomRatio: Float, availableZoomLevels: List<Pair<String, Float>>, onZoomSelected: (Float) -> Unit,+ zoomRange: Pair<Float, Float> =+ (availableZoomLevels.minOfOrNull { it.second } ?: 1f) to+ (availableZoomLevels.maxOfOrNull { it.second } ?: 1f), exposureComp: Float, onExposureComp: (Float) -> Unit, shadows: Float,@@ -105,7 +108,8 @@ internal fun CameraSettingsColumn( ZoomBar( currentZoom = zoomRatio, zoomLevels = availableZoomLevels,- onZoomSelected = onZoomSelected+ onZoomSelected = onZoomSelected,+ zoomRange = zoomRange ) } }
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.ktnew file mode 100644index 0000000000..067bea0ca2--- /dev/null+++ b/camera/src/main/java/com/vayunmathur/camera/ui/ZoomBar.kt@@ -0,0 +1,151 @@+package com.vayunmathur.camera.ui++import androidx.compose.foundation.background+import androidx.compose.foundation.gestures.awaitEachGesture+import androidx.compose.foundation.gestures.awaitFirstDown+import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.size+import androidx.compose.foundation.shape.CircleShape+import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.rememberUpdatedState+import androidx.compose.runtime.setValue+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.draw.clip+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.input.pointer.pointerInput+import androidx.compose.ui.layout.onGloballyPositioned+import androidx.compose.ui.layout.positionInParent+import androidx.compose.ui.semantics.onClick+import androidx.compose.ui.semantics.semantics+import androidx.compose.ui.text.font.FontWeight+import androidx.compose.ui.unit.dp+import androidx.compose.ui.unit.sp+import com.vayunmathur.camera.domain.ZoomScrubRouter+import com.vayunmathur.camera.platform.formatZoomLabel+import com.vayunmathur.library.ui.Text++/** Horizontal extent of one ZoomBar chip in the bar, for tap resolution. */+private data class ZoomChipBounds(val ratio: Float, val left: Float, val right: Float)++/** The chip at [xPx], or the nearest chip when [xPx] falls in a gap/padding. */+private fun zoomChipAt(bounds: Collection<ZoomChipBounds>, xPx: Float): Float? =+ bounds.minByOrNull { chip ->+ when {+ xPx < chip.left -> chip.left - xPx+ xPx > chip.right -> xPx - chip.right+ else -> 0f+ }+ }?.ratio++@Composable+internal fun ZoomBar(+ currentZoom: Float,+ zoomLevels: List<Pair<String, Float>>,+ onZoomSelected: (Float) -> Unit,+ modifier: Modifier = Modifier,+ // Device zoom range (session zoomRatioRange) the scrub traverses;+ // defaults to the offered levels' span for previews/tests without a+ // live session.+ zoomRange: Pair<Float, Float> =+ (zoomLevels.minOfOrNull { it.second } ?: 1f) to (zoomLevels.maxOfOrNull { it.second } ?: 1f)+) {+ // Insert the live zoom ratio between the two fixed levels it falls between,+ // unless it already matches one of the listed options.+ val displayLevels = remember(zoomLevels, currentZoom) {+ if (zoomLevels.any { kotlin.math.abs(it.second - currentZoom) < 0.05f }) {+ zoomLevels+ } else {+ val entry = formatZoomLabel(currentZoom) to currentZoom+ val insertAt = zoomLevels.indexOfFirst { it.second > currentZoom }+ if (insertAt < 0) zoomLevels + entry+ else zoomLevels.toMutableList().apply { add(insertAt, entry) }+ }+ }+ // Latest values for the long-lived gesture handler below.+ val currentOnZoomSelected by rememberUpdatedState(onZoomSelected)+ val currentZoomRange by rememberUpdatedState(zoomRange)+ var chipBounds by remember { mutableStateOf<List<ZoomChipBounds>>(emptyList()) }+ val currentChipBounds by rememberUpdatedState(chipBounds)+ Row(+ modifier = modifier+ .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+ // live through onZoomSelected (CameraViewModel.setZoomRatio, so+ // clamping and the panorama/photosphere zoom lock compose+ // exactly as for pinch); a release without scrubbing selects+ // the chip under the finger. Chips carry only a semantics click+ // action (accessibility / test clicks), no touch detector of+ // their own.+ .pointerInput(Unit) {+ val router = ZoomScrubRouter(onZoom = { currentOnZoomSelected(it) })+ awaitEachGesture {+ val down = awaitFirstDown(requireUnconsumed = false)+ val (minZoom, maxZoom) = currentZoomRange+ router.updateZoomRange(minZoom, maxZoom)+ router.dragStart(down.position.x, size.width.toFloat())+ try {+ while (true) {+ val event = awaitPointerEvent()+ val pressed = event.changes.filter { it.pressed }+ if (pressed.isEmpty()) break+ router.dragTo(pressed[0].position.x, viewConfiguration.touchSlop)+ if (router.isScrubbing) event.changes.forEach { it.consume() }+ }+ if (!router.dragEnd()) {+ zoomChipAt(currentChipBounds, down.position.x)+ ?.let { currentOnZoomSelected(it) }+ }+ } catch (e: Exception) {+ router.dragCancel()+ throw e+ }+ }+ }+ .padding(horizontal = 6.dp, vertical = 4.dp),+ horizontalArrangement = Arrangement.spacedBy(2.dp),+ verticalAlignment = Alignment.CenterVertically+ ) {+ displayLevels.forEachIndexed { index, (label, ratio) ->+ val isSelected = kotlin.math.abs(currentZoom - ratio) < 0.05f+ Box(+ modifier = Modifier+ .size(36.dp)+ .selectedPill(isSelected, CircleShape)+ .clip(CircleShape)+ .onGloballyPositioned { coordinates ->+ val bounds = ZoomChipBounds(+ ratio = ratio,+ left = coordinates.positionInParent().x,+ right = coordinates.positionInParent().x + coordinates.size.width,+ )+ val previous = chipBounds.getOrNull(index)+ if (previous != bounds || chipBounds.size != displayLevels.size) {+ chipBounds = chipBounds.toMutableList().apply {+ while (size <= index) add(bounds)+ set(index, bounds)+ }.take(displayLevels.size)+ }+ }+ .semantics { onClick { onZoomSelected(ratio); true } },+ contentAlignment = Alignment.Center+ ) {+ Text(+ label,+ color = if (isSelected) Color.White else Color(0xFFBBBBBB),+ fontSize = 13.sp,+ fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Normal+ )+ }+ }+ }+}
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.ktnew file mode 100644index 0000000000..16799d394a--- /dev/null+++ b/camera/src/test/java/com/vayunmathur/camera/domain/ZoomScrubTest.kt@@ -0,0 +1,140 @@+package com.vayunmathur.camera.domain++import kotlin.math.sqrt+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertTrue++/** Log-scale position->ratio mapping and scrub routing for the ZoomBar. */+class ZoomScrubTest {++ @Test+ fun endpointsAreMinAndMax() {+ assertEquals(0.5f, zoomRatioForFraction(0f, 0.5f, 10f), 0.0001f)+ assertEquals(10f, zoomRatioForFraction(1f, 0.5f, 10f), 0.0001f)+ assertEquals(0.5f, zoomRatioForPosition(0f, 200f, 0.5f, 10f), 0.0001f)+ assertEquals(10f, zoomRatioForPosition(200f, 200f, 0.5f, 10f), 0.0001f)+ }++ @Test+ fun midpointIsGeometricMeanNotArithmeticMean() {+ // sqrt(0.5 * 8) = 2, whereas the arithmetic midpoint would be 4.25.+ assertEquals(2f, zoomRatioForFraction(0.5f, 0.5f, 8f), 0.001f)+ assertEquals(sqrt(0.5f * 10f), zoomRatioForPosition(100f, 200f, 0.5f, 10f), 0.001f)+ }++ @Test+ fun mappingIsMonotonicAcrossBar() {+ val ratios = (0..10).map { zoomRatioForFraction(it / 10f, 0.5f, 10f) }+ assertEquals(ratios.sorted(), ratios)+ assertTrue(ratios.zipWithNext().all { (a, b) -> a < b })+ }++ @Test+ fun outOfRangeInputsClampToRange() {+ assertEquals(0.5f, zoomRatioForFraction(-1f, 0.5f, 10f), 0.0001f)+ assertEquals(10f, zoomRatioForFraction(2f, 0.5f, 10f), 0.0001f)+ assertEquals(0.5f, zoomRatioForPosition(-50f, 200f, 0.5f, 10f), 0.0001f)+ assertEquals(10f, zoomRatioForPosition(999f, 200f, 0.5f, 10f), 0.0001f)+ assertEquals(0.5f, zoomRatioForFraction(Float.NaN, 0.5f, 10f), 0.0001f)+ }++ @Test+ fun degenerateAndInvalidRangesCollapse() {+ // min == max: every position is that ratio.+ assertEquals(2f, zoomRatioForFraction(0f, 2f, 2f), 0.0001f)+ assertEquals(2f, zoomRatioForFraction(0.7f, 2f, 2f), 0.0001f)+ assertEquals(2f, zoomRatioForPosition(123f, 200f, 2f, 2f), 0.0001f)+ // Non-finite / inverted / non-positive ranges collapse to 1x,+ // matching PreviewGestureRouter.updateZoomRange.+ assertEquals(1f, zoomRatioForFraction(0.5f, Float.NaN, 10f), 0.0001f)+ assertEquals(1f, zoomRatioForFraction(0.5f, 10f, 0.5f), 0.0001f)+ assertEquals(1f, zoomRatioForFraction(0.5f, 0f, 10f), 0.0001f)+ assertEquals(1f, zoomRatioForPosition(10f, 0f, 1f, 1f), 0.0001f)+ }++ @Test+ fun fractionInverseRoundTrips() {+ for (ratio in listOf(0.5f, 1f, 2f, 5f, 10f)) {+ val fraction = fractionForZoomRatio(ratio, 0.5f, 10f)+ assertEquals(ratio, zoomRatioForFraction(fraction, 0.5f, 10f), 0.001f)+ }+ assertEquals(0f, fractionForZoomRatio(0.1f, 0.5f, 10f), 0.0001f)+ assertEquals(1f, fractionForZoomRatio(99f, 0.5f, 10f), 0.0001f)+ }++ @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)+ // 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)+ assertEquals(2f, zoomRatioForDragDelta(2f, 100f, 0f, 0.5f, 8f), 0.001f)+ }++ @Test+ fun routerEmitsOnlyAfterSlopAndReportsScrub() {+ val emitted = mutableListOf<Float>()+ val router = ZoomScrubRouter(onZoom = { emitted.add(it) })+ router.updateZoomRange(0.5f, 8f)+ router.dragStart(100f, 200f)+ router.dragTo(105f, touchSlopPx = 10f)+ assertTrue(emitted.isEmpty(), "movement within slop must not zoom")+ assertFalse(router.isScrubbing)+ router.dragTo(150f, touchSlopPx = 10f)+ 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])+ assertTrue(router.dragEnd(), "a scrubbed gesture must not read as a tap")+ }++ @Test+ fun routerTapAndCancelEmitNothing() {+ val emitted = mutableListOf<Float>()+ val router = ZoomScrubRouter(onZoom = { emitted.add(it) })+ router.updateZoomRange(0.5f, 8f)+ router.dragStart(100f, 200f)+ router.dragTo(102f, touchSlopPx = 10f)+ assertFalse(router.dragEnd(), "a plain press-release is a tap")+ assertTrue(emitted.isEmpty())++ router.dragStart(100f, 200f)+ 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)+ assertFalse(router.dragEnd())+ }++ @Test+ fun routerFreezesWidthCapturedAtDragStart() {+ 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.dragTo(100f, touchSlopPx = 1f)+ router.dragTo(100f, touchSlopPx = 1f)+ assertEquals(listOf(4f, 4f), emitted)+ }++ @Test+ fun routerDegenerateRangeEmitsLockedRatio() {+ val emitted = mutableListOf<Float>()+ val router = ZoomScrubRouter(onZoom = { emitted.add(it) })+ router.updateZoomRange(Float.NaN, -1f)+ router.dragStart(0f, 100f)+ 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.ktnew file mode 100644index 0000000000..466ae99142--- /dev/null+++ b/camera/src/test/java/com/vayunmathur/camera/ui/ZoomBarScrubComposeTest.kt@@ -0,0 +1,123 @@+package com.vayunmathur.camera.ui++import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableFloatStateOf+import androidx.compose.runtime.setValue+import androidx.compose.ui.geometry.Offset+import androidx.compose.ui.test.assertIsDisplayed+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onAllNodesWithText+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.onRoot+import androidx.compose.ui.test.performClick+import androidx.compose.ui.test.performTouchInput+import androidx.test.ext.junit.runners.AndroidJUnit4+import com.vayunmathur.camera.platform.CameraMode+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue+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.+ */+@RunWith(AndroidJUnit4::class)+class ZoomBarScrubComposeTest {++ @get:Rule val rule = createComposeRule()++ private val levels = listOf(".5" to 0.5f, "1x" to 1f, "2x" to 2f, "5x" to 5f)++ @Test fun horizontalDragScrubsContinuouslyAcrossRange() {+ val emitted = mutableListOf<Float>()+ var current by mutableFloatStateOf(1f)+ rule.setContent {+ ZoomBar(+ currentZoom = current,+ zoomLevels = levels,+ onZoomSelected = { emitted.add(it); current = it },+ zoomRange = 0.5f to 5f,+ )+ }+ rule.onRoot().performTouchInput {+ down(Offset(4f, center.y))+ moveBy(Offset(width * 0.4f, 0f))+ moveBy(Offset(width * 0.4f, 0f))+ up()+ }+ rule.waitForIdle()+ assertTrue(emitted.size >= 2, "scrub must emit live, got $emitted")+ assertTrue(emitted.all { it in 0.5f..5f }, "scrub emissions must stay in range: $emitted")+ assertTrue(+ emitted.zipWithNext().all { (a, b) -> b >= a },+ "dragging right must not zoom out: $emitted",+ )+ assertTrue(emitted.last() > emitted.first(), "dragging right must zoom in: $emitted")+ // The live ratio follows the scrub (state fed back as currentZoom).+ assertEquals(emitted.last(), current)+ }++ @Test fun dragLeftZoomsOut() {+ val emitted = mutableListOf<Float>()+ rule.setContent {+ ZoomBar(+ currentZoom = 5f,+ zoomLevels = levels,+ onZoomSelected = { emitted.add(it) },+ zoomRange = 0.5f to 5f,+ )+ }+ rule.onRoot().performTouchInput {+ down(Offset(width - 4f, center.y))+ moveBy(Offset(-width * 0.6f, 0f))+ up()+ }+ rule.waitForIdle()+ assertTrue(emitted.isNotEmpty(), "leftward scrub must emit")+ assertTrue(emitted.all { it in 0.5f..5f }, "emissions in range: $emitted")+ assertTrue(emitted.last() < 5f, "dragging left must zoom out: $emitted")+ }++ @Test fun tapStillSelectsChip() {+ var selected: Float? = null+ rule.setContent {+ ZoomBar(+ currentZoom = 1f,+ zoomLevels = levels,+ onZoomSelected = { selected = it },+ zoomRange = 0.5f to 5f,+ )+ }+ rule.onNodeWithText("2x").assertIsDisplayed()+ rule.onNodeWithText("2x").performClick()+ assertEquals(2f, selected)+ }++ @Test fun lockedModesRenderNoZoomBarToScrub() {+ // Panorama/Photosphere: the column hides the bar entirely, and any+ // setZoomRatio that did arrive is ignored by the ViewModel (see+ // PanoramaZoomLockTest), so a scrub cannot move the locked 1x.+ rule.setContent {+ CameraSettingsColumn(+ activeSetting = null,+ availableLenses = emptyList(), selectedLens = null,+ onLensSelected = {}, zoomRatio = 1f,+ availableZoomLevels = levels,+ onZoomSelected = {}, exposureComp = 0f, onExposureComp = {},+ shadows = 0f, onShadows = {}, warmth = 0f, onWarmth = {},+ exposureTimeIndex = 0, onExposureTimeIndex = {},+ blurStrength = 0.5f, onBlurStrength = {},+ manualIsoIndex = 0, isoStops = emptyList(), onManualIsoIndex = {},+ cameraMode = CameraMode.PANORAMA, onSelectSetting = {},+ zoomRange = 0.5f to 5f,+ )+ }+ rule.waitForIdle()+ assertTrue(rule.onAllNodesWithText("2x").fetchSemanticsNodes().isEmpty())+ assertTrue(rule.onAllNodesWithText("5x").fetchSemanticsNodes().isEmpty())+ }+}