Rewrite the launcher in MoonBit (old code moves to legacy/) (#1)
This commit was merged in pull request #1.
This commit is contained in:
1 parent
7d2bf5996f
commit
2fa7bd745b
150 files changed
+8319
-462
No files matched your search
+354
@@ -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)
|
||||
}
|
||||
Reference in new issue
Block a user