Add the pure MoonBit core: grid, gestures, updater, vdom, app
grid: packing, pinning, resize steps (port of Layout.js and app.py's grid logic). gesture: pointer state machine (tap, long press, drag, release, swipe). updater: release/version parsing and the update decision. vdom: pure virtual DOM with an HTML-text renderer for tests. app: Elm-style Model/Msg/update/view with effects as data, persistence (JSON layout, import of the old WebView build's localStorage), drawer/search, menus, drag and resize. 60 headless tests drive it through pointer input. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
66e8e90da1
commit
28e0284d9b
28 files changed
+4278
No files matched your search
@@ -0,0 +1,162 @@
|
||||
///|
|
||||
fn app(x? : Int = -1, y? : Int = -1) -> Slot {
|
||||
let s = Slot::app("a")
|
||||
s.x = x
|
||||
s.y = y
|
||||
s
|
||||
}
|
||||
|
||||
///|
|
||||
fn widget(w : Int, rows : Int, x? : Int = -1, y? : Int = -1) -> Slot {
|
||||
let s = Slot::widget("W", 1, w~, rows~)
|
||||
s.x = x
|
||||
s.y = y
|
||||
s
|
||||
}
|
||||
|
||||
///|
|
||||
fn cells(
|
||||
home : Array[Slot],
|
||||
cols? : Int = 5,
|
||||
nrows? : Int = 0,
|
||||
) -> Array[(Int, Int, Int, Int)?] {
|
||||
pack(home, cols, nrows).cells.map(fn(c) {
|
||||
match c {
|
||||
Some(c) => Some((c.col, c.row, c.w, c.h))
|
||||
None => None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
///|
|
||||
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)),
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
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))])
|
||||
}
|
||||
|
||||
///|
|
||||
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)),
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
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)),
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
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))],
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
test "rows: at least nrows, more if the slots need them" {
|
||||
assert_eq(pack([app()], 5, 9).rows, 9)
|
||||
assert_eq(pack([app(x=0, y=12)], 5, 9).rows, 13)
|
||||
}
|
||||
|
||||
///|
|
||||
test "a pin that no longer fits the columns flows instead" {
|
||||
assert_eq(cells([widget(3, 1, x=3, y=0)]), [Some((0, 0, 3, 1))])
|
||||
}
|
||||
|
||||
///|
|
||||
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 }))
|
||||
}
|
||||
|
||||
///|
|
||||
test "pinning stays on the grid; dropping unpins what it covers" {
|
||||
let h = [widget(2, 2), app(x=1, y=1), app(x=4, y=4)]
|
||||
pin(h, 0, 4, 9, 5, 8)
|
||||
assert_eq((h[0].x, h[0].y), (3, 6))
|
||||
pin(h, 0, 0, 0, 5, 8)
|
||||
clear_cells(h, 0, 5)
|
||||
assert_eq((h[1].x, h[1].y, h[2].x, h[2].y), (-1, -1, 4, 4))
|
||||
}
|
||||
|
||||
///|
|
||||
test "moving in the order" {
|
||||
let h = ["a", "b", "c", "d"]
|
||||
let s = h.map(fn(x) { Slot::app(x) })
|
||||
move_slot(s, 0, 2)
|
||||
assert_eq(s.map(fn(x) { x.str }), ["b", "c", "a", "d"])
|
||||
move_slot(s, 3, 0)
|
||||
assert_eq(s.map(fn(x) { x.str }), ["d", "b", "c", "a"])
|
||||
}
|
||||
|
||||
///|
|
||||
test "widget heights in px to rows" {
|
||||
assert_eq([40, 64, 100, 136, 208, 420].map(widget_rows), [1, 1, 2, 2, 3, 6])
|
||||
}
|
||||
|
||||
///|
|
||||
test "resize steps snap to cells within the widget's limits" {
|
||||
assert_eq(fit(150.0, 1, 5, 64.0, 8.0, 0, 0), 2)
|
||||
assert_eq(fit(10.0, 1, 5, 64.0, 8.0, 200, 0), 3)
|
||||
assert_eq(fit(400.0, 1, 5, 64.0, 8.0, 0, 150), 2)
|
||||
}
|
||||
|
||||
///|
|
||||
test "a new widget's columns from its natural width" {
|
||||
assert_eq(widget_cols(0, 70.0, 5), 0)
|
||||
assert_eq(widget_cols(110, 70.0, 5), 2)
|
||||
assert_eq(widget_cols(400, 70.0, 5), 0)
|
||||
}
|
||||
|
||||
///|
|
||||
test "boxes and cells in px" {
|
||||
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)))
|
||||
assert_eq(cell_under(72.0, 150.0, 70.0), None) // in the column gap
|
||||
assert_eq(cell_under(-1.0, 5.0, 70.0), None)
|
||||
}
|
||||
|
||||
///|
|
||||
test "viewport to grid" {
|
||||
assert_eq(columns_for(390), 5)
|
||||
assert_eq(columns_for(600), 6)
|
||||
assert_eq(column_width(358.0, 5), 65.2)
|
||||
assert_eq(rows_fit(500.0), 7)
|
||||
assert_eq(rows_fit(10.0), 1)
|
||||
}
|
||||
Reference in new issue
Block a user