///| Implementation of `moonplasma:desktop/plasmoid` for the task manager: one pill per open /// window, in the order the windows were opened (so the bar doesn't reshuffle when focus changes). /// The focused window's pill is lit, a minimized window's is dimmed; a click focuses the window and /// restores it if it was minimized. When the bar is too narrow the pills shrink to fit. ///| Widest a pill gets, the gap between pills, and the narrowest before titles stop being readable. let pill_max : Int = 140 ///| let gap : Int = 6 ///| let pill_min : Int = 56 ///| The width the module was last drawn at, and the pill the pointer is over (-1: none). let last_w : Ref[Int] = { val: 300 } ///| let hover : Ref[Int] = { val: -1 } ///| Windows in opening order (ids grow as windows open), whatever the host's stacking order says. fn windows_sorted() -> Array[@windows.WindowInfo] { let ws = @windows.list_windows() let n = ws.length() for i = 1; i < n; i = i + 1 { let cur = ws[i] let mut j = i - 1 while j >= 0 && ws[j].id > cur.id { ws[j + 1] = ws[j] j = j - 1 } ws[j + 1] = cur } ws } ///| Pill width for `n` windows in a bar `w` wide. fn pill_w(n : Int, w : Int) -> Int { if n <= 0 { return pill_max } let fit = (w - gap * (n - 1)) / n if fit > pill_max { pill_max } else if fit < pill_min { pill_min } else { fit } } ///| The pill under a widget-local x, or -1. fn pill_at(x : Int, n : Int) -> Int { let pw = pill_w(n, last_w.val) if x < 0 { return -1 } let i = x / (pw + gap) if i < n && x - i * (pw + gap) < pw { i } else { -1 } } ///| `s` shortened with an ellipsis until it fits `max_w` pixels. fn fit_text(s : String, size : Int, max_w : Int) -> String { if (s.length() * ((size * 11 + 19) / 20)) <= max_w { return s } let mut n = s.length() while n > 1 && ((n + 1) * ((size * 11 + 19) / 20)) > max_w { n = n - 1 } s[:n].to_string() + "…" } ///| pub fn info() -> PlasmoidInfo { { id: "org.moonplasma.tasks", name: "Task Manager", preferred: { w: 300, h: 34 }, min_width: 120, stretch: true, } } ///| pub fn render(size : @types.Size) -> Array[@types.DrawOp] { let t = @theme.get_tokens() let w = size.w.reinterpret_as_int() let h = size.h.reinterpret_as_int() last_w.val = w let r = t.radius.reinterpret_as_int() let fs = t.font_size.reinterpret_as_int() let ws = windows_sorted() let pw = pill_w(ws.length(), w) let ops : Array[@types.DrawOp] = [] for i = 0; i < ws.length(); i = i + 1 { let x = i * (pw + gap) if x >= w { break } let win = ws[i] let focused = win.focused == 1U let minimized = win.minimized == 1U let bg = if focused { t.palette.accent } else if i == hover.val { t.palette.surface_popup } else { t.palette.surface_hover } let fg = if focused { t.palette.accent_foreground } else if minimized { @ui.with_alpha(t.palette.foreground, 140) } else { t.palette.foreground } ops.push(@ui.fill_rect(@ui.rect(x, 0, pw, h), bg, radius=r)) if minimized && !focused { ops.push(@ui.stroke_rect(@ui.rect(x, 0, pw, h), t.palette.border, width=1, radius=r)) } let title = if win.title.length() > 0 { win.title } else { "(untitled)" } ops.push(@ui.text(x + 10, h / 2, fit_text(title, fs, pw - 20), fs, fg, bold=focused)) } ops } ///| pub fn popup() -> PopupSurface? { None } ///| pub fn on_event(target : Surface, event : @types.InputEvent) -> Bool { match (target, event) { (WIDGET, PointerMove(p)) => { let i = pill_at(p.x, windows_sorted().length()) if i != hover.val { hover.val = i true } else { false } } (WIDGET, PointerLeave) => if hover.val != -1 { hover.val = -1 true } else { false } (WIDGET, PointerDown(p)) => { let ws = windows_sorted() let i = pill_at(p.x, ws.length()) if i >= 0 { @windows.focus_window(ws[i].id) } true } _ => false } }