///| Panel layout algorithm (pure integer geometry, no WIT types). ///| pub(all) struct SlotIn { id : String pref_w : Int pref_h : Int min_w : Int stretch : Bool end_align : Bool } ///| pub(all) struct Placed { id : String x : Int y : Int w : Int h : Int } ///| pub(all) struct PanelLayout { x : Int y : Int w : Int h : Int placements : Array[Placed] } ///| Bottom, full-width panel. Widgets pack towards their edge in slot order. /// Free space goes to `stretch` slots; if even the minimum widths do not fit /// the call fails (Err) rather than silently dropping widgets. pub fn layout( screen_w : Int, screen_h : Int, panel_h : Int, margin : Int, spacing : Int, slots : Array[SlotIn], ) -> Result[PanelLayout, String] { if panel_h <= 0 || panel_h > screen_h { return Err("panel height \{panel_h} does not fit screen height \{screen_h}") } let n = slots.length() let avail = screen_w - 2 * margin - (if n > 1 { spacing * (n - 1) } else { 0 }) let widths : Array[Int] = slots.map(s => s.pref_w) let mut total = 0 for w in widths { total += w } if total > avail { // Shrink towards min_w, taking the deficit from the widest slack first. let mut deficit = total - avail let mut guard_iter = 0 while deficit > 0 && guard_iter < 100000 { guard_iter += 1 let mut best = -1 let mut best_slack = 0 for i = 0; i < n; i = i + 1 { let slack = widths[i] - slots[i].min_w if slack > best_slack { best_slack = slack best = i } } if best < 0 { return Err( "panel overflow: slots need at least \{avail + deficit}px but only \{avail}px available", ) } widths[best] = widths[best] - 1 deficit -= 1 } } else if total < avail { let mut free = avail - total let mut stretchers = 0 for s in slots { if s.stretch { stretchers += 1 } } if stretchers > 0 { let share = free / stretchers let mut rem = free - share * stretchers for i = 0; i < n; i = i + 1 { if slots[i].stretch { widths[i] = widths[i] + share + (if rem > 0 { 1 } else { 0 }) if rem > 0 { rem -= 1 } } } free = 0 } } let inner_h = panel_h - 2 * margin let py = screen_h - panel_h let placed : Array[Placed?] = Array::make(n, None) let mut x = margin for i = 0; i < n; i = i + 1 { if !slots[i].end_align { placed[i] = Some(place(slots[i], x, py, margin, inner_h, widths[i])) x += widths[i] + spacing } } let mut rx = screen_w - margin for i = n - 1; i >= 0; i = i - 1 { if slots[i].end_align { rx -= widths[i] placed[i] = Some(place(slots[i], rx, py, margin, inner_h, widths[i])) rx -= spacing } } let out : Array[Placed] = [] for p in placed { match p { Some(v) => out.push(v) None => abort("unreachable: unplaced slot") } } Ok({ x: 0, y: py, w: screen_w, h: panel_h, placements: out }) } ///| fn place( s : SlotIn, x : Int, py : Int, margin : Int, inner_h : Int, w : Int, ) -> Placed { let h = if s.pref_h < inner_h { s.pref_h } else { inner_h } { id: s.id, x, y: py + margin + (inner_h - h) / 2, w, h } } ///| Popup rectangle: above the panel, left-aligned with its anchor widget and /// clamped inside the screen. pub fn popup_position( screen_w : Int, panel_y : Int, anchor_x : Int, popup_w : Int, popup_h : Int, gap : Int, ) -> (Int, Int) { let max_x = screen_w - popup_w let x = if anchor_x > max_x { max_x } else { anchor_x } let x = if x < 0 { 0 } else { x } let y = panel_y - popup_h - gap let y = if y < 0 { 0 } else { y } (x, y) } ///| fn slot(id : String, pref : Int, min : Int, stretch : Bool, end_ : Bool) -> SlotIn { { id, pref_w: pref, pref_h: 30, min_w: min, stretch, end_align: end_ } } ///| test "start and end packing" { let r = layout( 1000, 600, 40, 4, 6, [slot("a", 100, 50, false, false), slot("b", 80, 40, false, true)], ).unwrap() assert_eq((r.x, r.y, r.w, r.h), (0, 560, 1000, 40)) assert_eq((r.placements[0].x, r.placements[0].w), (4, 100)) assert_eq((r.placements[1].x, r.placements[1].w), (916, 80)) // 30px tall widget centred in 32px inner area. assert_eq(r.placements[0].y, 565) } ///| test "stretch takes the free space" { let r = layout( 500, 300, 40, 0, 0, [slot("a", 100, 50, false, false), slot("s", 10, 10, true, false)], ).unwrap() assert_eq(r.placements[1].w, 400) } ///| test "shrinks to minimum then fails loudly" { let ok = layout( 200, 300, 40, 0, 0, [slot("a", 150, 80, false, false), slot("b", 150, 80, false, false)], ).unwrap() assert_eq((ok.placements[0].w, ok.placements[1].w), (100, 100)) let bad = layout( 100, 300, 40, 0, 0, [slot("a", 150, 80, false, false), slot("b", 150, 80, false, false)], ) assert_true(bad is Err(_)) } ///| test "popup clamps to screen" { assert_eq(popup_position(1000, 560, 4, 300, 200, 6), (4, 354)) assert_eq(popup_position(1000, 560, 900, 300, 200, 6), (700, 354)) assert_eq(popup_position(1000, 100, 4, 300, 200, 6), (4, 0)) }