Files
chandra/components/session-settings/gen/interface/chandra/desktop/plasmoid/stub.mbt
T
Claude 2636373a4f
CI / test (push) Failing after 18s
Power settings: screen off and suspend when idle, battery, suspend / restart / power off
- Settings gets a Power tab (fourth; key 4 or SettingsTab::Power): the battery when there is
  one (the system capability), "Turn screen off after" (Never, 1 min ... 1 hour; default 10 min)
  and "Suspend after" (Never, 5 min ... 2 hours; default never), a click stepping through the
  choices, and Suspend / Restart / Power off now. Restart and Power off need a second click.
- New `power` capability (screen-off-minutes, suspend-minutes and setters, suspend, power-off,
  reboot), served by the wasmer stub (host ints 5/6, requests 13-15) and host-wasm
  (publish-power, set-screen-off / set-suspend-after / power-action).
- The timeouts are kept in state.edn as :power {:screen-off N :suspend N} when not the defaults.
- Login session only (compositor/host/power.mbt): input restarts the idle time; power_tick,
  run every pass of the main loop (an idle loop still wakes about once a second), turns the
  screen off through DPMS and back on at the next input, and suspends once per idle spell.
  Monotonic time stops in suspend, so a resumed machine waits for input and a new idle spell.
  Suspend, restart and power off run `systemctl <verb> || loginctl <verb>` (logind, so its
  inhibitors and polkit rules apply); a nested window only shows a notice.
- Display: chandra:host/display set-power and Kms::set_power; the shared C shim sets the
  connector's legacy DPMS property, shows no frames while off (frame_ready false, so the idle
  loop still sleeps), redoes the modeset when turned on, and keeps the screen off across a VT
  switch.
- components/screenshot bindings regenerated (they were stale: the module didn't build).
- Tests: idle timer sequence, the Power tab driving the shell (timeouts, two-click power off),
  the state.edn round trip, a host-wasm test of the tab, and logind commands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hgxf6exeYrDGcy4WW3
2026-11-25 19:37:21 +00:00

467 lines
14 KiB
MoonBit

///| Implementation of `chandra:desktop/plasmoid` for the settings module: the panel's "Settings"
/// button. Its popup has three tabs, split by where things live:
///
/// - Session: the session settings the shell offers (`session-settings.settings`: reopen windows
/// at login, remember window positions, dark mode), each with an on/off switch;
/// - Desktop: what is drawn on the desktop. Today that is the wallpaper (`wallpaper.wallpapers`,
/// the current one ticked); modules can't be placed on the desktop yet, so when they can they
/// get their own section here;
/// - Panel: the modules in the panel (click to remove), every module found on the search path
/// (click to add another instance) and "Browse..." (`modules`). The panel menu's "Modules..."
/// opens this tab;
/// - Power: the battery (`system`, when there is one), how long without input before the screen
/// turns off and before the computer suspends (a click steps through the choices), and Suspend,
/// Restart and Power off now (`power`); Restart and Power off ask for a second click.
///
/// Clicking a row acts at once and the popup stays open. Up/Down move the highlight, Left/Right
/// (or 1-4) switch tabs, Enter or Space acts on the highlighted row, Escape or a click
/// elsewhere closes. The shell remembers settings and wallpaper in state.edn, the panel in
/// panel.edn.
///| Popup geometry: the tab bar, then a column of rows, with a margin around them.
let popup_w : Int = 300
///|
let margin : Int = 6
///|
let tab_h : Int = 30
///| Top of the first row (below the tab bar).
let rows_top : Int = 40
///|
let row_h : Int = 28
///| The switch at the right end of a setting row.
let switch_w : Int = 34
///|
let switch_h : Int = 18
///|
let tab_names : Array[String] = ["Session", "Desktop", "Panel", "Power"]
///| A popup row.
enum Row {
Heading(String)
Setting(Int, String)
Wallpaper(Int, String)
/// A loaded panel instance: slot and name.
Loaded(String, String)
/// A module on the search path: path, name and how many instances are loaded.
Available(String, String, UInt)
Browse
/// A line of text (the battery).
Info(String)
/// A timeout that steps through `choices`: 0 screen off, 1 suspend.
Timeout(Int, String)
/// Suspend (0), power off (1) or restart (2) now.
PowerNow(Int, String)
}
///| The timeout choices, in minutes (0: never).
let screen_off_choices : Array[Int] = [0, 1, 2, 5, 10, 15, 30, 60]
///|
let suspend_choices : Array[Int] = [0, 5, 10, 15, 30, 60, 120]
///| The power action waiting for its confirming second click (-1: none).
let armed : Ref[Int] = { val: -1 }
///| "Never", "5 min", "1 hour", "2 hours".
fn minutes_label(m : Int) -> String {
if m <= 0 {
"Never"
} else if m % 60 == 0 {
let h = m / 60
h.to_string() + (if h == 1 { " hour" } else { " hours" })
} else {
m.to_string() + " min"
}
}
///| The choice after `cur` in `choices` (wrapping round to the first).
fn next_choice(choices : Array[Int], cur : Int) -> Int {
for c in choices {
if c > cur {
return c
}
}
choices[0]
}
///| Whether the popup is open, its tab, and which row is highlighted (-1: none).
let open_ : Ref[Bool] = { val: false }
///|
let tab : Ref[Int] = { val: 0 }
///|
let hover : Ref[Int] = { val: -1 }
///| The rows of the current tab, read live from the host (loading a module changes them).
fn rows() -> Array[Row] {
let rs : Array[Row] = []
match tab.val {
0 => {
let names = @settings.settings()
for i = 0; i < names.length(); i = i + 1 {
rs.push(Setting(i, names[i]))
}
}
1 => {
rs.push(Heading("Wallpaper"))
let names = @wallpaper.wallpapers()
for i = 0; i < names.length(); i = i + 1 {
rs.push(Wallpaper(i, names[i]))
}
}
3 => {
let snap = @system.read()
if snap.battery_percent >= 0 {
rs.push(Heading("Battery"))
let state = if snap.charging { " (charging)" } else { "" }
rs.push(Info(snap.battery_percent.to_string() + "%" + state))
}
rs.push(Heading("When idle"))
rs.push(Timeout(0, "Turn screen off after"))
rs.push(Timeout(1, "Suspend after"))
rs.push(Heading("Now"))
rs.push(PowerNow(0, "Suspend"))
rs.push(PowerNow(2, "Restart"))
rs.push(PowerNow(1, "Power off"))
}
_ => {
rs.push(Heading("In the panel (click to remove)"))
for inst in @modules.loaded() {
rs.push(Loaded(inst.slot, inst.name))
}
rs.push(Heading("Add to the panel"))
for m in @modules.available() {
rs.push(Available(m.path, m.name, m.loaded))
}
rs.push(Browse)
}
}
rs
}
///| Whether row `i` of `rs` can be highlighted and acted on (headings can't).
fn active(rs : Array[Row], i : Int) -> Bool {
i >= 0 && i < rs.length() && !(rs[i] is (Heading(_) | Info(_)))
}
///| First active row of `rs`, or -1.
fn first_active(rs : Array[Row]) -> Int {
for i = 0; i < rs.length(); i = i + 1 {
if active(rs, i) {
return i
}
}
-1
}
///| Tab under a popup-local point, or -1.
fn tab_at(x : Int, y : Int) -> Int {
if y < margin || y >= margin + tab_h || x < margin || x >= popup_w - margin {
return -1
}
let i = (x - margin) / tab_w()
if i < tab_names.length() { i } else { -1 }
}
///|
fn tab_w() -> Int {
(popup_w - 2 * margin) / tab_names.length()
}
///| Row under a popup-local point, or -1 (headings count as none).
fn row_at(rs : Array[Row], x : Int, y : Int) -> Int {
if x < margin || x >= popup_w - margin || y < rows_top {
return -1
}
let i = (y - rows_top) / row_h
if active(rs, i) { i } else { -1 }
}
///|
fn popup_h(n : Int) -> Int {
rows_top + n * row_h + margin
}
///|
fn set_tab(i : Int) -> Unit {
tab.val = i
armed.val = -1
hover.val = first_active(rows())
}
///|
fn close() -> Unit {
open_.val = false
armed.val = -1
hover.val = -1
}
///| Act on row `i`: flip a setting, show a wallpaper, unload or load a module, or browse.
fn act(rs : Array[Row], i : Int) -> Unit {
if !active(rs, i) {
return
}
let was_armed = armed.val
armed.val = -1
match rs[i] {
Timeout(0, _) => @power.set_screen_off_minutes(next_choice(screen_off_choices, @power.screen_off_minutes().reinterpret_as_int()).reinterpret_as_uint())
Timeout(_, _) => @power.set_suspend_minutes(next_choice(suspend_choices, @power.suspend_minutes().reinterpret_as_int()).reinterpret_as_uint())
PowerNow(0, _) => @power.suspend()
PowerNow(a, _) =>
// Restart and power off take a second click on the same row.
if was_armed == a {
if a == 1 {
@power.power_off()
} else {
@power.reboot()
}
} else {
armed.val = a
}
Info(_) => ()
Setting(k, _) => {
let idx = k.reinterpret_as_uint()
@settings.set_setting(idx, !@settings.is_on(idx))
}
Wallpaper(k, _) => @wallpaper.set_wallpaper(k.reinterpret_as_uint())
Loaded(slot, _) => @modules.unload_instance(slot)
Available(path, _, _) => @modules.load_module(path)
Browse => @modules.browse()
Heading(_) => ()
}
}
///| Move the highlight by `step` rows, skipping headings; stays put at either end.
fn move_hover(step : Int) -> Unit {
let rs = rows()
let mut i = hover.val + step
while i >= 0 && i < rs.length() {
if active(rs, i) {
hover.val = i
return
}
i = i + step
}
}
///|
pub fn info() -> PlasmoidInfo {
{
id: "org.chandra.session-settings",
name: "Settings",
preferred: { w: 92, h: 34 },
min_width: 34,
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 small switch glyph (track and knob), then the label when there is room.
let gx = 8
let gy = h / 2
let ops = [
@ui.fill_rect(@ui.rect(0, 0, w, h), bg, radius=r),
@ui.stroke_rect(@ui.rect(gx, gy - 6, 20, 12), fg, width=1, radius=6),
@ui.circle(gx + 14, gy, 4, fg),
]
if w >= 70 {
ops.push(@ui.text(gx + 26, gy, "Settings", fs - 2, fg))
}
ops
}
///|
pub fn popup() -> PopupSurface? {
if !open_.val {
return None
}
let t = @theme.get_tokens()
let p = t.palette
let rs = rows()
let w = popup_w
let h = popup_h(rs.length())
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), p.surface_popup, radius=r),
@ui.stroke_rect(@ui.rect(0, 0, w, h), p.border, width=1, radius=r),
]
// Tab bar: the current tab filled with the accent.
let tw = tab_w()
for i = 0; i < tab_names.length(); i = i + 1 {
let x = margin + i * tw
let cur = i == tab.val
if cur {
ops.push(@ui.fill_rect(@ui.rect(x + 1, margin, tw - 2, tab_h - 2), p.accent, radius=r))
}
let fg = if cur { p.accent_foreground } else { p.foreground_muted }
ops.push(@ui.text(x + tw / 2, margin + (tab_h - 2) / 2, tab_names[i], fs - 2, fg, align=CENTER, bold=cur))
}
ops.push(@ui.line(margin, rows_top - 3, w - margin, rows_top - 3, p.border, width=1))
let cur_wall = @wallpaper.current_wallpaper().reinterpret_as_int()
for i = 0; i < rs.length(); i = i + 1 {
let y = rows_top + i * row_h
let mid = y + (row_h - 2) / 2
let hot = i == hover.val
// Hover: a soft fill behind switches, the accent behind list rows.
let list_fg = if hot { p.accent_foreground } else { p.foreground }
match rs[i] {
Heading(_) => ()
Setting(_, _) =>
if hot {
ops.push(@ui.fill_rect(@ui.rect(margin, y, w - 2 * margin, row_h - 2), p.surface_hover, radius=r))
}
_ =>
if hot {
ops.push(@ui.fill_rect(@ui.rect(margin, y, w - 2 * margin, row_h - 2), p.accent, radius=r))
}
}
match rs[i] {
Heading(label) => ops.push(@ui.text(margin + 8, mid, label, fs - 3, p.foreground_muted, bold=true))
Setting(k, name) => {
ops.push(@ui.text(margin + 10, mid, name, fs - 1, p.foreground))
// The switch: an accent track with the knob on the right when on, a muted outline when off.
let on = @settings.is_on(k.reinterpret_as_uint())
let sx = w - margin - 10 - switch_w
let sy = mid - switch_h / 2
let knob = switch_h / 2 - 3
if on {
ops.push(@ui.fill_rect(@ui.rect(sx, sy, switch_w, switch_h), p.accent, radius=switch_h / 2))
ops.push(@ui.circle(sx + switch_w - switch_h / 2, mid, knob, p.accent_foreground))
} else {
ops.push(@ui.stroke_rect(@ui.rect(sx, sy, switch_w, switch_h), p.foreground_muted, width=1, radius=switch_h / 2))
ops.push(@ui.circle(sx + switch_h / 2, mid, knob, p.foreground_muted))
}
}
Wallpaper(k, name) => {
if k == cur_wall {
// A tick: two strokes.
ops.push(@ui.line(margin + 8, mid, margin + 11, mid + 4, list_fg, width=2))
ops.push(@ui.line(margin + 11, mid + 4, margin + 18, mid - 4, list_fg, width=2))
}
ops.push(@ui.text(margin + 26, mid, name, fs - 1, list_fg, bold=k == cur_wall))
}
Loaded(_, name) => {
ops.push(@ui.text(margin + 10, mid, name, fs - 1, list_fg))
ops.push(@ui.text(w - margin - 10, mid, "x", fs - 1, list_fg, align=RIGHT, bold=true))
}
Available(_, name, n) => {
ops.push(@ui.text(margin + 10, mid, name, fs - 1, list_fg))
if n > 0U {
ops.push(@ui.text(w - margin - 10, mid, "x" + n.to_string(), fs - 3, list_fg, align=RIGHT))
}
}
Browse => ops.push(@ui.text(margin + 10, mid, "Browse...", fs - 1, list_fg, bold=true))
Info(text) => ops.push(@ui.text(margin + 10, mid, text, fs - 1, p.foreground))
Timeout(k, label) => {
let m = if k == 0 { @power.screen_off_minutes() } else { @power.suspend_minutes() }
ops.push(@ui.text(margin + 10, mid, label, fs - 1, list_fg))
ops.push(@ui.text(w - margin - 10, mid, minutes_label(m.reinterpret_as_int()), fs - 1, list_fg, align=RIGHT, bold=true))
}
PowerNow(a, label) => {
let text = if armed.val == a { "Click again to " + label.to_lower() } else { label }
ops.push(@ui.text(margin + 10, mid, text, fs - 1, list_fg, bold=armed.val == a))
}
}
}
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
set_tab(tab.val) // reopens on the tab it was left on
}
true
}
(POPUP, PointerMove(p)) => {
let i = row_at(rows(), p.x, p.y)
if i != hover.val && i >= 0 {
hover.val = i
true
} else {
false
}
}
(POPUP, PointerDown(p)) => {
let ti = tab_at(p.x, p.y)
if ti >= 0 {
if ti != tab.val {
set_tab(ti)
}
return true
}
let rs = rows()
let i = row_at(rs, p.x, p.y)
if i >= 0 {
act(rs, i)
// A module list changes length: keep the highlight on a real row.
let after = rows()
hover.val = if active(after, i) { i } else { first_active(after) }
}
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" || k == " " || k == "Space" {
let rs = rows()
act(rs, hover.val)
let after = rows()
if !active(after, hover.val) {
hover.val = first_active(after)
}
true
} else if k == "ArrowDown" || k == "Down" {
move_hover(1)
true
} else if k == "ArrowUp" || k == "Up" {
move_hover(-1)
true
} else if k == "ArrowRight" || k == "Right" {
set_tab((tab.val + 1) % tab_names.length())
true
} else if k == "ArrowLeft" || k == "Left" {
set_tab((tab.val + tab_names.length() - 1) % tab_names.length())
true
} else if k == "1" || k == "2" || k == "3" || k == "4" {
set_tab(k[0].to_int() - '1'.to_int())
true
} else {
false
}
_ => false
}
}