Modern Apps git

← Modern-Apps.git

pipes: describe the board and each cell to TalkBack (#ux-audit)

Commit
e2116692d2003965bd9f31fa154ca96400411334
Author
Vayun Mathur <[email protected]>
When
2026-11-13 01:29 UTC (23 days ago)
Parents
5fdbfd7
Symptom: with TalkBack on, the game board was a single silent
canvas: no description of the level, its progress, or any cell.

Root cause: GameBoard draws everything on one Canvas with no
semantics, so the accessibility tree had nothing for the board.

Fix:
- The canvas gets a content description summarising the board:
  "Board, 5 by 5. Pairs connected: 2 of 4. Cells filled: 12 of 25."
  (worded so no count is followed by a noun, which would need plurals)
  (pairs counted with StrokeRules.isComplete, so triplets count only
  when every dot is visited).
- New BoardCellSemantics lays one invisible node over each cell,
  labelled "Row r, column c, dot B" / "pipe B" / "empty". Colours are
  named by the same letter colour-blind mode draws, so no colour-name
  strings are needed. The nodes have no pointer input, so drawing
  still goes to the canvas, and they are placed with
  AbsoluteAlignment.TopLeft + absoluteOffset so they stay over their
  cells in RTL (the canvas never mirrors; a plain Box put them at the
  top-right, which the test below caught).
- Strings are English-only like the rest of the module (the locale
  files are empty and MissingTranslation is disabled).

Not done: drawing by TalkBack gestures (per-cell actions). Exploring
the board and hearing its state is the feasible first step; a full
non-visual drawing mode is a bigger design question.

User-visible effect: TalkBack announces the board summary and lets
the player explore cell by cell.

Verification: GameBoardGestureTest.talkBackSeesABoardSummaryAndEveryCell
renders a board with one partial line under RTL and asserts the
summary, dot, pipe and empty labels (22 empty cells), and that the
first cell's node shares the canvas's top-left corner.

Diff

games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/BoardCellSemantics.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/BoardCellSemantics.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/BoardCellSemantics.ktnew file mode 100644index 0000000000..cb3b310333--- /dev/null+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/BoardCellSemantics.kt@@ -0,0 +1,58 @@+package com.vayunmathur.games.pipes.ui++import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.absoluteOffset+import androidx.compose.foundation.layout.size+import androidx.compose.runtime.Composable+import androidx.compose.ui.AbsoluteAlignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.platform.LocalDensity+import androidx.compose.ui.res.stringResource+import androidx.compose.ui.semantics.contentDescription+import androidx.compose.ui.semantics.semantics+import androidx.compose.ui.unit.Dp+import androidx.compose.ui.unit.IntOffset+import com.vayunmathur.games.pipes.R+import com.vayunmathur.games.pipes.data.CellPos+import com.vayunmathur.games.pipes.platform.PipesGameState+import kotlin.math.roundToInt++/**+ * Invisible per-cell nodes laid over the board canvas so TalkBack can explore it: each+ * cell reads its row and column and whether it holds a dot, a pipe or nothing, naming the+ * colour by the same letter colour-blind mode draws. The nodes take no input, so touches+ * still reach the canvas underneath.+ *+ * Positions use AbsoluteAlignment.TopLeft plus absoluteOffset: the canvas never mirrors+ * in RTL, so neither may these (a plain Box would align them to the top-right in RTL).+ */+@Composable+fun BoardCellSemantics(geometry: BoardGeometry, gameState: PipesGameState, boardSize: Dp) {+    val cellDp = with(LocalDensity.current) { geometry.cellSizePx.toDp() }+    val dots = geometry.levelData.endpoints.flatMap { ep -> ep.cells.map { it to ep.colorIndex } }.toMap()+    Box(Modifier.size(boardSize), contentAlignment = AbsoluteAlignment.TopLeft) {+        for ((cell, rect) in geometry.cellRects) {+            val description = cellDescription(cell, dots[cell], gameState.cellOwner[cell])+            Box(+                Modifier+                    .absoluteOffset { IntOffset(rect.left.roundToInt(), rect.top.roundToInt()) }+                    .size(cellDp)+                    .semantics { contentDescription = description }+            )+        }+    }+}++@Composable+private fun cellDescription(cell: CellPos, dotColor: Int?, owner: Int?): String {+    val row = cell.row + 1+    val col = cell.col + 1+    return when {+        dotColor != null -> stringResource(R.string.board_cell_dot, row, col, colorLetter(dotColor))+        owner != null -> stringResource(R.string.board_cell_pipe, row, col, colorLetter(owner))+        else -> stringResource(R.string.board_cell_empty, row, col)+    }+}++/** The letter colour-blind mode draws for [colorIndex]. */+internal fun colorLetter(colorIndex: Int): String = ('A' + (colorIndex % 26)).toString()
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 4f39f6f296..f65e3e93fa 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@@ -17,8 +17,13 @@ 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 androidx.compose.ui.res.stringResource+import androidx.compose.ui.semantics.contentDescription+import androidx.compose.ui.semantics.semantics+import com.vayunmathur.games.pipes.R import com.vayunmathur.games.pipes.data.CellPos import com.vayunmathur.games.pipes.data.LevelData+import com.vayunmathur.games.pipes.domain.StrokeRules import com.vayunmathur.games.pipes.platform.PipesGameState  /** Letter size on pipe segments, as a fraction of the cell: fits inside the half-cell-wide pipe. */@@ -57,6 +62,13 @@ fun GameBoard(     val currentStartDraw by rememberUpdatedState(onStartDraw)     val currentExtendPath by rememberUpdatedState(onExtendPath)     val currentCommitDraw by rememberUpdatedState(onCommitDraw)+    val connected = levelData.endpoints.count {+        StrokeRules.isComplete(levelData, it.colorIndex, gameState.paths[it.colorIndex].orEmpty())+    }+    val summary = stringResource(+        R.string.board_summary, levelData.rows, levelData.cols,+        connected, levelData.endpoints.size, gameState.cellOwner.size, levelData.cells.size,+    )      Canvas(         modifier = Modifier@@ -65,6 +77,7 @@ fun GameBoard(             // Strokes start at the board's edge cells, which on a full-width board sit in             // the system back-gesture zone; claim the board so they draw instead of exiting.             .systemGestureExclusion()+            .semantics { contentDescription = summary }             .pointerInput(geometry, isLevelWon) {                 if (isLevelWon) return@pointerInput                 // One stroke per gesture, driven only by the first finger: a second finger@@ -180,6 +193,7 @@ fun GameBoard(             }         }     }+    BoardCellSemantics(geometry, gameState, boardSize)     } } 
games/pipes/src/main/res/values/strings.xml
diff --git a/games/pipes/src/main/res/values/strings.xml b/games/pipes/src/main/res/values/strings.xmlindex cdc1ee9d11..902367ff8e 100644--- a/games/pipes/src/main/res/values/strings.xml+++ b/games/pipes/src/main/res/values/strings.xml@@ -22,4 +22,12 @@     <string name="back_to_packs">Back to packs</string>     <!-- GameBoardScreen: Hint shown when every pair of dots is connected but some cells are still empty, so the level is not solved yet. Sentence -->     <string name="fill_every_cell_hint">Every pair is connected. Fill every cell to finish.</string>+    <!-- GameBoard: TalkBack summary of the whole board. %1$d rows by %2$d columns; %3$d of %4$d colour pairs joined; %5$d of %6$d cells covered by pipes. Sentences -->+    <string name="board_summary">Board, <xliff:g id="rows" example="5">%1$d</xliff:g> by <xliff:g id="cols" example="5">%2$d</xliff:g>. Pairs connected: <xliff:g id="connected" example="2">%3$d</xliff:g> of <xliff:g id="pairs" example="4">%4$d</xliff:g>. Cells filled: <xliff:g id="filled" example="12">%5$d</xliff:g> of <xliff:g id="cells" example="25">%6$d</xliff:g>.</string>+    <!-- GameBoard: TalkBack label of a board cell holding a dot. %3$s is the colour's letter (A, B, C...), as drawn in colorblind mode -->+    <string name="board_cell_dot">Row <xliff:g id="row" example="2">%1$d</xliff:g>, column <xliff:g id="column" example="3">%2$d</xliff:g>, dot <xliff:g id="letter" example="B">%3$s</xliff:g></string>+    <!-- GameBoard: TalkBack label of a board cell covered by a pipe. %3$s is the colour's letter (A, B, C...), as drawn in colorblind mode -->+    <string name="board_cell_pipe">Row <xliff:g id="row" example="2">%1$d</xliff:g>, column <xliff:g id="column" example="3">%2$d</xliff:g>, pipe <xliff:g id="letter" example="B">%3$s</xliff:g></string>+    <!-- GameBoard: TalkBack label of an empty board cell -->+    <string name="board_cell_empty">Row <xliff:g id="row" example="2">%1$d</xliff:g>, column <xliff:g id="column" example="3">%2$d</xliff:g>, empty</string> </resources>
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 39b80c8891..83eb638195 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@@ -9,6 +9,9 @@ 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.assertCountEquals+import androidx.compose.ui.test.hasContentDescription+import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.performTouchInput import androidx.compose.ui.unit.LayoutDirection@@ -125,4 +128,28 @@ class GameBoardGestureTest {         assertTrue(rects.isNotEmpty(), "no gesture exclusion rects")         assertTrue(rects.any { it.width() > 0 && it.height() > 0 })     }++    @Test fun talkBackSeesABoardSummaryAndEveryCell() {+        val paths = mapOf(0 to listOf(CellPos(0, 0), CellPos(0, 1)))+        val state = PipesGameState(paths, com.vayunmathur.games.pipes.domain.BoardEdits.owners(level, paths))+        rule.setContent {+            CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {+                GameBoard(+                    level, state, null, emptyList(),+                    onStartDraw = {}, onExtendPath = {}, onCommitDraw = {},+                    isLevelWon = false, modifier = Modifier.size(300.dp),+                )+            }+        }+        rule.onNodeWithContentDescription("Board, 5 by 5. Pairs connected: 0 of 1. Cells filled: 2 of 25.").assertExists()+        rule.onNodeWithContentDescription("Row 1, column 1, dot A").assertExists()+        rule.onNodeWithContentDescription("Row 1, column 2, pipe A").assertExists()+        rule.onNodeWithContentDescription("Row 5, column 5, dot A").assertExists()+        rule.onAllNodes(hasContentDescription("empty", substring = true)).assertCountEquals(22)+        // Cell nodes sit over their cells even in RTL (the canvas never mirrors).+        val board = rule.onNodeWithTag(GAME_BOARD_TAG).fetchSemanticsNode().boundsInRoot+        val first = rule.onNodeWithContentDescription("Row 1, column 1, dot A").fetchSemanticsNode().boundsInRoot+        assertEquals(board.left, first.left, 1f)+        assertEquals(board.top, first.top, 1f)+    } }