Modern Apps git

← Modern-Apps.git

sudoku: preview notes on the pad and outline the keys

Commit
2a569fcd67467dacc8e46f3490f92af3841e5be8
Author
vayun-mathur <[email protected]>
When
2026-08-24 20:09 UTC (3 months ago)
Parents
30bcb7f

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 86b6070145..66356fb468 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@@ -1,20 +1,29 @@ package com.vayunmathur.games.sudoku.ui.components +import androidx.compose.foundation.background import androidx.compose.foundation.border+import androidx.compose.foundation.clickable+import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column-import androidx.compose.foundation.layout.PaddingValues 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.size import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier-import androidx.compose.ui.graphics.RectangleShape+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.sp+import com.vayunmathur.games.sudoku.R+import com.vayunmathur.games.sudoku.data.BoardSize import com.vayunmathur.games.sudoku.data.SudokuGameState import com.vayunmathur.games.sudoku.data.sudokuSymbol-import com.vayunmathur.library.ui.Button-import com.vayunmathur.library.ui.FilledTonalButton import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.Text @@ -25,10 +34,11 @@ import com.vayunmathur.library.ui.Text  * 3x3 for 9x9, 3x4 for 12x12 — so a digit sits in the same relative position on the pad as it does in  * the pencil marks.  *- * Keys are fixed-size squares packed flush against each other rather than stretched across the width:- * a full-width grid would give keys around 90dp across on a 12x12, which crowds out the board. A- * hairline in the background colour separates them, so the block reads as distinct keys without- * needing gaps.+ * Keys are fixed-size squares packed flush against each other rather than stretched across the width: a+ * full-width grid would give keys around 90dp across on a 12x12, which crowds out the board.+ *+ * They are drawn like the grid's own cells — background-coloured inside, outlined — rather than as+ * filled buttons, so a block of ten of them does not dominate the screen above the board it serves.  *  * No key is ever disabled. A wrong digit is accepted silently, so there is nothing to gate on, and a  * digit that already appears the maximum number of times may still be one the player wants to move.@@ -47,7 +57,8 @@ fun NumberPad(                     val digit = row * size.boxCols + col + 1                     DigitKey(                         digit = digit,-                        highlighted = game.notesMode,+                        size = size,+                        notesMode = game.notesMode,                         onClick = { onDigit(digit) },                     )                 }@@ -56,37 +67,93 @@ fun NumberPad(     } } +/**+ * One key.+ *+ * In notes mode the digit shrinks into the slot it would occupy as a pencil mark, so the key is a+ * preview of the mark it will place — which is also what tells the player the mode is on, without+ * needing a second colour for it.+ */ @Composable private fun DigitKey(     digit: Int,-    highlighted: Boolean,+    size: BoardSize,+    notesMode: Boolean,     onClick: () -> Unit, ) {-    val shared = Modifier-        .size(KeySize)-        // Drawn in the background colour, so it separates neighbouring keys without adding a gap.-        .border(HairlineWidth, MaterialTheme.colorScheme.background)-    val label = @Composable {-        Text(text = sudokuSymbol(digit), fontSize = 19.sp)+    val scheme = MaterialTheme.colorScheme+    val label = sudokuSymbol(digit)+    val description = stringResource(+        if (notesMode) R.string.cd_note_key else R.string.cd_digit_key,+        label,+    )+    Box(+        Modifier+            .size(KeySize)+            .background(scheme.background)+            .border(OutlineWidth, scheme.outline)+            .clickable(onClick = onClick)+            .clearAndSetSemantics { contentDescription = description },+        contentAlignment = Alignment.Center,+    ) {+        if (notesMode) {+            NoteSlot(digit = digit, label = label, size = size)+        } else {+            Text(+                text = label,+                color = scheme.onSurface,+                fontSize = 19.sp,+                textAlign = TextAlign.Center,+            )+        }     }-    if (highlighted) {-        FilledTonalButton(-            onClick = onClick,-            modifier = shared,-            shape = RectangleShape,-            contentPadding = PaddingValues(0.dp),-        ) { label() }-    } else {-        Button(-            onClick = onClick,-            modifier = shared,-            shape = RectangleShape,-            contentPadding = PaddingValues(0.dp),-        ) { label() }+}++/**+ * The digit drawn where it would sit among a cell's pencil marks.+ *+ * Lays out the full mini-grid and fills only this digit's slot, so every key positions its digit on the+ * same lattice — the pad reads as one cell's worth of marks spread across the keys.+ */+@Composable+private fun NoteSlot(digit: Int, label: String, size: BoardSize) {+    val slotHeight = KeySize / size.boxRows+    val fontSize = (KeySize.value / size.boxCols * 0.62f).coerceIn(7f, 12f).sp+    val target = digit - 1+    Column(Modifier.fillMaxSize()) {+        for (row in 0 until size.boxRows) {+            Row(+                Modifier+                    .fillMaxWidth()+                    .weight(1f)+            ) {+                for (col in 0 until size.boxCols) {+                    Box(+                        Modifier+                            .weight(1f)+                            .fillMaxHeight(),+                        contentAlignment = Alignment.Center,+                    ) {+                        if (row * size.boxCols + col == target) {+                            Text(+                                text = label,+                                color = MaterialTheme.colorScheme.onSurfaceVariant,+                                fontSize = fontSize,+                                // 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,+                                fontWeight = FontWeight.Medium,+                                textAlign = TextAlign.Center,+                            )+                        }+                    }+                }+            }+        }     } } -/** Comfortably above the 48dp touch target once the hairline is accounted for, and no larger. */+/** Comfortably above the 48dp touch target once the outline is accounted for, and no larger. */ private val KeySize = 50.dp -private val HairlineWidth = 1.dp+private val OutlineWidth = 1.dp
games/sudoku/src/main/res/values/strings.xml
diff --git a/games/sudoku/src/main/res/values/strings.xml b/games/sudoku/src/main/res/values/strings.xmlindex 643f239d96..f5ba32f6ac 100644--- a/games/sudoku/src/main/res/values/strings.xml+++ b/games/sudoku/src/main/res/values/strings.xml@@ -51,6 +51,10 @@      <!-- GameBoardScreen: Content description of a single grid cell. %1$d = row number starting at 1, e.g. 3. %2$d = column number starting at 1, e.g. 7. %3$s = the digit showing or the word for empty, e.g. 5 -->     <string name="cd_cell">Row <xliff:g id="row" example="3">%1$d</xliff:g>, column <xliff:g id="column" example="7">%2$d</xliff:g>, <xliff:g id="value" example="5">%3$s</xliff:g></string>+    <!-- NumberPad: Content description of a key that writes a digit. %1$s = the digit's symbol, e.g. 7 or B -->+    <string name="cd_digit_key">Enter <xliff:g id="digit" example="7">%1$s</xliff:g></string>+    <!-- NumberPad: Content description of a key that pencils a digit in, while notes mode is on. %1$s = the digit's symbol, e.g. 7 or B -->+    <string name="cd_note_key">Note <xliff:g id="digit" example="7">%1$s</xliff:g></string>     <!-- GameBoardScreen: Spoken in place of a digit for a cell with nothing in it. Adjective -->     <string name="cd_empty">empty</string> </resources>
metadata_data/photos/games-sudoku/1.png
diff --git a/metadata_data/photos/games-sudoku/1.png b/metadata_data/photos/games-sudoku/1.pngindex a37dba7c93..f69f05abb3 100644Binary files a/metadata_data/photos/games-sudoku/1.png and b/metadata_data/photos/games-sudoku/1.png differ
metadata_data/photos/games-sudoku/2.png
diff --git a/metadata_data/photos/games-sudoku/2.png b/metadata_data/photos/games-sudoku/2.pngindex b402c39f57..4c038414d2 100644Binary files a/metadata_data/photos/games-sudoku/2.png and b/metadata_data/photos/games-sudoku/2.png differ
metadata_data/photos/games-sudoku/3.png
diff --git a/metadata_data/photos/games-sudoku/3.png b/metadata_data/photos/games-sudoku/3.pngindex 0cd9b4c137..270db4c573 100644Binary files a/metadata_data/photos/games-sudoku/3.png and b/metadata_data/photos/games-sudoku/3.png differ
metadata_data/photos/games-sudoku/4.png
diff --git a/metadata_data/photos/games-sudoku/4.png b/metadata_data/photos/games-sudoku/4.pngindex 91f87be414..914ca99079 100644Binary files a/metadata_data/photos/games-sudoku/4.png and b/metadata_data/photos/games-sudoku/4.png differ
metadata_data/photos/games-sudoku/5.png
diff --git a/metadata_data/photos/games-sudoku/5.png b/metadata_data/photos/games-sudoku/5.pngindex 3ea5ab6977..42276720da 100644Binary files a/metadata_data/photos/games-sudoku/5.png and b/metadata_data/photos/games-sudoku/5.png differ