///| 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) } ///| test "fit clamps anchored spans within lo..hi and negative targets" { // Widget pinned at col 2 of 5 can only grow to hi = 3 columns on the right assert_eq(fit(500.0, 1, 3, 65.2, 8.0, 64, 0), 3) // Negative drag past the left/top edge clamps cleanly to 1 assert_eq(fit(-120.0, 1, 4, 65.2, 8.0, 0, 0), 1) // Expanding a widget unpins slots now overlapped by it let h = [widget(2, 2, x=1, y=1), app(x=3, y=1), app(x=0, y=0)] h[0].w = 3 clear_cells(h, 0, 5) assert_eq((h[1].x, h[1].y, h[2].x, h[2].y), (-1, -1, 0, 0)) }