Add an icon size setting for the home grid and the app drawer
Launcher / test (pull_request) Successful in 1m29s
Launcher / apk (pull_request) Skipped

Slider (70-160%, step 5) with Small/Medium/Large presets and a live preview in the
home menu. Icon, label and row height scale together; the home and drawer column
counts reflow; the size is saved in localStorage; a corrupt saved value is logged
and shown, not silently replaced. 100% is the original layout.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
SpriteandClaude Sonnet 5.5 committed 2026-11-24 22:32:50 +00:00
1 parent 3070db4fb4
commit 23317b949a
22 files changed
+901 -51

No files matched your search

+28 -15
View File
@@ -63,8 +63,8 @@ pub fn Slot::widget(
///|
/// Rows of app tiles nearest to a widget height in px, at least one.
pub fn widget_rows(px : Int32) -> Int32 {
let k = (px + row_gap + (cell_px + row_gap) / 2) / (cell_px + row_gap)
pub fn widget_rows(px : Int32, ch~ : Int32 = cell_px) -> Int32 {
let k = (px + row_gap + (ch + row_gap) / 2) / (ch + row_gap)
if k < 1 {
1
} else {
@@ -192,38 +192,51 @@ pub fn pack(
}
///|
/// A cell's box in px, given the column width: (x, y, w, h).
pub fn box(cell : Cell, col_w : Double) -> (Double, Double, Double, Double) {
/// A cell's box in px, given the column width and the row height `ch`: (x, y, w, h).
pub fn box(
cell : Cell,
col_w : Double,
ch~ : Int32 = cell_px,
) -> (Double, Double, Double, Double) {
let cg = col_gap.to_double()
let rg = row_gap.to_double()
(
cell.col.to_double() * (col_w + cg),
cell.row.to_double() * (cell_px.to_double() + rg),
cell.row.to_double() * (ch.to_double() + rg),
cell.w.to_double() * col_w + (cell.w - 1).to_double() * cg,
cell.h.to_double() * cell_px.to_double() + (cell.h - 1).to_double() * rg,
cell.h.to_double() * ch.to_double() + (cell.h - 1).to_double() * rg,
)
}
///|
/// The cell nearest to a point (px in the grid, from its top left): (col, row).
pub fn cell_at(x : Double, y : Double, col_w : Double) -> (Int32, Int32) {
pub fn cell_at(
x : Double,
y : Double,
col_w : Double,
ch~ : Int32 = cell_px,
) -> (Int32, Int32) {
let c = (x / (col_w + col_gap.to_double()) + 0.5).floor().to_int()
let r = (y / (cell_px + row_gap).to_double() + 0.5).floor().to_int()
let r = (y / (ch + row_gap).to_double() + 0.5).floor().to_int()
(@cmp.maximum(0, c), @cmp.maximum(0, r))
}
///|
/// The cell whose box contains a point, or `None` outside every cell (in the gaps).
pub fn cell_under(x : Double, y : Double, col_w : Double) -> (Int32, Int32)? {
pub fn cell_under(
x : Double,
y : Double,
col_w : Double,
ch~ : Int32 = cell_px,
) -> (Int32, Int32)? {
if x < 0.0 || y < 0.0 {
return None
}
let pw = col_w + col_gap.to_double()
let ph = (cell_px + row_gap).to_double()
let ph = (ch + row_gap).to_double()
let c = (x / pw).floor().to_int()
let r = (y / ph).floor().to_int()
if x - c.to_double() * pw > col_w ||
y - r.to_double() * ph > cell_px.to_double() {
if x - c.to_double() * pw > col_w || y - r.to_double() * ph > ch.to_double() {
None
} else {
Some((c, r))
@@ -345,9 +358,9 @@ pub fn column_width(grid_width : Double, cols : Int32) -> Double {
}
///|
/// How many rows of the grid fit in `height` px.
pub fn rows_fit(height : Double) -> Int32 {
let r = ((height + row_gap.to_double()) / (cell_px + row_gap).to_double())
/// How many rows of height `ch` fit in `height` px.
pub fn rows_fit(height : Double, ch~ : Int32 = cell_px) -> Int32 {
let r = ((height + row_gap.to_double()) / (ch + row_gap).to_double())
.floor()
.to_int()
@cmp.maximum(1, r)
+3 -1
View File
@@ -120,7 +120,9 @@ test "moving in the order" {
///|
test "widget heights in px to rows" {
assert_eq([40, 64, 100, 136, 208, 420].map(widget_rows), [1, 1, 2, 2, 3, 6])
assert_eq([40, 64, 100, 136, 208, 420].map(fn(p) { widget_rows(p) }), [
1, 1, 2, 2, 3, 6,
])
}
///|
+1
View File
@@ -1,3 +1,4 @@
import {
"moonbitlang/core/cmp",
"moonbitlang/core/string",
}
+41 -5
View File
@@ -6,13 +6,15 @@ import {
}
// Values
pub fn box(Cell, Double) -> (Double, Double, Double, Double)
pub fn box(Cell, Double, ch? : Int32) -> (Double, Double, Double, Double)
pub fn cell_at(Double, Double, Double) -> (Int32, Int32)
pub fn cell_at(Double, Double, Double, ch? : Int32) -> (Int32, Int32)
pub fn cell_height(Int32) -> Int32
pub let cell_px : Int32
pub fn cell_under(Double, Double, Double) -> (Int32, Int32)?
pub fn cell_under(Double, Double, Double, ch? : Int32) -> (Int32, Int32)?
pub fn clear_cells(Array[Slot], Int32, Int32) -> Unit
@@ -22,23 +24,57 @@ pub fn column_width(Double, Int32) -> Double
pub fn columns_for(Int32) -> Int32
pub fn css_factor(Int32) -> String
pub fn drawer_columns(Int32, Int32) -> Int32
pub let drawer_icon_px : Int32
pub fn fit(Double, Int32, Int32, Double, Double, Int32, Int32) -> Int32
pub fn home_columns(Int32, Double, Int32) -> Int32
pub let home_icon_px : Int32
pub let min_cols : Int32
pub fn move_slot(Array[Slot], Int32, Int32) -> Unit
pub fn pack(Array[Slot], Int32, Int32, hidden? : (Int32) -> Bool) -> Packed
pub fn parse_scale(String) -> Result[Int32, String]
pub fn pin(Array[Slot], Int32, Int32, Int32, Int32, Int32) -> Unit
pub let row_gap : Int32
pub fn rows_fit(Double) -> Int32
pub fn rows_fit(Double, ch? : Int32) -> Int32
pub let scale_default : Int32
pub let scale_large : Int32
pub let scale_max : Int32
pub let scale_medium : Int32
pub let scale_min : Int32
pub let scale_small : Int32
pub let scale_step : Int32
pub fn scale_valid(Int32) -> Bool
pub fn scaled_columns(Int32, Int32, Double, Int32, Int32) -> Int32
pub fn scaled_px(Int32, Int32) -> Int32
pub fn span(Int32, Double, Double) -> Double
pub fn widget_cols(Int32, Double, Int32) -> Int32
pub fn widget_rows(Int32) -> Int32
pub fn widget_rows(Int32, ch? : Int32) -> Int32
// Errors
+142
View File
@@ -0,0 +1,142 @@
///|
/// The icon size setting as plain functions: the allowed range, parsing a stored or
/// slider value, and how icon size, row height and column count follow from it.
/// The size is a whole percent of today's size (`scale_default` = 100: no change).
///|
pub let scale_min : Int32 = 70
///|
pub let scale_max : Int32 = 160
///|
pub let scale_default : Int32 = 100
///|
/// Slider step in percent.
pub let scale_step : Int32 = 5
///|
/// The presets: Small, Medium (today's size), Large.
pub let scale_small : Int32 = 80
///|
pub let scale_medium : Int32 = 100
///|
pub let scale_large : Int32 = 130
///|
/// The home icon at 100%, in px (`.slots .icon`).
pub let home_icon_px : Int32 = 40
///|
/// The drawer icon at 100%, in px (`.icon`).
pub let drawer_icon_px : Int32 = 52
///|
/// Fewest columns either grid is reduced to, however large the icons.
pub let min_cols : Int32 = 3
///|
/// A size is a valid setting: in range and on the step.
pub fn scale_valid(pct : Int32) -> Bool {
pct >= scale_min && pct <= scale_max && pct % scale_step == 0
}
///|
/// Parse a stored or slider value (decimal whole percent, nothing else). The error says
/// what was wrong and what is allowed; callers show and log it.
pub fn parse_scale(text : String) -> Result[Int32, String] {
let t = text.trim().to_owned()
let n = @string.parse_int(t) catch {
_ => return Err("icon size \"\{text}\" is not a whole number")
}
if scale_valid(n) {
Ok(n)
} else {
Err(
"icon size \{n} is outside \{scale_min}..\{scale_max} in steps of \{scale_step}",
)
}
}
///|
/// `base` px at `pct` percent, rounded to a whole px, at least 1.
pub fn scaled_px(base : Int32, pct : Int32) -> Int32 {
@cmp.maximum(1, (base * pct + 50) / 100)
}
///|
/// Height of a home row at `pct`.
pub fn cell_height(pct : Int32) -> Int32 {
scaled_px(cell_px, pct)
}
///|
/// The percent as a CSS number (`1`, `1.25`, `0.8`), for the `--s` custom property.
pub fn css_factor(pct : Int32) -> String {
let whole = pct / 100
let frac = pct % 100
if frac == 0 {
whole.to_string()
} else if frac < 10 {
"\{whole}.0\{frac}"
} else if frac % 10 == 0 {
"\{whole}.\{frac / 10}"
} else {
"\{whole}.\{frac}"
}
}
///|
/// Columns of a grid `avail` px wide whose icons are `icon` px: `base` columns at 100%,
/// `base * 100 / pct` (rounded) at `pct`, no fewer than `min_cols` and then never so
/// many that a column (`gap` px apart) is narrower than its icon.
pub fn scaled_columns(
base : Int32,
pct : Int32,
avail : Double,
gap : Int32,
icon : Int32,
) -> Int32 {
let mut c = @cmp.maximum(min_cols, (base * 100 + pct / 2) / pct)
while c > 1 && column_width_in(avail, c, gap) < icon.to_double() {
c -= 1
}
c
}
///|
fn column_width_in(avail : Double, cols : Int32, gap : Int32) -> Double {
(avail - (cols - 1).to_double() * gap.to_double()) / cols.to_double()
}
///|
/// Home grid columns for a viewport width, the width of the grid and the size.
pub fn home_columns(vw : Int32, grid_w : Double, pct : Int32) -> Int32 {
scaled_columns(
columns_for(vw),
pct,
grid_w,
col_gap,
scaled_px(home_icon_px, pct),
)
}
///|
/// Drawer columns (4 below 480px, else 5, at 100%); its list is the viewport less 16px
/// padding each side, with 8px between columns.
pub fn drawer_columns(vw : Int32, pct : Int32) -> Int32 {
scaled_columns(
if vw >= 480 {
5
} else {
4
},
pct,
(vw - 32).to_double(),
8,
scaled_px(drawer_icon_px, pct),
)
}
+136
View File
@@ -0,0 +1,136 @@
///|
test "the default size is today's size" {
assert_eq(scale_default, 100)
assert_eq(scale_medium, scale_default)
assert_eq(cell_height(scale_default), cell_px)
assert_eq(scaled_px(home_icon_px, scale_default), 40)
assert_eq(scaled_px(drawer_icon_px, scale_default), 52)
// the column counts the app had before the setting existed
assert_eq(home_columns(390, 358.0, 100), columns_for(390))
assert_eq(home_columns(600, 528.0, 100), columns_for(600))
assert_eq(drawer_columns(390, 100), 4)
assert_eq(drawer_columns(600, 100), 5)
}
///|
test "presets and range are valid settings" {
for p in [scale_small, scale_medium, scale_large, scale_min, scale_max] {
assert_true(scale_valid(p))
}
assert_true(scale_min < scale_default && scale_default < scale_max)
}
///|
test "parse_scale accepts steps in range" {
assert_eq(parse_scale("100"), Ok(100))
assert_eq(parse_scale(" 85 "), Ok(85))
assert_eq(parse_scale("70"), Ok(70))
assert_eq(parse_scale("160"), Ok(160))
}
///|
test "parse_scale rejects corrupt values with a reason" {
assert_eq(parse_scale("abc"), Err("icon size \"abc\" is not a whole number"))
assert_eq(parse_scale(""), Err("icon size \"\" is not a whole number"))
assert_eq(parse_scale("1.5"), Err("icon size \"1.5\" is not a whole number"))
assert_eq(
parse_scale("65"),
Err("icon size 65 is outside 70..160 in steps of 5"),
)
assert_eq(
parse_scale("500"),
Err("icon size 500 is outside 70..160 in steps of 5"),
)
assert_eq(
parse_scale("-10"),
Err("icon size -10 is outside 70..160 in steps of 5"),
)
// in range but off the step
assert_eq(
parse_scale("102"),
Err("icon size 102 is outside 70..160 in steps of 5"),
)
}
///|
test "sizes scale together" {
assert_eq(scaled_px(home_icon_px, 150), 60)
assert_eq(scaled_px(home_icon_px, 70), 28)
assert_eq(cell_height(150), 96)
assert_eq(cell_height(70), 45)
assert_eq(cell_height(scale_min) < cell_px, true)
assert_eq(scaled_px(1, 70), 1) // never below a pixel
}
///|
test "css_factor" {
assert_eq(css_factor(100), "1")
assert_eq(css_factor(130), "1.3")
assert_eq(css_factor(80), "0.8")
assert_eq(css_factor(105), "1.05")
assert_eq(css_factor(160), "1.6")
assert_eq(css_factor(70), "0.7")
}
///|
test "larger icons mean fewer columns, smaller mean more" {
assert_eq(home_columns(390, 358.0, 70), 7)
assert_eq(home_columns(390, 358.0, 130), 4)
assert_eq(home_columns(390, 358.0, 160), 3)
assert_eq(drawer_columns(390, 70), 6)
assert_eq(drawer_columns(390, 160), 3)
}
///|
test "columns never go below min_cols while icons fit" {
for pct in 70..<=160 {
if pct % scale_step == 0 {
assert_true(home_columns(390, 358.0, pct) >= min_cols)
assert_true(drawer_columns(390, pct) >= min_cols)
}
}
}
///|
test "icons never overlap or clip: a column is at least as wide as its icon" {
// every size, on phones of several widths (grid is the viewport less 32px, max 528)
for vw in [320, 360, 390, 412, 480, 600, 800] {
let grid_w = ((if vw > 560 { 560 } else { vw }) - 32).to_double()
for pct in 70..<=160 {
if pct % scale_step != 0 {
continue
}
let c = home_columns(vw, grid_w, pct)
assert_true(
column_width(grid_w, c) >= scaled_px(home_icon_px, pct).to_double(),
)
let d = drawer_columns(vw, pct)
let dw = (vw - 32).to_double()
assert_true(
(dw - (d - 1).to_double() * 8.0) / d.to_double() >=
scaled_px(drawer_icon_px, pct).to_double(),
)
}
}
}
///|
test "a very narrow grid sheds columns to keep the icons whole" {
// 160px of grid at 160%: icons are 64px, so only two columns fit with the 8px gap
assert_eq(scaled_columns(5, 160, 160.0, 8, 64), 2)
assert_eq(scaled_columns(5, 100, 400.0, 8, 40), 5)
}
///|
test "row height parameter threads through the cell maths" {
let c : Cell = { col: 1, row: 2, w: 2, h: 2, }
let (x, y, w, h) = box(c, 60.0, ch=96)
assert_eq((x, y, w, h), (68.0, 208.0, 128.0, 200.0))
assert_eq(box(c, 60.0), box(c, 60.0, ch=cell_px))
assert_eq(rows_fit(500.0, ch=96), 4)
assert_eq(rows_fit(500.0), 7)
assert_eq(widget_rows(200, ch=96), 2)
assert_eq(cell_at(80.0, 220.0, 70.0, ch=96), (1, 2))
assert_eq(cell_under(80.0, 220.0, 70.0, ch=96), Some((1, 2)))
assert_eq(cell_under(80.0, 100.0, 70.0, ch=96), None) // in the row gap
}