Modern Apps git

← Modern-Apps.git

minesweeper: size cell numbers from the cell so 200% font fits (#ux-audit)

Commit
4b781def8edb77dafe6e6684656ccfd3e66033d9
Author
Vayun Mathur <[email protected]>
When
2026-11-13 01:21 UTC (23 days ago)
Parents
fc8235e
Symptom: at the 200% system font size the neighbour counts on a Large
field (12 columns, ~27 dp cells on a phone) were taller than their
cells and spilled over the edges, overlapping neighbouring cells.

Root cause: MineCell drew the count at a fixed 15.sp, which scales
with the font setting, while the cell is sized from the field
geometry and does not.

Fix: MineFieldGrid now passes its computed cell size to MineCell, and
the count's font size is 15.sp unless that would render taller than
70% of the cell's inner size (cell minus the 1 dp gaps), in which case
it is capped there; the comparison is done in pixels (a private
Density.fitSp helper) so Android 14's non-linear font scaling is
respected. The line box is pinned to 1.15em so it scales with the
capped size rather than the theme's sp line height.

User-visible effect: unchanged at normal font sizes; at large font
sizes the numbers grow until they fill the cell and stop there, on
every field size.

Verification: new MinesweeperBoardTest
countsStayInsideLargeFieldCellsAtDoubleFontScale renders a 27 dp-cell
grid at fontScale 2 and asserts the count's laid-out height is within
its cell. :games:minesweeper:testDevUnitTest in the toolchain
container.

Diff

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 fc8ad52422..05d320d2a8 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@@ -25,7 +25,11 @@ import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.onClick import androidx.compose.ui.semantics.onLongClick import androidx.compose.ui.text.font.FontWeight+import androidx.compose.ui.unit.Density+import androidx.compose.ui.unit.Dp+import androidx.compose.ui.unit.TextUnit import androidx.compose.ui.unit.dp+import androidx.compose.ui.unit.em import androidx.compose.ui.unit.sp import com.vayunmathur.games.minesweeper.R import com.vayunmathur.games.minesweeper.data.GameOutcome@@ -73,6 +77,7 @@ fun MineFieldGrid(                             index = index,                             onTap = { onTap(index) },                             onLongPress = { onLongPress(index) },+                            cell = cell,                             modifier = Modifier.size(cell),                         )                     }@@ -88,9 +93,11 @@ private fun MineCell(     index: Int,     onTap: () -> Unit,     onLongPress: () -> Boolean,+    cell: Dp,     modifier: Modifier = Modifier, ) {     val scheme = MaterialTheme.colorScheme+    val density = LocalDensity.current     val haptics = LocalHapticFeedback.current      val revealed = game.revealed[index]@@ -164,7 +171,10 @@ private fun MineCell(                 text = count.toString(),                 color = countColor(count, scheme),                 fontWeight = FontWeight.Bold,-                fontSize = 15.sp,+                // 15sp follows the font setting only until the digit fills the cell: a Large+                // field's ~27dp cells overflowed at 200% font, and the number spilled out.+                fontSize = density.fitSp(15.sp, (cell - CellGap * 2) * COUNT_CELL_FRACTION),+                lineHeight = COUNT_LINE_EM.em,             )         }     }@@ -173,6 +183,19 @@ private fun MineCell( /** Gap between cells, so the field reads as a grid without needing drawn lines. */ private val CellGap = 1.dp +/** A count never renders taller than this share of its cell. */+private const val COUNT_CELL_FRACTION = 0.7f++/** Line box of a count relative to its size, so the line cannot outgrow the cell. */+private const val COUNT_LINE_EM = 1.15f++/**+ * [preferred], unless at the current font scale it would render taller than [cap] — then [cap].+ * Compared in pixels, so Android 14's non-linear font scaling is handled.+ */+private fun Density.fitSp(preferred: TextUnit, cap: Dp): TextUnit =+    if (preferred.toPx() > cap.toPx()) cap.toSp() else preferred+ /**  * The traditional per-number colours, mapped onto theme roles so they still work in both light and  * dark and against a dynamic palette. Beyond four the numbers are rare enough that one shared colour
games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperBoardTest.kt
diff --git a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperBoardTest.kt b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperBoardTest.ktindex 9801520d51..59ee8ce20a 100644--- a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperBoardTest.kt+++ b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperBoardTest.kt@@ -10,7 +10,14 @@ import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performSemanticsAction+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.size import androidx.compose.runtime.CompositionLocalProvider+import androidx.compose.ui.Modifier+import androidx.compose.ui.platform.LocalDensity+import androidx.compose.ui.test.getUnclippedBoundsInRoot+import androidx.compose.ui.unit.Density+import androidx.compose.ui.unit.dp import androidx.compose.ui.hapticfeedback.HapticFeedback import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.platform.LocalHapticFeedback@@ -112,6 +119,24 @@ class MinesweeperBoardTest {         assertEquals(listOf(HapticFeedbackType.LongPress), buzzes)     } +    @Test+    fun countsStayInsideLargeFieldCellsAtDoubleFontScale() {+        // A Large-field-sized cell (~27dp) with a revealed count, at 200% font.+        val revealed = List(9) { it == 0 }+        composeRule.setContent {+            val current = LocalDensity.current+            CompositionLocalProvider(LocalDensity provides Density(current.density, fontScale = 2f)) {+                Box(Modifier.size(81.dp)) {+                    MineFieldGrid(field(revealed = revealed), onTap = {}, onLongPress = { false })+                }+            }+        }+        val cell = composeRule.onNodeWithContentDescription("Row 1, column 1", substring = true)+            .getUnclippedBoundsInRoot()+        val digit = composeRule.onNodeWithText("1", useUnmergedTree = true).getUnclippedBoundsInRoot()+        assertTrue(digit.bottom - digit.top <= cell.bottom - cell.top, "count fits its cell: $digit in $cell")+    }+     @Test     fun gridRevealedNumberAndFlagDescriptions() {         val revealed = List(9) { it == 0 }