library: make the result overlay modal for TalkBack and keyboard (#ux-audit)
- Commit
931403d063892b9b96daed595bcb5e79d1b43e76- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 01:18 UTC (23 days ago)
- Parents
071cf33
Symptom: while GameResultOverlay showed over a finished board, TalkBack
could still swipe from the card onto every cell and key behind it, and
a double-tap there ran the cell's click action - accessibility actions
bypass the pointer barrier entirely. With a keyboard or D-pad, Tab and
the arrow keys walked focus off the card onto the board, which was
still focusable.
Root cause: the overlay was just another layer in the caller's Box. It
declared nothing to accessibility services and nothing to the focus
system, so both treated the board as live content beside it.
Fix (inside GameResultOverlay only; no call-site changes):
- The overlay root is now a pane: semantics { paneTitle = title }. A
pane title makes the node important for accessibility, and Compose's
accessibility delegate leaves out every node wholly covered by an
important node drawn above it. The board under the overlay therefore
drops out of the tree TalkBack sees, and the pane is announced by
its title when it appears.
- The root is a focus target with focusProperties { onExit =
cancelFocusChange() } and takes focus when it appears, so keyboard
focus lands on the overlay and Tab / D-pad can only move among the
card's own buttons.
- A Dialog window was rejected: it would sit above the activity window
and so above the achievement toast, which unlocks at exactly the
moment a win shows this card; it would also change system-back
routing for every game and drop the card from preview-based store
screenshots.
User-visible effect: TalkBack users hear "Solved" (or the game's own
title) when the card appears and can only explore the card; keyboard
users start on it and cannot tab out to the dead board. Sighted touch
use is unchanged.
Verification: GameResultOverlayTest asks the view's real
AccessibilityNodeProvider for nodes (accessibility + touch exploration
enabled via Robolectric): the board node is present before the overlay
(control), the card title is present with it, and the board node is
absent while it shows. A second test drives FocusManager Next /
Previous / Up / Down repeatedly with a focusable board behind and
asserts the board never gains focus.Diff
library/ui/src/main/java/com/vayunmathur/library/ui/game/GameResultOverlay.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/game/GameResultOverlay.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/game/GameResultOverlay.ktindex fde34e1123..b11ebc1c92 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/game/GameResultOverlay.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/game/GameResultOverlay.kt@@ -9,9 +9,17 @@ import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier+import androidx.compose.ui.focus.FocusRequester+import androidx.compose.ui.focus.focusProperties+import androidx.compose.ui.focus.focusRequester+import androidx.compose.ui.focus.focusTarget import androidx.compose.ui.input.pointer.pointerInput+import androidx.compose.ui.semantics.paneTitle+import androidx.compose.ui.semantics.semantics import androidx.compose.ui.res.stringResource import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.MaterialTheme@@ -30,6 +38,12 @@ import com.vayunmathur.library.ui.TextButton * The scrim is also a touch barrier: it swallows every pointer event that lands outside the card, so a * tap meant to dismiss nothing cannot fall through to a cell or a number-pad key on the finished board. *+ * It is modal for assistive technology and keyboards too. The overlay is a pane titled [title]: it is+ * announced when it appears, and because it covers the board, TalkBack drops the covered board nodes+ * from its tree, so swiping cannot reach a cell whose click action would bypass the touch barrier.+ * Keyboard focus moves onto the overlay when it appears and cannot leave it, so Tab and the D-pad stay+ * on the card's buttons.+ * * [won] only picks the title colour; the caller supplies the words, because "Solved", "Field Cleared" * and "Congratulations" all mean the same thing in different games and none of them belongs here. * [stats] is a free slot for the lines a game wants to boast about, which is the one part that has@@ -48,7 +62,13 @@ fun GameResultOverlay( backLabel: String = stringResource(R.string.game_back), stats: @Composable ColumnScope.() -> Unit = {}, ) {- Box(modifier.fillMaxSize()) {+ val focusRequester = remember { FocusRequester() }+ LaunchedEffect(focusRequester) { focusRequester.requestFocus() }+ Box(+ modifier+ .fillMaxSize()+ .modalPane(title, focusRequester),+ ) { // A sibling under the card rather than its parent: a parent that swallowed events would also // eat the moves of a press on the card's own buttons and cancel it. Box(@@ -89,6 +109,19 @@ fun GameResultOverlay( } } +/**+ * The accessibility and focus half of being modal.+ *+ * A pane title makes the node important for accessibility, and Compose leaves out of the accessibility+ * tree any node wholly covered by an important node drawn above it โ that is what hides the board from+ * TalkBack. The node is also a focus target that refuses to let focus leave it.+ */+private fun Modifier.modalPane(title: String, focusRequester: FocusRequester): Modifier = this+ .semantics { paneTitle = title }+ .focusRequester(focusRequester)+ .focusProperties { onExit = { cancelFocusChange() } }+ .focusTarget()+ /** * Makes this node a hit target that consumes everything it receives, so nothing drawn beneath it โ and * no ancestor gesture detector โ sees the event.
library/ui/src/test/java/com/vayunmathur/library/ui/game/GameResultOverlayTest.kt
diff --git a/library/ui/src/test/java/com/vayunmathur/library/ui/game/GameResultOverlayTest.kt b/library/ui/src/test/java/com/vayunmathur/library/ui/game/GameResultOverlayTest.ktindex 6c7d177281..6690388009 100644--- a/library/ui/src/test/java/com/vayunmathur/library/ui/game/GameResultOverlayTest.kt+++ b/library/ui/src/test/java/com/vayunmathur/library/ui/game/GameResultOverlayTest.kt@@ -1,10 +1,21 @@ package com.vayunmathur.library.ui.game +import android.view.View+import android.view.accessibility.AccessibilityManager import androidx.compose.foundation.clickable+import androidx.compose.foundation.focusable import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier+import androidx.compose.ui.focus.FocusDirection+import androidx.compose.ui.focus.FocusManager+import androidx.compose.ui.focus.onFocusChanged import androidx.compose.ui.geometry.Offset+import androidx.compose.ui.platform.LocalFocusManager+import androidx.compose.ui.platform.LocalView import androidx.compose.ui.platform.testTag import androidx.compose.ui.test.click import androidx.compose.ui.test.junit4.createComposeRule@@ -12,13 +23,17 @@ import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performTouchInput+import androidx.compose.ui.text.BasicText import androidx.test.core.app.ApplicationProvider import com.vayunmathur.library.ui.R as UiR import org.junit.Rule import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner+import org.robolectric.Shadows.shadowOf import kotlin.test.Test import kotlin.test.assertEquals+import kotlin.test.assertNotNull+import kotlin.test.assertNull /** The result card over a finished board: what it lets through, and what it doesn't. */ @RunWith(RobolectricTestRunner::class)@@ -69,6 +84,64 @@ class GameResultOverlayTest { assertEquals(0, behindTaps) } + @Test+ fun `TalkBack cannot reach the board while the overlay shows`() {+ val am = appContext.getSystemService(AccessibilityManager::class.java)+ shadowOf(am).setEnabled(true)+ shadowOf(am).setTouchExplorationEnabled(true)+ var showOverlay by mutableStateOf(false)+ lateinit var view: View+ rule.setContent {+ view = LocalView.current+ Box(Modifier.fillMaxSize()) {+ Box(Modifier.fillMaxSize().clickable { behindTaps++ }) { BasicText("Behind") }+ if (showOverlay) GameResultOverlay(title = "Solved", won = true, onBack = {})+ }+ }+ val behindId = rule.onNodeWithText("Behind").fetchSemanticsNode().id+ assertNotNull(accessibilityNode(view, behindId), "control: the board is reachable before the overlay")++ showOverlay = true+ rule.waitForIdle()+ val titleId = rule.onNodeWithText("Solved").fetchSemanticsNode().id+ assertNotNull(accessibilityNode(view, titleId), "the card itself stays reachable")+ assertNull(accessibilityNode(view, behindId), "the covered board is out of the accessibility tree")+ }++ @Test+ fun `keyboard focus moves onto the overlay and cannot leave it`() {+ var behindFocused = 0+ lateinit var focusManager: FocusManager+ rule.setContent {+ focusManager = LocalFocusManager.current+ Box(Modifier.fillMaxSize()) {+ Box(+ Modifier+ .fillMaxSize()+ .onFocusChanged { if (it.isFocused) behindFocused++ }+ .focusable(),+ )+ GameResultOverlay(title = "Solved", won = true, onBack = {}, onPlayAgain = {})+ }+ }+ rule.runOnIdle {+ listOf(FocusDirection.Next, FocusDirection.Previous, FocusDirection.Down, FocusDirection.Up)+ .forEach { direction -> repeat(4) { focusManager.moveFocus(direction) } }+ }+ rule.waitForIdle()+ assertEquals(0, behindFocused)+ }++ /** What an accessibility service sees for one semantics node; null when it is left out of the tree. */+ private fun accessibilityNode(view: View, id: Int): Any? {+ var node: Any? = null+ rule.runOnIdle {+ val provider = checkNotNull(view.accessibilityNodeProvider) { "compose exposes a node provider" }+ node = provider.createAccessibilityNodeInfo(id)+ }+ return node+ }+ private companion object { const val ROOT = "root" }