pipes: re-key board touches on geometry after resize (#ux-audit)
- Commit
9338e58c594aa1f0d4972be6446431bc5e9f9f05- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 01:14 UTC (23 days ago)
- Parents
524306c
Symptom: after rotating the phone, unfolding a foldable or resizing the window (split screen, freeform), drawing landed on the wrong cells: strokes started and extended from where the cells used to be. Root cause: the activity handles configuration changes itself, so the screen is not recreated. GameBoard's pointerInput was keyed only on (levelData, isLevelWon), so its coroutine kept the hitTest closure and cell rects captured at the first composition even after the board was laid out at a new cell size. Fix: key pointerInput on the remembered BoardGeometry (which is rebuilt whenever the cell size in px changes) instead of levelData, and read the three draw callbacks through rememberUpdatedState so the long-lived gesture coroutine never calls a stale lambda. The canvas gets a GAME_BOARD_TAG test tag for gesture tests. User-visible effect: drawing hits the cell under the finger after any size change, without reopening the level. Verification: new GameBoardGestureTest (Robolectric Compose) taps the same relative spot at 300dp, 150dp and 400dp and asserts the expected cell each time (stale geometry resolves the 150dp tap to column 2), and checks that RTL layout direction does not mirror hit-testing.
Diff
games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoard.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoard.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoard.ktindex c694ddf10f..2eaf4a1ee6 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoard.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoard.kt@@ -4,16 +4,22 @@ import androidx.compose.foundation.Canvas import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.size import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue import androidx.compose.runtime.remember+import androidx.compose.runtime.rememberUpdatedState import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Rect import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.platform.LocalDensity+import androidx.compose.ui.platform.testTag import com.vayunmathur.games.pipes.data.CellPos import com.vayunmathur.games.pipes.data.LevelData import com.vayunmathur.games.pipes.platform.PipesGameState +/** Test tag of the drawing surface, for gesture tests. */+internal const val GAME_BOARD_TAG = "pipes_game_board"+ @Composable fun GameBoard( levelData: LevelData,@@ -38,11 +44,18 @@ fun GameBoard( val cellRects = geometry.cellRects val isHex = geometry.isHex val labelPaint = remember { ColorLabelPaint() }+ // The gesture coroutine outlives recompositions, so it must never read values captured+ // at launch: it restarts whenever the geometry changes (rotation, fold, resize), and+ // reads the callbacks through rememberUpdatedState.+ val currentStartDraw by rememberUpdatedState(onStartDraw)+ val currentExtendPath by rememberUpdatedState(onExtendPath)+ val currentCommitDraw by rememberUpdatedState(onCommitDraw) Canvas( modifier = Modifier .size(boardSize)- .pointerInput(levelData, isLevelWon) {+ .testTag(GAME_BOARD_TAG)+ .pointerInput(geometry, isLevelWon) { if (isLevelWon) return@pointerInput awaitPointerEventScope { while (true) {@@ -52,14 +65,14 @@ fun GameBoard( when { event.changes.any { it.pressed && !it.previousPressed } -> { val cell = geometry.hitTest(pos)- if (cell != null) onStartDraw(cell)+ if (cell != null) currentStartDraw(cell) } event.changes.any { it.pressed } -> { val cell = geometry.hitTestDrag(pos)- if (cell != null) onExtendPath(cell)+ if (cell != null) currentExtendPath(cell) } event.changes.any { !it.pressed && it.previousPressed } -> {- onCommitDraw()+ currentCommitDraw() } } event.changes.forEach { it.consume() }
games/pipes/src/test/java/com/vayunmathur/games/pipes/ui/GameBoardGestureTest.kt
diff --git a/games/pipes/src/test/java/com/vayunmathur/games/pipes/ui/GameBoardGestureTest.kt b/games/pipes/src/test/java/com/vayunmathur/games/pipes/ui/GameBoardGestureTest.ktnew file mode 100644index 0000000000..1587696fd6--- /dev/null+++ b/games/pipes/src/test/java/com/vayunmathur/games/pipes/ui/GameBoardGestureTest.kt@@ -0,0 +1,84 @@+package com.vayunmathur.games.pipes.ui++import androidx.compose.foundation.layout.size+import androidx.compose.runtime.CompositionLocalProvider+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.setValue+import androidx.compose.ui.Modifier+import androidx.compose.ui.geometry.Offset+import androidx.compose.ui.platform.LocalLayoutDirection+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithTag+import androidx.compose.ui.test.performTouchInput+import androidx.compose.ui.unit.LayoutDirection+import androidx.compose.ui.unit.dp+import com.vayunmathur.games.pipes.data.CellPos+import com.vayunmathur.games.pipes.data.EndpointPair+import com.vayunmathur.games.pipes.data.LevelData+import com.vayunmathur.games.pipes.data.computeAdjacency+import com.vayunmathur.games.pipes.platform.PipesGameState+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.Test+import kotlin.test.assertEquals++@RunWith(RobolectricTestRunner::class)+class GameBoardGestureTest {+ @get:Rule val rule = createComposeRule()++ private val cells = buildSet { for (r in 0 until 5) for (c in 0 until 5) add(CellPos(r, c)) }+ private val level = LevelData(+ "t", 5, 5, cells, computeAdjacency(cells), null,+ listOf(EndpointPair(0, listOf(CellPos(0, 0), CellPos(4, 4)))), emptySet(), 1,+ )++ private fun tapAt(fx: Float, fy: Float) {+ rule.onNodeWithTag(GAME_BOARD_TAG).performTouchInput {+ down(Offset(width * fx, height * fy))+ up()+ }+ rule.waitForIdle()+ }++ @Test fun touchesUseTheCurrentGeometryAfterAResize() {+ val starts = mutableListOf<CellPos>()+ var side by mutableStateOf(300.dp)+ rule.setContent {+ GameBoard(+ level, PipesGameState(), null, emptyList(),+ onStartDraw = { starts += it }, onExtendPath = {}, onCommitDraw = {},+ isLevelWon = false, modifier = Modifier.size(side),+ )+ }+ tapAt(0.9f, 0.1f)+ assertEquals(CellPos(0, 4), starts.last())+ // Shrink the board (rotation / fold / split screen). The same relative spot must+ // still be the top-right cell; stale 300dp geometry would resolve it to column 2.+ side = 150.dp+ rule.waitForIdle()+ tapAt(0.9f, 0.1f)+ assertEquals(CellPos(0, 4), starts.last())+ side = 400.dp+ rule.waitForIdle()+ tapAt(0.1f, 0.9f)+ assertEquals(CellPos(4, 0), starts.last())+ }++ @Test fun rtlDoesNotMirrorHitTesting() {+ val starts = mutableListOf<CellPos>()+ rule.setContent {+ CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {+ GameBoard(+ level, PipesGameState(), null, emptyList(),+ onStartDraw = { starts += it }, onExtendPath = {}, onCommitDraw = {},+ isLevelWon = false, modifier = Modifier.size(300.dp),+ )+ }+ }+ // The canvas draws in absolute coordinates, so the top-left cell stays top-left.+ tapAt(0.1f, 0.1f)+ assertEquals(CellPos(0, 0), starts.last())+ }+}