pipes: track only the first finger while drawing (#ux-audit)
- Commit
5a7522da4ff4ef2ee767317c6f5e573c2f5baa30- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 01:15 UTC (23 days ago)
- Parents
9338e58
Symptom: putting a second finger on the board mid-stroke (a palm, a thumb resting on the edge, or a deliberate two-finger touch) restarted the stroke from the second finger's cell and replaced the undo snapshot with the half-drawn board, so Undo no longer went back to the board as it was before the stroke. Root cause: GameBoard's gesture loop looked at event.changes.firstOrNull() and treated any "pressed && !previously pressed" change as a new stroke, whichever pointer it came from. The ViewModel's startDraw then took the current (mid-stroke) gameState as preDrawState. Fix: - GameBoard now uses awaitEachGesture/awaitFirstDown and follows only the first pointer's id until it lifts; other pointers are consumed and ignored. The commit runs in a finally block so a gesture that is cancelled (e.g. the board resized mid-stroke) never leaves a stroke open in the ViewModel. - PipesViewModel.startDraw first abandons any stroke that is still open, restoring preDrawState, so even a stray second start cannot stack a mid-stroke snapshot onto the undo history. User-visible effect: extra fingers are ignored while drawing; Undo always returns to the pre-stroke board. Verification: GameBoardGestureTest drives a two-pointer gesture and asserts one start, extends only from the first finger, one commit; new PipesStrokeTest checks that a second startDraw mid-stroke leaves a single undo step back to the empty board.
Diff
games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.ktindex 8242ffb9fe..d27ad9e6e0 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt@@ -190,10 +190,24 @@ class PipesViewModel(application: Application) : AndroidViewModel(application), } override fun startDraw(cell: CellPos) {+ if (_uiState.value.isLevelWon) return+ // A stroke still open (its release was lost, or a second finger got through) is+ // abandoned, not stacked: the new stroke starts from the board as it was before it,+ // so the undo snapshot stays the real pre-stroke board.+ _uiState.update { cur ->+ if (cur.activeColor == null) {+ cur+ } else {+ cur.copy(+ gameState = cur.preDrawState ?: cur.gameState,+ activeColor = null,+ activePath = emptyList(),+ preDrawState = null,+ )+ }+ } val s = _uiState.value- if (s.isLevelWon || s.levelData == null) return-- val levelData = s.levelData+ val levelData = s.levelData ?: return val endpointColor = levelData.endpoints.find { ep -> cell in ep.cells }?.colorIndex if (endpointColor != null) {
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 2eaf4a1ee6..877e22237b 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@@ -1,6 +1,8 @@ package com.vayunmathur.games.pipes.ui import androidx.compose.foundation.Canvas+import androidx.compose.foundation.gestures.awaitEachGesture+import androidx.compose.foundation.gestures.awaitFirstDown import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.size import androidx.compose.runtime.Composable@@ -57,25 +59,25 @@ fun GameBoard( .testTag(GAME_BOARD_TAG) .pointerInput(geometry, isLevelWon) { if (isLevelWon) return@pointerInput- awaitPointerEventScope {- while (true) {- val event = awaitPointerEvent()- val pos = event.changes.firstOrNull()?.position ?: continue-- when {- event.changes.any { it.pressed && !it.previousPressed } -> {- val cell = geometry.hitTest(pos)- if (cell != null) currentStartDraw(cell)- }- event.changes.any { it.pressed } -> {- val cell = geometry.hitTestDrag(pos)- if (cell != null) currentExtendPath(cell)- }- event.changes.any { !it.pressed && it.previousPressed } -> {- currentCommitDraw()- }+ // One stroke per gesture, driven only by the first finger: a second finger+ // used to restart the stroke mid-drag (and re-snapshot undo); now it is ignored.+ awaitEachGesture {+ val down = awaitFirstDown()+ down.consume()+ val start = geometry.hitTest(down.position) ?: return@awaitEachGesture+ currentStartDraw(start)+ try {+ while (true) {+ val event = awaitPointerEvent()+ val change = event.changes.firstOrNull { it.id == down.id }+ event.changes.forEach { it.consume() }+ if (change == null || !change.pressed) break+ geometry.hitTestDrag(change.position)?.let { currentExtendPath(it) } }- event.changes.forEach { it.consume() }+ } finally {+ // Also on cancellation (the board resized mid-stroke): never leave a+ // stroke open in the ViewModel.+ currentCommitDraw() } } }
games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesStrokeTest.kt
diff --git a/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesStrokeTest.kt b/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesStrokeTest.ktnew file mode 100644index 0000000000..22ec28f945--- /dev/null+++ b/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesStrokeTest.kt@@ -0,0 +1,71 @@+package com.vayunmathur.games.pipes.platform++import android.app.Application+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.games.pipes.data.CellPos+import com.vayunmathur.games.pipes.data.LevelData+import com.vayunmathur.games.pipes.data.LevelPack+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.ExperimentalCoroutinesApi+import kotlinx.coroutines.test.resetMain+import kotlinx.coroutines.test.setMain+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.AfterTest+import kotlin.test.BeforeTest+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull+import kotlin.test.assertTrue++/** ViewModel-level stroke behaviour added by the UX-audit fixes. */+@OptIn(ExperimentalCoroutinesApi::class)+@RunWith(RobolectricTestRunner::class)+class PipesStrokeTest {+ private val app get() = ApplicationProvider.getApplicationContext<Application>()++ @BeforeTest fun setUp() {+ Dispatchers.setMain(Dispatchers.Unconfined)+ LevelPack.init(app)+ try {+ androidx.work.WorkManager.initialize(app, androidx.work.Configuration.Builder().build())+ } catch (_: IllegalStateException) { /* already initialised in this sandbox */ }+ app.getSharedPreferences("pipes_progress", android.content.Context.MODE_PRIVATE).edit().clear().commit()+ }++ @AfterTest fun tearDown() { Dispatchers.resetMain() }++ private fun vm(pack: Int = 0, level: Int = 0) = PipesViewModel(app).also { it.loadLevel(pack, level) }+ private val PipesViewModel.level: LevelData get() = uiState.value.levelData!!++ private fun LevelData.isEndpoint(c: CellPos) = endpoints.any { c in it.cells }+ private fun LevelData.freeNeighbour(of: CellPos, not: Set<CellPos> = emptySet()) =+ (adjacency[of] ?: emptyList()).first { !isEndpoint(it) && it !in not }++ private fun draw(vm: PipesViewModel, path: List<CellPos>) {+ vm.startDraw(path.first())+ for (c in path.drop(1)) vm.extendPath(c)+ vm.commitDraw()+ }++ @Test fun aSecondStartDuringAStrokeKeepsTheRealUndoSnapshot() {+ val vm = vm()+ val a = vm.level.endpoints[0]+ val b = vm.level.endpoints[1]+ val aNext = vm.level.freeNeighbour(a.cells[0])+ vm.startDraw(a.cells[0])+ vm.extendPath(aNext)+ // A second start arrives before the first stroke was released.+ vm.startDraw(b.cells[0])+ assertEquals(b.colorIndex, vm.uiState.value.activeColor)+ val bNext = vm.level.freeNeighbour(b.cells[0], setOf(aNext))+ vm.extendPath(bNext)+ vm.commitDraw()+ // The abandoned stroke left nothing behind, and one undo restores the empty board.+ assertNull(vm.uiState.value.gameState.paths[a.colorIndex])+ assertEquals(listOf(b.cells[0], bNext), vm.uiState.value.gameState.paths[b.colorIndex])+ assertEquals(1, vm.getCurrentMoves())+ vm.onUndo()+ assertTrue(vm.uiState.value.gameState.paths.isEmpty())+ }+}
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.ktindex 1587696fd6..9c8dcd6fdf 100644--- 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.kt@@ -23,6 +23,7 @@ import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import kotlin.test.Test import kotlin.test.assertEquals+import kotlin.test.assertTrue @RunWith(RobolectricTestRunner::class) class GameBoardGestureTest {@@ -81,4 +82,31 @@ class GameBoardGestureTest { tapAt(0.1f, 0.1f) assertEquals(CellPos(0, 0), starts.last()) }++ @Test fun aSecondFingerNeitherRestartsNorSteersTheStroke() {+ val starts = mutableListOf<CellPos>()+ val extends = mutableListOf<CellPos>()+ var commits = 0+ rule.setContent {+ GameBoard(+ level, PipesGameState(), null, emptyList(),+ onStartDraw = { starts += it }, onExtendPath = { extends += it },+ onCommitDraw = { commits++ }, isLevelWon = false, modifier = Modifier.size(300.dp),+ )+ }+ rule.onNodeWithTag(GAME_BOARD_TAG).performTouchInput {+ down(0, Offset(width * 0.1f, height * 0.1f))+ moveTo(0, Offset(width * 0.3f, height * 0.1f))+ down(1, Offset(width * 0.9f, height * 0.9f))+ moveTo(1, Offset(width * 0.7f, height * 0.9f))+ up(1)+ moveTo(0, Offset(width * 0.5f, height * 0.1f))+ up(0)+ }+ rule.waitForIdle()+ assertEquals(listOf(CellPos(0, 0)), starts)+ assertTrue(extends.all { it.row == 0 }, "extends=$extends")+ assertTrue(CellPos(0, 2) in extends, "extends=$extends")+ assertEquals(1, commits)+ } }