///| Implementation of `moonplasma:desktop/plasmoid` for the wallpaper chooser. The panel button /// opens a popup listing the wallpapers the shell can draw (`wallpaper.wallpapers`), the current /// one ticked. Clicking a row switches the desktop to it at once (the list stays open to compare); Enter /// picks the highlighted row and closes, Escape or a click elsewhere closes. The shell remembers the choice across restarts. ///| Popup geometry: a column of rows with a margin around them. let popup_w : Int = 180 ///| let row_h : Int = 30 ///| let margin : Int = 6 ///| Whether the popup is open, and which row is highlighted (-1: none). let open_ : Ref[Bool] = { val: false } ///| let hover : Ref[Int] = { val: -1 } ///| The names, read when the popup opens (the list is fixed while it is open). let names : Ref[Array[String]] = { val: [] } ///| fn popup_h() -> Int { margin * 2 + names.val.length() * row_h } ///| Row under a popup-local point, or -1. fn row_at(x : Int, y : Int) -> Int { if x < margin || x >= popup_w - margin || y < margin { return -1 } let i = (y - margin) / row_h if i < names.val.length() { i } else { -1 } } ///| fn open() -> Unit { names.val = @wallpaper.wallpapers() open_.val = true hover.val = @wallpaper.current_wallpaper().reinterpret_as_int() } ///| fn close() -> Unit { open_.val = false hover.val = -1 } ///| fn choose(i : Int) -> Unit { if i >= 0 && i < names.val.length() { @wallpaper.set_wallpaper(i.reinterpret_as_uint()) } } ///| pub fn info() -> PlasmoidInfo { { id: "org.moonplasma.wallpaper", name: "Wallpaper", preferred: { w: 96, h: 34 }, min_width: 70, 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 r = t.radius.reinterpret_as_int() let fs = t.font_size.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 } // A tiny landscape swatch, then the label. let sw = 18 let sx = 8 let sy = (h - 14) / 2 [ @ui.fill_rect(@ui.rect(0, 0, w, h), bg, radius=r), @ui.fill_rect(@ui.rect(sx, sy, sw, 14), t.palette.desktop_top, radius=2), @ui.circle(sx + sw - 5, sy + 4, 2, t.palette.accent_foreground), @ui.fill_rect(@ui.rect(sx, sy + 9, sw, 5), t.palette.desktop_bottom, radius=2), @ui.stroke_rect(@ui.rect(sx, sy, sw, 14), fg, width=1, radius=2), @ui.text(sx + sw + 6, h / 2, "Wallpaper", fs - 2, fg), ] } ///| pub fn popup() -> PopupSurface? { if !open_.val { return None } let t = @theme.get_tokens() let w = popup_w let h = popup_h() let r = t.radius.reinterpret_as_int() let fs = t.font_size.reinterpret_as_int() let cur = @wallpaper.current_wallpaper().reinterpret_as_int() let ops : Array[@types.DrawOp] = [ @ui.fill_rect(@ui.rect(0, 0, w, h), t.palette.surface_popup, radius=r), @ui.stroke_rect(@ui.rect(0, 0, w, h), t.palette.border, width=1, radius=r), ] for i = 0; i < names.val.length(); i = i + 1 { let y = margin + i * row_h let hot = i == hover.val if hot { ops.push(@ui.fill_rect(@ui.rect(margin, y, w - 2 * margin, row_h - 2), t.palette.accent, radius=r)) } let fg = if hot { t.palette.accent_foreground } else { t.palette.foreground } let mid = y + (row_h - 2) / 2 if i == cur { // A tick: two strokes. ops.push(@ui.line(margin + 8, mid, margin + 11, mid + 4, fg, width=2)) ops.push(@ui.line(margin + 11, mid + 4, margin + 18, mid - 4, fg, width=2)) } ops.push(@ui.text(margin + 26, mid, names.val[i], fs - 1, fg, bold=i == cur)) } Some({ size: { w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() }, ops }) } ///| pub fn on_event(target : Surface, event : @types.InputEvent) -> Bool { match (target, event) { (WIDGET, PointerDown(_)) => { if open_.val { close() } else { open() } true } (POPUP, PointerMove(p)) => { let i = row_at(p.x, p.y) if i != hover.val && i >= 0 { hover.val = i true } else { false } } (POPUP, PointerDown(p)) => { let i = row_at(p.x, p.y) if i >= 0 { choose(i) hover.val = i } true } (_, FocusLost) => if open_.val { close() true } else { false } (_, KeyDown(k)) => if !open_.val { false } else if k == "Escape" { close() true } else if k == "Enter" { choose(hover.val) close() true } else { false } _ => false } }