Files
chandra/compositor/host/face_window.mbt
T
Claude 9f7489cfea
CI / test (push) Failing after 12m30s
Face unlock window: live camera preview, Snap, thumbnails, one password on Apply
Settings > Session "Add or remove faces…" (and "Remove…" on a face) opens a
compositor-drawn Face unlock window instead of running root steps one by one:

- chandra-face camera runs as the user (no password): it streams a small live
  picture with the face box and a cropped thumbnail per snap; the snaps'
  embeddings stay in that process, nothing is written to disk.
- Click a snap to drop it; mark saved faces Remove/Keep.
- Apply saves the snaps as one face and removes the marked ones in one root
  `chandra-face apply` (JSON on stdin, validated) through run0/pkexec: one
  password prompt.
- New raster host function image-pixels (host-wasm, wasmhost, native) for the
  window's pictures.

Tests: camera session and apply in face/test_face.py; the window end to end
against a fake tool in compositor/host/face_test.mbt. Docs explain why the
change itself still needs root.

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

291 lines
11 KiB
MoonBit

///| The Face unlock window (docs/face-unlock.md): Settings > Session > "Add or remove faces…"
/// opens it. A live camera picture with the face box, Snap to keep the picture in view (its
/// thumbnail shows below; click one to drop it), the saved faces with Remove / Keep, and Apply,
/// which saves the snaps and removes the marked faces in one root step: polkit asks for the
/// password once, in the password dialog drawn above this window.
///
/// The camera runs as the user (`chandra-face camera`, compositor/shell/face.mbt); the pictures
/// only live in memory, and only the face data (embeddings) is saved.
///
/// Like the password dialog it is drawn by the compositor above the windows, on a dimmed
/// screen, and takes the keyboard and the pointer while it is up: Space snaps, Enter applies,
/// Esc cancels.
///| A control of the window.
pub(all) enum FaceHit {
Snap
DropSnap(Int)
ToggleFace(String)
Apply
Cancel
} derive(Eq)
///| Follow the camera process and redraw when the window changed.
fn ChandraCompositor::face_window_tick(self : ChandraCompositor) -> Unit {
let f = self.shell.face
f.poll_window()
if f.window_rev != self.face_rev {
self.face_rev = f.window_rev
self.damage.mark_full()
}
}
///| The control under the pointer.
fn ChandraCompositor::face_window_hit(self : ChandraCompositor) -> FaceHit? {
let x = self.ptr_x.to_int()
let y = self.ptr_y.to_int()
for h in self.face_hits {
let r = h.0
if x >= r.x && x < r.x + r.w && y >= r.y && y < r.y + r.h {
return Some(h.1)
}
}
None
}
///| Do what control `h` does.
fn ChandraCompositor::face_window_activate(self : ChandraCompositor, h : FaceHit) -> Unit {
let f = self.shell.face
match h {
Snap => f.snap()
DropSnap(i) => f.drop_snap(i)
ToggleFace(id) => f.toggle_remove(id)
Apply => f.apply()
Cancel => f.cancel_window()
}
self.damage.mark_full()
}
///| Input while the window is up: all of it goes to the window.
fn ChandraCompositor::face_window_input(self : ChandraCompositor, ev : @nested.InputEvent) -> Bool {
if self.shell.face.window is None {
return false
}
match ev {
Key(code, pressed) => {
if code == key_lsuper || code == key_rsuper {
self.super_solo = false
}
if pressed {
self.auth_swallow.push(code)
if code == key_esc {
self.face_window_activate(Cancel)
} else if code == key_enter || code == key_kp_enter {
self.face_window_activate(Apply)
} else if code == key_space {
self.face_window_activate(Snap)
}
}
true
}
Modifiers(_, _, _, _) => true
PointerMove(x, y) => {
self.ptr_x = x
self.ptr_y = y
self.damage.mark_full()
true
}
PointerButton(button, pressed) => {
if pressed && button == 272U {
match self.face_window_hit() {
Some(h) => self.face_window_activate(h)
None => ()
}
}
true
}
Scroll(_, _, _) => true
}
}
///|
let key_space : UInt = 57
///| The window, when it is open.
fn ChandraCompositor::face_window_ops(self : ChandraCompositor) -> Array[@scene.DrawOp] {
let ops : Array[@scene.DrawOp] = []
let f = self.shell.face
let win = match f.window {
Some(w) => w
None => {
self.face_hits.clear()
return ops
}
}
let p = self.theme.palette
let (err_c, _) = auth_colors(p)
let fs = self.theme.font_size
let rad = self.theme.radius + 2
let sw = self.output_w
let sh = self.output_h
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(0, 0, sw, sh), @scene.Rgba::new(0, 0, 0, 150), 0))
let w = (sw - 32).min(560)
let pad = 24
let inner = w - pad * 2
let hover = self.face_window_hit()
let body : Array[@scene.DrawOp] = []
let hits : Array[(@scene.Rect, FaceHit)] = []
let mut y = pad
body.push(@scene.DrawOp::Text(pad, y + 7, "FACE UNLOCK", fs - 3, p.accent, @scene.TextAlign::Left, true))
y = y + 18
body.push(@scene.DrawOp::Text(pad, y + (fs + 8) / 2, "Add or remove faces", fs + 3, p.foreground, @scene.TextAlign::Left, true))
y = y + fs + 10
let intro = "Snap a few pictures: straight on, turned a little, with and without glasses. " +
"Pictures stay in memory; Apply saves only the face data, after one password."
for l in wrap_text(intro, fs - 1, false, false, inner) {
body.push(@scene.DrawOp::Text(pad, y + (fs + 4) / 2, l, fs - 1, p.foreground_muted, @scene.TextAlign::Left, false))
y = y + fs + 4
}
y = y + 12
// The live picture, as big as fits (4:3 until the first one says otherwise).
let (aw, ah) = if win.pic_w > 0 && win.pic_h > 0 { (win.pic_w, win.pic_h) } else { (4, 3) }
let max_h = (sh * 2 / 5).max(120)
let mut pw = inner
let mut ph = pw * ah / aw
if ph > max_h {
ph = max_h
pw = ph * aw / ah
}
let pr = @scene.Rect::new(pad + (inner - pw) / 2, y, pw, ph)
body.push(@scene.DrawOp::FillRect(pr, @scene.Rgba::new(16, 16, 18, 255), rad))
if win.picture != 0U && !win.ended {
body.push(@scene.DrawOp::Image(pr, win.picture))
match win.face_box {
Some((bx, by, bw, bh)) => {
let r = @scene.Rect::new(pr.x + bx * pw / win.pic_w, pr.y + by * ph / win.pic_h, bw * pw / win.pic_w, bh * ph / win.pic_h)
body.push(@scene.DrawOp::StrokeRect(r, p.accent, 2, 6))
}
None => ()
}
} else {
face_glyph(pr.x + pw / 2, pr.y + ph / 2 - 12, 22, @scene.Rgba::new(120, 120, 128, 255), body)
let t = if win.ended { "No camera picture" } else { "Starting the camera…" }
body.push(@scene.DrawOp::Text(pr.x + pw / 2, pr.y + ph / 2 + 30, t, fs - 1, @scene.Rgba::new(200, 200, 205, 255), @scene.TextAlign::Center, false))
}
y = y + ph + 12
// What the camera said, and Snap.
let bh = 34
let snap_w = @renderer.text_width("Snap", fs, true, false) + 40
let sr = @scene.Rect::new(pad + inner - snap_w, y, snap_w, bh)
let can_snap = !win.applying && !win.ended && win.picture != 0U
auth_button(sr, "Snap", true, false, hover == Some(Snap), can_snap, p, fs, rad, body)
if can_snap {
hits.push((sr, Snap))
}
let msg_lines = wrap_text(win.message, fs - 1, false, false, inner - snap_w - 12)
let mut my = y + bh / 2 - (msg_lines.length() - 1) * (fs + 3) / 2
for l in msg_lines {
body.push(@scene.DrawOp::Text(pad, my, l, fs - 1, if win.error { err_c } else { p.foreground }, @scene.TextAlign::Left, false))
my = my + fs + 3
}
y = y + bh.max(msg_lines.length() * (fs + 3)) + 14
// The snaps: click one to drop it.
if win.snaps.length() > 0 {
body.push(@scene.DrawOp::Text(pad, y + 8, "New pictures (click one to drop it)", fs - 2, p.foreground_muted, @scene.TextAlign::Left, true))
y = y + 20
let ts = 64
let gap = 8
let per_row = ((inner + gap) / (ts + gap)).max(1)
for i, s in win.snaps {
let r = @scene.Rect::new(pad + i % per_row * (ts + gap), y + i / per_row * (ts + gap), ts, ts)
body.push(@scene.DrawOp::Image(r, s.image))
let hov = hover == Some(DropSnap(i))
body.push(@scene.DrawOp::StrokeRect(r, if hov { err_c } else { p.border }, if hov { 2 } else { 1 }, 4))
if hov {
// A cross over it: this one goes.
body.push(@scene.DrawOp::Circle(r.x + ts - 10, r.y + 10, 8, err_c, true))
body.push(@scene.DrawOp::Line(r.x + ts - 13, r.y + 7, r.x + ts - 7, r.y + 13, p.surface_popup, 2))
body.push(@scene.DrawOp::Line(r.x + ts - 7, r.y + 7, r.x + ts - 13, r.y + 13, p.surface_popup, 2))
}
if !win.applying {
hits.push((r, DropSnap(i)))
}
}
y = y + (win.snaps.length() + per_row - 1) / per_row * (ts + gap) + 8
}
// The saved faces, each with Remove / Keep.
if f.faces.length() > 0 {
body.push(@scene.DrawOp::Text(pad, y + 8, "Saved faces", fs - 2, p.foreground_muted, @scene.TextAlign::Left, true))
y = y + 20
let rh = 34
for face in f.faces {
let (id, label, added) = face
let marked = win.remove.contains(id)
let btn = if marked { "Keep" } else { "Remove" }
let bw = @renderer.text_width("Remove", fs - 1, false, false) + 28
let br = @scene.Rect::new(pad + inner - bw, y + 3, bw, rh - 6)
face_glyph(pad + 12, y + rh / 2, 9, if marked { p.foreground_muted } else { p.foreground }, body)
body.push(@scene.DrawOp::Text(pad + 32, y + rh / 2, label, fs, if marked { p.foreground_muted } else { p.foreground }, @scene.TextAlign::Left, !marked))
let lw = @renderer.text_width(label, fs, !marked, false)
let note = if marked { "will be removed" } else { "added " + added }
body.push(@scene.DrawOp::Text(pad + 32 + lw + 10, y + rh / 2, note, fs - 2, if marked { err_c } else { p.foreground_muted }, @scene.TextAlign::Left, false))
if marked {
body.push(@scene.DrawOp::Line(pad + 30, y + rh / 2, pad + 34 + lw, y + rh / 2, p.foreground_muted, 1))
}
auth_button(br, btn, false, false, hover == Some(ToggleFace(id)), !win.applying, p, fs - 1, rad, body)
if !win.applying {
hits.push((br, ToggleFace(id)))
}
y = y + rh
}
y = y + 8
}
// Footer: what Apply will do, Cancel, Apply.
y = y + 6
body.push(@scene.DrawOp::Line(pad, y, pad + inner, y, p.border, 1))
y = y + 14
let adds = win.snaps.length()
let rems = win.remove.length()
let summary = if win.applying {
"Saving…"
} else if adds == 0 && rems == 0 {
"Nothing to save yet"
} else {
let parts : Array[String] = []
if adds > 0 {
parts.push("adds a face from " + adds.to_string() + (if adds == 1 { " picture" } else { " pictures" }))
}
if rems > 0 {
parts.push("removes " + rems.to_string() + (if rems == 1 { " face" } else { " faces" }))
}
let s = parts.join(", ")
"Apply " + s
}
let apply_w = @renderer.text_width("Apply", fs, true, false) + 40
let cancel_w = @renderer.text_width("Cancel", fs, false, false) + 32
let ar = @scene.Rect::new(pad + inner - apply_w, y, apply_w, bh)
let cr = @scene.Rect::new(ar.x - 10 - cancel_w, y, cancel_w, bh)
let sl = wrap_text(summary, fs - 2, false, false, cr.x - pad - 12)
let sl = if sl.length() > 2 { sl[:2].to_array() } else { sl }
let lh = fs + 2
for i, l in sl {
body.push(@scene.DrawOp::Text(pad, y + bh / 2 - (sl.length() - 1) * lh / 2 + i * lh, l, fs - 2, p.foreground_muted, @scene.TextAlign::Left, false))
}
auth_button(cr, "Cancel", false, false, hover == Some(Cancel), !win.applying, p, fs, rad, body)
auth_button(ar, "Apply", true, false, hover == Some(Apply), win.can_apply(), p, fs, rad, body)
if !win.applying {
hits.push((cr, Cancel))
}
if win.can_apply() {
hits.push((ar, Apply))
}
y = y + bh + pad
// Centre the card, with the password dialog's shadow.
let h = y
let cx = (sw - w) / 2
let cyy = ((sh - h) / 2).max(8)
for k = 1; k <= 4; k = k + 1 {
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(cx - k * 2, cyy - k * 2 + 6, w + k * 4, h + k * 4), @scene.Rgba::new(0, 0, 0, 22), rad + 6 + k * 2))
}
let opaque = @scene.Rgba::new(p.surface_popup.r.to_int(), p.surface_popup.g.to_int(), p.surface_popup.b.to_int(), 255)
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(cx, cyy, w, h), opaque, rad + 6))
ops.push(@scene.DrawOp::StrokeRect(@scene.Rect::new(cx, cyy, w, h), p.border, 1, rad + 6))
ops.append(offset_ops_all(body, cx, cyy))
self.face_hits.clear()
for hit in hits {
let r = hit.0
self.face_hits.push((@scene.Rect::new(r.x + cx, r.y + cyy, r.w, r.h), hit.1))
}
ops
}