///| Implementation of `moonplasma:desktop/plasmoid` for the module manager: a panel /// button whose popup lists the loaded instances (click to unload), every module found /// on the search path (click to load another instance) and a "Browse..." row. ///| let open_ : Ref[Bool] = Ref::new(false) ///| let hover : Ref[Int] = Ref::new(-1) ///| Layout, shared with the host tests: header 24, rows 28, 4px margin. let margin : Int = 4 ///| let header_h : Int = 24 ///| let row_h : Int = 28 ///| let popup_w : Int = 300 ///| pub fn info() -> PlasmoidInfo { { id: "org.moonplasma.module-manager", name: "Module Manager", preferred: { w: 110, h: 34 }, min_width: 60, stretch: false, } } ///| 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() let bg = if open_.val { t.palette.accent } else { t.palette.surface_hover } let fg = if open_.val { t.palette.accent_foreground } else { t.palette.foreground } [ @ui.fill_rect(@ui.rect(0, 0, w, h), bg, radius=t.radius.reinterpret_as_int()), @ui.text(w / 2, h / 2, "Modules", t.font_size.reinterpret_as_int(), fg, align=CENTER, bold=true), ] } ///| Total popup height for `l` loaded and `a` available rows. fn popup_h(l : Int, a : Int) -> Int { margin + header_h + l * row_h + header_h + a * row_h + row_h + margin } ///| pub fn popup() -> PopupSurface? { if !open_.val { return None } let t = @theme.get_tokens() let p = t.palette let fs = t.font_size.reinterpret_as_int() let r = t.radius.reinterpret_as_int() let loaded = @modules.loaded() let avail = @modules.available() let w = popup_w let h = popup_h(loaded.length(), avail.length()) let ops = [ @ui.fill_rect(@ui.rect(0, 0, w, h), p.surface_popup, radius=r + 4), @ui.stroke_rect(@ui.rect(0, 0, w, h), p.border, radius=r + 4), ] let mut y = margin ops.push(@ui.text(14, y + header_h / 2, "Loaded (click to unload)", fs - 2, p.foreground_muted, bold=true)) y += header_h let mut row = 0 for inst in loaded { row_bg(ops, row, y, w, r, p) let fg = if row == hover.val { p.accent_foreground } else { p.foreground } ops.push(@ui.text(14, y + row_h / 2, inst.name, fs, fg)) ops.push(@ui.text(w - 14, y + row_h / 2, "x", fs, fg, align=RIGHT, bold=true)) y += row_h row += 1 } ops.push(@ui.text(14, y + header_h / 2, "Available (click to load)", fs - 2, p.foreground_muted, bold=true)) y += header_h for m in avail { row_bg(ops, row, y, w, r, p) let fg = if row == hover.val { p.accent_foreground } else { p.foreground } ops.push(@ui.text(14, y + row_h / 2, m.name, fs, fg)) if m.loaded > 0U { ops.push(@ui.text(w - 14, y + row_h / 2, "x" + m.loaded.to_string(), fs - 2, fg, align=RIGHT)) } y += row_h row += 1 } row_bg(ops, row, y, w, r, p) let fg = if row == hover.val { p.accent_foreground } else { p.foreground } ops.push(@ui.text(14, y + row_h / 2, "Browse...", fs, fg, bold=true)) Some({ size: { w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() }, ops }) } ///| fn row_bg( ops : Array[@types.DrawOp], row : Int, y : Int, w : Int, r : Int, p : @theme.Palette, ) -> Unit { if row == hover.val { ops.push(@ui.fill_rect(@ui.rect(6, y, w - 12, row_h - 2), p.accent, radius=r)) } } ///| Row index under popup-local `pt` (loaded rows, then available rows, then Browse), or -1. fn row_at(pt : @types.Point, l : Int, a : Int) -> Int { if pt.x < 6 || pt.x >= popup_w - 6 { return -1 } let mut y = pt.y - margin - header_h if y >= 0 && y < l * row_h { return y / row_h } y -= l * row_h + header_h if y >= 0 && y < (a + 1) * row_h { return l + y / row_h } -1 } ///| pub fn on_event(target : Surface, event : @types.InputEvent) -> Bool { match (target, event) { (WIDGET, PointerDown(_)) => { open_.val = !open_.val hover.val = -1 true } (POPUP, PointerMove(pt)) => { let i = row_at(pt, @modules.loaded().length(), @modules.available().length()) if i != hover.val { hover.val = i true } else { false } } (POPUP, PointerLeave) => if hover.val != -1 { hover.val = -1 true } else { false } (POPUP, PointerDown(pt)) => { let loaded = @modules.loaded() let avail = @modules.available() let i = row_at(pt, loaded.length(), avail.length()) if i >= 0 { if i < loaded.length() { @modules.unload_instance(loaded[i].slot) } else if i - loaded.length() < avail.length() { @modules.load_module(avail[i - loaded.length()].path) } else { @modules.browse() } hover.val = -1 } true } (_, FocusLost) => if open_.val { open_.val = false hover.val = -1 true } else { false } (_, KeyDown("Escape")) => if open_.val { open_.val = false true } else { false } _ => false } }