Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
SpriteandClaude Sonnet 5.5 committed 2026-11-23 01:08:53 +00:00
1 parent dccbfd7d86
commit 17f81c60c8
20 files changed
+636 -447

No files matched your search

+63 -62
View File
@@ -10,15 +10,15 @@
///|
/// Row height in px.
pub let cell_px : Int = 64
pub let cell_px : Int32 = 64
///|
/// Gap between rows in px.
pub let row_gap : Int = 8
pub let row_gap : Int32 = 8
///|
/// Gap between columns in px.
pub let col_gap : Int = 8
pub let col_gap : Int32 = 8
///|
#derive(Eq, Debug)
@@ -34,36 +34,36 @@ pub(all) struct Slot {
/// An app's component name, or a widget's label.
str : String
/// A widget's host id (0 for apps).
wid : Int
wid : Int32
/// A widget's width in columns (0: the whole row).
mut w : Int
mut w : Int32
/// A widget's height in rows.
mut rows : Int
mut x : Int
mut y : Int
mut rows : Int32
mut x : Int32
mut y : Int32
/// A stable identity for the view's keyed diff (not persisted).
key : Int
key : Int32
}
///|
pub fn Slot::app(str : String, key~ : Int = 0) -> Slot {
{ kind: App, str, wid: 0, w: 0, rows: 1, x: -1, y: -1, key }
pub fn Slot::app(str : String, key~ : Int32 = 0) -> Slot {
{ kind: App, str, wid: 0, w: 0, rows: 1, x: -1, y: -1, key, }
}
///|
pub fn Slot::widget(
str : String,
wid : Int,
w~ : Int = 0,
rows~ : Int = 1,
key~ : Int = 0,
wid : Int32,
w~ : Int32 = 0,
rows~ : Int32 = 1,
key~ : Int32 = 0,
) -> Slot {
{ kind: Widget, str, wid, w, rows, x: -1, y: -1, key }
{ kind: Widget, str, wid, w, rows, x: -1, y: -1, key, }
}
///|
/// Rows of app tiles nearest to a widget height in px, at least one.
pub fn widget_rows(px : Int) -> Int {
pub fn widget_rows(px : Int32) -> Int32 {
let k = (px + row_gap + (cell_px + row_gap) / 2) / (cell_px + row_gap)
if k < 1 {
1
@@ -73,7 +73,7 @@ pub fn widget_rows(px : Int) -> Int {
}
///|
pub fn Slot::cols(self : Slot, cols : Int) -> Int {
pub fn Slot::cols(self : Slot, cols : Int32) -> Int32 {
match self.kind {
App => 1
Widget => if self.w <= 0 || self.w > cols { cols } else { self.w }
@@ -81,7 +81,7 @@ pub fn Slot::cols(self : Slot, cols : Int) -> Int {
}
///|
pub fn Slot::height(self : Slot) -> Int {
pub fn Slot::height(self : Slot) -> Int32 {
match self.kind {
App => 1
Widget => if self.rows < 1 { 1 } else { self.rows }
@@ -89,7 +89,7 @@ pub fn Slot::height(self : Slot) -> Int {
}
///|
pub fn Slot::pinned(self : Slot, cols : Int) -> Bool {
pub fn Slot::pinned(self : Slot, cols : Int32) -> Bool {
self.x >= 0 && self.y >= 0 && self.x + self.cols(cols) <= cols
}
@@ -97,10 +97,10 @@ pub fn Slot::pinned(self : Slot, cols : Int) -> Bool {
/// Where a slot goes, in cells.
#derive(Eq, Debug)
pub(all) struct Cell {
col : Int
row : Int
w : Int
h : Int
col : Int32
row : Int32
w : Int32
h : Int32
}
///|
@@ -108,7 +108,7 @@ pub(all) struct Packed {
/// One per slot, `None` for a hidden one.
cells : Array[Cell?]
/// Rows used, at least the rows that fill the screen.
rows : Int
rows : Int32
}
///|
@@ -118,16 +118,16 @@ pub(all) struct Packed {
/// leaves slot i out (an app that is not installed).
pub fn pack(
slots : Array[Slot],
cols : Int,
nrows : Int,
hidden~ : (Int) -> Bool = fn(_) { false },
cols : Int32,
nrows : Int32,
hidden~ : (Int32) -> Bool = fn(_) { false },
) -> Packed {
let occ : Array[Array[Bool]] = []
fn taken(c : Int, r : Int) -> Bool {
fn taken(c : Int32, r : Int32) -> Bool {
r < occ.length() && occ[r][c]
}
fn fits(c : Int, r : Int, w : Int, h : Int) -> Bool {
fn fits(c : Int32, r : Int32, w : Int32, h : Int32) -> Bool {
if c < 0 || c + w > cols {
return false
}
@@ -141,7 +141,7 @@ pub fn pack(
true
}
fn mark(c : Int, r : Int, w : Int, h : Int) -> Unit {
fn mark(c : Int32, r : Int32, w : Int32, h : Int32) -> Unit {
for y in r..<(r + h) {
while occ.length() <= y {
occ.push(Array::make(cols, false))
@@ -162,7 +162,7 @@ pub fn pack(
}
let w = s.cols(cols)
let h = s.height()
cells[i] = Some({ col: s.x, row: s.y, w, h })
cells[i] = Some({ col: s.x, row: s.y, w, h, })
mark(s.x, s.y, w, h)
rows = @cmp.maximum(rows, s.y + h)
}
@@ -178,7 +178,7 @@ pub fn pack(
let mut c = 0
while c + w <= cols {
if fits(c, r, w, h) {
cells[i] = Some({ col: c, row: r, w, h })
cells[i] = Some({ col: c, row: r, w, h, })
mark(c, r, w, h)
rows = @cmp.maximum(rows, r + h)
break
@@ -188,7 +188,7 @@ pub fn pack(
r += 1
}
}
{ cells, rows }
{ cells, rows, }
}
///|
@@ -206,19 +206,15 @@ pub fn box(cell : Cell, col_w : Double) -> (Double, Double, Double, Double) {
///|
/// 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) -> (Int, Int) {
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()
pub fn cell_at(x : Double, y : Double, col_w : Double) -> (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()
(@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,
) -> (Int, Int)? {
pub fn cell_under(x : Double, y : Double, col_w : Double) -> (Int32, Int32)? {
if x < 0.0 || y < 0.0 {
return None
}
@@ -226,7 +222,8 @@ pub fn cell_under(
let ph = (cell_px + 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 > cell_px.to_double() {
None
} else {
Some((c, r))
@@ -238,11 +235,11 @@ pub fn cell_under(
/// 0: no limit on rows).
pub fn pin(
slots : Array[Slot],
i : Int,
col : Int,
row : Int,
cols : Int,
nrows : Int,
i : Int32,
col : Int32,
row : Int32,
cols : Int32,
nrows : Int32,
) -> Unit {
let s = slots[i]
let mut x = col
@@ -259,7 +256,7 @@ pub fn pin(
///|
/// Unpin the slots under pinned slot `i`, so they flow into free cells.
pub fn clear_cells(slots : Array[Slot], i : Int, cols : Int) -> Unit {
pub fn clear_cells(slots : Array[Slot], i : Int32, cols : Int32) -> Unit {
let a = slots[i]
for j in 0..<slots.length() {
let b = slots[j]
@@ -278,14 +275,14 @@ pub fn clear_cells(slots : Array[Slot], i : Int, cols : Int) -> Unit {
///|
/// Take slot `a` out and put it back at `b`, shifting the slots between.
pub fn move_slot(slots : Array[Slot], a : Int, b : Int) -> Unit {
pub fn move_slot(slots : Array[Slot], a : Int32, b : Int32) -> Unit {
let s = slots.remove(a)
slots.insert(b, s)
}
///|
/// The size in px of `k` cells of `size` px, `gap` apart.
pub fn span(k : Int, size : Double, gap : Double) -> Double {
pub fn span(k : Int32, size : Double, gap : Double) -> Double {
k.to_double() * size + (k - 1).to_double() * gap
}
@@ -295,13 +292,13 @@ pub fn span(k : Int, size : Double, gap : Double) -> Double {
/// `lo`..`hi` cells.
pub fn fit(
want : Double,
lo : Int,
hi : Int,
lo : Int32,
hi : Int32,
size : Double,
gap : Double,
min : Int,
max : Int,
) -> Int {
min : Int32,
max : Int32,
) -> Int32 {
let mut k = ((want + gap) / (size + gap) + 0.5).floor().to_int()
while k < hi && span(k, size, gap) < (min - 8).to_double() {
k += 1
@@ -314,13 +311,15 @@ pub fn fit(
///|
/// Columns a new widget of natural width `px` starts at (0: the whole row).
pub fn widget_cols(px : Int, col_w : Double, cols : Int) -> Int {
pub fn widget_cols(px : Int32, col_w : Double, cols : Int32) -> Int32 {
if px <= 0 {
return 0
}
let k = @cmp.maximum(
1,
((px + col_gap - 8).to_double() / (col_w + col_gap.to_double())).ceil().to_int(),
((px + col_gap - 8).to_double() / (col_w + col_gap.to_double()))
.ceil()
.to_int(),
)
if k >= cols {
0
@@ -331,7 +330,7 @@ pub fn widget_cols(px : Int, col_w : Double, cols : Int) -> Int {
///|
/// How many grid columns the screen has for a viewport width in CSS px.
pub fn columns_for(width : Int) -> Int {
pub fn columns_for(width : Int32) -> Int32 {
if width >= 480 {
6
} else {
@@ -341,13 +340,15 @@ pub fn columns_for(width : Int) -> Int {
///|
/// The width of one column, given the width of the grid.
pub fn column_width(grid_width : Double, cols : Int) -> Double {
pub fn column_width(grid_width : Double, cols : Int32) -> Double {
(grid_width - (cols - 1).to_double() * col_gap.to_double()) / cols.to_double()
}
///|
/// How many rows of the grid fit in `height` px.
pub fn rows_fit(height : Double) -> Int {
let r = ((height + row_gap.to_double()) / (cell_px + row_gap).to_double()).floor().to_int()
pub fn rows_fit(height : Double) -> Int32 {
let r = ((height + row_gap.to_double()) / (cell_px + row_gap).to_double())
.floor()
.to_int()
@cmp.maximum(1, r)
}
+39 -44
View File
@@ -1,5 +1,5 @@
///|
fn app(x? : Int = -1, y? : Int = -1) -> Slot {
fn app(x~ : Int32 = -1, y~ : Int32 = -1) -> Slot {
let s = Slot::app("a")
s.x = x
s.y = y
@@ -7,7 +7,7 @@ fn app(x? : Int = -1, y? : Int = -1) -> Slot {
}
///|
fn widget(w : Int, rows : Int, x? : Int = -1, y? : Int = -1) -> Slot {
fn widget(w : Int32, rows : Int32, x~ : Int32 = -1, y~ : Int32 = -1) -> Slot {
let s = Slot::widget("W", 1, w~, rows~)
s.x = x
s.y = y
@@ -17,9 +17,9 @@ fn widget(w : Int, rows : Int, x? : Int = -1, y? : Int = -1) -> Slot {
///|
fn cells(
home : Array[Slot],
cols? : Int = 5,
nrows? : Int = 0,
) -> Array[(Int, Int, Int, Int)?] {
cols~ : Int32 = 5,
nrows~ : Int32 = 0,
) -> Array[(Int32, Int32, Int32, Int32)?] {
pack(home, cols, nrows).cells.map(fn(c) {
match c {
Some(c) => Some((c.col, c.row, c.w, c.h))
@@ -30,59 +30,54 @@ fn cells(
///|
test "apps fill rows left to right" {
assert_eq(
cells([app(), app(), app(), app(), app(), app()]),
[
Some((0, 0, 1, 1)),
Some((1, 0, 1, 1)),
Some((2, 0, 1, 1)),
Some((3, 0, 1, 1)),
Some((4, 0, 1, 1)),
Some((0, 1, 1, 1)),
],
)
assert_eq(cells([app(), app(), app(), app(), app(), app()]), [
Some((0, 0, 1, 1)),
Some((1, 0, 1, 1)),
Some((2, 0, 1, 1)),
Some((3, 0, 1, 1)),
Some((4, 0, 1, 1)),
Some((0, 1, 1, 1)),
])
}
///|
test "a whole-row widget takes its rows; apps go below" {
assert_eq(cells([widget(0, 2), app()]), [Some((0, 0, 5, 2)), Some((0, 2, 1, 1))])
assert_eq(cells([widget(0, 2), app()]), [
Some((0, 0, 5, 2)),
Some((0, 2, 1, 1)),
])
}
///|
test "dense: apps fill the cells beside a narrow widget" {
assert_eq(
cells([widget(3, 2), app(), app(), app(), app(), app()]),
[
Some((0, 0, 3, 2)),
Some((3, 0, 1, 1)),
Some((4, 0, 1, 1)),
Some((3, 1, 1, 1)),
Some((4, 1, 1, 1)),
Some((0, 2, 1, 1)),
],
)
assert_eq(cells([widget(3, 2), app(), app(), app(), app(), app()]), [
Some((0, 0, 3, 2)),
Some((3, 0, 1, 1)),
Some((4, 0, 1, 1)),
Some((3, 1, 1, 1)),
Some((4, 1, 1, 1)),
Some((0, 2, 1, 1)),
])
}
///|
test "a widget too wide for the rest of a row starts the next; apps backfill" {
assert_eq(
cells([app(), app(), app(), widget(3, 1), app()]),
[
Some((0, 0, 1, 1)),
Some((1, 0, 1, 1)),
Some((2, 0, 1, 1)),
Some((0, 1, 3, 1)),
Some((3, 0, 1, 1)),
],
)
assert_eq(cells([app(), app(), app(), widget(3, 1), app()]), [
Some((0, 0, 1, 1)),
Some((1, 0, 1, 1)),
Some((2, 0, 1, 1)),
Some((0, 1, 3, 1)),
Some((3, 0, 1, 1)),
])
}
///|
test "pinned slots keep their cell; the rest flow around them" {
assert_eq(
cells([app(), app(x=2, y=3), app()]),
[Some((0, 0, 1, 1)), Some((2, 3, 1, 1)), Some((1, 0, 1, 1))],
)
assert_eq(cells([app(), app(x=2, y=3), app()]), [
Some((0, 0, 1, 1)),
Some((2, 3, 1, 1)),
Some((1, 0, 1, 1)),
])
}
///|
@@ -100,7 +95,7 @@ test "a pin that no longer fits the columns flows instead" {
test "hidden slots take no cell" {
let p = pack([app(), app()], 5, 0, hidden=fn(i) { i == 0 })
assert_eq(p.cells[0], None)
assert_eq(p.cells[1], Some({ col: 0, row: 0, w: 1, h: 1 }))
assert_eq(p.cells[1], Some({ col: 0, row: 0, w: 1, h: 1, }))
}
///|
@@ -144,7 +139,7 @@ test "a new widget's columns from its natural width" {
///|
test "boxes and cells in px" {
let b = box({ col: 1, row: 2, w: 2, h: 1 }, 70.0)
let b = box({ col: 1, row: 2, w: 2, h: 1, }, 70.0)
assert_eq(b, (78.0, 144.0, 148.0, 64.0))
assert_eq(cell_at(80.0, 150.0, 70.0), (1, 2))
assert_eq(cell_under(80.0, 150.0, 70.0), Some((1, 2)))