Modern Apps git

← Modern-Apps.git

library: result overlay swallows taps outside the card (#ux-audit)

Commit
071cf33f53fda6b871c010590e7a6dd51b7e8cea
Author
Vayun Mathur <[email protected]>
When
2026-11-13 01:17 UTC (23 days ago)
Parents
4000223
Symptom: with the shared GameResultOverlay showing over a finished
board (sudoku, minesweeper, solitaire, logicgate), a tap on the dimmed
scrim fell straight through to whatever was under it - a sudoku cell,
a number-pad key, a minesweeper cell, the action row. The board looked
modal but stayed live.

Root cause: the scrim was only a background on the overlay's root Box.
A layout with a background but no pointer-input modifier is not a hit
target in Compose, so hit testing skipped it and delivered the event to
the board drawn beneath.

Fix: the scrim is now its own sibling layer under the card
(matchParentSize) carrying a pointer-input modifier that consumes every
event it receives. Being a hit target stops sibling layers beneath it
from receiving the event at all; consuming it also stops ancestor
gesture detectors from acting on it. It is a sibling of the card, not
its parent, on purpose: a parent that consumed events in the Main pass
would also consume the move events of a press on the card's own
buttons, and Compose's tap detector cancels a press whose moves were
consumed.

User-visible effect: tapping anywhere on the dimmed area now does
nothing; only the card's buttons respond. Layout, colours and the
card's position are unchanged, so callers and store screenshots look
the same.

Verification: new GameResultOverlayTest puts a full-size clickable
under the overlay and taps three scrim points (corners, top edge): the
clickable never fires (on main those taps reach it: the scrim there
is not a hit target). A second test checks Play
again / Back still work over the barrier. :library:ui:testDebugUnitTest.

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 49333cf7f3..fde34e1123 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@@ -11,6 +11,7 @@ import androidx.compose.foundation.layout.padding import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier+import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.res.stringResource import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.MaterialTheme@@ -26,6 +27,9 @@ import com.vayunmathur.library.ui.TextButton  * Drawn over a scrim so it reads as modal: without one, whatever the game puts under the board — a  * number pad, an action row — shows around the card's edges and the board looks half-live.  *+ * 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.+ *  * [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@@ -44,37 +48,58 @@ fun GameResultOverlay(     backLabel: String = stringResource(R.string.game_back),     stats: @Composable ColumnScope.() -> Unit = {}, ) {-    Box(-        modifier-            .fillMaxSize()-            .background(MaterialTheme.colorScheme.scrim.copy(alpha = SCRIM_ALPHA))-            .padding(Spacing.lg),-        contentAlignment = Alignment.Center,-    ) {-        Surface(-            shape = MaterialTheme.shapes.extraLarge,-            color = MaterialTheme.colorScheme.surfaceContainerHigh,+    Box(modifier.fillMaxSize()) {+        // 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(+            Modifier+                .matchParentSize()+                .background(MaterialTheme.colorScheme.scrim.copy(alpha = SCRIM_ALPHA))+                .swallowPointerInput(),+        )+        Box(+            Modifier+                .fillMaxSize()+                .padding(Spacing.lg),+            contentAlignment = Alignment.Center,         ) {-            Column(-                Modifier.padding(Spacing.xl),-                horizontalAlignment = Alignment.CenterHorizontally,-                verticalArrangement = Arrangement.spacedBy(Spacing.md),+            Surface(+                shape = MaterialTheme.shapes.extraLarge,+                color = MaterialTheme.colorScheme.surfaceContainerHigh,             ) {-                Text(-                    text = title,-                    style = MaterialTheme.typography.headlineSmall,-                    color = if (won) MaterialTheme.colorScheme.primary-                    else MaterialTheme.colorScheme.error,-                )-                stats()-                if (onPlayAgain != null) {-                    Button(onClick = onPlayAgain, Modifier.fillMaxWidth()) { Text(playAgainLabel) }+                Column(+                    Modifier.padding(Spacing.xl),+                    horizontalAlignment = Alignment.CenterHorizontally,+                    verticalArrangement = Arrangement.spacedBy(Spacing.md),+                ) {+                    Text(+                        text = title,+                        style = MaterialTheme.typography.headlineSmall,+                        color = if (won) MaterialTheme.colorScheme.primary+                        else MaterialTheme.colorScheme.error,+                    )+                    stats()+                    if (onPlayAgain != null) {+                        Button(onClick = onPlayAgain, Modifier.fillMaxWidth()) { Text(playAgainLabel) }+                    }+                    TextButton(onClick = onBack, Modifier.fillMaxWidth()) { Text(backLabel) }                 }-                TextButton(onClick = onBack, Modifier.fillMaxWidth()) { Text(backLabel) }             }         }     } } +/**+ * Makes this node a hit target that consumes everything it receives, so nothing drawn beneath it — and+ * no ancestor gesture detector — sees the event.+ */+private fun Modifier.swallowPointerInput(): Modifier = pointerInput(Unit) {+    awaitPointerEventScope {+        while (true) {+            awaitPointerEvent().changes.forEach { it.consume() }+        }+    }+}+ /** Dark enough to mute the board behind, light enough to keep it recognisable. */ private const val SCRIM_ALPHA = 0.7f
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.ktnew file mode 100644index 0000000000..6c7d177281--- /dev/null+++ b/library/ui/src/test/java/com/vayunmathur/library/ui/game/GameResultOverlayTest.kt@@ -0,0 +1,75 @@+package com.vayunmathur.library.ui.game++import androidx.compose.foundation.clickable+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.ui.Modifier+import androidx.compose.ui.geometry.Offset+import androidx.compose.ui.platform.testTag+import androidx.compose.ui.test.click+import androidx.compose.ui.test.junit4.createComposeRule+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.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 kotlin.test.Test+import kotlin.test.assertEquals++/** The result card over a finished board: what it lets through, and what it doesn't. */+@RunWith(RobolectricTestRunner::class)+class GameResultOverlayTest {++    @get:Rule+    val rule = createComposeRule()++    private val appContext: android.content.Context+        get() = ApplicationProvider.getApplicationContext()++    private var behindTaps = 0++    /** A board-sized clickable under the overlay, standing in for the cells and keys of a game. */+    private fun setBoardWithOverlay(overlay: @androidx.compose.runtime.Composable () -> Unit) {+        rule.setContent {+            Box(Modifier.fillMaxSize().testTag(ROOT)) {+                Box(Modifier.fillMaxSize().clickable { behindTaps++ })+                overlay()+            }+        }+    }++    @Test+    fun `a tap on the backdrop does not reach a clickable behind it`() {+        setBoardWithOverlay { GameResultOverlay(title = "Solved", won = true, onBack = {}) }+        // Corners and edges are all scrim: the card is centred with padding around it.+        rule.onNodeWithTag(ROOT).performTouchInput {+            click(Offset(2f, 2f))+            click(Offset(width - 2f, height - 2f))+            click(Offset(centerX, 2f))+        }+        rule.waitForIdle()+        assertEquals(0, behindTaps)+    }++    @Test+    fun `the card's own buttons still work over the barrier`() {+        var again = 0+        var back = 0+        setBoardWithOverlay {+            GameResultOverlay(title = "Solved", won = true, onBack = { back++ }, onPlayAgain = { again++ })+        }+        rule.onNodeWithText(appContext.getString(UiR.string.game_play_again)).performClick()+        rule.onNodeWithText(appContext.getString(UiR.string.game_back)).performClick()+        assertEquals(1, again)+        assertEquals(1, back)+        assertEquals(0, behindTaps)+    }++    private companion object {+        const val ROOT = "root"+    }+}