///| Implementation of `moonplasma:desktop/plasmoid` for the logout button. A click on the panel /// button opens a small popup, "Log out | Cancel", so a stray click can't end the session; Enter /// confirms and Escape (or a click elsewhere) cancels. Confirming calls `session.logout`, which /// makes the host stop the compositor and hand the screen back to the display manager. ///| Popup geometry: a column of two rows with a margin around them. let popup_w : Int = 150 ///| let row_h : Int = 30 ///| let margin : Int = 6 ///| let rows : Array[String] = ["Log out", "Cancel"] ///| Whether the popup is open, and which row the pointer is over (-1: none). let open_ : Ref[Bool] = { val: false } ///| let hover : Ref[Int] = { val: -1 } ///| fn popup_h() -> Int { margin * 2 + rows.length() * row_h } ///| Row under a popup-local y, 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 < rows.length() { i } else { -1 } } ///| fn close() -> Unit { open_.val = false hover.val = -1 } ///| pub fn info() -> PlasmoidInfo { { id: "org.moonplasma.logout", name: "Log out", preferred: { w: 84, 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 } [ @ui.fill_rect(@ui.rect(0, 0, w, h), bg, radius=r), @ui.text(w / 2, h / 2, "Log out", fs - 2, fg, align=CENTER), ] } ///| 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 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 < rows.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 } ops.push(@ui.text(margin + 12, y + (row_h - 2) / 2, rows[i], fs - 1, fg, bold=i == 0)) } 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_.val = true hover.val = -1 } true } (POPUP, PointerMove(p)) => { let i = row_at(p.x, p.y) if i != hover.val { hover.val = i true } else { false } } (POPUP, PointerLeave) => if hover.val != -1 { hover.val = -1 true } else { false } (POPUP, PointerDown(p)) => { let i = row_at(p.x, p.y) close() if i == 0 { @session.logout() } 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" { close() @session.logout() true } else { false } _ => false } }