calculator: fix graph zoom and pan gestures
- Commit
cf903738122922045fafb4934f7f4cb57f16dfd2- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-21 20:09 UTC (3 months ago)
- Parents
04315de
Diff
calculator/src/main/java/com/vayunmathur/calculator/ui/GraphPage.kt
diff --git a/calculator/src/main/java/com/vayunmathur/calculator/ui/GraphPage.kt b/calculator/src/main/java/com/vayunmathur/calculator/ui/GraphPage.ktindex ae13fb5433..9840b3c952 100644--- a/calculator/src/main/java/com/vayunmathur/calculator/ui/GraphPage.kt+++ b/calculator/src/main/java/com/vayunmathur/calculator/ui/GraphPage.kt@@ -25,6 +25,7 @@ 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.rememberUpdatedState import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment@@ -112,6 +113,9 @@ private fun GraphCanvas(state: GraphUiState, actions: GraphActions, modifier: Mo // against the background whatever colour the curve happens to be. val markerTextColor = MaterialTheme.colorScheme.onSurface val textMeasurer = rememberTextMeasurer()+ // Both gesture handlers are keyed on Unit so a pinch isn't torn down mid-gesture, which+ // means they would otherwise capture the first composition's state forever.+ val currentState by rememberUpdatedState(state) // Colour lookup for markers, and the localised kind names, resolved outside the draw scope. val colorOf = state.functions.associate { it.id to it.color }@@ -134,38 +138,38 @@ private fun GraphCanvas(state: GraphUiState, actions: GraphActions, modifier: Mo .onSizeChanged { actions.setViewSize(it.width.toFloat(), it.height.toFloat()) } .pointerInput(Unit) { detectTapGestures { offset ->- val vp = state.viewport- val gx = vp.centerX + (offset.x - size.width / 2) / vp.scale- val gy = vp.centerY + (size.height / 2 - offset.y) / vp.scale- actions.tapGraph(GraphPoint(gx, gy), TouchSlop.toPx() / vp.scale)+ val vp = currentState.viewport+ val w = size.width.toFloat()+ val h = size.height.toFloat()+ val point = GraphPoint(vp.graphX(offset.x, w), vp.graphY(offset.y, h))+ actions.tapGraph(point, TouchSlop.toPx() / vp.scale) } } .pointerInput(Unit) { detectTransformGestures { centroid, pan, zoom, _ ->- val vp = state.viewport- val newScale = (vp.scale * zoom).coerceIn(2.0, 400000.0)- val gx = vp.centerX + (centroid.x - size.width / 2) / vp.scale- val gy = vp.centerY + (size.height / 2 - centroid.y) / vp.scale+ val vp = currentState.viewport actions.setViewport(- centerX = gx - (centroid.x - size.width / 2) / newScale - pan.x / newScale,- centerY = gy - (size.height / 2 - centroid.y) / newScale + pan.y / newScale,- scale = newScale,+ vp.transformed(+ centroidX = centroid.x,+ centroidY = centroid.y,+ panX = pan.x,+ panY = pan.y,+ zoom = zoom,+ widthPx = size.width.toFloat(),+ heightPx = size.height.toFloat(),+ ), ) } }, ) { val w = size.width val h = size.height- val scale = state.viewport.scale- val cx = state.viewport.centerX- val cy = state.viewport.centerY- fun px(gx: Double) = ((gx - cx) * scale + w / 2).toFloat()- fun py(gy: Double) = (h / 2 - (gy - cy) * scale).toFloat()+ val vp = state.viewport+ val scale = vp.scale+ fun px(gx: Double) = vp.screenX(gx, w)+ fun py(gy: Double) = vp.screenY(gy, h) - val minX = cx - (w / 2) / scale- val maxX = cx + (w / 2) / scale- val minY = cy - (h / 2) / scale- val maxY = cy + (h / 2) / scale+ val (minX, maxX, minY, maxY) = vp.bounds(w, h) val step = niceStep((w / 2) / scale) // ---- Grid + tick labels ----
calculator/src/main/java/com/vayunmathur/calculator/util/CalculatorUiContract.kt
diff --git a/calculator/src/main/java/com/vayunmathur/calculator/util/CalculatorUiContract.kt b/calculator/src/main/java/com/vayunmathur/calculator/util/CalculatorUiContract.ktindex fe1bf21633..b6a24fdaad 100644--- a/calculator/src/main/java/com/vayunmathur/calculator/util/CalculatorUiContract.kt+++ b/calculator/src/main/java/com/vayunmathur/calculator/util/CalculatorUiContract.kt@@ -12,12 +12,68 @@ package com.vayunmathur.calculator.util /** One evaluated expression, kept for the history sheet. */ data class HistoryEntry(val expression: String, val result: String) -/** Where the graph is looking: centre in graph units, and pixels per unit. */+/** How far the graph can be zoomed out and in, in pixels per unit. */+private const val MinGraphScale = 2.0+private const val MaxGraphScale = 400000.0++/** The visible window in graph units. */+data class GraphBounds(+ val xMin: Double,+ val xMax: Double,+ val yMin: Double,+ val yMax: Double,+)++/**+ * Where the graph is looking: centre in graph units, and pixels per unit.+ *+ * Owns the screen<->graph transform so drawing, tap handling and analysis all share one+ * definition of it. Kept free of Compose types so it stays a plain value type.+ */ data class GraphViewport( val centerX: Double = 0.0, val centerY: Double = 0.0, val scale: Double = 60.0,-)+) {+ fun graphX(screenX: Float, widthPx: Float): Double = centerX + (screenX - widthPx / 2) / scale++ fun graphY(screenY: Float, heightPx: Float): Double = centerY + (heightPx / 2 - screenY) / scale++ fun screenX(graphX: Double, widthPx: Float): Float = ((graphX - centerX) * scale + widthPx / 2).toFloat()++ fun screenY(graphY: Double, heightPx: Float): Float = (heightPx / 2 - (graphY - centerY) * scale).toFloat()++ fun bounds(widthPx: Float, heightPx: Float): GraphBounds = GraphBounds(+ xMin = graphX(0f, widthPx),+ xMax = graphX(widthPx, widthPx),+ yMin = graphY(heightPx, heightPx),+ yMax = graphY(0f, heightPx),+ )++ /**+ * The viewport after a transform gesture: zoom about the centroid, so the graph point+ * under the fingers stays under them, then pan at the new scale.+ */+ fun transformed(+ centroidX: Float,+ centroidY: Float,+ panX: Float,+ panY: Float,+ zoom: Float,+ widthPx: Float,+ heightPx: Float,+ ): GraphViewport {+ val newScale = (scale * zoom).coerceIn(MinGraphScale, MaxGraphScale)+ // Reused only for its centre->centroid offset at the new scale.+ val atNewScale = GraphViewport(scale = newScale)+ return GraphViewport(+ centerX = graphX(centroidX, widthPx) - atNewScale.graphX(centroidX, widthPx) - panX / newScale,+ // Screen Y grows downward, so the pan is added rather than subtracted.+ centerY = graphY(centroidY, heightPx) - atNewScale.graphY(centroidY, heightPx) + panY / newScale,+ scale = newScale,+ )+ }+} /** Everything the keypad screen draws. */ data class CalculatorUiState(@@ -84,7 +140,7 @@ interface GraphActions { fun setViewSize(widthPx: Float, heightPx: Float) {} /** Pan/zoom result from a transform gesture. */- fun setViewport(centerX: Double, centerY: Double, scale: Double) {}+ fun setViewport(viewport: GraphViewport) {} /** Reveal or dismiss the notable point nearest [at], within [radius] graph units. */ fun tapGraph(at: GraphPoint, radius: Double) {}
calculator/src/main/java/com/vayunmathur/calculator/util/CalculatorViewModel.kt
diff --git a/calculator/src/main/java/com/vayunmathur/calculator/util/CalculatorViewModel.kt b/calculator/src/main/java/com/vayunmathur/calculator/util/CalculatorViewModel.ktindex 8d6cc01062..666fd83372 100644--- a/calculator/src/main/java/com/vayunmathur/calculator/util/CalculatorViewModel.kt+++ b/calculator/src/main/java/com/vayunmathur/calculator/util/CalculatorViewModel.kt@@ -257,10 +257,10 @@ class CalculatorViewModel(application: Application) : viewHeightPx = heightPx } - override fun setViewport(centerX: Double, centerY: Double, scale: Double) {- this.centerX = centerX- this.centerY = centerY- this.scale = scale+ override fun setViewport(viewport: GraphViewport) {+ centerX = viewport.centerX+ centerY = viewport.centerY+ scale = viewport.scale } private fun dropMarkersFor(id: Long) = markers.removeAll { id in it.curveIds }@@ -449,10 +449,7 @@ class CalculatorViewModel(application: Application) : */ fun sampleCurves(widthPx: Float, heightPx: Float): List<SampledCurve> { if (widthPx <= 0f || heightPx <= 0f) return emptyList()- val xMin = centerX - (widthPx / 2) / scale- val xMax = centerX + (widthPx / 2) / scale- val yMin = centerY - (heightPx / 2) / scale- val yMax = centerY + (heightPx / 2) / scale+ val (xMin, xMax, yMin, yMax) = GraphViewport(centerX, centerY, scale).bounds(widthPx, heightPx) return functions.filter { it.enabled && it.text.isNotBlank() }.mapNotNull { fn -> val expr = runCatching { Expression.parse(fn.text) }.getOrNull() ?: return@mapNotNull null GraphAnalysis.sample(fn.id, expr, fn.polar, angleMode, xMin, xMax, yMin, yMax, widthPx.toInt())