Modern Apps git

← Modern-Apps.git

astronomy: pinch zoom keeps device tracking, pan takes manual

Commit
9f7169ad1fc55d9b6ef7e828c03298a4a7268ca3
Author
Vayun Mathur <[email protected]>
When
2026-12-05 16:15 UTC (21 hours ago)
Parents
7729e1a
While the sky view followed device orientation, any pinch zoom
also took manual control of the center, so the most common gesture
- zooming while looking around - stopped the sky following the
phone and demanded a 45-degree turn to get tracking back.

The transform gesture's channels are now separated. Pinch and pan
arrive together from detectTransformGestures, so a new
GesturePanGate in ViewTransform accumulates the pan component
against the view-configuration touch slop: sub-slop centroid drift
during a pinch is withheld, the crossing event releases the full
accumulated pan, and later deltas pass through. In
ui/SkyCanvasGestures.kt, while the device drives the center, zoom
alone reports only the new field of view through the existing
setFov path (the centroid-fixed center shift from zoomedAbout is
discarded, since the sensors own the center), and only a released
pan reports an absolute manual view. In manual mode zoom and pan
apply together exactly as before. SkyMapPage/SkyMapScreen pass the
DevicePointing state down and wire onFovChange to setFov, so the
Settings slider and the pinch share the one zoom-only entry point;
no other zoom entry points exist in the app. The ViewModel's
orientation collector is extracted to onOrientationSample so the
arbitration is testable; setFov is documented as never swapping
the center mode.

The 45-degree re-engagement rule and zoom persistence across mode
swaps are unchanged: the anchor is captured only on a pan
takeover, and zoom applied before or after it survives.

Tests: ViewTransformTest pins the pan gate (drift withheld,
accumulated release on crossing, reset between gestures);
AstronomyViewModelTest pins zoom-only keeping DevicePointing,
pan taking manual control, zoom in manual mode keeping the
manual center, and 45-degree re-engagement after a pan takeover
with zoom applied before and after.

Verified: ./gradlew :astronomy:lint :astronomy:lintVitalRelease
:astronomy:testDevUnitTest --max-workers=128. Lint green; 81 JVM
tests with only the pre-existing SettingsScreenInteractionsTest
'sliders announce formatted values' failure (identical on a clean
origin/main worktree), and 76 native tests all passing. ktlint
test source set green; ktlint main shows only the pre-existing
SettingsScreen.kt blank-line violation in an untouched file.

Diff

astronomy/AGENTS.md
diff --git a/astronomy/AGENTS.md b/astronomy/AGENTS.mdindex 19472a01dd..51c6377677 100644--- a/astronomy/AGENTS.md+++ b/astronomy/AGENTS.md@@ -12,7 +12,7 @@ _Install: ./install astronomy (dev by default)._  ## Overview **Purpose:** Offline AR night-sky viewer — sensor-tracked sky map, camera passthrough, time scrubber, bundled star/planet catalogs, rise/set search. 100% offline.-**Key features:** `ui/SkyCanvas` + grid/art layers over `domain/projection/` (stereographic) fed by `domain/engine/` calculators (solar/lunar/planetary/rise-set/time) with a Rust core (`domain/AstronomyNative`); catalogs in `data/` validated at build time by `scripts/` generators. The sky view pinches to zoom about the fingers and drags to pan (`domain/projection/ViewTransform.kt`, FOV clamped to `ViewState` 10-120 degrees, shared with the Settings slider): the first gesture takes manual control from the sensors, and a phone rotation of 45 degrees or more from the takeover orientation (`platform/ManualViewArbitration.kt`) hands the center back to the sensors (the zoomed FOV is kept).+**Key features:** `ui/SkyCanvas` + grid/art layers over `domain/projection/` (stereographic) fed by `domain/engine/` calculators (solar/lunar/planetary/rise-set/time) with a Rust core (`domain/AstronomyNative`); catalogs in `data/` validated at build time by `scripts/` generators. The sky view pinches to zoom and drags to pan (`domain/projection/ViewTransform.kt`, FOV clamped to `ViewState` 10-120 degrees, shared with the Settings slider): while device orientation drives the center, pinch zoom changes only the field of view and never takes manual control (the Settings slider's `setFov` path behaves the same); a pan whose accumulated movement crosses the touch slop (`GesturePanGate` in `ViewTransform.kt`) takes manual control from the sensors, and a phone rotation of 45 degrees or more from the takeover orientation (`platform/ManualViewArbitration.kt`) hands the center back to the sensors (the zoomed FOV is kept). **Entry points:** `Route` → `MainActivity.kt` → `Navigation.kt` → `ui/SkyMapPage.kt` → `platform/AstronomyViewModel.kt` → `domain/engine/TimeEngine.kt`. Assistant entry: `intents/TonightIntent` (OpenAssistant `astronomy_get_tonight`) returns one capped tonight summary (Sun/Moon times, Moon phase, brightest visible planets) computed by the domain calculators; DTOs in `library/intents/astronomy/`, signature-permission gated. **Gotchas:** Catalog data is generated (`scripts/` DB generators) — never hand-edit; sensor + location drive the sky, test with movement, not static. @@ -47,6 +47,7 @@ _Install: ./install astronomy (dev by default)._ ### ui/ — sky map + detail + search - com/vayunmathur/astronomy/ui/SkyMapPage.kt - com/vayunmathur/astronomy/ui/SkyCanvas.kt+- com/vayunmathur/astronomy/ui/SkyCanvasGestures.kt — transform-gesture channels (zoom-only vs pan takeover) for `SkyCanvas` - com/vayunmathur/astronomy/ui/DrawDeepSkyObjects.kt - com/vayunmathur/astronomy/ui/SkyGrid.kt - com/vayunmathur/astronomy/ui/SkyArt.kt
astronomy/src/main/java/com/vayunmathur/astronomy/domain/projection/ViewTransform.kt
diff --git a/astronomy/src/main/java/com/vayunmathur/astronomy/domain/projection/ViewTransform.kt b/astronomy/src/main/java/com/vayunmathur/astronomy/domain/projection/ViewTransform.ktindex d6a489c5d4..f80374cc9c 100644--- a/astronomy/src/main/java/com/vayunmathur/astronomy/domain/projection/ViewTransform.kt+++ b/astronomy/src/main/java/com/vayunmathur/astronomy/domain/projection/ViewTransform.kt@@ -74,6 +74,53 @@ fun ViewState.pannedBy(drag: Offset): ViewState {     return copy(centerAzRad = newCenter.azRad, centerAltRad = newCenter.altRad) } +/**+ * Accumulates the pan component of one transform gesture until it is a+ * deliberate pan rather than centroid drift.+ *+ * Pinch gestures report pan and zoom together: spreading or closing the+ * fingers also moves their centroid a little, and that drift must not count+ * as panning (in device-pointing mode a pan takes manual control of the+ * view, while zoom alone must not). Feed each event's pan delta through+ * [consumePan]: deltas are withheld while the accumulated movement stays+ * within [slopPx]; the event that crosses the slop releases the full+ * accumulated pan at once, so the view does not jump when the pan channel+ * opens, and every later delta passes straight through. [beginGesture]+ * resets the accumulation for the next gesture.+ */+class GesturePanGate(+    private val slopPx: Float,+) {+    private var accumulatedPan = Offset.Zero++    /** True once the accumulated pan has crossed the slop for this gesture. */+    var isPanActive: Boolean = false+        private set++    /** Starts a new gesture: pan is withheld again until it crosses the slop. */+    fun beginGesture() {+        accumulatedPan = Offset.Zero+        isPanActive = false+    }++    /**+     * Feeds one event's pan delta and returns the pan to apply for that+     * event: [Offset.Zero] while the gesture has not yet panned beyond the+     * slop, the accumulated pan on the crossing event, and the delta itself+     * once the pan channel is open.+     */+    fun consumePan(pan: Offset): Offset {+        if (pan == Offset.Zero) return Offset.Zero+        if (isPanActive) return pan+        accumulatedPan += pan+        if (accumulatedPan.getDistance() <= slopPx) return Offset.Zero+        isPanActive = true+        val released = accumulatedPan+        accumulatedPan = Offset.Zero+        return released+    }+}+ private const val ZOOM_REFINE_PASSES = 24 private const val ZOOM_STEP_FRACTION = 0.5f private const val ZOOM_TOLERANCE_PX = 0.5f
astronomy/src/main/java/com/vayunmathur/astronomy/platform/AstronomyUiContract.kt
diff --git a/astronomy/src/main/java/com/vayunmathur/astronomy/platform/AstronomyUiContract.kt b/astronomy/src/main/java/com/vayunmathur/astronomy/platform/AstronomyUiContract.ktindex c7a02ffdff..3c1447b792 100644--- a/astronomy/src/main/java/com/vayunmathur/astronomy/platform/AstronomyUiContract.kt+++ b/astronomy/src/main/java/com/vayunmathur/astronomy/platform/AstronomyUiContract.kt@@ -40,6 +40,8 @@ data class SkyMapUiState(     val nightMode: Boolean = false,     val trajectory: List<TrajectoryPoint> = emptyList(),     val selectedObjectId: String? = null,+    /** True while the center follows the device orientation; pinch zoom stays zoom-only there. */+    val followsDeviceOrientation: Boolean = false,     /** Magnetometer accuracy from the device sensors; drives the calibration banner. */     val accuracy: Int = SensorManager.SENSOR_STATUS_ACCURACY_HIGH, )@@ -47,9 +49,10 @@ data class SkyMapUiState( /** Sky map callbacks. */ @OptIn(ExperimentalTime::class) interface SkyMapActions {+    /** Zoom-only: sets the field of view; never changes who owns the center. */     fun setFov(v: Float) {} -    /** Applies a pan/pinch gesture's absolute center and field of view. */+    /** Applies a pan gesture's absolute center and field of view; takes manual control. */     fun setView(         centerAzRad: Double,         centerAltRad: Double,
astronomy/src/main/java/com/vayunmathur/astronomy/platform/AstronomyViewModel.kt
diff --git a/astronomy/src/main/java/com/vayunmathur/astronomy/platform/AstronomyViewModel.kt b/astronomy/src/main/java/com/vayunmathur/astronomy/platform/AstronomyViewModel.ktindex 2b6b55267c..0e7d625fc5 100644--- a/astronomy/src/main/java/com/vayunmathur/astronomy/platform/AstronomyViewModel.kt+++ b/astronomy/src/main/java/com/vayunmathur/astronomy/platform/AstronomyViewModel.kt@@ -376,19 +376,7 @@ class AstronomyViewModel(             updateRiseSetForSelected()         }         viewModelScope.launch {-            orientationMgr.orientation.collect {-                _deviceOrientation.value = it-                // While the user is exploring manually the sensors must not-                // fight the pan, but a deliberate large turn of the phone-                // means "look here now": drop the manual center and follow-                // the phone again. Small rotations leave the manual view be.-                if (_viewCenter.value is CenterOption.Manual &&-                    shouldReengageTracking(manualAnchorOrientation, it)-                ) {-                    manualAnchorOrientation = null-                    _viewCenter.value = CenterOption.DevicePointing-                }-            }+            orientationMgr.orientation.collect { onOrientationSample(it) }         }         orientationMgr.start()         _viewCenter.value = CenterOption.DevicePointing@@ -471,26 +459,54 @@ class AstronomyViewModel(         prefs.saveDouble("astro_mag_limit", v.toDouble())     } +    /**+     * Zoom-only entry point (Settings slider and pinch-zoom while the+     * device orientation drives the center): changes the field of view and+     * nothing else. It never takes manual control and never drops it, so+     * the view keeps following the device at the new zoom, and a zoom+     * applied while manual survives the later re-engagement.+     */     override fun setFov(v: Float) {         _fovDeg.value = ViewState.clampFov(v)         prefs.saveDouble("astro_fov", _fovDeg.value.toDouble())     } +    /**+     * Handles one device-orientation sample: while the user is exploring+     * manually the sensors must not fight the pan, but a deliberate large+     * turn of the phone means "look here now": drop the manual center and+     * follow the phone again. Small rotations leave the manual view be.+     * The field of view is never touched here, so zoom applied before or+     * after a manual takeover survives re-engagement.+     */+    internal fun onOrientationSample(orientation: DeviceOrientation?) {+        _deviceOrientation.value = orientation+        if (_viewCenter.value is CenterOption.Manual &&+            shouldReengageTracking(manualAnchorOrientation, orientation)+        ) {+            manualAnchorOrientation = null+            _viewCenter.value = CenterOption.DevicePointing+        }+    }+     // The device orientation captured when the user took manual control of-    // the view by panning or pinching; null while the sensors (or an-    // explicit non-gesture mode change) own the view. A phone rotation of+    // the view by panning; null while the sensors (or an explicit+    // non-gesture mode change) own the view. A phone rotation of     // REENGAGE_ROTATION_DEG or more from this sample hands the view back to     // the sensors, however far the user had panned.     private var manualAnchorOrientation: DeviceOrientation? = null      /**-     * Applies a manual pan/pinch result from the sky canvas: the absolute-     * view center and field of view the gesture computed through the-     * projection. The first gesture while the phone is tracking takes-     * manual control (the current orientation becomes the re-engagement-     * anchor and the sensors stop driving the center); later gestures just-     * move the manual view. The field of view itself is kept when tracking-     * later re-engages — only the panned center is dropped.+     * Applies a manual pan result from the sky canvas: the absolute view+     * center and field of view the gesture computed through the projection+     * (a pan that crossed the touch slop, with any pinch zoom from the same+     * gesture already folded in). The first pan while the phone is tracking+     * takes manual control (the current orientation becomes the+     * re-engagement anchor and the sensors stop driving the center); later+     * pans just move the manual view. Zoom alone never goes through here —+     * it goes through [setFov] and leaves the center mode alone. The field+     * of view itself is kept when tracking later re-engages — only the+     * panned center is dropped.      */     override fun setView(         centerAzRad: Double,@@ -583,7 +599,8 @@ class AstronomyViewModel(         _isLive.value = true     } -    // No still mode per request: always tracks phone. Pan disabled in device mode (only zoom allowed)+    // No still mode per request: the view tracks the phone until a pan+    // beyond the touch slop takes manual control; zoom never swaps mode.      private fun updateTrajectoryForSelected() {         val id = _selectedObjectId.value ?: return
astronomy/src/main/java/com/vayunmathur/astronomy/ui/SkyCanvas.kt
diff --git a/astronomy/src/main/java/com/vayunmathur/astronomy/ui/SkyCanvas.kt b/astronomy/src/main/java/com/vayunmathur/astronomy/ui/SkyCanvas.ktindex 15d5bf7888..5b35b3b334 100644--- a/astronomy/src/main/java/com/vayunmathur/astronomy/ui/SkyCanvas.kt+++ b/astronomy/src/main/java/com/vayunmathur/astronomy/ui/SkyCanvas.kt@@ -1,7 +1,6 @@ package com.vayunmathur.astronomy.ui  import androidx.compose.foundation.Canvas-import androidx.compose.foundation.gestures.detectTransformGestures import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue@@ -14,15 +13,15 @@ import androidx.compose.ui.graphics.Path import androidx.compose.ui.graphics.drawscope.Stroke import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.platform.LocalContext+import androidx.compose.ui.platform.LocalViewConfiguration import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.drawText import androidx.compose.ui.text.rememberTextMeasurer import androidx.compose.ui.unit.sp+import com.vayunmathur.astronomy.domain.projection.GesturePanGate import com.vayunmathur.astronomy.domain.projection.StereographicProjection import com.vayunmathur.astronomy.domain.projection.ViewState-import com.vayunmathur.astronomy.domain.projection.pannedBy import com.vayunmathur.astronomy.domain.projection.projectAll-import com.vayunmathur.astronomy.domain.projection.zoomedAbout import com.vayunmathur.astronomy.platform.TrajectoryPoint import com.vayunmathur.astronomy.platform.VisibleSky import kotlin.math.sqrt@@ -40,7 +39,11 @@ fun SkyCanvas(     transparentBackground: Boolean = false,     trajectory: List<TrajectoryPoint> = emptyList(),     selectedId: String? = null,-    /** Reports the absolute view a pan/pinch gesture computed through the projection. */+    /** True while the center follows device orientation: zoom stays zoom-only, pan takes manual control. */+    followsDeviceOrientation: Boolean = false,+    /** Reports a zoom-only field-of-view change; never takes manual control. */+    onFovChange: ((Float) -> Unit)? = null,+    /** Reports the absolute view a pan gesture computed through the projection. */     onViewChange: (ViewState) -> Unit,     onTap: (Offset) -> Unit,     onObjectTap: (String) -> Unit,@@ -53,8 +56,7 @@ fun SkyCanvas(     val artCache = remember { mutableMapOf<String, android.graphics.Bitmap?>() }     val artPaint =         remember {-            // Stellarium figure art is on a black background; additive blend makes the-            // black vanish and only the artwork glows over the sky.+            // Stellarium art is on black; additive blend makes black vanish.             android.graphics.Paint().apply {                 isFilterBitmap = true                 isAntiAlias = true@@ -70,7 +72,6 @@ fun SkyCanvas(             }.getOrNull()         } -    // Kept fresh so the pointer handlers always see the latest selection + callbacks.     val currentSelectedId by rememberUpdatedState(selectedId)     val currentShowPlanets by rememberUpdatedState(showPlanets)     val currentOnObjectTap by rememberUpdatedState(onObjectTap)@@ -78,11 +79,15 @@ fun SkyCanvas(     val currentOnTap by rememberUpdatedState(onTap)     val currentViewState by rememberUpdatedState(viewState)     val currentOnViewChange by rememberUpdatedState(onViewChange)+    val currentOnFovChange by rememberUpdatedState(onFovChange)+    val currentFollowsDeviceOrientation by rememberUpdatedState(followsDeviceOrientation)+    val touchSlop = LocalViewConfiguration.current.touchSlop+    val panGate = remember(touchSlop) { GesturePanGate(touchSlop) }+    val currentPanGate by rememberUpdatedState(panGate)      val projectedStars =         remember(visibleSky.stars, projection) {-            // Stars are the largest list; batch-project them (native fast path when-            // available) and zip results back positionally, dropping culled points.+            // Stars are the largest list; batch-project them and zip back positionally.             val projected = projectAll(viewState, visibleSky.stars.map { it.altAz })             visibleSky.stars.mapIndexedNotNull { i, vs ->                 projected[i]?.let { Triple(vs, it, vs.star.mag) }@@ -107,17 +112,13 @@ fun SkyCanvas(         modifier =             modifier                 .fillMaxSize()-                .pointerInput(Unit) {-                    // One transform handler for pan and pinch (never both a drag and a pinch): pinch-                    // zooms about the centroid, pan moves the zoomed view, both through the projection.-                    detectTransformGestures { centroid, pan, zoom, _ ->-                        val start = currentViewState-                        var transformed = start-                        if (zoom != 1f) transformed = transformed.zoomedAbout(centroid, zoom)-                        if (pan != Offset.Zero) transformed = transformed.pannedBy(pan)-                        if (transformed != start) currentOnViewChange(transformed)-                    }-                }.pointerInput(+                .skyViewGestures(+                    panGate = currentPanGate,+                    viewState = { currentViewState },+                    followsDeviceOrientation = { currentFollowsDeviceOrientation },+                    onFovChange = currentOnFovChange,+                    onViewChange = currentOnViewChange,+                ).pointerInput(                     visibleSky,                     projection,                     projectedStars,@@ -277,9 +278,7 @@ fun SkyCanvas(             if (mag < 1.0) drawCircle(col.copy(alpha = 0.2f), r * 2.2f, off)         } -        // The Settings toggle is labelled "Planets / Sun / Moon", so the-        // Sun and Moon hide with the planets; they used to be drawn (and-        // tap-targetable) even with the toggle off.+        // The "Planets / Sun / Moon" toggle hides the Sun and Moon with the planets.         if (showPlanets) {             sunProj?.let { (_, off) ->                 if (off != null) {
astronomy/src/main/java/com/vayunmathur/astronomy/ui/SkyCanvasGestures.kt
diff --git a/astronomy/src/main/java/com/vayunmathur/astronomy/ui/SkyCanvasGestures.kt b/astronomy/src/main/java/com/vayunmathur/astronomy/ui/SkyCanvasGestures.ktnew file mode 100644index 0000000000..2d0c1aeeb4--- /dev/null+++ b/astronomy/src/main/java/com/vayunmathur/astronomy/ui/SkyCanvasGestures.kt@@ -0,0 +1,71 @@+package com.vayunmathur.astronomy.ui++import androidx.compose.foundation.gestures.detectTransformGestures+import androidx.compose.ui.Modifier+import androidx.compose.ui.geometry.Offset+import androidx.compose.ui.input.pointer.pointerInput+import com.vayunmathur.astronomy.domain.projection.GesturePanGate+import com.vayunmathur.astronomy.domain.projection.ViewState+import com.vayunmathur.astronomy.domain.projection.pannedBy+import com.vayunmathur.astronomy.domain.projection.zoomedAbout++/**+ * The sky canvas's transform gestures: one handler reports pinch and pan+ * together, so the channels are separated here. The zoom factor always+ * applies; the pan delta is gated behind the touch slop (see+ * [GesturePanGate]).+ *+ * While the device orientation drives the center ([followsDeviceOrientation]),+ * zoom alone changes only the field of view through [onFovChange] — the+ * center shift `zoomedAbout` computes to keep the centroid's sky point+ * fixed is discarded, because the sensors own the center — and only a pan+ * that crosses the slop is reported through [onViewChange] as a manual+ * view, taking manual control. Once manual, zoom and pan both apply to the+ * manual view exactly as the projection computes them.+ *+ * A second observer resets [panGate] whenever no pointer is down: this+ * Compose version's `detectTransformGestures` has no gesture start/end+ * hooks, and a moment with no pointer down is the boundary between+ * gestures.+ */+internal fun Modifier.skyViewGestures(+    panGate: GesturePanGate,+    viewState: () -> ViewState,+    followsDeviceOrientation: () -> Boolean,+    onFovChange: ((Float) -> Unit)?,+    onViewChange: (ViewState) -> Unit,+): Modifier =+    this+        .pointerInput(Unit) {+            detectTransformGestures { centroid, pan, zoom, _ ->+                val start = viewState()+                var zoomed = start+                if (zoom != 1f) zoomed = zoomed.zoomedAbout(centroid, zoom)+                if (followsDeviceOrientation()) {+                    val panToApply = panGate.consumePan(pan)+                    if (panToApply != Offset.Zero) {+                        // Deliberate pan: zoom and the released pan together+                        // become the manual view.+                        val transformed = zoomed.pannedBy(panToApply)+                        if (transformed != start) onViewChange(transformed)+                    } else if (zoomed.fovDeg != start.fovDeg) {+                        if (onFovChange != null) {+                            onFovChange(zoomed.fovDeg)+                        } else {+                            onViewChange(start.copy(fovDeg = zoomed.fovDeg))+                        }+                    }+                } else {+                    var transformed = zoomed+                    if (pan != Offset.Zero) transformed = transformed.pannedBy(pan)+                    if (transformed != start) onViewChange(transformed)+                }+            }+        }.pointerInput(Unit) {+            awaitPointerEventScope {+                while (true) {+                    val event = awaitPointerEvent()+                    if (event.changes.none { it.pressed }) panGate.beginGesture()+                }+            }+        }
astronomy/src/main/java/com/vayunmathur/astronomy/ui/SkyMapPage.kt
diff --git a/astronomy/src/main/java/com/vayunmathur/astronomy/ui/SkyMapPage.kt b/astronomy/src/main/java/com/vayunmathur/astronomy/ui/SkyMapPage.ktindex 3a72907853..9533f1620c 100644--- a/astronomy/src/main/java/com/vayunmathur/astronomy/ui/SkyMapPage.kt+++ b/astronomy/src/main/java/com/vayunmathur/astronomy/ui/SkyMapPage.kt@@ -10,6 +10,7 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import com.vayunmathur.astronomy.Route import com.vayunmathur.astronomy.platform.AstronomyViewModel+import com.vayunmathur.astronomy.platform.CenterOption import com.vayunmathur.astronomy.platform.SkyMapActions import com.vayunmathur.astronomy.platform.SkyMapUiState import com.vayunmathur.astronomy.platform.resolveViewCenter@@ -82,6 +83,7 @@ fun SkyMapPage(                 nightMode = nightMode,                 trajectory = trajectory,                 selectedObjectId = selectedId,+                followsDeviceOrientation = viewCenter is CenterOption.DevicePointing,                 accuracy = deviceOrient?.accuracy ?: SensorManager.SENSOR_STATUS_ACCURACY_HIGH,             ),         actions = viewModel,
astronomy/src/main/java/com/vayunmathur/astronomy/ui/SkyMapScreen.kt
diff --git a/astronomy/src/main/java/com/vayunmathur/astronomy/ui/SkyMapScreen.kt b/astronomy/src/main/java/com/vayunmathur/astronomy/ui/SkyMapScreen.ktindex 7e8a0eb285..2010e79089 100644--- a/astronomy/src/main/java/com/vayunmathur/astronomy/ui/SkyMapScreen.kt+++ b/astronomy/src/main/java/com/vayunmathur/astronomy/ui/SkyMapScreen.kt@@ -79,8 +79,13 @@ fun SkyMapScreen(             transparentBackground = cameraOn,             trajectory = state.trajectory,             selectedId = state.selectedObjectId,-            // Pan/pinch explores manually: the gesture's absolute view-            // takes manual control until the phone turns well away.+            followsDeviceOrientation = state.followsDeviceOrientation,+            // Zoom-only (pinch while tracking, like the Settings slider):+            // changes the field of view and never takes manual control.+            onFovChange = { actions.setFov(it) },+            // A pan beyond the touch slop explores manually: the gesture's+            // absolute view takes manual control until the phone turns+            // well away.             onViewChange = { actions.setView(it.centerAzRad, it.centerAltRad, it.fovDeg) },             onTap = { _ -> },             onObjectTap = { id -> actions.selectObject(id) },
astronomy/src/test/java/com/vayunmathur/astronomy/domain/projection/ViewTransformTest.kt
diff --git a/astronomy/src/test/java/com/vayunmathur/astronomy/domain/projection/ViewTransformTest.kt b/astronomy/src/test/java/com/vayunmathur/astronomy/domain/projection/ViewTransformTest.ktindex 702a72080b..cd30fd51cf 100644--- a/astronomy/src/test/java/com/vayunmathur/astronomy/domain/projection/ViewTransformTest.kt+++ b/astronomy/src/test/java/com/vayunmathur/astronomy/domain/projection/ViewTransformTest.kt@@ -7,6 +7,7 @@ import kotlin.math.abs import kotlin.math.hypot import kotlin.test.Test import kotlin.test.assertEquals+import kotlin.test.assertFalse import kotlin.test.assertNotNull import kotlin.test.assertTrue @@ -109,6 +110,40 @@ class ViewTransformTest {         )     } +    @Test+    fun pan_gate_withholds_centroid_drift_below_the_slop() {+        val gate = GesturePanGate(slopPx = 24f)+        gate.beginGesture()+        // Pinch centroid drift: several small deltas that never add up to a pan.+        assertEquals(Offset.Zero, gate.consumePan(Offset(10f, 0f)))+        assertEquals(Offset.Zero, gate.consumePan(Offset(0f, 10f)))+        assertFalse(gate.isPanActive)+    }++    @Test+    fun pan_gate_releases_the_accumulated_pan_once_the_slop_is_crossed() {+        val gate = GesturePanGate(slopPx = 24f)+        gate.beginGesture()+        assertEquals(Offset.Zero, gate.consumePan(Offset(20f, 0f)))+        // Crossing event releases everything withheld so far, not just this delta.+        assertEquals(Offset(30f, 0f), gate.consumePan(Offset(10f, 0f)))+        assertTrue(gate.isPanActive)+        // Once open, each delta passes straight through.+        assertEquals(Offset(5f, -3f), gate.consumePan(Offset(5f, -3f)))+        assertEquals(Offset.Zero, gate.consumePan(Offset.Zero))+    }++    @Test+    fun pan_gate_resets_between_gestures() {+        val gate = GesturePanGate(slopPx = 24f)+        gate.beginGesture()+        gate.consumePan(Offset(30f, 0f))+        assertTrue(gate.isPanActive)+        gate.beginGesture()+        assertFalse(gate.isPanActive)+        assertEquals(Offset.Zero, gate.consumePan(Offset(10f, 0f)))+    }+     @Test     fun nearby_objects_separate_and_pick_correctly_at_high_zoom() {         // Two stars two degrees apart in azimuth, east of the view center.
astronomy/src/test/java/com/vayunmathur/astronomy/platform/AstronomyViewModelTest.kt
diff --git a/astronomy/src/test/java/com/vayunmathur/astronomy/platform/AstronomyViewModelTest.kt b/astronomy/src/test/java/com/vayunmathur/astronomy/platform/AstronomyViewModelTest.ktindex 0adb024576..737b0f209e 100644--- a/astronomy/src/test/java/com/vayunmathur/astronomy/platform/AstronomyViewModelTest.kt+++ b/astronomy/src/test/java/com/vayunmathur/astronomy/platform/AstronomyViewModelTest.kt@@ -3,6 +3,7 @@ package com.vayunmathur.astronomy.platform import android.app.Application import androidx.test.core.app.ApplicationProvider import com.vayunmathur.astronomy.domain.engine.RaDec+import com.vayunmathur.library.sensor.DeviceOrientation import com.vayunmathur.testsupport.awaitWithMainIdle import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi@@ -85,13 +86,84 @@ class AstronomyViewModelTest {         assertTrue(vm.isArMode.value)     } -    @Test fun `manual pan and pinch take the view off sensor tracking`() {+    private fun orientation(+        pointingAzDeg: Double = 90.0,+        pointingAltDeg: Double = 30.0,+        viewRotationDeg: Double = 0.0,+    ) = DeviceOrientation(+        azimuthTrueDeg = pointingAzDeg,+        azimuthMagDeg = pointingAzDeg,+        pitchDeg = 0.0,+        rollDeg = 0.0,+        accuracy = 3,+        pointingAzTrueDeg = pointingAzDeg,+        pointingAltDeg = pointingAltDeg,+        viewRotationDeg = viewRotationDeg,+    )++    @Test fun `zoom only while device pointing keeps tracking and changes fov`() {+        val vm = AstronomyViewModel(app)+        assertTrue(vm.viewCenter.value is CenterOption.DevicePointing)+        // The zoom entry point (pinch while tracking, Settings slider):+        // field of view changes, the center keeps following the device.+        vm.setFov(35f)+        assertTrue(vm.viewCenter.value is CenterOption.DevicePointing)+        assertEquals(35f, vm.fovDeg.value)+        vm.setFov(30f)+        assertTrue(vm.viewCenter.value is CenterOption.DevicePointing)+        assertEquals(30f, vm.fovDeg.value)+        // Zoom clamps like every other path, still without swapping mode.+        vm.setFov(500f)+        assertTrue(vm.viewCenter.value is CenterOption.DevicePointing)+        assertEquals(120f, vm.fovDeg.value)+    }++    @Test fun `zoom in manual mode keeps the manual center and changes fov`() {+        val vm = AstronomyViewModel(app)+        vm.setView(centerAzRad = 1.0, centerAltRad = 0.5, fovDeg = 70f)+        assertTrue(vm.viewCenter.value is CenterOption.Manual)+        vm.setFov(25f)+        val manual = vm.viewCenter.value as CenterOption.Manual+        assertEquals(1.0, manual.azRad)+        assertEquals(0.5, manual.altRad)+        assertEquals(25f, vm.fovDeg.value)+        // A later pan/pinch view update in manual mode is unchanged:+        // both center and fov apply, still manual.+        vm.setView(centerAzRad = 1.2, centerAltRad = 0.4, fovDeg = 20f)+        val moved = vm.viewCenter.value as CenterOption.Manual+        assertEquals(1.2, moved.azRad)+        assertEquals(20f, vm.fovDeg.value)+    }++    @Test fun `reengagement after pan takeover survives zoom before and after`() {+        val vm = AstronomyViewModel(app)+        val anchor = orientation()+        vm.onOrientationSample(anchor)+        // Zoom before the takeover: still tracking, no anchor taken.+        vm.setFov(35f)+        assertTrue(vm.viewCenter.value is CenterOption.DevicePointing)+        // Pan takes manual control at the anchor orientation.+        vm.setView(centerAzRad = 1.0, centerAltRad = 0.5, fovDeg = 35f)+        assertTrue(vm.viewCenter.value is CenterOption.Manual)+        // Zoom after the takeover: manual view and center unchanged.+        vm.setFov(50f)+        assertTrue(vm.viewCenter.value is CenterOption.Manual)+        assertEquals(50f, vm.fovDeg.value)+        // A small rotation from the anchor leaves the manual view alone.+        vm.onOrientationSample(orientation(pointingAzDeg = 90.0 + 44.0))+        assertTrue(vm.viewCenter.value is CenterOption.Manual)+        // At 45 degrees or more tracking re-engages; the zoom is kept.+        vm.onOrientationSample(orientation(pointingAzDeg = 90.0 + 50.0))+        assertTrue(vm.viewCenter.value is CenterOption.DevicePointing)+        assertEquals(50f, vm.fovDeg.value)+    }++    @Test fun `manual pan takes the view off sensor tracking`() {         val vm = AstronomyViewModel(app)         assertTrue(vm.viewCenter.value is CenterOption.DevicePointing)         vm.setView(centerAzRad = 1.0, centerAltRad = 0.5, fovDeg = 25f)-        val manual = vm.viewCenter.value-        assertTrue(manual is CenterOption.Manual)-        assertEquals(1.0, (manual as CenterOption.Manual).azRad)+        val manual = vm.viewCenter.value as CenterOption.Manual+        assertEquals(1.0, manual.azRad)         assertEquals(0.5, manual.altRad)         assertEquals(25f, vm.fovDeg.value)         // The gesture path clamps the field of view like every other path.
astronomy/src/test/java/com/vayunmathur/astronomy/platform/ManualViewArbitrationTest.kt
diff --git a/astronomy/src/test/java/com/vayunmathur/astronomy/platform/ManualViewArbitrationTest.kt b/astronomy/src/test/java/com/vayunmathur/astronomy/platform/ManualViewArbitrationTest.ktindex 5e546d41c4..0c633a1ae6 100644--- a/astronomy/src/test/java/com/vayunmathur/astronomy/platform/ManualViewArbitrationTest.kt+++ b/astronomy/src/test/java/com/vayunmathur/astronomy/platform/ManualViewArbitrationTest.kt@@ -7,9 +7,10 @@ import kotlin.test.assertFalse import kotlin.test.assertTrue  /**- * Manual-view arbitration: exploring by pan/pinch hands the view to the user,- * and only a large phone rotation (>= 45 degrees from the orientation at- * takeover) hands it back to the sensors. Small rotations must not.+ * Manual-view arbitration: exploring by pan (beyond the touch slop) hands+ * the view to the user — zoom alone never does — and only a large phone+ * rotation (>= 45 degrees from the orientation at takeover) hands it back+ * to the sensors. Small rotations must not.  */ class ManualViewArbitrationTest {     private fun orientation(