Files
chandra/components/session-settings/gen/interface/chandra/desktop/plasmoid/stub.mbt
T
Claude 932fca627e
CI / test (push) Successful in 2m6s
Face unlock: a Howdy-style PAM module, shown in the password dialog and set up in Settings
- face/pam_chandra_face.so (`-auth sufficient`): runs `chandra-face compare` as root with a clean
  environment, forwards what it says as PAM_TEXT_INFO / PAM_ERROR_MSG, and maps its exit status
  to success / auth error / unavailable, so the password always still works. Skips remote
  sessions (PAM_RHOST, SSH_CONNECTION), a closed lid and users without faces; refuses a helper
  that isn't root's; kills one that hangs (timeout + 6 s).
- face/chandra-face (Python + OpenCV): YuNet finds the face, SFace embeds it, cosine similarity
  against the enrolled embeddings (threshold 0.42, two matching frames, dark IR warm-up frames
  skipped, optional require_ir). enroll / list / remove / clear / test / status / pam
  enable|disable / fetch-models (sha256-pinned opencv_zoo models). Embeddings only, in
  /var/lib/chandra-face/USER/faces.json (root, 0600); labels in faces.list (0644). Store, config
  and models must be root's and not writable by others. Root commands re-run through run0 (or
  pkexec), so polkit asks first. Settings in /etc/chandra/face.conf.
- `just install-face [--pam]`, `just uninstall-face [--purge]` (face/install.py; uninstall takes
  the module out of every PAM service first). `pam enable` inserts before the first auth or
  @include line (Debian's sudo has only @include common-auth), keeping a .bak.
- Password dialog: a face row ("Looking for your face…" with a sweeping ring, "Face
  recognized", or why not, "Type your password instead"); typing and Enter during the scan go in
  when PAM asks for the password. Howdy's pam_howdy.so messages are understood too.
- Settings > Session > Face unlock: "Use my face for passwords" (polkit-1 + sudo), the enrolled
  faces (Remove takes a second click), "Add a face…", and the tool's progress. New `face-unlock`
  capability: tables 24 (status) and 25 (faces), request 44, in the wasmer/wasmtime stub, the Rust
  host, the MoonBit wasmhost and chandra:host/plasmoids. compositor/shell/face.mbt runs the tool
  on a socket so the compositor never waits.
- Tests: face/test_face.py (fake camera of PGM frames + fake recognizer; --pam builds the module
  and runs it through pamtester), dialog states and message parsing (polkit_test), the dialog via
  a mock helper (auth_test, shots 14/15), Settings against a fake chandra-face (face_test).
  scripts/test.sh runs them (compositor/polkit was missing from its native list), CI installs
  libpam0g-dev + pamtester. Also regenerated two stale desktop-module bindings (doc comments).
- docs/face-unlock.md, including the limits of 2D face recognition.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hhiJfJNAYqWzQiuKMf
2026-11-26 01:19:29 +00:00

1017 lines
35 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 fills the desktop (`wallpaper.desktops`: the wallpaper alone, the clock card,
/// or an installed desktop module, see docs/desktop-modules.md) and the wallpaper
/// (`wallpaper.wallpapers`), the current one of each ticked;
/// - Panel: the modules in the panel in panel order (left to right), each with move-up/down and
/// remove buttons; then the modules that can be added, grouped by kind with a short description
/// and an Add button (a module already in the panel is left out unless more than one makes
/// sense, like a clock); and a "Browse for a module file..." button (`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;
/// - Shortcuts: the compositor's keyboard shortcuts (`shortcuts`) with their combos. Click one (or
/// Enter) and press the new keys: the compositor takes that combo for it (Escape cancels,
/// Backspace unbinds; clicking the row again also cancels). "Reset" at the end of a changed
/// row, or Backspace on the highlighted row, puts it back; "Reset all to defaults" does every
/// one. Combos two shortcuts share are shown in red and listed under the shortcuts;
/// - Apps: the default app for each kind of thing (`default-apps`): web browser, email, file
/// manager, terminal, text editor, image viewer, video and music player, PDF and documents,
/// archives. Each row shows the current one with its icon; click it to list the installed apps
/// that can take its place, click one of those to make it the default (the list folds up
/// again). A chosen app that has since been uninstalled is named in red under its row.
///
/// Clicking a row acts at once and the popup stays open; a row in the panel list only acts through
/// its buttons, so a stray click doesn't remove anything. Up/Down move the highlight, Left/Right
/// (or 1-6) switch tabs, Enter or Space acts on the highlighted row, Backspace or Delete removes
/// the highlighted panel module (or resets the highlighted shortcut) and PageUp/PageDown move it,
/// Escape or a click elsewhere closes. The shell remembers settings, wallpaper and shortcuts 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 = 390
///|
let margin : Int = 6
///|
let tab_h : Int = 30
///| Top of the first row (below the tab bar). Rows are `row_h` tall except on the Panel tab (see
/// `row_height`).
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", "Shortcuts", "Apps"]
///| The Apps tab's category whose choices are listed (-1: none).
let expanded : Ref[Int] = { val: -1 }
///| Size of the app icons on the Apps tab.
let icon_size : Int = 18
///| A popup row.
enum Row {
Heading(String)
Setting(Int, String)
Wallpaper(Int, String)
/// What fills the desktop: wallpaper only, the clock card, or a desktop module.
DesktopChoice(Int, String)
/// A group of modules in the Add list ("Clocks").
Category(String)
/// A loaded panel instance: slot, name, its place in the panel and how many there are.
Loaded(String, String, Int, Int)
/// A module that can be added: path, name, description and how many instances are loaded.
Available(String, String, String, UInt)
/// Every module is in the panel already.
NothingToAdd
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)
/// A keyboard shortcut: its index and what the host says about it.
Shortcut(Int, @shortcuts.Shortcut)
/// "Reset all to defaults" (the shortcuts).
ResetShortcuts
/// A kind of default app: its index, name, the current default among its choices (None: not
/// one of them), the current default's name ("" for none) and how many apps can take it.
AppKind(Int, String, @default_apps.AppChoice?, String, Int)
/// One of a kind's apps, listed under it while it is expanded: the kind, its index there, the app.
AppOption(Int, Int, @default_apps.AppChoice)
/// Face unlock's switch: the password dialog and sudo try the face first.
FaceUse
/// An enrolled face: its index, label and when it was added (a second click removes it).
FaceRow(Int, String, String)
/// "Add a face..."
FaceAdd
}
///| An on/off switch at the right end of a row centred on `mid`: an accent track with the knob
/// on the right when on, a muted outline when off.
fn switch_ops(w : Int, mid : Int, on : Bool, p : @theme.Palette, ops : Array[@types.DrawOp]) -> Unit {
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))
}
}
///| Width of the "Reset" button at the end of a changed shortcut's row.
let reset_w : Int = 50
///| Conflicting combos are drawn in this red.
fn conflict_color() -> @types.Color {
@ui.color(220, 72, 64, 255)
}
///| 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 groups of the Add list, in order.
let categories : Array[String] = ["Apps and windows", "System", "Clocks", "Other"]
///| Which group a module (by id, its file stem) belongs to; modules we don't know go in "Other".
fn category_of(id : String) -> String {
match id {
"launcher" | "dock" | "layout" => "Apps and windows"
"session-settings" | "network" | "battery" | "screenshot" => "System"
"clock-digital" | "clock-binary" | "moon-disc" => "Clocks"
_ => "Other"
}
}
///| One line on what a module does ("" for modules we don't know).
fn description_of(id : String) -> String {
match id {
"launcher" => "Start apps; reload, log out or power off"
"dock" => "Pinned apps and your open windows"
"layout" => "Switch tiling and floating"
"session-settings" => "This settings window"
"network" => "Wi-Fi and wired network"
"battery" => "Charge and time left"
"screenshot" => "Capture or record the screen"
"clock-digital" => "The time, 12 or 24-hour"
"clock-binary" => "The time in binary"
"moon-disc" => "Tonight's moon phase"
"inspector" => "Inspect the shell's interface"
_ => ""
}
}
///| Whether more than one of a module makes sense in the panel; the others leave the Add list
/// once they are in the panel.
fn allows_many(id : String) -> Bool {
id.has_prefix("clock-")
}
///| 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]))
}
face_rows(rs)
}
1 => {
rs.push(Heading("On the desktop"))
let desks = @wallpaper.desktops()
for i = 0; i < desks.length(); i = i + 1 {
rs.push(DesktopChoice(i, desks[i]))
}
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"))
}
5 => {
let cats = @default_apps.categories()
let apps = @default_apps.apps()
for c = 0; c < cats.length(); c = c + 1 {
let mine = apps.filter(a => a.category.reinterpret_as_int() == c)
let cur = mine.iter().find_first(a => a.current)
rs.push(AppKind(c, cats[c].name, cur, cats[c].current, mine.length()))
if cats[c].missing != "" {
rs.push(Info("! " + cats[c].missing + " is not installed any more"))
}
if expanded.val == c {
for k, a in mine {
rs.push(AppOption(c, k, a))
}
}
}
}
4 => {
rs.push(Heading("Click one, then press its new keys"))
let list = @shortcuts.shortcuts()
for i = 0; i < list.length(); i = i + 1 {
rs.push(Shortcut(i, list[i]))
}
// Each shared combo once, under the list: which shortcut gets it.
for i = 0; i < list.length(); i = i + 1 {
let sc = list[i]
if sc.conflict != "" && !list[:i].iter().any(e => e.binding == sc.binding) {
rs.push(Info("! " + sc.binding + " is used twice: " + sc.name + " wins"))
}
}
rs.push(ResetShortcuts)
}
_ => {
rs.push(Heading("In the panel, left to right"))
let loaded = @modules.loaded()
for i = 0; i < loaded.length(); i = i + 1 {
rs.push(Loaded(loaded[i].slot, loaded[i].name, i, loaded.length()))
}
rs.push(Heading("Add to the panel"))
let avail = @modules.available()
let mut any = false
for cat in categories {
let mut first = true
for m in avail {
if category_of(m.id) != cat || (m.loaded > 0U && !allows_many(m.id)) {
continue
}
if first {
rs.push(Category(cat))
first = false
}
rs.push(Available(m.path, m.name, description_of(m.id), m.loaded))
any = true
}
}
if !any {
rs.push(NothingToAdd)
}
rs.push(Browse)
}
}
rs
}
///| Face unlock's rows on the Session tab (docs/face-unlock.md): the switch, the enrolled faces,
/// "Add a face..." and what the last change said. Changes ask for the password first.
fn face_rows(rs : Array[Row]) -> Unit {
let st = @face_unlock.status()
rs.push(Heading("Face unlock"))
if !st.installed {
rs.push(Info("Not installed: see docs/face-unlock.md"))
return
}
if st.problem != "" {
rs.push(Info("! " + st.problem))
}
if st.howdy != "" {
rs.push(Info("Howdy also unlocks " + st.howdy))
}
rs.push(FaceUse)
for i, f in @face_unlock.faces() {
rs.push(FaceRow(i, f.label, f.added))
}
rs.push(FaceAdd)
if st.message != "" {
rs.push(Info(st.message))
}
}
///| `armed` value for "click again to remove" on face `i`.
fn face_armed(i : Int) -> Int {
100 + i
}
///| 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(_) | Category(_) | NothingToAdd))
}
///| 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()
}
///| Height of a row: the Panel tab's rows vary (two-line Add rows, slim group labels); every
/// other row is `row_h`.
fn row_height(r : Row) -> Int {
match r {
Loaded(_, _, _, _) => loaded_h
Category(_) => category_h
Available(_, _, _, _) => available_h
Browse => browse_h
_ => row_h
}
}
///|
let loaded_h : Int = 30
///|
let category_h : Int = 22
///|
let available_h : Int = 42
///|
let browse_h : Int = 44
///| Top of each row, popup-local.
fn row_tops(rs : Array[Row]) -> Array[Int] {
let tops = []
let mut y = rows_top
for r in rs {
tops.push(y)
y = y + row_height(r)
}
tops
}
///| 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 tops = row_tops(rs)
for i = 0; i < rs.length(); i = i + 1 {
if y >= tops[i] && y < tops[i] + row_height(rs[i]) {
return if active(rs, i) { i } else { -1 }
}
}
-1
}
///|
fn popup_h(rs : Array[Row]) -> Int {
let mut h = rows_top + margin
for r in rs {
h = h + row_height(r)
}
h
}
///| The small buttons at the right end of a panel-list row, popup-local: move up, move down,
/// remove.
let btn_size : Int = 24
///|
fn loaded_button(k : Int, top : Int) -> @types.Rect {
// Remove sits apart from the two move buttons.
let right = popup_w - margin - 6
let x = match k {
2 => right - btn_size
1 => right - btn_size - 12 - btn_size
_ => right - btn_size - 12 - 2 * btn_size - 2
}
@ui.rect(x, top + (loaded_h - 2 - btn_size) / 2, btn_size, btn_size)
}
///| Which button of a panel-list row is under popup-local `x`/`y` (0 up, 1 down, 2 remove), or -1.
fn loaded_button_at(top : Int, x : Int, y : Int) -> Int {
for k = 0; k < 3; k = k + 1 {
let b = loaded_button(k, top)
if x >= b.x && x < b.x + @ui.rect_w(b) && y >= b.y && y < b.y + @ui.rect_h(b) {
return k
}
}
-1
}
///| The button under the pointer in the highlighted row (-1: none).
let hover_btn : Ref[Int] = { val: -1 }
///|
fn set_tab(i : Int) -> Unit {
cancel_capture()
expanded.val = -1
if i == 0 {
// Faces may have been added or removed elsewhere (chandra-face, another session).
@face_unlock.refresh()
}
if i == 5 && tab.val != 5 {
// Another program may have changed a default since the tab was last open.
@default_apps.refresh()
}
tab.val = i
armed.val = -1
hover.val = first_active(rows())
hover_btn.val = -1
}
///|
fn close() -> Unit {
cancel_capture()
open_.val = false
armed.val = -1
hover.val = -1
}
///| Stop a shortcut capture this module started, if one is waiting.
fn cancel_capture() -> Unit {
if tab.val == 4 && @shortcuts.shortcuts().iter().any(s => s.capturing) {
@shortcuts.cancel_capture()
}
}
///| Whether a popup-local x is on the "Reset" button of a changed shortcut row.
fn on_reset(sc : @shortcuts.Shortcut, x : Int) -> Bool {
sc.binding != sc.default_binding && !sc.capturing && x >= popup_w - margin - reset_w
}
///| Act on row `i`: flip a setting, show a wallpaper, load a module, or browse. A panel-list row
/// acts only through button `btn` (0 move up, 1 move down, 2 remove).
fn act(rs : Array[Row], i : Int, btn~ : Int = -1, x~ : Int = 0) -> 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(_) => ()
FaceUse =>
if !@face_unlock.status().busy {
@face_unlock.set_in_use(!@face_unlock.status().in_use)
}
FaceRow(k, _, _) =>
if was_armed == face_armed(k) {
@face_unlock.remove(k.reinterpret_as_uint())
} else {
armed.val = face_armed(k)
}
FaceAdd => if !@face_unlock.status().busy { @face_unlock.enroll() }
Shortcut(k, sc) =>
if sc.capturing {
@shortcuts.cancel_capture()
} else if on_reset(sc, x) {
@shortcuts.reset(k.reinterpret_as_uint())
} else {
@shortcuts.capture(k.reinterpret_as_uint())
}
ResetShortcuts => @shortcuts.reset_all()
AppKind(c, _, _, _, n) => expanded.val = if expanded.val == c || n == 0 { -1 } else { c }
AppOption(c, k, _) => {
@default_apps.set_default(c.reinterpret_as_uint(), k.reinterpret_as_uint())
expanded.val = -1
}
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())
DesktopChoice(k, _) => @wallpaper.set_desktop(k.reinterpret_as_uint())
Loaded(slot, _, idx, n) =>
match btn {
0 => if idx > 0 { @modules.move_instance(slot, -1) }
1 => if idx < n - 1 { @modules.move_instance(slot, 1) }
2 => @modules.unload_instance(slot)
_ => ()
}
Available(path, _, _, _) => @modules.load_module(path)
Browse => @modules.browse()
Heading(_) | Category(_) | NothingToAdd => ()
}
}
///| An app's icon with its left edge at `x`, centred on `mid`: the host's image, or a rounded
/// square with the name's first letter.
fn app_icon_ops(
x : Int,
mid : Int,
a : @default_apps.AppChoice,
fg : @types.Color,
) -> Array[@types.DrawOp] {
let r = @ui.rect(x, mid - icon_size / 2, icon_size, icon_size)
if a.icon != 0U {
return [Image({ rect: r, image: a.icon })]
}
let glyph = match a.name.to_array().get(0) {
Some(ch) => ch.to_string().to_upper()
None => "?"
}
[
@ui.stroke_rect(r, @ui.with_alpha(fg, 120), width=1, radius=4),
@ui.text(x + icon_size / 2, mid, glyph, 10, fg, align=CENTER, bold=true),
]
}
///| 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)
let tops = row_tops(rs)
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()
let cur_desk = if tab.val == 1 { @wallpaper.current_desktop().reinterpret_as_int() } else { -1 }
for i = 0; i < rs.length(); i = i + 1 {
let y = tops[i]
let rh = row_height(rs[i])
let mid = y + (rh - 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(_) | Category(_) | NothingToAdd | Browse => ()
// Rows with their own buttons get the soft fill, so the buttons stand out.
Setting(_, _) | FaceUse | Loaded(_, _, _, _) | Available(_, _, _, _) =>
if hot {
ops.push(@ui.fill_rect(@ui.rect(margin, y, w - 2 * margin, rh - 2), p.surface_hover, radius=r))
}
Info(_) => ()
_ =>
if hot {
ops.push(@ui.fill_rect(@ui.rect(margin, y, w - 2 * margin, rh - 2), p.accent, radius=r))
}
}
match rs[i] {
Heading(label) => ops.push(@ui.text(margin + 8, mid + 2, label, fs - 1, p.foreground, bold=true))
Category(label) => ops.push(@ui.text(margin + 10, mid, label, fs - 3, p.foreground_muted, bold=true))
NothingToAdd => ops.push(@ui.text(margin + 10, mid, "Everything is in the panel", fs - 2, p.foreground_muted))
Setting(k, name) => {
ops.push(@ui.text(margin + 10, mid, name, fs - 1, p.foreground))
switch_ops(w, mid, @settings.is_on(k.reinterpret_as_uint()), p, ops)
}
FaceUse => {
let st = @face_unlock.status()
ops.push(@ui.text(margin + 10, mid, "Use my face for passwords", fs - 1, p.foreground))
switch_ops(w, mid, st.in_use, p, ops)
}
FaceRow(k, label, added) => {
// A face glyph, the label and date; "Remove" at the right (a second click removes).
let gx = margin + 18
ops.push(@ui.circle(gx, mid, 7, list_fg, filled=false))
ops.push(@ui.circle(gx - 3, mid - 2, 1, list_fg))
ops.push(@ui.circle(gx + 3, mid - 2, 1, list_fg))
ops.push(@ui.line(gx - 3, mid + 3, gx + 3, mid + 3, list_fg, width=1))
ops.push(@ui.text(margin + 32, mid, label, fs - 1, list_fg))
let lw = @ui.text_width(label, fs - 1)
ops.push(@ui.text(margin + 40 + lw, mid, "added " + added, fs - 3, if hot { list_fg } else { p.foreground_muted }))
let armed_here = armed.val == face_armed(k)
let rlabel = if armed_here { "Click again to remove" } else { "Remove" }
ops.push(@ui.text(w - margin - 10, mid, rlabel, fs - 2, if hot { list_fg } else { p.accent }, align=RIGHT, bold=armed_here))
}
FaceAdd => {
let busy = @face_unlock.status().busy
ops.push(@ui.text(margin + 10, mid, if busy { "Working…" } else { "Add a face…" }, fs - 1, if busy && !hot { p.foreground_muted } else { list_fg }, bold=true))
}
DesktopChoice(k, name) => {
if k == cur_desk {
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_desk))
}
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, idx, n) => {
ops.push(@ui.text(margin + 10, mid, name, fs - 1, p.foreground))
for k = 0; k < 3; k = k + 1 {
let enabled = match k {
0 => idx > 0
1 => idx < n - 1
_ => true
}
let b = loaded_button(k, y)
let bx = b.x
let by = b.y
let cx = bx + btn_size / 2
let cy = by + btn_size / 2
let btn_hot = hot && hover_btn.val == k && enabled
if btn_hot {
ops.push(@ui.fill_rect(b, p.accent, radius=r))
}
let ic = if btn_hot {
p.accent_foreground
} else if !enabled {
@ui.with_alpha(p.foreground_muted, 40)
} else if hot {
p.foreground
} else {
p.foreground_muted
}
match k {
0 => {
ops.push(@ui.line(cx - 5, cy + 2, cx, cy - 3, ic, width=2))
ops.push(@ui.line(cx, cy - 3, cx + 5, cy + 2, ic, width=2))
}
1 => {
ops.push(@ui.line(cx - 5, cy - 2, cx, cy + 3, ic, width=2))
ops.push(@ui.line(cx, cy + 3, cx + 5, cy - 2, ic, width=2))
}
_ => {
ops.push(@ui.line(cx - 4, cy - 4, cx + 4, cy + 4, ic, width=2))
ops.push(@ui.line(cx - 4, cy + 4, cx + 4, cy - 4, ic, width=2))
}
}
}
}
Available(_, name, desc, n) => {
if desc == "" {
ops.push(@ui.text(margin + 10, mid, name, fs - 1, p.foreground))
} else {
ops.push(@ui.text(margin + 10, y + 13, name, fs - 1, p.foreground))
ops.push(@ui.text(margin + 10, y + 29, desc, fs - 3, p.foreground_muted))
}
// The Add button: outlined, filled with the accent while its row is highlighted.
let label = if n > 0U { "Add another" } else { "Add" }
let bw = @ui.text_width(label, fs - 2) + 20
let bh = 24
let bx = w - margin - 8 - bw
let by = mid - bh / 2
if hot {
ops.push(@ui.fill_rect(@ui.rect(bx, by, bw, bh), p.accent, radius=bh / 2))
} else {
ops.push(@ui.stroke_rect(@ui.rect(bx, by, bw, bh), p.accent, width=1, radius=bh / 2))
}
let bfg = if hot { p.accent_foreground } else { p.accent }
ops.push(@ui.text(bx + bw / 2, mid, label, fs - 2, bfg, align=CENTER, bold=true))
}
Browse => {
// A secondary button below the list: an outline, softly filled while highlighted.
ops.push(@ui.line(margin + 4, y + 2, w - margin - 4, y + 2, p.border, width=1))
let label = "Browse for a module file..."
let bw = @ui.text_width(label, fs - 2) + 28
let bh = 28
let bx = (w - bw) / 2
let by = y + 9
if hot {
ops.push(@ui.fill_rect(@ui.rect(bx, by, bw, bh), p.surface_hover, radius=r))
}
ops.push(@ui.stroke_rect(@ui.rect(bx, by, bw, bh), if hot { p.accent } else { p.border }, width=1, radius=r))
ops.push(@ui.text(w / 2, by + bh / 2, label, fs - 2, if hot { p.foreground } else { p.foreground_muted }, align=CENTER))
}
Info(text) =>
if text.has_prefix("! ") {
ops.push(@ui.text(margin + 10, mid, text, fs - 2, conflict_color()))
} else {
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))
}
Shortcut(_, sc) => {
ops.push(@ui.text(margin + 10, mid, sc.name, fs - 1, list_fg))
let changed = sc.binding != sc.default_binding
let right = w - margin - 10
let combo_x = if changed && !sc.capturing { right - reset_w } else { right }
if sc.capturing {
ops.push(@ui.text(right, mid, "Press keys...", fs - 2, if hot { list_fg } else { p.accent }, align=RIGHT, bold=true))
} else {
let (text, fg) = if sc.binding == "" {
("Unbound", if hot { list_fg } else { p.foreground_muted })
} else if sc.conflict != "" {
(sc.binding, conflict_color())
} else {
(sc.binding, list_fg)
}
ops.push(@ui.text(combo_x, mid, text, fs - 2, fg, align=RIGHT, bold=true))
if changed {
ops.push(@ui.text(right, mid, "Reset", fs - 3, if hot { list_fg } else { p.accent }, align=RIGHT))
}
}
}
ResetShortcuts => ops.push(@ui.text(margin + 10, mid, "Reset all to defaults", fs - 1, list_fg, bold=true))
AppKind(c, name, cur, cur_name, n) => {
let open = expanded.val == c
ops.push(@ui.text(margin + 10, mid, name, fs - 1, list_fg, bold=open))
// The current app at the right, then a chevron (down while its list is open).
let cx = w - margin - 14
let cfg = if hot { list_fg } else { p.foreground_muted }
if n > 0 {
if open {
ops.push(@ui.line(cx - 4, mid - 2, cx, mid + 2, cfg, width=2))
ops.push(@ui.line(cx, mid + 2, cx + 4, mid - 2, cfg, width=2))
} else {
ops.push(@ui.line(cx - 2, mid - 4, cx + 2, mid, cfg, width=2))
ops.push(@ui.line(cx + 2, mid, cx - 2, mid + 4, cfg, width=2))
}
}
let right = cx - 12
match cur {
Some(a) => {
let label = a.name
ops.push(@ui.text(right, mid, label, fs - 2, list_fg, align=RIGHT))
let tw = @ui.text_width(label, fs - 2)
ops.append(app_icon_ops(right - tw - 6 - icon_size, mid, a, list_fg))
}
None => {
let label = if cur_name != "" {
cur_name
} else if n == 0 {
"None installed"
} else {
"Not set"
}
ops.push(@ui.text(right, mid, label, fs - 2, if hot { list_fg } else { p.foreground_muted }, align=RIGHT))
}
}
}
AppOption(_, _, a) => {
let x = margin + 26
if a.current {
ops.push(@ui.line(margin + 10, mid, margin + 13, mid + 4, list_fg, width=2))
ops.push(@ui.line(margin + 13, mid + 4, margin + 20, mid - 4, list_fg, width=2))
}
ops.append(app_icon_ops(x, mid, a, list_fg))
ops.push(@ui.text(x + icon_size + 8, mid, a.name, fs - 1, list_fg, bold=a.current))
}
}
}
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 rs = rows()
let i = row_at(rs, p.x, p.y)
let b = if i >= 0 && rs[i] is Loaded(_, _, _, _) {
loaded_button_at(row_tops(rs)[i], p.x, p.y)
} else {
-1
}
if i >= 0 && (i != hover.val || b != hover_btn.val) {
hover.val = i
hover_btn.val = b
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 {
let btn = if rs[i] is Loaded(_, _, _, _) {
loaded_button_at(row_tops(rs)[i], p.x, p.y)
} else {
-1
}
act(rs, i, btn~, x=p.x)
// 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) }
// A moved module: keep the highlight on it, under the pointer's button.
match rs[i] {
Loaded(_, _, idx, n) =>
if btn == 0 && idx > 0 {
hover.val = i - 1
} else if btn == 1 && idx < n - 1 {
hover.val = i + 1
}
_ => ()
}
hover_btn.val = -1
}
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 == "Backspace" || k == "Delete" || k == "PageUp" || k == "PageDown" {
let rs = rows()
match (if active(rs, hover.val) { Some(rs[hover.val]) } else { None }) {
Some(Loaded(_, _, idx, n)) => {
let btn = if k == "PageUp" { 0 } else if k == "PageDown" { 1 } else { 2 }
act(rs, hover.val, btn~)
if btn == 0 && idx > 0 {
hover.val = hover.val - 1
} else if btn == 1 && idx < n - 1 {
hover.val = hover.val + 1
}
let after = rows()
if !active(after, hover.val) {
hover.val = first_active(after)
}
true
}
// Back to the default for the highlighted shortcut.
Some(Shortcut(i, _)) if k == "Backspace" || k == "Delete" => {
@shortcuts.reset(i.reinterpret_as_uint())
true
}
_ => false
}
} 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" || k == "5" || k == "6" {
set_tab(k[0].to_int() - '1'.to_int())
true
} else {
false
}
_ => false
}
}