sudoku: keep cell, note and keypad glyphs inside their boxes at 200% (#ux-audit)
- Commit
79c610b9bfce81b66778b88b7ca2d2fc6135b0d7- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 01:14 UTC (23 days ago)
- Parents
b427b4d
Symptom: with the system font size at 200%, the number pad's remaining-count badge was cut off at the bottom of each key, pencil marks overflowed their slots (the bottom row of marks clipped), and 12x12 cell digits spilled past the cell edges. Root cause: every glyph that sits in a fixed-size box - a cell sized from the board width, a pencil-mark slot (a fraction of a cell), a fixed 50 dp key - used a font size in sp, which scales with the font setting while the box does not. Line heights were sp too (NumberPad's note slot even converted its dp height with `.value.sp`), so the line boxes also doubled. Fix: - New internal Density.fitSp(preferred, cap) (ui/components/TextFit.kt): returns the preferred sp size unless, at the current font scale, it would render larger than `cap` (compared in px, so Android 14's non-linear font scaling is handled), in which case it returns the cap expressed in sp. - SudokuGrid: cell digits capped at 62% of the cell with a 1.15em line box; pencil marks capped by their slot's height and width, with the line height pinned to the slot in non-scaling units. The cell size is passed down from the grid's BoxWithConstraints. - NumberPad: digit and badge capped at 42% / 22% of the key with 1.1em line boxes ((0.42 + 0.22) x 1.1 < 1, so both always fit); the notes preview digit capped by its slot, line height pinned to the slot. User-visible effect: at default and moderately enlarged font sizes nothing changes (the preferred sizes are below the caps); at large font scales the glyphs grow until they fill their box and then stop, so nothing is clipped. Verification: new TextFitTest covers fitSp at 1x (unchanged), 2x (rendered px equals the cap) and 1.1x (still grows), and renders a 12x12 grid with full pencil marks plus both pad modes at fontScale 2. :games:sudoku:testDevUnitTest in the toolchain container; visual check via :games:sudoku:metadata.
Diff
games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/NumberPad.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/NumberPad.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/NumberPad.ktindex 4223bcbe8d..f36b5657e3 100644--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/NumberPad.kt+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/NumberPad.kt@@ -19,12 +19,14 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color+import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.res.stringResource import androidx.compose.ui.semantics.clearAndSetSemantics import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp+import androidx.compose.ui.unit.em import androidx.compose.ui.unit.sp import com.vayunmathur.games.sudoku.R import com.vayunmathur.games.sudoku.data.BoardSize@@ -158,6 +160,11 @@ private fun DigitKey( else -> scheme.onSurfaceVariant } val label = sudokuSymbol(digit)+ val density = LocalDensity.current+ // The key is a fixed 50dp, so the digit and the badge stacked under it are capped by the key:+ // at 200% font they used to total ~66sp of line boxes and both clipped.+ val digitSize = density.fitSp(FullFontSize, KeySize * KEY_DIGIT_FRACTION)+ val badgeSize = density.fitSp(BadgeFontSize, KeySize * KEY_BADGE_FRACTION) val description = stringResource( if (notesMode) R.string.cd_note_key else R.string.cd_digit_key, label,@@ -178,9 +185,9 @@ private fun DigitKey( Text( text = label, color = contentColor,- fontSize = FullFontSize,+ fontSize = digitSize, // Tight line boxes: the badge below must fit the 50dp key with the digit.- lineHeight = FullFontSize * 1.1f,+ lineHeight = TIGHT_LINE_EM.em, textAlign = TextAlign.Center, ) // Unspoken: the key's clearAndSetSemantics replaces the whole subtree, so the@@ -189,8 +196,8 @@ private fun DigitKey( Text( text = remaining.toString(), color = contentColor,- fontSize = BadgeFontSize,- lineHeight = BadgeFontSize * 1.1f,+ fontSize = badgeSize,+ lineHeight = TIGHT_LINE_EM.em, textAlign = TextAlign.Center, ) }@@ -210,6 +217,15 @@ private fun DigitKey( @Composable private fun NoteSlot(digit: Int, label: String, size: BoardSize, contentColor: Color) { val slotHeight = KeySize / size.boxRows+ val slotWidth = KeySize / size.boxCols+ val density = LocalDensity.current+ // Capped by the slot, and the line box in non-scaling sp: an sp line height grew with the+ // font setting and overflowed the slot, clipping the 3x4 pad's bottom row at 200%.+ val noteSize = density.fitSp(+ NoteFontSize,+ minOf(slotHeight * NOTE_SLOT_HEIGHT_FRACTION, slotWidth * NOTE_SLOT_WIDTH_FRACTION),+ )+ val noteLine = with(density) { slotHeight.toSp() } val target = digit - 1 Column(Modifier.fillMaxSize()) { for (row in 0 until size.boxRows) {@@ -229,10 +245,10 @@ private fun NoteSlot(digit: Int, label: String, size: BoardSize, contentColor: C Text( text = label, color = contentColor,- fontSize = NoteFontSize,+ fontSize = noteSize, // Matched to the slot, or the glyph's own line box overflows it and the // bottom row of a 3x4 pad is clipped.- lineHeight = slotHeight.value.sp,+ lineHeight = noteLine, fontWeight = FontWeight.Medium, textAlign = TextAlign.Center, )@@ -255,6 +271,17 @@ private val FullFontSize = 19.sp /** Remaining-count badge under the digit: small enough to share the 50dp key with it. */ private val BadgeFontSize = 11.sp +/** Line box relative to font size for the digit and badge stacked in a key. */+private const val TIGHT_LINE_EM = 1.1f++/** Caps, as shares of the key, so digit + badge always fit: (0.42 + 0.22) * 1.1 < 1. */+private const val KEY_DIGIT_FRACTION = 0.42f+private const val KEY_BADGE_FRACTION = 0.22f++/** A notes-mode preview digit's cap, as shares of its slot's height and width. */+private const val NOTE_SLOT_HEIGHT_FRACTION = 0.8f+private const val NOTE_SLOT_WIDTH_FRACTION = 1.1f+ /** How long a refused key stays red before settling back, in milliseconds. */ private const val FLASH_MILLIS = 450L
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 51d41bc343..897b2046c1 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@@ -18,12 +18,14 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.drawBehind import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color+import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.semantics.clearAndSetSemantics import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp+import androidx.compose.ui.unit.em import androidx.compose.ui.unit.sp import com.vayunmathur.games.sudoku.R import com.vayunmathur.games.sudoku.data.BoardSize@@ -80,6 +82,7 @@ fun SudokuGrid( game.valueAt(index) == selectedValue, isConflict = index in conflicts, highlightMistakes = highlightMistakes,+ cell = cell, modifier = Modifier .size(cell) .clickable { onSelect(index) },@@ -111,9 +114,11 @@ private fun SudokuCell( isSameValue: Boolean, isConflict: Boolean, highlightMistakes: Boolean,+ cell: Dp, modifier: Modifier = Modifier, ) { val scheme = MaterialTheme.colorScheme+ val density = LocalDensity.current val value = game.valueAt(index) val given = game.isGiven(index) @@ -154,13 +159,15 @@ private fun SudokuCell( value != 0 -> Text( text = sudokuSymbol(value), color = digitColor,- // Sized from the cell so a 4x4 does not render tiny digits in large squares.- fontSize = (game.size.digitScale * 22).sp,+ // Scaled per board so a 6x6 does not render tiny digits in large squares, and+ // capped by the cell so a large font scale cannot push it past the cell edges.+ fontSize = density.fitSp((game.size.digitScale * 22).sp, cell * DIGIT_CELL_FRACTION),+ lineHeight = LINE_HEIGHT_EM.em, fontWeight = if (given) FontWeight.Bold else FontWeight.Medium, textAlign = TextAlign.Center, ) - game.notes[index] != 0 -> NoteMarks(game.size, game.notes[index])+ game.notes[index] != 0 -> NoteMarks(game.size, game.notes[index], cell) } } }@@ -173,7 +180,19 @@ private fun SudokuCell( * rows of three for 6x6, 2x2 for 4x4. */ @Composable-private fun NoteMarks(size: BoardSize, mask: Int) {+private fun NoteMarks(size: BoardSize, mask: Int, cell: Dp) {+ // Each mark's slot is a fixed fraction of the cell, so the cap is too: the narrower of+ // the slot's height (one line) and its width (one glyph, 12x12 slots are tall and thin).+ val slotHeight = cell / size.boxRows+ val slotWidth = cell / size.boxCols+ val density = LocalDensity.current+ val markSize = density.fitSp(+ (size.digitScale * 8).sp,+ minOf(slotHeight * MARK_SLOT_HEIGHT_FRACTION, slotWidth * MARK_SLOT_WIDTH_FRACTION),+ )+ // The slot height in non-scaling sp: an sp line height grows with the font setting and+ // overflowed the slot at 200%, clipping the bottom row of marks.+ val markLine = with(density) { slotHeight.toSp() } // Weighted rows and columns rather than laid-out text: a space is narrower than a digit, so // padding absent marks with " " would let the columns drift out of line. Column(Modifier.fillMaxSize()) {@@ -195,8 +214,8 @@ private fun NoteMarks(size: BoardSize, mask: Int) { Text( text = sudokuSymbol(digit), color = MaterialTheme.colorScheme.onSurfaceVariant,- fontSize = (size.digitScale * 8).sp,- lineHeight = (size.digitScale * 9).sp,+ fontSize = markSize,+ lineHeight = markLine, textAlign = TextAlign.Center, ) }@@ -242,6 +261,16 @@ private fun Modifier.drawGridLines( } } +/** A cell digit never renders taller than this share of the cell. */+private const val DIGIT_CELL_FRACTION = 0.62f++/** Line box of a cell digit, relative to its size, so the line cannot outgrow the cell. */+private const val LINE_HEIGHT_EM = 1.15f++/** A pencil mark's size cap, as shares of its slot's height and width. */+private const val MARK_SLOT_HEIGHT_FRACTION = 0.8f+private const val MARK_SLOT_WIDTH_FRACTION = 1.1f+ /** Scales type with the board: a 6x6 cell is twice the width of a 12x12 cell. */ private val BoardSize.digitScale: Float get() = when (this) {
games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/TextFit.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/TextFit.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/TextFit.ktnew file mode 100644index 0000000000..f8e178bd01--- /dev/null+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/TextFit.kt@@ -0,0 +1,18 @@+package com.vayunmathur.games.sudoku.ui.components++import androidx.compose.ui.unit.Density+import androidx.compose.ui.unit.Dp+import androidx.compose.ui.unit.TextUnit++/**+ * [preferred], unless at the current font scale it would render taller than [cap].+ *+ * Board glyphs live in boxes of a fixed physical size — a cell, a pencil-mark slot, a 50 dp key —+ * so a font size in `sp` alone clips at large font scales: at 200% a 12x12 digit, a 9x9 pencil mark+ * and the key's remaining-count badge all outgrew their boxes. Comparing in pixels keeps the+ * preferred size (and so today's look) at normal scales and lets it grow with the user's setting+ * until it fills the box, never past it. Pixels rather than raw values because Android 14's+ * non-linear font scaling makes `sp` to `px` non-proportional.+ */+internal fun Density.fitSp(preferred: TextUnit, cap: Dp): TextUnit =+ if (preferred.toPx() > cap.toPx()) cap.toSp() else preferred
games/sudoku/src/test/java/com/vayunmathur/games/sudoku/ui/components/TextFitTest.kt
diff --git a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/ui/components/TextFitTest.kt b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/ui/components/TextFitTest.ktnew file mode 100644index 0000000000..9c49b8cfef--- /dev/null+++ b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/ui/components/TextFitTest.kt@@ -0,0 +1,68 @@+package com.vayunmathur.games.sudoku.ui.components++import androidx.compose.runtime.CompositionLocalProvider+import androidx.compose.ui.platform.LocalDensity+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithContentDescription+import androidx.compose.ui.unit.Density+import androidx.compose.ui.unit.dp+import androidx.compose.ui.unit.sp+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.domain.SudokuGenerator+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.random.Random+import kotlin.test.Test+import kotlin.test.assertEquals++/** Board glyphs follow the font scale only until they fill their fixed-size box. */+@RunWith(RobolectricTestRunner::class)+class TextFitTest {++ @get:Rule val rule = createComposeRule()++ @Test+ fun preferredSizeIsKeptAtNormalScale() {+ val density = Density(density = 2.625f, fontScale = 1f)+ assertEquals(19.sp, density.fitSp(19.sp, 21.dp))+ }++ @Test+ fun largeFontScaleIsCappedToTheBox() {+ val density = Density(density = 2.625f, fontScale = 2f)+ val fitted = density.fitSp(19.sp, 21.dp)+ // Rendered height in px equals the cap, not 2 x 19sp.+ with(density) { assertEquals(21.dp.toPx(), fitted.toPx(), 0.01f) }+ }++ @Test+ fun scaleBelowTheCapStillGrows() {+ val density = Density(density = 2f, fontScale = 1.1f)+ assertEquals(11.sp, density.fitSp(11.sp, 20.dp), "a modest scale is honoured")+ }++ @Test+ fun boardAndPadRenderAtDoubleFontScale() {+ val ctx = ApplicationProvider.getApplicationContext<android.content.Context>()+ val base = SudokuGameState.from(SudokuGenerator.generate(BoardSize.TWELVE, Difficulty.EASY, Random(5)))+ val blank = base.givens.indices.first { base.givens[it] == 0 }+ val game = base.copy(notes = base.notes.toMutableList().also { it[blank] = 0xFFF })+ rule.setContent {+ val current = LocalDensity.current+ CompositionLocalProvider(LocalDensity provides Density(current.density, fontScale = 2f)) {+ androidx.compose.foundation.layout.Column {+ SudokuGrid(game = game, onSelect = {})+ NumberPad(game = game, onDigit = {})+ NumberPad(game = game.copy(notesMode = true), onDigit = {})+ }+ }+ }+ rule.onNodeWithContentDescription(ctx.getString(R.string.cd_digit_key, "C")).assertExists()+ rule.onNodeWithContentDescription(ctx.getString(R.string.cd_note_key, "C")).assertExists()+ }+}