///| 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) UseCamera(String) ToggleLight Apply Cancel } derive(Eq) ///| Boost display backlight while the screen acts as a face fill-light, and restore it when done. fn ChandraCompositor::sync_face_fill_light(self : ChandraCompositor) -> Unit { let enroll_light = match self.shell.face.window { Some(w) => !w.applying && !w.ended && w.fill_light None => false } let auth_light = match self.auth { Some(u) => u.view.face == Scanning None => false } let want_boost = enroll_light || auth_light if want_boost { if self.face_saved_brightness < 0 { let cur = @shell.read_backlight_percent() if cur >= 0 && cur < 90 { self.face_saved_brightness = cur ignore(self.shell.set_brightness(90)) } } } else if self.face_saved_brightness >= 0 { let prev = self.face_saved_brightness self.face_saved_brightness = -1 ignore(self.shell.set_brightness(prev)) } } ///| 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() self.sync_face_fill_light() 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) UseCamera(path) => f.use_camera(path) ToggleLight => f.toggle_fill_light() 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 { match self.shell.face.window { // While Apply waits for the password the window steps aside: the password dialog (ours, // or another agent's window) must get the keyboard and the pointer. Some(w) if !w.applying => () _ => 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) } else if code == key_l { self.face_window_activate(ToggleLight) } } 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 ///| let key_l : UInt = 38 ///| The window, when it is open. fn ChandraCompositor::face_window_ops(self : ChandraCompositor) -> Array[@scene.DrawOp] { self.sync_face_fill_light() 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 if win.applying { // Out of the way of the password dialog: a note at the top, no dimming, no input. self.face_hits.clear() let fs = self.theme.font_size let text = "Face unlock: " + win.message let tw = @renderer.text_width(text, fs - 1, false, false) + 32 let r = @scene.Rect::new((self.output_w - tw) / 2, 12, tw, fs + 18) 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(r, opaque, r.h / 2)) ops.push(@scene.DrawOp::StrokeRect(r, p.border, 1, r.h / 2)) ops.push(@scene.DrawOp::Text(r.x + tw / 2, r.y + r.h / 2, text, fs - 1, p.foreground, @scene.TextAlign::Center, false)) return ops } 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 let backdrop = if win.fill_light && !win.ended { @scene.Rgba::new(255, 250, 242, 245) } else { @scene.Rgba::new(0, 0, 0, 150) } ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(0, 0, sw, sh), backdrop, 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)) if !win.ended { let light_label = if win.fill_light { "Light: On" } else { "Light: Off" } let light_w = @renderer.text_width("Light: Off", fs - 2, false, false) + 22 let lr = @scene.Rect::new(pad + inner - light_w, pad, light_w, 28) auth_button(lr, light_label, false, false, hover == Some(ToggleLight), true, p, fs - 2, rad, body) hits.push((lr, ToggleLight)) } 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 configured camera can't be used: the ones that can, to pick from (saved as root). if win.ended && win.cameras.length() > 0 { body.push(@scene.DrawOp::Text(pad, y + 8, "Cameras here", fs - 2, p.foreground_muted, @scene.TextAlign::Left, true)) y = y + 20 let rh = 34 for c in win.cameras { let (path, name, why) = c let bw = @renderer.text_width("Use", fs - 1, false, false) + 32 let br = @scene.Rect::new(pad + inner - bw, y + 3, bw, rh - 6) let title = if name != "" { name } else { path } body.push(@scene.DrawOp::Text(pad, y + rh / 2 - 7, title, fs - 1, if why == "" { p.foreground } else { p.foreground_muted }, @scene.TextAlign::Left, why == "")) let sub = if why == "" { path } else { path + ": " + why } let sub_lines = wrap_text(sub, fs - 3, false, false, inner - bw - 12) body.push(@scene.DrawOp::Text(pad, y + rh / 2 + 9, sub_lines[0], fs - 3, p.foreground_muted, @scene.TextAlign::Left, false)) if why == "" { auth_button(br, "Use", false, false, hover == Some(UseCamera(path)), true, p, fs - 1, rad, body) hits.push((br, UseCamera(path))) } y = y + rh + 4 } y = y + 8 } // 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 }