Modern Apps git

← Modern-Apps.git

library: hide the board behind the result overlay with a barrier (#ux-audit)

Commit
2e1f2a4b578dc4e8f5c7b4da1a0df4ef0de64f80
Author
Vayun Mathur <[email protected]>
When
2026-11-13 03:47 UTC (23 days ago)
Parents
2ef2331
Follow-up to "make the result overlay modal for TalkBack and keyboard"
(c9ffc4023), whose two new tests failed in the toolchain run:

- TalkBack: the covered board node was still exposed. Relying on the
  pane-title node being "important" enough for Compose to treat the
  board as covered did not hold up.
- Keyboard: focus still reached a focusable board. The overlay root was
  itself the focused node, and Compose only runs a node's onExit when a
  descendant holds focus (performCustomExit is reached for ActiveParent
  nodes), so the guard never fired.

Fix:
- The scrim layer now also carries Modifier.interactionBarrier()
  (Compose UI 1.13, which library/ui resolves through foundation
  1.13.0-beta01). That is Compose's own modal-scrim primitive: nodes it
  covers are left out of the accessibility tree and cannot take focus,
  and anything drawn above it - the card, the board-view bar - is
  unaffected.
- The pane root is now a focus group (paneTitle + onExit guard +
  focusGroup), and focus rests on a dedicated focus target drawn just
  above the barrier inside it. Because focus is held by a descendant,
  the group's onExit runs and cancels any move out of the overlay.
- Pointer swallowing, the View board flow and the pane announcement
  are unchanged.

The TalkBack test now walks the real AccessibilityNodeProvider tree
from the host view (as a service does) instead of asking for single
ids: the board is reachable before the overlay (control), the card is
reachable with it, and the board is not.

Verification: :library:ui:testDebugUnitTest (GameResultOverlayTest),
callers' compileDevKotlin.

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 17f60410eb..3675a2941b 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@@ -2,6 +2,7 @@ package com.vayunmathur.library.ui.game  import androidx.activity.compose.BackHandler import androidx.compose.foundation.background+import androidx.compose.foundation.focusGroup import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column@@ -24,6 +25,7 @@ import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier+import androidx.compose.ui.interactionBarrier import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusProperties import androidx.compose.ui.focus.focusRequester@@ -51,11 +53,11 @@ 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.+ * It is modal for assistive technology and keyboards too. The overlay is a pane titled [title], announced+ * when it appears, and the scrim is an interaction barrier: the board it covers drops out of the tree+ * TalkBack sees, so swiping cannot reach a cell whose click action would bypass the touch barrier, and+ * keyboard focus cannot land on it. 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.@@ -95,7 +97,7 @@ fun GameResultOverlay(     Box(         modifier             .fillMaxSize()-            .modalPane(title, focusRequester),+            .modalPane(title),     ) {         // 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. It stays in place while the@@ -103,8 +105,17 @@ fun GameResultOverlay(         Box(             Modifier                 .matchParentSize()+                .interactionBarrier()                 .swallowPointerInput(),         )+        // Where focus rests while the overlay shows. Drawn above the barrier, so the barrier does not+        // count it as covered, and inside the pane, so the pane's exit guard holds it.+        Box(+            Modifier+                .matchParentSize()+                .focusRequester(focusRequester)+                .focusTarget(),+        )         FadeVisibility(visible = !showingBoard, modifier = Modifier.matchParentSize()) {             Box(                 Modifier@@ -175,17 +186,13 @@ 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.+ * The pane half of being modal: announced by [title], and a focus group that will not let focus leave.+ * Hiding the board itself is the scrim's interaction barrier.  */-private fun Modifier.modalPane(title: String, focusRequester: FocusRequester): Modifier = this+private fun Modifier.modalPane(title: String): Modifier = this     .semantics { paneTitle = title }-    .focusRequester(focusRequester)     .focusProperties { onExit = { cancelFocusChange() } }-    .focusTarget()+    .focusGroup()  /**  * Makes this node a hit target that consumes everything it receives, so nothing drawn beneath it — and
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 5ed4f39264..50882cadbb 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@@ -2,6 +2,8 @@ package com.vayunmathur.library.ui.game  import android.view.View import android.view.accessibility.AccessibilityManager+import android.view.accessibility.AccessibilityNodeInfo+import android.view.accessibility.AccessibilityNodeProvider import androidx.activity.OnBackPressedDispatcher import androidx.activity.compose.LocalOnBackPressedDispatcherOwner import androidx.compose.foundation.clickable@@ -35,8 +37,8 @@ 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+import kotlin.test.assertFalse+import kotlin.test.assertTrue  /** The result card over a finished board: what it lets through, and what it doesn't. */ @RunWith(RobolectricTestRunner::class)@@ -102,13 +104,14 @@ class GameResultOverlayTest {             }         }         val behindId = rule.onNodeWithText("Behind").fetchSemanticsNode().id-        assertNotNull(accessibilityNode(view, behindId), "control: the board is reachable before the overlay")+        assertTrue(behindId in reachableIds(view), "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")+        val reachable = reachableIds(view)+        assertTrue(titleId in reachable, "the card itself stays reachable")+        assertFalse(behindId in reachable, "the covered board is out of the accessibility tree")     }      @Test@@ -197,14 +200,26 @@ class GameResultOverlayTest {      private fun str(id: Int): String = appContext.getString(id) -    /** 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+    /**+     * Every virtual node an accessibility service can reach by walking down from the host view, the way+     * TalkBack builds its traversal. Child ids come from AccessibilityNodeInfo's hidden getChildId, whose+     * high 32 bits are the virtual id.+     */+    private fun reachableIds(view: View): Set<Int> {+        val seen = mutableSetOf<Int>()         rule.runOnIdle {             val provider = checkNotNull(view.accessibilityNodeProvider) { "compose exposes a node provider" }-            node = provider.createAccessibilityNodeInfo(id)+            val getChildId = AccessibilityNodeInfo::class.java.getMethod("getChildId", Int::class.javaPrimitiveType)+            fun walk(id: Int) {+                val info = provider.createAccessibilityNodeInfo(id) ?: return+                for (i in 0 until info.childCount) {+                    val childId = ((getChildId.invoke(info, i) as Long) ushr 32).toInt()+                    if (seen.add(childId)) walk(childId)+                }+            }+            walk(AccessibilityNodeProvider.HOST_VIEW_ID)         }-        return node+        return seen     }      private companion object {