///| /// 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 : Int = 64 ///| /// Gap between rows in px. pub let row_gap : Int = 8 ///| /// Gap between columns in px. pub let col_gap : Int = 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 : Int /// A widget's width in columns (0: the whole row). mut w : Int /// A widget's height in rows. mut rows : Int mut x : Int mut y : Int /// A stable identity for the view's keyed diff (not persisted). key : Int } ///| 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::widget( str : String, wid : Int, w~ : Int = 0, rows~ : Int = 1, key~ : Int = 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 : Int) -> Int { 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 : Int) -> Int { match self.kind { App => 1 Widget => if self.w <= 0 || self.w > cols { cols } else { self.w } } } ///| pub fn Slot::height(self : Slot) -> Int { match self.kind { App => 1 Widget => if self.rows < 1 { 1 } else { self.rows } } } ///| pub fn Slot::pinned(self : Slot, cols : Int) -> 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 : Int row : Int w : Int h : Int } ///| 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 } ///| /// 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 : Int, nrows : Int, hidden~ : (Int) -> Bool = fn(_) { false }, ) -> Packed { let occ : Array[Array[Bool]] = [] fn taken(c : Int, r : Int) -> Bool { r < occ.length() && occ[r][c] } fn fits(c : Int, r : Int, w : Int, h : Int) -> 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 : Int, r : Int, w : Int, h : Int) -> 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.. (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) -> (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() (@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)? { 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 : Int, col : Int, row : Int, cols : Int, nrows : Int, ) -> 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 : Int, cols : Int) -> Unit { let a = slots[i] for j in 0.. 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 { 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 : Int, hi : Int, size : Double, gap : Double, min : Int, max : Int, ) -> Int { 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 : Int, col_w : Double, cols : Int) -> Int { 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 : Int) -> Int { 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 : Int) -> 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() @cmp.maximum(1, r) }