Modern Apps git

← Modern-Apps.git

nonogram: let TalkBack fill and cross cells via named actions (#ux-audit)

Commit
3e6ec0ceb6ffd706f16ad417d8b999e11a0a6e96
Author
Vayun Mathur <[email protected]>
When
2026-11-13 01:17 UTC (23 days ago)
Parents
0b7cb4f
Symptom: with TalkBack on, a cell could be read ("Row 4, column 6,
Blank") but not played - double tap and double-tap-and-hold did
nothing, so the game was unplayable for screen-reader users.

Root cause: each cell used clearAndSetSemantics with only a
contentDescription; input came from a raw pointerInput tap detector,
which exposes no accessibility actions.

Fix:
- Cell semantics now carry the position as contentDescription
  ("Row 4, column 6"), the mark as stateDescription (Filled / Crossed
  out / Empty, revealed by a mistake / Blank), and onClick + onLongClick
  actions that call the same tap / long-press handlers a finger uses.
- Action labels follow the mark mode and the cell: in Fill mode, tap =
  "Fill", hold = "Cross out" (or "Remove cross" on an X); in Cross mode
  the two swap. TalkBack announces them as "double tap to Fill" etc.
- Settled cells (correct fills, revealed blanks) and finished boards
  expose no actions instead of ones that silently do nothing.
- NonogramBoard takes the current markMode to label the actions.
- cd_cell (position + state in one string) is replaced by
  cd_cell_position plus stateDescription; Polish strings updated.

User-visible effect: TalkBack users can solve puzzles: swipe to a cell,
double tap for the current mode's mark, double tap and hold for the
other.

Verification: Compose tests assert state descriptions, the action labels
in both modes, that OnClick/OnLongClick reach the tap/long-press
handlers, and that a filled cell has no click action; existing board
tests migrated to the new description/state split.

Diff

games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.kt
diff --git a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.kt b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.ktindex 128c01fc25..93dba66368 100644--- a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.kt+++ b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.kt@@ -176,6 +176,7 @@ fun NonogramGameScreen(                                 game = game,                                 onTap = actions::tapCell,                                 onLongPress = actions::crossCell,+                                markMode = state.markMode,                                 modifier = Modifier                                     .widthIn(max = BoardMaxWidth)                                     .fillMaxWidth(),@@ -208,6 +209,7 @@ fun NonogramGameScreen(                         game = game,                         onTap = actions::tapCell,                         onLongPress = actions::crossCell,+                        markMode = state.markMode,                         modifier = Modifier                             .widthIn(max = BoardMaxWidth)                             .fillMaxWidth(),
games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/components/NonogramBoard.kt
diff --git a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/components/NonogramBoard.kt b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/components/NonogramBoard.ktindex 347f362a92..f4b8f75688 100644--- a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/components/NonogramBoard.kt+++ b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/components/NonogramBoard.kt@@ -27,6 +27,9 @@ import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.res.stringResource import androidx.compose.ui.semantics.clearAndSetSemantics import androidx.compose.ui.semantics.contentDescription+import androidx.compose.ui.semantics.onClick+import androidx.compose.ui.semantics.onLongClick+import androidx.compose.ui.semantics.stateDescription import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.Dp@@ -34,6 +37,7 @@ import androidx.compose.ui.unit.TextUnit import androidx.compose.ui.unit.dp import com.vayunmathur.games.nonogram.R import com.vayunmathur.games.nonogram.data.CellMark+import com.vayunmathur.games.nonogram.data.MarkMode import com.vayunmathur.games.nonogram.data.NonogramGameState import com.vayunmathur.library.ui.ColorScheme import com.vayunmathur.library.ui.MaterialTheme@@ -53,6 +57,7 @@ fun NonogramBoard(     onTap: (Int) -> Unit,     onLongPress: (Int) -> Unit,     modifier: Modifier = Modifier,+    markMode: MarkMode = MarkMode.FILL, ) {     val size = game.size     // Widest clue list decides the gutter, so a row needing "3 1 2" is not cut off.@@ -85,7 +90,7 @@ fun NonogramBoard(             }             Row {                 RowClues(game, rowGutterCells, cell, clueCell, fontSize, lineHeight)-                Grid(game, cell, onTap, onLongPress)+                Grid(game, markMode, cell, onTap, onLongPress)             }         }     }@@ -168,6 +173,7 @@ private fun ClueText( @Composable private fun Grid(     game: NonogramGameState,+    markMode: MarkMode,     cell: Dp,     onTap: (Int) -> Unit,     onLongPress: (Int) -> Unit,@@ -179,7 +185,11 @@ private fun Grid(         crossed = stringResource(R.string.cd_crossed),         revealed = stringResource(R.string.cd_revealed),         blank = stringResource(R.string.cd_blank),+        fill = stringResource(R.string.action_fill),+        cross = stringResource(R.string.action_cross),+        uncross = stringResource(R.string.action_uncross),     )+    val over = game.isOver     // Stable callbacks: the cells get the same lambda instance every recomposition, so a cell whose     // mark did not change is skipped outright instead of being recomposed for a new closure.     val currentTap by rememberUpdatedState(onTap)@@ -195,12 +205,16 @@ private fun Grid(                         // Each cell sees only its own mark, so one tap recomposes one cell, not all                         // 225 of a 15x15.                         val mark = game.markAt(index)+                        // Settled cells and finished boards take no input, so TalkBack offers no+                        // actions there rather than ones that silently do nothing.+                        val actionable = !over && !game.isLocked(index)                         NonogramCell(                             mark = mark,                             index = index,-                            row = row,-                            col = col,+                            position = CellPosition(row, col),                             stateWord = labels.wordFor(mark),+                            tapLabel = labels.tapLabel(mark, markMode).takeIf { actionable },+                            longPressLabel = labels.longPressLabel(mark, markMode).takeIf { actionable },                             onTap = tap,                             onLongPress = longPress,                             modifier = Modifier.size(cell),@@ -217,13 +231,16 @@ private fun Grid(     } } -/** The four state words TalkBack reads, looked up once per grid. */+/** The state words and action names TalkBack reads, looked up once per grid. */ @Immutable private data class CellLabels(     val filled: String,     val crossed: String,     val revealed: String,     val blank: String,+    val fill: String,+    val cross: String,+    val uncross: String, ) {     fun wordFor(mark: CellMark): String = when (mark) {         CellMark.FILLED -> filled@@ -231,15 +248,30 @@ private data class CellLabels(         CellMark.REVEALED -> revealed         CellMark.BLANK -> blank     }++    /** What a tap (TalkBack double tap) does: the mode's own mark. */+    fun tapLabel(mark: CellMark, mode: MarkMode): String =+        if (mode == MarkMode.FILL) fill else noteLabel(mark)++    /** What a long press (TalkBack double tap and hold) does: the other mark. */+    fun longPressLabel(mark: CellMark, mode: MarkMode): String =+        if (mode == MarkMode.FILL) noteLabel(mark) else fill++    private fun noteLabel(mark: CellMark) = if (mark == CellMark.CROSSED) uncross else cross } +/** Zero-based row and column, kept together so the cell signature stays short. */+@Immutable+private data class CellPosition(val row: Int, val col: Int)+ @Composable private fun NonogramCell(     mark: CellMark,     index: Int,-    row: Int,-    col: Int,+    position: CellPosition,     stateWord: String,+    tapLabel: String?,+    longPressLabel: String?,     onTap: (Int) -> Unit,     onLongPress: (Int) -> Unit,     modifier: Modifier = Modifier,@@ -256,7 +288,7 @@ private fun NonogramCell(         CellMark.REVEALED -> scheme.errorContainer         else -> Color.Transparent     }-    val description = stringResource(R.string.cd_cell, row + 1, col + 1, stateWord)+    val description = stringResource(R.string.cd_cell_position, position.row + 1, position.col + 1)      Box(         modifier@@ -272,7 +304,14 @@ private fun NonogramCell(                     },                 )             }-            .clearAndSetSemantics { contentDescription = description },+            // Position as the description, the mark as state, and the same two moves a finger has+            // as named actions — without them TalkBack could read a cell but never fill or cross it.+            .clearAndSetSemantics {+                contentDescription = description+                stateDescription = stateWord+                if (tapLabel != null) onClick(label = tapLabel) { onTap(index); true }+                if (longPressLabel != null) onLongClick(label = longPressLabel) { onLongPress(index); true }+            },         contentAlignment = Alignment.Center,     ) {         if (mark == CellMark.CROSSED || mark == CellMark.REVEALED) {
games/nonogram/src/main/res/values-pl/strings.xml
diff --git a/games/nonogram/src/main/res/values-pl/strings.xml b/games/nonogram/src/main/res/values-pl/strings.xmlindex 992075aa81..0607856b33 100644--- a/games/nonogram/src/main/res/values-pl/strings.xml+++ b/games/nonogram/src/main/res/values-pl/strings.xml@@ -29,8 +29,10 @@     <string name="generation_failed">Nie udało się wygenerować łamigłówki dla tego poziomu.</string>     <!-- NonogramGameScreen: Retries generation after a failure, and the level after running out of hearts. Verb -->     <string name="try_again">Spróbuj ponownie</string>-    <!-- NonogramBoard: Content description of a cell. %1$d = row number starting at 1, e.g. 4. %2$d = column number starting at 1, e.g. 6. %3$s = the cell's state word, e.g. Filled -->-    <string name="cd_cell">Wiersz <xliff:g id="row" example="4">%1$d</xliff:g>, kolumna <xliff:g id="column" example="6">%2$d</xliff:g>, <xliff:g id="state" example="Filled">%3$s</xliff:g></string>+    <string name="cd_cell_position">Wiersz <xliff:g id="row" example="4">%1$d</xliff:g>, kolumna <xliff:g id="column" example="6">%2$d</xliff:g></string>+    <string name="action_fill">Zamaluj</string>+    <string name="action_cross">Wykreśl</string>+    <string name="action_uncross">Usuń wykreślenie</string>     <!-- NonogramBoard: Cell state, painted as part of the picture. Verb past participle -->     <string name="cd_filled">Zamalowane</string>     <!-- NonogramBoard: Cell state, ruled out by the player. Verb past participle -->
games/nonogram/src/main/res/values/strings.xml
diff --git a/games/nonogram/src/main/res/values/strings.xml b/games/nonogram/src/main/res/values/strings.xmlindex 5ab2d4d0d4..154a6e7355 100644--- a/games/nonogram/src/main/res/values/strings.xml+++ b/games/nonogram/src/main/res/values/strings.xml@@ -33,8 +33,14 @@     <!-- NonogramGameScreen: Retries generation after a failure, and the level after running out of hearts. Verb -->     <string name="try_again">Try Again</string> -    <!-- NonogramBoard: Content description of a cell. %1$d = row number starting at 1, e.g. 4. %2$d = column number starting at 1, e.g. 6. %3$s = the cell's state word, e.g. Filled -->-    <string name="cd_cell">Row <xliff:g id="row" example="4">%1$d</xliff:g>, column <xliff:g id="column" example="6">%2$d</xliff:g>, <xliff:g id="state" example="Filled">%3$s</xliff:g></string>+    <!-- NonogramBoard: Content description of a cell; TalkBack reads its state after it. %1$d = row number starting at 1, e.g. 4. %2$d = column number starting at 1, e.g. 6 -->+    <string name="cd_cell_position">Row <xliff:g id="row" example="4">%1$d</xliff:g>, column <xliff:g id="column" example="6">%2$d</xliff:g></string>+    <!-- NonogramBoard: TalkBack action that fills a cell as part of the picture. Verb, imperative -->+    <string name="action_fill">Fill</string>+    <!-- NonogramBoard: TalkBack action that marks a cell as empty with an X. Verb phrase, imperative -->+    <string name="action_cross">Cross out</string>+    <!-- NonogramBoard: TalkBack action that removes the player's own X from a cell. Verb phrase, imperative -->+    <string name="action_uncross">Remove cross</string>     <!-- NonogramBoard: Cell state, painted as part of the picture. Verb past participle -->     <string name="cd_filled">Filled</string>     <!-- NonogramBoard: Cell state, ruled out by the player. Verb past participle -->
games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.kt
diff --git a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.kt b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.ktindex daff037c36..93f6624bf0 100644--- a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.kt+++ b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.kt@@ -8,6 +8,9 @@ import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performTouchInput import androidx.compose.ui.semantics.SemanticsActions+import androidx.compose.ui.semantics.SemanticsProperties+import androidx.compose.ui.test.SemanticsMatcher+import androidx.compose.ui.test.assert import androidx.compose.ui.test.performSemanticsAction import androidx.compose.ui.test.longClick import androidx.test.core.app.ApplicationProvider@@ -60,6 +63,36 @@ class NonogramScreensTest {         NonogramGameState(puzzle(), filled, crossed, revealed, hearts, mode, level = 2)     private fun answer() = puzzle().solution.indices.filter { puzzle().solution[it] }.toSet() +    /** The cell at 1-based [row], [col], asserted to announce [state] as its state. */+    private fun cell(row: Int, col: Int, state: Int) =+        rule.onNodeWithContentDescription(str(R.string.cd_cell_position, row, col))+            .assert(SemanticsMatcher.expectValue(SemanticsProperties.StateDescription, str(state)))++    @Test fun talkBackCanFillAndCrossThroughNamedActions() {+        val taps = mutableListOf<Int>(); val longs = mutableListOf<Int>()+        rule.setContent {+            NonogramBoard(game = game(filled = setOf(1), crossed = setOf(0)), onTap = { taps += it }, onLongPress = { longs += it }, markMode = MarkMode.FILL)+        }+        // Fill mode: double tap fills, double-tap-and-hold crosses (or removes an existing cross).+        cell(2, 2, R.string.cd_blank)+            .assert(SemanticsMatcher("fill label") { it.config[SemanticsActions.OnClick].label == str(R.string.action_fill) })+            .assert(SemanticsMatcher("cross label") { it.config[SemanticsActions.OnLongClick].label == str(R.string.action_cross) })+            .performSemanticsAction(SemanticsActions.OnClick)+        cell(2, 2, R.string.cd_blank).performSemanticsAction(SemanticsActions.OnLongClick)+        cell(1, 1, R.string.cd_crossed)+            .assert(SemanticsMatcher("uncross label") { it.config[SemanticsActions.OnLongClick].label == str(R.string.action_uncross) })+        // A settled cell offers nothing to do.+        cell(1, 2, R.string.cd_filled).assert(SemanticsMatcher.keyNotDefined(SemanticsActions.OnClick))+        assertEquals(listOf(4), taps); assertEquals(listOf(4), longs)+    }++    @Test fun talkBackActionsFollowCrossMode() {+        rule.setContent { NonogramBoard(game = game(crossed = setOf(0)), onTap = {}, onLongPress = {}, markMode = MarkMode.CROSS) }+        cell(1, 1, R.string.cd_crossed)+            .assert(SemanticsMatcher("uncross on tap") { it.config[SemanticsActions.OnClick].label == str(R.string.action_uncross) })+            .assert(SemanticsMatcher("fill on hold") { it.config[SemanticsActions.OnLongClick].label == str(R.string.action_fill) })+    }+     @Test fun loadingStateShowsBoardChrome() {         val a = Recording()         rule.setContent { NonogramGameScreen(NonogramUiState(generating = true), a, {}, {}) }@@ -133,22 +166,22 @@ class NonogramScreensTest {     @Test fun boardCellsReportDescriptionsAndTaps() {         val taps = mutableListOf<Int>()         rule.setContent { NonogramBoard(game = game(filled = setOf(1), crossed = setOf(0)), onTap = { taps += it }, onLongPress = {}) }-        rule.onNodeWithContentDescription(str(R.string.cd_cell, 1, 2, str(R.string.cd_filled))).assertExists()-        rule.onNodeWithContentDescription(str(R.string.cd_cell, 1, 1, str(R.string.cd_crossed))).assertExists()-        rule.onNodeWithContentDescription(str(R.string.cd_cell, 1, 3, str(R.string.cd_blank))).performClick()+        cell(1, 2, R.string.cd_filled).assertExists()+        cell(1, 1, R.string.cd_crossed).assertExists()+        cell(1, 3, R.string.cd_blank).performClick()         assertEquals(listOf(2), taps)     }      @Test fun boardTellsARevealedBlankApartFromANote() {         rule.setContent { NonogramBoard(game = game(crossed = setOf(0), revealed = setOf(2)), onTap = {}, onLongPress = {}) }-        rule.onNodeWithContentDescription(str(R.string.cd_cell, 1, 1, str(R.string.cd_crossed))).assertExists()-        rule.onNodeWithContentDescription(str(R.string.cd_cell, 1, 3, str(R.string.cd_revealed))).assertExists()+        cell(1, 1, R.string.cd_crossed).assertExists()+        cell(1, 3, R.string.cd_revealed).assertExists()     }      @Test fun boardLongPressFiresCross() {         val crosses = mutableListOf<Int>()         rule.setContent { NonogramBoard(game = game(), onTap = {}, onLongPress = { crosses += it }) }-        rule.onNodeWithContentDescription(str(R.string.cd_cell, 2, 2, str(R.string.cd_blank)))+        cell(2, 2, R.string.cd_blank)             .performTouchInput { longClick() }         assertEquals(listOf(4), crosses)     }@@ -158,7 +191,7 @@ class NonogramScreensTest {             true,true,false,false, false,false,false,false, false,false,true,false, false,false,false,true))         val g = NonogramGameState(p, emptySet(), emptySet(), emptySet(), STARTING_HEARTS, GameMode.CASUAL, 1)         rule.setContent { NonogramBoard(game = g, onTap = {}, onLongPress = {}) }-        rule.onNodeWithContentDescription(str(R.string.cd_cell, 2, 1, str(R.string.cd_blank))).assertExists()+        cell(2, 1, R.string.cd_blank).assertExists()         rule.onNodeWithText("0").assertExists()     } @@ -166,7 +199,7 @@ class NonogramScreensTest {         val p = requireNotNull(com.vayunmathur.games.nonogram.domain.NonogramGenerator.generateSeeded(15, 99L))         val g = NonogramGameState(p, setOf(0), setOf(1), emptySet(), 2, GameMode.CASUAL, 25)         rule.setContent { NonogramBoard(game = g, onTap = {}, onLongPress = {}) }-        rule.onNodeWithContentDescription(str(R.string.cd_cell, 15, 15, str(R.string.cd_blank))).assertExists()+        cell(15, 15, R.string.cd_blank).assertExists()     }      @Test fun cluesFitTheirSlotsAtLargeFontScale() {