Modern Apps git

← Modern-Apps.git

sudoku: fit the landscape board to the space it actually gets (#ux-audit)

Commit
b427b4dd58da1311b16e61a3dfda6093e95b8a4a
Author
Vayun Mathur <[email protected]>
When
2026-11-13 01:13 UTC (23 days ago)
Parents
5a31c7a
Symptom: on a landscape phone the side-by-side board was about 16 dp
taller than the room it was given, and the bottom row of cells was
clipped (partly or wholly hidden) - worst on 12x12.

Root cause: GameBoardScreen sized the square board as
minOf(maxWidth * 0.62f, maxHeight) from the outer BoxWithConstraints,
but the Row that holds it adds Spacing.sm (8 dp) of vertical padding on
each side. Modifier.size() was then coerced to the Row's smaller
height, leaving a non-square slot; SudokuGrid sized each cell from
maxWidth / side only, so the cells' total height overflowed the slot.

Fix:
- The board size is computed from the space inside the Row's padding
  (maxHeight - 2 * vertical padding, and the width share from
  maxWidth - 2 * horizontal padding). The padding values are named
  constants used by both the Row and the calculation, so they cannot
  drift apart again.
- SudokuGrid sizes cells from minOf(maxWidth, maxHeight) / side, so
  even if a caller's constraints ever override the aspect ratio the
  whole grid stays visible instead of losing its last row.

User-visible effect: in landscape the entire board, including the
bottom row and its frame, is visible and the cells stay square.
Portrait is unchanged (its slot is square, so min == width).

Verification: new SudokuLandscapeLayoutTest (Robolectric,
w800dp-h360dp-land) asserts for 9x9 and 12x12 that the last cell ends
inside the window, cells are square, and first-to-last row spans
side * cell. :games:sudoku:testDevUnitTest in the toolchain container.

Diff

games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.ktindex 5b1b337fb5..6e212f6699 100644--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.kt+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.kt@@ -56,6 +56,10 @@ import com.vayunmathur.library.ui.isExpandedWidth /** Caps the grid on tablets, where a full-width board would put the digits absurdly far apart. */ private val BoardMaxWidth = 520.dp +/** Padding around the side-by-side (landscape) layout; the board is sized inside it. */+private val SideBySideHorizontal = Spacing.md+private val SideBySideVertical = Spacing.sm+ /**  * The board, with no dependency on the ViewModel so it can be rendered from a `@Preview` — see  * `src/screenshotTest`.@@ -104,12 +108,18 @@ fun GameBoardScreen(             val sideBySide = isCompactHeight() || (isExpandedWidth() && maxWidth > maxHeight)             // Hoisted out of the Row below: RowScope would shadow the             // BoxWithConstraintsScope receiver and the size would not resolve.-            val boardSize = minOf(maxWidth * 0.62f, maxHeight)+            // Measured inside the Row's own padding: sizing from the raw maxHeight made+            // the square 2 * SideBySideVertical too tall for the space it actually gets,+            // so the Row squashed its height and the bottom row of cells was clipped.+            val boardSize = minOf(+                (maxWidth - SideBySideHorizontal * 2) * 0.62f,+                maxHeight - SideBySideVertical * 2,+            )             if (sideBySide) {                 Row(                     Modifier                         .fillMaxSize()-                        .padding(horizontal = Spacing.md, vertical = Spacing.sm),+                        .padding(horizontal = SideBySideHorizontal, vertical = SideBySideVertical),                     horizontalArrangement = Arrangement.spacedBy(Spacing.lg),                     verticalAlignment = Alignment.CenterVertically,                 ) {
games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/SudokuGrid.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/SudokuGrid.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/SudokuGrid.ktindex a9f27c17e8..51d41bc343 100644--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/SudokuGrid.kt+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/SudokuGrid.kt@@ -59,7 +59,10 @@ fun SudokuGrid(     val conflicts = remember(game.size, game.givens, game.entries) { game.conflictingIndices() }      BoxWithConstraints(modifier.aspectRatio(1f)) {-        val cell: Dp = maxWidth / side+        // The smaller side, not the width: if a caller's constraints ever win over the aspect+        // ratio (a squashed landscape slot), width-sized cells would overflow the height and the+        // bottom row would be clipped. Sized this way the whole grid always stays visible.+        val cell: Dp = minOf(maxWidth, maxHeight) / side         Column {             for (row in 0 until side) {                 Row {
games/sudoku/src/test/java/com/vayunmathur/games/sudoku/ui/SudokuLandscapeLayoutTest.kt
diff --git a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/ui/SudokuLandscapeLayoutTest.kt b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/ui/SudokuLandscapeLayoutTest.ktnew file mode 100644index 0000000000..b72a9f10ec--- /dev/null+++ b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/ui/SudokuLandscapeLayoutTest.kt@@ -0,0 +1,66 @@+package com.vayunmathur.games.sudoku.ui++import android.content.Context+import androidx.compose.ui.test.getUnclippedBoundsInRoot+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithContentDescription+import androidx.compose.ui.test.onRoot+import androidx.compose.ui.unit.dp+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.games.sudoku.R+import com.vayunmathur.games.sudoku.data.BoardSize+import com.vayunmathur.games.sudoku.data.Difficulty+import com.vayunmathur.games.sudoku.data.SudokuGameState+import com.vayunmathur.games.sudoku.data.sudokuSymbol+import com.vayunmathur.games.sudoku.domain.SudokuGenerator+import com.vayunmathur.games.sudoku.platform.SudokuActions+import com.vayunmathur.games.sudoku.platform.SudokuUiState+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config+import kotlin.random.Random+import kotlin.test.Test+import kotlin.test.assertTrue++/** A landscape phone: the side-by-side layout must fit the whole board, bottom row included. */+@RunWith(RobolectricTestRunner::class)+@Config(qualifiers = "w800dp-h360dp-land")+class SudokuLandscapeLayoutTest {++    @get:Rule val rule = createComposeRule()+    private val ctx: Context get() = ApplicationProvider.getApplicationContext()++    private fun cellDesc(game: SudokuGameState, index: Int): String {+        val side = game.size.side+        val value = game.valueAt(index)+        val spoken = if (value == 0) ctx.getString(R.string.cd_empty) else sudokuSymbol(value)+        return ctx.getString(R.string.cd_cell, index / side + 1, index % side + 1, spoken)+    }++    @Test+    fun nineByNineFitsWithSquareCells() = assertBoardFits(BoardSize.NINE)++    @Test+    fun twelveByTwelveFitsWithSquareCells() = assertBoardFits(BoardSize.TWELVE)++    private fun assertBoardFits(size: BoardSize) {+        val game = SudokuGameState.from(SudokuGenerator.generate(size, Difficulty.EASY, Random(3)))+        rule.setContent {+            GameBoardScreen(state = SudokuUiState(game = game), actions = SudokuActions.Noop, onExit = {})+        }+        val root = rule.onRoot().getUnclippedBoundsInRoot()+        val bottomCell = rule.onNodeWithContentDescription(cellDesc(game, size.cellCount - 1))+            .getUnclippedBoundsInRoot()+        val topCell = rule.onNodeWithContentDescription(cellDesc(game, 0)).getUnclippedBoundsInRoot()+        assertTrue(+            bottomCell.bottom <= root.bottom,+            "$size: bottom row ends at ${bottomCell.bottom}, window at ${root.bottom}",+        )+        val width = bottomCell.right - bottomCell.left+        val height = bottomCell.bottom - bottomCell.top+        assertTrue((width - height).value in -0.5f..0.5f, "$size: cells stay square ($width x $height)")+        // First row to last spans side cells, so no row was squeezed out.+        assertTrue(bottomCell.bottom - topCell.top >= height * size.side - 1.dp, "$size: full grid height")+    }+}