Rewrite the launcher in MoonBit (old code moves to legacy/) (#1)
Launcher / test (push) Successful in 1m20s
Launcher / apk (push) Successful in 37s

This commit was merged in pull request #1.
This commit is contained in:
nandi committed 2026-11-23 01:57:10 +00:00
1 parent 7d2bf5996f
commit 2fa7bd745b
150 files changed
+8319 -462

No files matched your search

+354
View File
@@ -0,0 +1,354 @@
///|
/// The home grid as plain functions over a list of slots: packing, the cell under a
/// point, pinning, resize steps. Pure; ported from legacy/qt/qml/Layout.js and the
/// packing logic of legacy/web/app.py.
///
/// A slot is an app (one cell) or a widget (`w` columns by `rows` rows). `x`/`y` is the
/// cell it was dropped into (column, row from 0), or -1/-1: placed in order into the
/// first free cells that fit it ("dense" packing, so apps fill the cells beside a widget
/// and the grid has no holes).
///|
/// Row height in px.
pub let cell_px : Int32 = 64
///|
/// Gap between rows in px.
pub let row_gap : Int32 = 8
///|
/// Gap between columns in px.
pub let col_gap : Int32 = 8
///|
#derive(Eq, Debug)
pub(all) enum Kind {
App
Widget
}
///|
#derive(Eq, Debug)
pub(all) struct Slot {
kind : Kind
/// An app's component name, or a widget's label.
str : String
/// A widget's host id (0 for apps).
wid : Int32
/// A widget's width in columns (0: the whole row).
mut w : Int32
/// A widget's height in rows.
mut rows : Int32
mut x : Int32
mut y : Int32
/// A stable identity for the view's keyed diff (not persisted).
key : Int32
}
///|
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 : Int32,
w~ : Int32 = 0,
rows~ : Int32 = 1,
key~ : Int32 = 0,
) -> Slot {
{ 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 : Int32) -> Int32 {
let k = (px + row_gap + (cell_px + row_gap) / 2) / (cell_px + row_gap)
if k < 1 {
1
} else {
k
}
}
///|
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 }
}
}
///|
pub fn Slot::height(self : Slot) -> Int32 {
match self.kind {
App => 1
Widget => if self.rows < 1 { 1 } else { self.rows }
}
}
///|
pub fn Slot::pinned(self : Slot, cols : Int32) -> Bool {
self.x >= 0 && self.y >= 0 && self.x + self.cols(cols) <= cols
}
///|
/// Where a slot goes, in cells.
#derive(Eq, Debug)
pub(all) struct Cell {
col : Int32
row : Int32
w : Int32
h : Int32
}
///|
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 : Int32
}
///|
/// Where each slot goes, and the rows used (at least `nrows`, the rows that fill the
/// screen). Pinned slots keep their cell (they may overlap each other while one is
/// carried); the rest go in order into the first free cells that fit them. `hidden[i]`
/// leaves slot i out (an app that is not installed).
pub fn pack(
slots : Array[Slot],
cols : Int32,
nrows : Int32,
hidden~ : (Int32) -> Bool = fn(_) { false },
) -> Packed {
let occ : Array[Array[Bool]] = []
fn taken(c : Int32, r : Int32) -> Bool {
r < occ.length() && occ[r][c]
}
fn fits(c : Int32, r : Int32, w : Int32, h : Int32) -> Bool {
if c < 0 || c + w > cols {
return false
}
for y in r..<(r + h) {
for x in c..<(c + w) {
if taken(x, y) {
return false
}
}
}
true
}
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))
}
for x in c..<(c + w) {
occ[y][x] = true
}
}
}
let n = slots.length()
let cells : Array[Cell?] = Array::make(n, None)
let mut rows = if nrows < 0 { 0 } else { nrows }
for i in 0..<n {
let s = slots[i]
if hidden(i) || !s.pinned(cols) {
continue
}
let w = s.cols(cols)
let h = s.height()
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)
}
for i in 0..<n {
let s = slots[i]
if hidden(i) || cells[i] is Some(_) {
continue
}
let w = s.cols(cols)
let h = s.height()
let mut r = 0
while cells[i] is None {
let mut c = 0
while c + w <= cols {
if fits(c, r, w, h) {
cells[i] = Some({ col: c, row: r, w, h, })
mark(c, r, w, h)
rows = @cmp.maximum(rows, r + h)
break
}
c += 1
}
r += 1
}
}
{ cells, rows, }
}
///|
/// 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) {
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.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,
)
}
///|
/// 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) {
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) -> (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 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() {
None
} else {
Some((c, r))
}
}
///|
/// Pin slot `i` with its top left at `col`, `row`, kept on the screen's grid (`nrows` of
/// 0: no limit on rows).
pub fn pin(
slots : Array[Slot],
i : Int32,
col : Int32,
row : Int32,
cols : Int32,
nrows : Int32,
) -> Unit {
let s = slots[i]
let mut x = col
let mut y = row
if x + s.cols(cols) > cols {
x = cols - s.cols(cols)
}
if nrows > 0 && y + s.height() > nrows {
y = nrows - s.height()
}
s.x = @cmp.maximum(0, x)
s.y = @cmp.maximum(0, y)
}
///|
/// Unpin the slots under pinned slot `i`, so they flow into free cells.
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]
if j == i || b.x < 0 {
continue
}
if b.x < a.x + a.cols(cols) &&
a.x < b.x + b.cols(cols) &&
b.y < a.y + a.height() &&
a.y < b.y + b.height() {
b.x = -1
b.y = -1
}
}
}
///|
/// Take slot `a` out and put it back at `b`, shifting the slots between.
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 : Int32, size : Double, gap : Double) -> Double {
k.to_double() * size + (k - 1).to_double() * gap
}
///|
/// Resize steps: the number of cells (of `size` px, `gap` apart) nearest to `want` px,
/// kept within `min`..`max` px where it can be (`max` of 0: no limit), and within
/// `lo`..`hi` cells.
pub fn fit(
want : Double,
lo : Int32,
hi : Int32,
size : Double,
gap : Double,
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
}
while k > lo && max > 0 && span(k, size, gap) > (max + 8).to_double() {
k -= 1
}
@cmp.maximum(lo, @cmp.minimum(hi, k))
}
///|
/// Columns a new widget of natural width `px` starts at (0: the whole row).
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(),
)
if k >= cols {
0
} else {
k
}
}
///|
/// How many grid columns the screen has for a viewport width in CSS px.
pub fn columns_for(width : Int32) -> Int32 {
if width >= 480 {
6
} else {
5
}
}
///|
/// The width of one column, given the width of the grid.
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) -> Int32 {
let r = ((height + row_gap.to_double()) / (cell_px + row_gap).to_double())
.floor()
.to_int()
@cmp.maximum(1, r)
}
+157
View File
@@ -0,0 +1,157 @@
///|
fn app(x~ : Int32 = -1, y~ : Int32 = -1) -> Slot {
let s = Slot::app("a")
s.x = x
s.y = y
s
}
///|
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
s
}
///|
fn cells(
home : Array[Slot],
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))
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)
}
+3
View File
@@ -0,0 +1,3 @@
import {
"moonbitlang/core/cmp",
}
+84
View File
@@ -0,0 +1,84 @@
// Generated using `moon info`, DON'T EDIT IT
package "experiments/launcher/grid"
import {
"moonbitlang/core/debug",
}
// Values
pub fn box(Cell, Double) -> (Double, Double, Double, Double)
pub fn cell_at(Double, Double, Double) -> (Int32, Int32)
pub let cell_px : Int32
pub fn cell_under(Double, Double, Double) -> (Int32, Int32)?
pub fn clear_cells(Array[Slot], Int32, Int32) -> Unit
pub let col_gap : Int32
pub fn column_width(Double, Int32) -> Double
pub fn columns_for(Int32) -> Int32
pub fn fit(Double, Int32, Int32, Double, Double, Int32, Int32) -> Int32
pub fn move_slot(Array[Slot], Int32, Int32) -> Unit
pub fn pack(Array[Slot], Int32, Int32, hidden? : (Int32) -> Bool) -> Packed
pub fn pin(Array[Slot], Int32, Int32, Int32, Int32, Int32) -> Unit
pub let row_gap : Int32
pub fn rows_fit(Double) -> Int32
pub fn span(Int32, Double, Double) -> Double
pub fn widget_cols(Int32, Double, Int32) -> Int32
pub fn widget_rows(Int32) -> Int32
// Errors
// Types and methods
#derive(Eq, @debug.Debug)
pub(all) struct Cell {
col : Int32
row : Int32
w : Int32
h : Int32
}
#derive(Eq, @debug.Debug)
pub(all) enum Kind {
App
Widget
}
pub(all) struct Packed {
cells : Array[Cell?]
rows : Int32
}
#derive(Eq, @debug.Debug)
pub(all) struct Slot {
kind : Kind
str : String
wid : Int32
mut w : Int32
mut rows : Int32
mut x : Int32
mut y : Int32
key : Int32
}
pub fn Slot::app(String, key? : Int32) -> Self
pub fn Slot::cols(Self, Int32) -> Int32
pub fn Slot::height(Self) -> Int32
pub fn Slot::pinned(Self, Int32) -> Bool
pub fn Slot::widget(String, Int32, w? : Int32, rows? : Int32, key? : Int32) -> Self
// Type aliases
// Traits