minesweeper: fit field on screen; controls beside it in landscape (#ux-audit)
- Commit
fc8235e53d5bb2bdb35106d8760c50e215f6e83f- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 01:20 UTC (23 days ago)
- Parents
cf04808
Symptom: in landscape the field was ~670 dp tall inside a vertically scrolling column, so most of it was off screen; scrolling to reach it was error-prone, because a slow scroll that rested on a cell for the long-press timeout planted a flag. Tall fields on short portrait phones had the same scroll-to-play problem. Root cause: MineFieldGrid sized its cells from the width alone (weight + aspectRatio), so field height was cols-to-rows times the width regardless of the window, and GameBoardScreen put the field in a verticalScroll column in every orientation. Fix: - MineFieldGrid sizes square cells to min(maxWidth / cols, maxHeight / rows) inside a BoxWithConstraints and centres the grid, so the whole field always fits the space it is given (with unbounded height it falls back to width sizing, as before). - GameBoardScreen no longer scrolls the field. Portrait: counter, field (weight(1f, fill = false), so a field that fits by width keeps its old size and the controls sit right under it), then controls. Short windows (isCompactHeight) and wide landscape windows use a Row with the counter and controls on the start side (their own column scrolls if a large font needs it) and the field filling the height, mirroring sudoku's side-by-side layout. - The counter, hint/toggle/buttons were extracted into private MinesRemaining and FieldControls composables shared by both layouts. User-visible effect: the entire field is visible without scrolling in every orientation; accidental flags from scrolling are gone. Note for product: on a landscape phone the 12x18 Large field fits at ~15 dp cells (height-bound); portrait remains the comfortable orientation for Large. Verification: new MinesweeperLayoutTest (Robolectric) shows a Large field at w800dp-h360dp-land and w360dp-h640dp-port and asserts the first and last cells are inside the window, cells are square, controls are beside (landscape) or below (portrait) the field and on screen. :games:minesweeper:testDevUnitTest in the toolchain container.
Diff
games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameBoardScreen.kt
diff --git a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameBoardScreen.kt b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameBoardScreen.ktindex 38c7c29327..ac7b165d3b 100644--- a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameBoardScreen.kt+++ b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameBoardScreen.kt@@ -2,8 +2,10 @@ package com.vayunmathur.games.minesweeper.ui import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding@@ -46,10 +48,15 @@ import com.vayunmathur.library.ui.TextButton import com.vayunmathur.library.ui.appBarScrollBehavior import com.vayunmathur.library.ui.game.GameResultOverlay import com.vayunmathur.library.ui.game.formatDuration+import com.vayunmathur.library.ui.isCompactHeight+import com.vayunmathur.library.ui.isExpandedWidth /** Caps the field on tablets, where a full-width board would give absurdly large cells. */ private val FieldMaxWidth = 480.dp +/** The field's share of a side-by-side row, against 1 for the controls. */+private const val SIDE_FIELD_WEIGHT = 1.6f+ /** * The board, with no dependency on the ViewModel so it can be rendered from a `@Preview` — see * `src/screenshotTest`.@@ -90,7 +97,7 @@ fun GameBoardScreen( }, scrollBehavior = appBarScrollBehavior(), ) { padding ->- Box(+ BoxWithConstraints( Modifier .fillMaxSize() .padding(padding),@@ -100,70 +107,78 @@ fun GameBoardScreen( Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { CircularProgressIndicator() }- return@Box+ return@BoxWithConstraints } - Column(- Modifier- .widthIn(max = FieldMaxWidth)- .fillMaxWidth()- .verticalScroll(rememberScrollState())- .padding(horizontal = Spacing.md, vertical = Spacing.sm),- horizontalAlignment = Alignment.CenterHorizontally,- verticalArrangement = Arrangement.spacedBy(Spacing.md),- ) {+ val onNewField: () -> Unit = {+ if (hasProgress) confirmRestart = true else actions.restart()+ }+ val onGiveUp: () -> Unit = {+ if (hasProgress) {+ confirmGiveUp = true+ } else {+ actions.giveUp()+ onExit()+ }+ }+ // The field is never inside a scroll container: cells are fitted to whichever of the+ // width or height runs out first (see MineFieldGrid), so the whole field is always on+ // screen. In the old scrolling column a landscape field was ~670dp tall, and a slow+ // scroll that rested on a cell long enough fired the long press and planted a flag.+ // Short windows (landscape phones) and wide landscape windows put the controls beside+ // the field rather than under it, so the field gets the full height.+ val sideBySide = isCompactHeight() || (isExpandedWidth() && maxWidth > maxHeight)+ if (sideBySide) { Row(- Modifier.fillMaxWidth(),- horizontalArrangement = Arrangement.Center,+ Modifier+ .fillMaxSize()+ .padding(horizontal = Spacing.md, vertical = Spacing.sm),+ horizontalArrangement = Arrangement.spacedBy(Spacing.lg), verticalAlignment = Alignment.CenterVertically, ) {- IconFlag(tint = MaterialTheme.colorScheme.error)- Text(- " ${game.minesRemaining}",- style = MaterialTheme.typography.titleMedium,- )- }-- MineFieldGrid(- game = game,- onTap = actions::tapCell,- onLongPress = actions::flagCell,- modifier = Modifier.fillMaxWidth(),- )-- if (!game.started) {- // Follows the mode, so the hint never describes a gesture the toggle has swapped.- Text(- stringResource(- if (state.tapMode == TapMode.FLAG) R.string.how_to_play_flag- else R.string.how_to_play- ),- style = MaterialTheme.typography.bodySmall,- color = MaterialTheme.colorScheme.onSurfaceVariant,+ // Controls first (start side), as in sudoku; they scroll on their own if a+ // large font makes them taller than the window.+ Column(+ Modifier+ .weight(1f)+ .verticalScroll(rememberScrollState()),+ horizontalAlignment = Alignment.CenterHorizontally,+ verticalArrangement = Arrangement.spacedBy(Spacing.md),+ ) {+ MinesRemaining(game.minesRemaining)+ FieldControls(state, actions, onNewField, onGiveUp)+ }+ MineFieldGrid(+ game = game,+ onTap = actions::tapCell,+ onLongPress = actions::flagCell,+ modifier = Modifier+ .weight(SIDE_FIELD_WEIGHT)+ .fillMaxHeight(), ) }-- TapModeToggle(state.tapMode, actions::setTapMode)-- Row(- Modifier.fillMaxWidth(),- horizontalArrangement = Arrangement.spacedBy(Spacing.sm),+ } else {+ Column(+ Modifier+ .widthIn(max = FieldMaxWidth)+ .fillMaxSize()+ .padding(horizontal = Spacing.md, vertical = Spacing.sm),+ horizontalAlignment = Alignment.CenterHorizontally,+ verticalArrangement = Arrangement.spacedBy(Spacing.md), ) {- Button(- onClick = { if (hasProgress) confirmRestart = true else actions.restart() },- modifier = Modifier.weight(1f),- ) { Text(stringResource(R.string.new_field)) }- TextButton(- onClick = {- if (hasProgress) {- confirmGiveUp = true- } else {- actions.giveUp()- onExit()- }- },- modifier = Modifier.weight(1f),- ) { Text(stringResource(R.string.give_up)) }+ MinesRemaining(game.minesRemaining)+ // Whatever height the counter and controls leave; fill = false so a field+ // that fits by width wraps to its own height and the controls sit right under+ // it, exactly as before on fields that already fitted.+ MineFieldGrid(+ game = game,+ onTap = actions::tapCell,+ onLongPress = actions::flagCell,+ modifier = Modifier+ .weight(1f, fill = false)+ .fillMaxWidth(),+ )+ FieldControls(state, actions, onNewField, onGiveUp) } } @@ -215,6 +230,58 @@ fun GameBoardScreen( } } +/** The flag counter above the field: mines left to find, negative when over-flagged. */+@Composable+private fun MinesRemaining(count: Int) {+ Row(+ horizontalArrangement = Arrangement.Center,+ verticalAlignment = Alignment.CenterVertically,+ ) {+ IconFlag(tint = MaterialTheme.colorScheme.error)+ Text(" $count", style = MaterialTheme.typography.titleMedium)+ }+}++/** The hint (before the first move), the tap-mode toggle, and the New Field / Give Up buttons. */+@Composable+private fun FieldControls(+ state: MinesweeperUiState,+ actions: MinesweeperActions,+ onNewField: () -> Unit,+ onGiveUp: () -> Unit,+) {+ Column(+ horizontalAlignment = Alignment.CenterHorizontally,+ verticalArrangement = Arrangement.spacedBy(Spacing.md),+ ) {+ if (state.game?.started == false) {+ // Follows the mode, so the hint never describes a gesture the toggle has swapped.+ Text(+ stringResource(+ if (state.tapMode == TapMode.FLAG) R.string.how_to_play_flag+ else R.string.how_to_play+ ),+ style = MaterialTheme.typography.bodySmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ )+ }++ TapModeToggle(state.tapMode, actions::setTapMode)++ Row(+ Modifier.fillMaxWidth(),+ horizontalArrangement = Arrangement.spacedBy(Spacing.sm),+ ) {+ Button(onClick = onNewField, modifier = Modifier.weight(1f)) {+ Text(stringResource(R.string.new_field))+ }+ TextButton(onClick = onGiveUp, modifier = Modifier.weight(1f)) {+ Text(stringResource(R.string.give_up))+ }+ }+ }+}+ /** Its own restart scope, so the per-second tick recomposes this text and nothing else. */ @Composable private fun ClockText(elapsedSeconds: () -> Int) {
games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/components/MineFieldGrid.kt
diff --git a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/components/MineFieldGrid.kt b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/components/MineFieldGrid.ktindex 0d56d709c4..fc8ad52422 100644--- a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/components/MineFieldGrid.kt+++ b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/components/MineFieldGrid.kt@@ -2,12 +2,12 @@ package com.vayunmathur.games.minesweeper.ui.components import androidx.compose.foundation.background import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row-import androidx.compose.foundation.layout.aspectRatio import androidx.compose.foundation.layout.fillMaxSize-import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.size import androidx.compose.foundation.gestures.detectTapGestures import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment@@ -15,6 +15,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color import androidx.compose.ui.input.pointer.pointerInput+import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.res.pluralStringResource@@ -38,10 +39,14 @@ import com.vayunmathur.library.ui.Text /** * The mine field. *- * Cells are square and sized from the available width, so a taller field simply gets taller rather- * than squeezing the cells. Tap and long press are handled per cell by [detectTapGestures] rather- * than by `clickable` + `combinedClickable`, because the flag gesture needs to fire on press-and-hold- * without also delivering the tap that would open the cell.+ * Cells are square and sized to the smaller of `width / cols` and `height / rows`, so the whole+ * field always fits the space it is given — never wider or taller, never scrolled. Width-only sizing+ * made a landscape field far taller than the screen. With unbounded height (a scrolling parent) this+ * falls back to width sizing. The grid is centred in any space it does not fill.+ *+ * Tap and long press are handled per cell by [detectTapGestures] rather than by `clickable` ++ * `combinedClickable`, because the flag gesture needs to fire on press-and-hold without also+ * delivering the tap that would open the cell. * * @param onLongPress returns whether a flag actually changed; the press buzzes only then. */@@ -52,21 +57,25 @@ fun MineFieldGrid( onLongPress: (Int) -> Boolean, modifier: Modifier = Modifier, ) {- Column(modifier) {- for (row in 0 until game.rows) {- Row(Modifier.fillMaxWidth()) {- for (col in 0 until game.cols) {- val index = row * game.cols + col- MineCell(- game = game,- index = index,- onTap = { onTap(index) },- onLongPress = { onLongPress(index) },- // weight + aspectRatio keeps the cells square whatever the column count.- modifier = Modifier- .weight(1f)- .aspectRatio(1f),- )+ BoxWithConstraints(modifier, contentAlignment = Alignment.Center) {+ // Whole pixels: a fractional dp cell rounds up per cell, and 18 rounded-up rows overflow+ // the height they were fitted to, squashing the last rows.+ val heightPx = if (constraints.hasBoundedHeight) constraints.maxHeight / game.rows else Int.MAX_VALUE+ val cellPx = minOf(constraints.maxWidth / game.cols, heightPx)+ val cell = with(LocalDensity.current) { cellPx.toDp() }+ Column(Modifier.size(cell * game.cols, cell * game.rows)) {+ for (row in 0 until game.rows) {+ Row {+ for (col in 0 until game.cols) {+ val index = row * game.cols + col+ MineCell(+ game = game,+ index = index,+ onTap = { onTap(index) },+ onLongPress = { onLongPress(index) },+ modifier = Modifier.size(cell),+ )+ } } } }
games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperLayoutTest.kt
diff --git a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperLayoutTest.kt b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperLayoutTest.ktnew file mode 100644index 0000000000..6b316aa98e--- /dev/null+++ b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperLayoutTest.kt@@ -0,0 +1,76 @@+package com.vayunmathur.games.minesweeper.ui++import androidx.compose.ui.test.getUnclippedBoundsInRoot+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithContentDescription+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.onRoot+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.games.minesweeper.R+import com.vayunmathur.games.minesweeper.data.BoardSize+import com.vayunmathur.games.minesweeper.data.Difficulty+import com.vayunmathur.games.minesweeper.data.GameConfig+import com.vayunmathur.games.minesweeper.data.MinesweeperGameState+import com.vayunmathur.games.minesweeper.platform.MinesweeperActions+import com.vayunmathur.games.minesweeper.platform.MinesweeperUiState+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config+import kotlin.test.Test+import kotlin.test.assertTrue++/** The whole field fits the window in every orientation, with square cells and no scrolling. */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class MinesweeperLayoutTest {++ @get:Rule val rule = createComposeRule()+ private val ctx = ApplicationProvider.getApplicationContext<android.content.Context>()++ private fun showLarge(): GameConfig {+ val config = GameConfig(BoardSize.LARGE, Difficulty.MEDIUM)+ val state = MinesweeperUiState(config = config, game = MinesweeperGameState.empty(config))+ rule.setContent { GameBoardScreen(state, MinesweeperActions.Noop, onExit = {}) }+ return config+ }++ private fun assertFieldFits(config: GameConfig) {+ val root = rule.onRoot().getUnclippedBoundsInRoot()+ val first = rule.onNodeWithContentDescription("Row 1, column 1,", substring = true)+ .getUnclippedBoundsInRoot()+ val last = rule.onNodeWithContentDescription(+ "Row ${config.size.rows}, column ${config.size.cols},", substring = true,+ ).getUnclippedBoundsInRoot()+ assertTrue(first.top >= root.top && last.bottom <= root.bottom, "field fits vertically: $first .. $last in $root")+ assertTrue(first.left >= root.left && last.right <= root.right, "field fits horizontally")+ val w = last.right - last.left+ val h = last.bottom - last.top+ assertTrue((w - h).value in -0.5f..0.5f, "cells are square ($w x $h)")+ }++ @Test+ @Config(qualifiers = "w800dp-h360dp-land")+ fun landscapeFitsTheLargeFieldBesideTheControls() {+ val config = showLarge()+ assertFieldFits(config)+ // Controls sit beside the field, not below it.+ val field = rule.onNodeWithContentDescription("Row 1, column 1,", substring = true)+ .getUnclippedBoundsInRoot()+ val newField = rule.onNodeWithText(ctx.getString(R.string.new_field)).getUnclippedBoundsInRoot()+ assertTrue(newField.right <= field.left, "controls are on the start side of the field")+ }++ @Test+ @Config(qualifiers = "w360dp-h640dp-port")+ fun shortPortraitPhoneFitsTheLargeFieldAboveTheControls() {+ val config = showLarge()+ assertFieldFits(config)+ val last = rule.onNodeWithContentDescription(+ "Row ${config.size.rows}, column ${config.size.cols},", substring = true,+ ).getUnclippedBoundsInRoot()+ val newField = rule.onNodeWithText(ctx.getString(R.string.new_field)).getUnclippedBoundsInRoot()+ assertTrue(newField.top >= last.bottom, "controls are below the field")+ assertTrue(newField.bottom <= rule.onRoot().getUnclippedBoundsInRoot().bottom, "and still on screen")+ }+}