Modern Apps git

← Modern-Apps.git

nonogram: size clue text from its slot so large fonts don't clip (#ux-audit)

Commit
1e85b0f428b27a70d752e363c94f76ccc6472256
Author
Vayun Mathur <[email protected]>
When
2026-11-13 01:16 UTC (23 days ago)
Parents
c74c1a2
Symptom: with a large system font size (e.g. 200%) the row and column
clue digits were clipped - cut in half vertically and cropped at the
sides - on every board, worst on 15x15.

Root cause: NonogramBoard sized clue slots in dp from the cell size, but
gave the text `(clueCell.value * 0.78f).coerceIn(6f, 13f).sp` and a
lineHeight of `clueCell.value.sp`. `.sp` is multiplied by the font scale
after that fitting, so at 2x both the glyphs and the line box were
twice the slot they had been fitted to.

Fix: compute the clue size in dp as min(slot * 0.78, 13dp * fontScale)
and convert with Dp.toSp(), which divides the font scale back out; the
line height is the slot converted the same way. The user's font scale
still enlarges clues on roomy boards (5x5, tablets), up to the slot,
and never beyond it. Pinch-zoom (separate change) is the way to get
bigger clues on dense boards.

User-visible effect: clues are always fully visible; at large font sizes
they grow as far as their slot allows instead of overflowing it.

Verification: new Compose test renders a 15x15 board at fontScale 2 and
asserts every clue's font size fits its line box and its text layout
has no visual overflow.

Diff

games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/components/NonogramBoard.kt
diff --git a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/components/NonogramBoard.kt b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/components/NonogramBoard.ktindex d1632bd524..9f7e749e12 100644--- a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/components/NonogramBoard.kt+++ b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/components/NonogramBoard.kt@@ -18,6 +18,7 @@ import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.input.pointer.pointerInput+import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.res.stringResource import androidx.compose.ui.semantics.clearAndSetSemantics@@ -27,7 +28,6 @@ import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.TextUnit import androidx.compose.ui.unit.dp-import androidx.compose.ui.unit.sp import com.vayunmathur.games.nonogram.R import com.vayunmathur.games.nonogram.data.CellMark import com.vayunmathur.games.nonogram.data.NonogramGameState@@ -62,8 +62,16 @@ fun NonogramBoard(         // Sized from the clue slot, not the grid cell, and paired with a line height equal to the         // slot. Without that a 15x15's slots are shorter than the text's natural line box, the clue         // column overflows its fixed height, and the row nearest the grid is clipped in half.-        val fontSize = (clueCell.value * 0.78f).coerceIn(6f, 13f).sp-        val lineHeight = clueCell.value.sp+        //+        // The slot is laid out in dp, so the text is too: the user's font scale raises the preferred+        // size (ClueTextPreferred * fontScale) but never past what the slot can hold. Plain `.sp`+        // values here were multiplied by the font scale *after* fitting, so at 200% text the digits+        // were twice the height of their slot and clipped. Converting with toSp() divides the font+        // scale back out, which is what pins the rendered height to the slot.+        val density = LocalDensity.current+        val textSize = minOf(clueCell * CLUE_TEXT_FRACTION, ClueTextPreferred * density.fontScale)+        val fontSize = with(density) { textSize.toSp() }+        val lineHeight = with(density) { clueCell.toSp() }          Column {             Row {@@ -290,5 +298,11 @@ private fun Modifier.drawGridLines(size: Int, thin: Color, thick: Color) = drawB /** Cells per heavy guide line. */ private const val GROUP = 5 +/** Clue digit height as a fraction of its slot, leaving room for the side bearings of "15". */+private const val CLUE_TEXT_FRACTION = 0.78f++/** Clue size at 100% font scale on a board roomy enough not to cap it. */+private val ClueTextPreferred = 13.dp+ /** A clue slot's width as a fraction of a grid cell. Clues are digits, so they need less room. */ private const val CLUE_CELL_FRACTION = 0.62f
games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.kt
diff --git a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.kt b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.ktindex d9f9dffa5e..daff037c36 100644--- a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.kt+++ b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.kt@@ -27,6 +27,8 @@ import com.vayunmathur.library.ui.R as UiR import org.junit.Rule import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner+import androidx.compose.foundation.layout.size+import androidx.compose.ui.unit.dp import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue@@ -167,6 +169,34 @@ class NonogramScreensTest {         rule.onNodeWithContentDescription(str(R.string.cd_cell, 15, 15, str(R.string.cd_blank))).assertExists()     } +    @Test fun cluesFitTheirSlotsAtLargeFontScale() {+        val p = requireNotNull(com.vayunmathur.games.nonogram.domain.NonogramGenerator.generateSeeded(15, 25L))+        val g = NonogramGameState(p, emptySet(), emptySet(), emptySet(), STARTING_HEARTS, GameMode.CASUAL, 25)+        rule.setContent {+            val base = androidx.compose.ui.platform.LocalDensity.current+            androidx.compose.runtime.CompositionLocalProvider(+                androidx.compose.ui.platform.LocalDensity provides androidx.compose.ui.unit.Density(base.density, fontScale = 2f),+            ) {+                androidx.compose.foundation.layout.Box(androidx.compose.ui.Modifier.size(360.dp)) {+                    NonogramBoard(game = g, onTap = {}, onLongPress = {})+                }+            }+        }+        val clues = rule.onAllNodes(androidx.compose.ui.test.SemanticsMatcher.keyIsDefined(SemanticsActions.GetTextLayoutResult))+            .fetchSemanticsNodes()+        assertTrue(clues.isNotEmpty())+        for (node in clues) {+            val results = mutableListOf<androidx.compose.ui.text.TextLayoutResult>()+            node.config[SemanticsActions.GetTextLayoutResult].action?.invoke(results)+            val r = results.single()+            val text = r.layoutInput.text.text+            val fontPx = with(r.layoutInput.density) { r.layoutInput.style.fontSize.toPx() }+            val linePx = with(r.layoutInput.density) { r.layoutInput.style.lineHeight.toPx() }+            assertTrue(fontPx <= linePx + 0.5f, "clue '$text' is ${fontPx}px in a ${linePx}px slot")+            assertTrue(!r.hasVisualOverflow, "clue '$text' clips at 200% font scale")+        }+    }+     @Test fun settingsPageWithRealViewModelRenders() {         val vm = com.vayunmathur.games.nonogram.platform.NonogramViewModel(             androidx.test.core.app.ApplicationProvider.getApplicationContext<android.app.Application>())