Modern Apps git

← Modern-Apps.git

library: add a built-in View board action to the result overlay (#ux-audit)

Commit
402120550502a03771fde1a4afe7f45325667a97
Author
Vayun Mathur <[email protected]>
When
2026-11-13 01:19 UTC (23 days ago)
Parents
931403d
Symptom: once a game ended, the result card and its 70% scrim sat over
the board until the player left or replayed. There was no way to look
at the finished board - the solved sudoku grid, where the mine that
ended a minesweeper round was, the completed circuit - without
dismissing the result for good.

Root cause: GameResultOverlay only knew one state, "card shown", and
the games had no shared way to hide it temporarily.

Fix: GameResultOverlay gains a "View Board" text button (between the
primary action and Back). It fades the card and scrim out with the
shared FadeVisibility helper and fades in a small bar pinned to the
bottom (safeDrawing bottom/side insets) holding "Show Result" and the
card's primary action - Play again / the caller's label, or the
caller's back action when there is nothing to replay (daily boards,
logicgate's last level). Details:
- On by default through a new allowViewBoard = true parameter placed
  before the trailing stats slot, so sudoku, minesweeper, solitaire
  and logicgate get it with no call-site change; false opts out.
- The state is rememberSaveable, so rotation keeps the board view.
- The touch barrier, pane semantics and focus trap stay in place while
  viewing: the board is for looking at and must not take input after
  the game ended. Focus is re-requested on every switch because the
  focused button may have just faded out.
- System back while viewing returns to the card (BackHandler only
  composed in that state, so previews and the card state are
  unaffected); back from the card behaves as before.
- New strings game_view_board / game_show_result (English; translators
  pick them up with the rest of library/ui).

User-visible effect: every game using the shared overlay shows "View
Board" on its result card; tapping it reveals the undimmed final board
with a compact bar to return or play again.

Verification: GameResultOverlayTest - View board hides the card and
shows the bar, a tap on the viewed board still does not reach it, the
bar's primary action fires, Show result brings the card back; back
press in board view returns to the card; a board without play-again
offers Back in the bar; allowViewBoard = false hides the button.
Callers compile: :games:{sudoku,minesweeper,solitaire,logicgate}:
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 b11ebc1c92..17f60410eb 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@@ -1,16 +1,27 @@ package com.vayunmathur.library.ui.game +import androidx.activity.compose.BackHandler import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.ColumnScope+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.WindowInsets+import androidx.compose.foundation.layout.WindowInsetsSides import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.only import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.safeDrawing+import androidx.compose.foundation.layout.windowInsetsPadding import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember+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.focus.FocusRequester@@ -20,8 +31,10 @@ 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.unit.dp import androidx.compose.ui.res.stringResource import com.vayunmathur.library.ui.Button+import com.vayunmathur.library.ui.FadeVisibility import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.R import com.vayunmathur.library.ui.Spacing@@ -49,7 +62,15 @@ import com.vayunmathur.library.ui.TextButton  * [stats] is a free slot for the lines a game wants to boast about, which is the one part that has  * never been the same twice.  *- * @param onPlayAgain primary action. Null hides it, for a daily board where there is nothing to replay.+ * "View board" is built in: it fades the card and scrim away and leaves a small bar at the bottom with+ * "Show result" and the primary action, so the player can look at the finished board — the solved grid,+ * where the mine was — before moving on. The board stays untouchable while viewed. Back returns to the+ * card.+ *+ * @param onPlayAgain primary action. Null hides it, for a daily board where there is nothing to replay;+ *   the board-view bar then offers [onBack] instead.+ * @param allowViewBoard false drops the "View board" action, for a game whose board says nothing more+ *   once it is over.  */ @Composable fun GameResultOverlay(@@ -60,49 +81,93 @@ fun GameResultOverlay(     onPlayAgain: (() -> Unit)? = null,     playAgainLabel: String = stringResource(R.string.game_play_again),     backLabel: String = stringResource(R.string.game_back),+    allowViewBoard: Boolean = true,     stats: @Composable ColumnScope.() -> Unit = {}, ) {+    var viewingBoard by rememberSaveable { mutableStateOf(false) }+    val showingBoard = allowViewBoard && viewingBoard+    // Back from the board view returns to the result rather than leaving the game behind it.+    if (showingBoard) BackHandler { viewingBoard = false }+     val focusRequester = remember { FocusRequester() }-    LaunchedEffect(focusRequester) { focusRequester.requestFocus() }+    // Again on every switch: the focused button may just have faded out with the card or the bar.+    LaunchedEffect(showingBoard) { 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.+        // eat the moves of a press on the card's own buttons and cancel it. It stays in place while the+        // board is being viewed, which is for looking at: the game is over and must not take input.         Box(             Modifier                 .matchParentSize()-                .background(MaterialTheme.colorScheme.scrim.copy(alpha = SCRIM_ALPHA))                 .swallowPointerInput(),         )-        Box(-            Modifier-                .fillMaxSize()+        FadeVisibility(visible = !showingBoard, modifier = Modifier.matchParentSize()) {+            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,+                ) {+                    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) }+                        }+                        if (allowViewBoard) {+                            TextButton(onClick = { viewingBoard = true }, Modifier.fillMaxWidth()) {+                                Text(stringResource(R.string.game_view_board))+                            }+                        }+                        TextButton(onClick = onBack, Modifier.fillMaxWidth()) { Text(backLabel) }+                    }+                }+            }+        }+        FadeVisibility(+            visible = showingBoard,+            modifier = Modifier+                .align(Alignment.BottomCenter)+                .windowInsetsPadding(WindowInsets.safeDrawing.only(BarInsetSides))                 .padding(Spacing.lg),-            contentAlignment = Alignment.Center,         ) {             Surface(                 shape = MaterialTheme.shapes.extraLarge,                 color = MaterialTheme.colorScheme.surfaceContainerHigh,+                shadowElevation = BAR_ELEVATION,             ) {-                Column(-                    Modifier.padding(Spacing.xl),-                    horizontalAlignment = Alignment.CenterHorizontally,-                    verticalArrangement = Arrangement.spacedBy(Spacing.md),+                Row(+                    Modifier.padding(horizontal = Spacing.md, vertical = Spacing.sm),+                    horizontalArrangement = Arrangement.spacedBy(Spacing.sm),+                    verticalAlignment = Alignment.CenterVertically,                 ) {-                    Text(-                        text = title,-                        style = MaterialTheme.typography.headlineSmall,-                        color = if (won) MaterialTheme.colorScheme.primary-                        else MaterialTheme.colorScheme.error,-                    )-                    stats()+                    TextButton(onClick = { viewingBoard = false }) {+                        Text(stringResource(R.string.game_show_result))+                    }+                    // The card's primary action; a board with nothing to replay offers its way out instead.                     if (onPlayAgain != null) {-                        Button(onClick = onPlayAgain, Modifier.fillMaxWidth()) { Text(playAgainLabel) }+                        Button(onClick = onPlayAgain) { Text(playAgainLabel) }+                    } else {+                        Button(onClick = onBack) { Text(backLabel) }                     }-                    TextButton(onClick = onBack, Modifier.fillMaxWidth()) { Text(backLabel) }                 }             }         }@@ -136,3 +201,9 @@ private fun Modifier.swallowPointerInput(): Modifier = pointerInput(Unit) {  /** Dark enough to mute the board behind, light enough to keep it recognisable. */ private const val SCRIM_ALPHA = 0.7f++/** Lifts the board-view bar off a board that may be any colour under it. */+private val BAR_ELEVATION = 6.dp++/** The bar sits on the bottom edge, so it keeps clear of the navigation bar and any side cutout. */+private val BarInsetSides = WindowInsetsSides.Bottom + WindowInsetsSides.Horizontal
library/ui/src/main/res/values/strings.xml
diff --git a/library/ui/src/main/res/values/strings.xml b/library/ui/src/main/res/values/strings.xmlindex e900d03f3e..70f5e2d8ea 100644--- a/library/ui/src/main/res/values/strings.xml+++ b/library/ui/src/main/res/values/strings.xml@@ -214,6 +214,10 @@     <string name="game_play_again">Play Again</string>     <!-- Shared library: UI - GameResultOverlay: secondary button dismissing the result and leaving the board. Verb -->     <string name="game_back">Back</string>+    <!-- Shared library: UI - GameResultOverlay: button hiding the result card so the finished board can be looked at. Verb phrase, keep short -->+    <string name="game_view_board">View Board</string>+    <!-- Shared library: UI - GameResultOverlay: button on the small bar shown while viewing the finished board; brings the result card back. Verb phrase, keep short -->+    <string name="game_show_result">Show Result</string>     <!-- Shared library: UI - games: label preceding an elapsed time, e.g. Time: 04:12. Noun -->     <string name="game_time">Time</string>     <!-- Shared library: UI - LevelPickerBox: title over the current level number. Noun -->
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 6690388009..123bfe2bcd 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 androidx.activity.OnBackPressedDispatcher+import androidx.activity.compose.LocalOnBackPressedDispatcherOwner import androidx.compose.foundation.clickable import androidx.compose.foundation.focusable import androidx.compose.foundation.layout.Box@@ -17,6 +19,7 @@ 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.assertIsDisplayed import androidx.compose.ui.test.click import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag@@ -132,6 +135,68 @@ class GameResultOverlayTest {         assertEquals(0, behindFocused)     } +    @Test+    fun `view board swaps the card for a bar and back`() {+        var again = 0+        setBoardWithOverlay {+            GameResultOverlay(title = "Solved", won = true, onBack = {}, onPlayAgain = { again++ })+        }+        rule.onNodeWithText("Solved").assertIsDisplayed()+        rule.onNodeWithText(str(UiR.string.game_view_board)).performClick()+        rule.waitForIdle()+        rule.onNodeWithText("Solved").assertDoesNotExist()+        rule.onNodeWithText(str(UiR.string.game_show_result)).assertIsDisplayed()++        // The board is on view, not live: a tap on it still goes nowhere.+        rule.onNodeWithTag(ROOT).performTouchInput { click(Offset(centerX, centerY)) }+        rule.waitForIdle()+        assertEquals(0, behindTaps)++        // The bar carries the primary action...+        rule.onNodeWithText(str(UiR.string.game_play_again)).performClick()+        assertEquals(1, again)+        // ...and brings the card back.+        rule.onNodeWithText(str(UiR.string.game_show_result)).performClick()+        rule.waitForIdle()+        rule.onNodeWithText("Solved").assertIsDisplayed()+        rule.onNodeWithText(str(UiR.string.game_show_result)).assertDoesNotExist()+    }++    @Test+    fun `back from the board view returns to the card`() {+        lateinit var dispatcher: OnBackPressedDispatcher+        setBoardWithOverlay {+            dispatcher = checkNotNull(LocalOnBackPressedDispatcherOwner.current).onBackPressedDispatcher+            GameResultOverlay(title = "Solved", won = true, onBack = {})+        }+        rule.onNodeWithText(str(UiR.string.game_view_board)).performClick()+        rule.waitForIdle()+        rule.runOnIdle { dispatcher.onBackPressed() }+        rule.waitForIdle()+        rule.onNodeWithText("Solved").assertIsDisplayed()+    }++    @Test+    fun `a daily board's bar offers the way out`() {+        var back = 0+        setBoardWithOverlay { GameResultOverlay(title = "Daily", won = true, onBack = { back++ }) }+        rule.onNodeWithText(str(UiR.string.game_view_board)).performClick()+        rule.waitForIdle()+        rule.onNodeWithText(str(UiR.string.game_back)).performClick()+        assertEquals(1, back)+    }++    @Test+    fun `callers can opt out of view board`() {+        setBoardWithOverlay {+            GameResultOverlay(title = "Solved", won = true, onBack = {}, allowViewBoard = false)+        }+        rule.onNodeWithText("Solved").assertIsDisplayed()+        rule.onNodeWithText(str(UiR.string.game_view_board)).assertDoesNotExist()+    }++    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