///| The authentication dialog: Chandra's own polkit agent (compositor/polkit) shows it when /// run0, pkexec or any app needs an administrator's password. /// /// It is drawn by the compositor above everything, client menus included (they are hidden while /// it is up), on a dimmed screen, and it takes the keyboard and the pointer: no client gets input /// or keyboard focus until it closes, and no client can draw over it. Typing goes through the /// xkb keymap clients get, so the password is typed on the user's layout. The password is kept /// as characters that are wiped once sent, and is never logged. /// /// The conversation runs through polkit's helper (HelperSession): its prompts, errors and /// information messages are shown as they come (fingerprint readers, one-time codes, lockouts). ///| What the dialog needs while it is up. pub(all) struct AuthUi { dialog : @polkit.AuthDialog mut helper : @polkit.HelperSession? /// The window that had the keyboard before, given it back on close. prev_kbd : UInt64? /// The app icon (0: the lock drawn instead). icon : UInt /// Where each control was last drawn, for clicks and the cursor. hits : Array[(@scene.Rect, @polkit.Focus)] /// When it opened (the spinner's clock). opened : UInt64 } ///| The dialog that is up, if any. pub fn ChandraCompositor::auth_dialog(self : ChandraCompositor) -> @polkit.AuthDialog? { self.auth.map(a => a.dialog) } ///| Start Chandra's polkit agent, or the system's, per the Settings switch and /// CHANDRA_POLKIT_AGENT ("none": no agent; a path: that agent). Login session only. pub fn ChandraCompositor::start_auth_agent(self : ChandraCompositor) -> Unit { let env = @sys.getenv("CHANDRA_POLKIT_AGENT") if env == "none" { return } if env != "" && env != "chandra" { self.spawn_fallback_agent(env) return } if !self.shell.native_auth_agent { self.spawn_fallback_agent(@sys.getenv("CHANDRA_POLKIT_FALLBACK")) return } let agent = match self.polkit { Some(a) => a None => { let a = @polkit.PolkitAgent::new(bus_path=self.polkit_bus) self.polkit = Some(a) a } } if agent.start() { @sys.log("chandra: registering as the polkit authentication agent") } else { @sys.log("chandra: polkit agent not registered (" + agent.error() + ")") self.polkit = None self.spawn_fallback_agent(@sys.getenv("CHANDRA_POLKIT_FALLBACK")) } } ///| Start a third-party polkit agent (once), when one is installed. fn ChandraCompositor::spawn_fallback_agent(self : ChandraCompositor, path : String) -> Unit { if path == "" || self.fallback_agent_started { return } if @sys.spawn(path, self.shell.display) { self.fallback_agent_started = true @sys.log("chandra: started polkit agent " + path) } } ///| The Settings switch changed: register or leave. Leaving dismisses an open dialog and starts /// the system's agent (polkit allows one per session); coming back takes effect at the next /// login when that agent already runs. pub fn ChandraCompositor::auth_agent_setting_changed(self : ChandraCompositor) -> Unit { if !self.login_session { return } if self.shell.native_auth_agent { if self.polkit is None { self.start_auth_agent() } } else { match self.polkit { Some(a) => { self.close_auth(None) a.stop() self.polkit = None self.spawn_fallback_agent(@sys.getenv("CHANDRA_POLKIT_FALLBACK")) } None => () } } } ///| Serve the agent and the helper; open, update and close the dialog. Each main-loop pass. fn ChandraCompositor::auth_tick(self : ChandraCompositor) -> Unit { let agent = match self.polkit { Some(a) => a None => return } let was_registered = agent.registered() ignore(agent.tick()) if agent.failed() { if was_registered { @sys.log("chandra: polkit agent lost the system bus (" + agent.error() + ")") } else { @sys.log("chandra: polkit agent not registered (" + agent.error() + ")") self.spawn_fallback_agent(@sys.getenv("CHANDRA_POLKIT_FALLBACK")) } self.close_auth(None) self.polkit = None return } if agent.registered() && !was_registered { @sys.log("chandra: polkit authentication agent registered") } match self.auth { Some(ui) => if !agent.waiting(ui.dialog.req.cookie) { // polkitd withdrew it (the asking process went away). self.close_auth(None) } else { self.auth_helper_events(ui) } None => () } if self.auth is None { match agent.current() { Some(req) => self.open_auth(req) None => () } } } ///| Show the dialog for `req` and start the helper. fn ChandraCompositor::open_auth(self : ChandraCompositor, req : @polkit.AuthRequest) -> Unit { let desc = match self.auth_describe { Some(f) => f(req) None => @polkit.describe(req) } let dialog = @polkit.AuthDialog::new(req, desc) let icon = if desc.program != "" { self.shell.window_icon(desc.program) } else if req.icon_name != "" { self.shell.theme_icon(req.icon_name) } else { 0U } let ui : AuthUi = { dialog, helper: None, prev_kbd: self.kbd_win, icon, hits: [], opened: @sys.monotonic_ms(), } self.auth = Some(ui) // Nothing else gets the keyboard or the pointer while it is up. self.set_kbd_focus(None) self.leave_pointer() self.auth_start_helper(ui) self.damage.mark_full() } ///| (Re)start the helper for the selected user. fn ChandraCompositor::auth_start_helper(self : ChandraCompositor, ui : AuthUi) -> Unit { match ui.helper { Some(h) => h.close() None => () } ui.helper = None match ui.dialog.user() { Some(u) => { let routes = if self.auth_routes.length() > 0 { self.auth_routes } else { @polkit.helper_routes() } match @polkit.HelperSession::start_any(routes, u.name, ui.dialog.req.cookie) { Some(h) => ui.helper = Some(h) None => ui.dialog.error = "polkit's authentication helper could not be started." } } None => () } } ///| Feed what the helper said to the dialog. fn ChandraCompositor::auth_helper_events(self : ChandraCompositor, ui : AuthUi) -> Unit { match ui.helper { Some(h) => { let evs = h.read() for ev in evs { self.auth_effect(ui, ui.dialog.helper(ev, @sys.monotonic_ms())) if self.auth is None { return } } if evs.length() > 0 { self.damage.mark_full() } } None => () } } ///| Carry out what the dialog asked for. fn ChandraCompositor::auth_effect(self : ChandraCompositor, ui : AuthUi, e : @polkit.DialogEffect) -> Unit { match e { Nothing => () Answer(b) => { match ui.helper { Some(h) => if !h.respond(b) { ui.dialog.error = "The authentication helper stopped." } None => () } @polkit.zero(b) } Restart => self.auth_start_helper(ui) Cancelled => self.close_auth(Some(Err("The user dismissed the authentication dialog"))) Authorized => self.close_auth(Some(Ok(()))) } self.damage.mark_full() } ///| Close the dialog; `result` answers polkitd (None: it is already answered or withdrawn). fn ChandraCompositor::close_auth(self : ChandraCompositor, result : Result[Unit, String]?) -> Unit { match self.auth { Some(ui) => { ui.dialog.wipe() match ui.helper { Some(h) => h.close() None => () } self.auth = None match (result, self.polkit) { (Some(r), Some(a)) => a.finish(ui.dialog.req.cookie, r) _ => () } match ui.prev_kbd { Some(w) => if self.windows.contains(w) { self.set_kbd_focus(Some(w)) } None => () } self.damage.mark_full() } None => () } } ///| The keymap typing goes through: the one clients have, else US. fn ChandraCompositor::auth_keymap(self : ChandraCompositor) -> @polkit.Keymap { match self.keymap { Some((src, k)) if physical_equal(src, self.server.keymap) => k _ => { let k = @polkit.Keymap::from_text(@utf8.decode_lossy(self.server.keymap)) self.keymap = Some((self.server.keymap, k)) k } } } ///| Input while the dialog is up: all of it is the dialog's. True when it took the event. fn ChandraCompositor::auth_input(self : ChandraCompositor, ev : @nested.InputEvent) -> Bool { // Releases of keys the dialog took must not reach the client that gets the keyboard back. match ev { Key(code, false) => if self.auth_swallow.contains(code) { self.auth_swallow.retain(c => c != code) return true } _ => () } let ui = match self.auth { Some(u) => u 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) let mods = self.mods | self.mods_latched | self.mods_locked let ch = self.auth_keymap().char_for(code, mods, self.mods_group) self.auth_effect(ui, ui.dialog.key(code, self.mods, ch)) } true } Modifiers(_, _, lock, _) => { ui.dialog.caps = (lock & @polkit.mask_lock) != 0U self.damage.mark_full() 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.auth_hit(ui) { Some(f) => self.auth_effect(ui, ui.dialog.activate(f)) None => () } } true } Scroll(_, _, _) => true } } ///| The control under the pointer. fn ChandraCompositor::auth_hit(self : ChandraCompositor, ui : AuthUi) -> @polkit.Focus? { let x = self.ptr_x.to_int() let y = self.ptr_y.to_int() for h in ui.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 } ///| Cursor over the dialog: a text beam over the field, a hand over buttons, else the arrow. fn ChandraCompositor::auth_cursor(self : ChandraCompositor, ui : AuthUi) -> UInt { match self.auth_hit(ui) { Some(Field) => 9U // text Some(_) => 4U // pointer None => 1U // default } } ///| Whether the dialog animates (a failed attempt shakes it; a spinner while PAM checks). fn ChandraCompositor::auth_animating(self : ChandraCompositor) -> Bool { match self.auth { Some(ui) => ui.dialog.shaking(@sys.monotonic_ms()) || ui.dialog.phase == Checking || ui.dialog.face == Scanning None => false } } ///| The fds to wake on: the agent's bus and the helper. pub fn ChandraCompositor::auth_fds(self : ChandraCompositor) -> Array[Int] { let out : Array[Int] = [] match self.polkit { Some(a) => if a.fd() >= 0 { out.push(a.fd()) } None => () } match self.auth { Some({ helper: Some(h), .. }) => if h.fd() >= 0 { out.push(h.fd()) } _ => () } out } // ---- drawing ---- ///| Colours the palette has no slot for: errors and warnings, readable on the card. fn auth_colors(p : @scene.ThemePalette) -> (@scene.Rgba, @scene.Rgba) { // A light card (bright foreground means dark theme). let dark = p.foreground.r.to_int() + p.foreground.g.to_int() + p.foreground.b.to_int() > 384 if dark { (@scene.Rgba::from_hex("#f87171"), @scene.Rgba::from_hex("#fbbf24")) } else { (@scene.Rgba::from_hex("#c62828"), @scene.Rgba::from_hex("#8a5a00")) } } ///| `text` broken into lines no wider than `max_w`, at word boundaries (long words by character). fn wrap_text(text : String, size : Int, bold : Bool, mono : Bool, max_w : Int) -> Array[String] { let out : Array[String] = [] for para in text.split("\n") { let mut line = "" for w in para.split(" ") { let word = w.to_string() let cand = if line == "" { word } else { line + " " + word } if @renderer.text_width(cand, size, bold, mono) <= max_w { line = cand continue } if line != "" { out.push(line) line = "" } // A word wider than the line: split it. let mut part = "" for c in word { let next = part + c.to_string() if @renderer.text_width(next, size, bold, mono) > max_w && part != "" { out.push(part) part = c.to_string() } else { part = next } } line = part } out.push(line) } out } ///| A padlock, for requests without an app icon. fn lock_glyph(x : Int, y : Int, s : Int, c : @scene.Rgba, bg : @scene.Rgba, ops : Array[@scene.DrawOp]) -> Unit { let bw = s * 5 / 8 let bh = s / 2 let bx = x + (s - bw) / 2 let by = y + s - bh - s / 10 let sw = bw * 5 / 8 let shx = x + (s - sw) / 2 ops.push(@scene.DrawOp::StrokeRect(@scene.Rect::new(shx, by - bh * 3 / 4, sw, bh), c, s / 12 + 1, sw / 2)) ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(bx, by, bw, bh), c, s / 10)) ops.push(@scene.DrawOp::Circle(x + s / 2, by + bh * 2 / 5, s / 14 + 1, bg, true)) ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(x + s / 2 - 1, by + bh * 2 / 5, 3, bh / 3), bg, 1)) } ///| A button: filled (the default action) or outlined; with a focus ring. fn auth_button( r : @scene.Rect, label : String, primary : Bool, focused : Bool, hover : Bool, enabled : Bool, p : @scene.ThemePalette, fs : Int, rad : Int, ops : Array[@scene.DrawOp], ) -> Unit { if primary { let a = p.accent let alpha = if enabled { if hover { 230 } else { 255 } } else { 120 } ops.push(@scene.DrawOp::FillRect(r, @scene.Rgba::new(a.r.to_int(), a.g.to_int(), a.b.to_int(), alpha), rad)) ops.push(@scene.DrawOp::Text(r.x + r.w / 2, r.y + r.h / 2, label, fs, p.accent_foreground, @scene.TextAlign::Center, true)) } else { if hover { ops.push(@scene.DrawOp::FillRect(r, p.surface_hover, rad)) } ops.push(@scene.DrawOp::StrokeRect(r, p.border, 1, rad)) ops.push(@scene.DrawOp::Text(r.x + r.w / 2, r.y + r.h / 2, label, fs, p.foreground, @scene.TextAlign::Center, false)) } if focused { ops.push(@scene.DrawOp::StrokeRect(@scene.Rect::new(r.x - 3, r.y - 3, r.w + 6, r.h + 6), p.accent, 2, rad + 3)) } } ///| A face outline (head, eyes, smile) centred at (cx, cy), `r` the head's radius. fn face_glyph(cx : Int, cy : Int, r : Int, c : @scene.Rgba, ops : Array[@scene.DrawOp]) -> Unit { ops.push(@scene.DrawOp::Circle(cx, cy, r, c, false)) ops.push(@scene.DrawOp::Circle(cx - r * 2 / 5, cy - r / 4, r / 8 + 1, c, true)) ops.push(@scene.DrawOp::Circle(cx + r * 2 / 5, cy - r / 4, r / 8 + 1, c, true)) ops.push(@scene.DrawOp::Line(cx - r / 2, cy + r / 4, cx - r / 5, cy + r / 2, c, 2)) ops.push(@scene.DrawOp::Line(cx - r / 5, cy + r / 2, cx + r / 5, cy + r / 2, c, 2)) ops.push(@scene.DrawOp::Line(cx + r / 5, cy + r / 2, cx + r / 2, cy + r / 4, c, 2)) } ///| Point `k` of 12 on a circle of radius `r` (clockwise from the top). fn ring_point(k : Int, r : Int) -> (Int, Int) { // sin and cos of k * 30 degrees, times 1000. let s = [0, 500, 866, 1000, 866, 500, 0, -500, -866, -1000, -866, -500] let c = [1000, 866, 500, 0, -500, -866, -1000, -866, -500, 0, 500, 866] (s[k % 12] * r / 1000, -c[k % 12] * r / 1000) } ///| A right-pointing (`down`: down-pointing) chevron centred at (cx, cy). fn chevron(cx : Int, cy : Int, s : Int, down : Bool, left : Bool, c : @scene.Rgba, ops : Array[@scene.DrawOp]) -> Unit { if down { ops.push(@scene.DrawOp::Line(cx - s, cy - s / 2, cx, cy + s / 2, c, 2)) ops.push(@scene.DrawOp::Line(cx, cy + s / 2, cx + s, cy - s / 2, c, 2)) } else if left { ops.push(@scene.DrawOp::Line(cx + s / 2, cy - s, cx - s / 2, cy, c, 2)) ops.push(@scene.DrawOp::Line(cx - s / 2, cy, cx + s / 2, cy + s, c, 2)) } else { ops.push(@scene.DrawOp::Line(cx - s / 2, cy - s, cx + s / 2, cy, c, 2)) ops.push(@scene.DrawOp::Line(cx + s / 2, cy, cx - s / 2, cy + s, c, 2)) } } ///| Draw the dialog (everything below it dimmed) and note where its controls are. fn ChandraCompositor::auth_ops(self : ChandraCompositor) -> Array[@scene.DrawOp] { let ops : Array[@scene.DrawOp] = [] let ui = match self.auth { Some(u) => u None => return ops } let d = ui.dialog let now = @sys.monotonic_ms() let p = self.theme.palette let (err_c, warn_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(460) let pad = 24 let inner = w - pad * 2 let hover = self.auth_hit(ui) // Lay out top to bottom into `body` (card-relative y), then place the card. let body : Array[@scene.DrawOp] = [] let hits : Array[(@scene.Rect, @polkit.Focus)] = [] let mut y = pad // Header: icon, overline, title, subtitle. let icon_s = 48 let tx = pad + icon_s + 16 let tw = w - tx - pad let header_top = y let mut hy = y body.push(@scene.DrawOp::Text(tx, hy + 7, "AUTHENTICATION REQUIRED", fs - 3, p.accent, @scene.TextAlign::Left, true)) hy = hy + 18 let title_lines = wrap_text(d.desc.title, fs + 3, true, false, tw) for l in title_lines { body.push(@scene.DrawOp::Text(tx, hy + (fs + 8) / 2, l, fs + 3, p.foreground, @scene.TextAlign::Left, true)) hy = hy + fs + 8 } hy = hy + 2 for l in wrap_text(d.desc.subtitle, fs - 1, false, false, tw) { body.push(@scene.DrawOp::Text(tx, hy + (fs + 4) / 2, l, fs - 1, p.foreground_muted, @scene.TextAlign::Left, false)) hy = hy + fs + 4 } // Icon tile. let ix = pad let iy = header_top + 2 body.push(@scene.DrawOp::FillRect(@scene.Rect::new(ix, iy, icon_s, icon_s), p.surface_hover, 12)) if ui.icon != 0U { body.push(@scene.DrawOp::Image(@scene.Rect::new(ix + 6, iy + 6, icon_s - 12, icon_s - 12), ui.icon)) } else { lock_glyph(ix + 8, iy + 8, icon_s - 16, p.accent, p.surface_hover, body) } y = hy.max(iy + icon_s) + 14 // The command, as a shell would read it. if d.desc.command != "" { let mfs = fs - 1 let lines = wrap_text(d.desc.command, mfs, false, true, inner - 24) let shown = if lines.length() > 4 { lines[:4].to_array() } else { lines } if lines.length() > 4 { shown[3] = shown[3] + " …" } let lh = mfs + 6 let bh = shown.length() * lh + 16 body.push(@scene.DrawOp::FillRect(@scene.Rect::new(pad, y, inner, bh), p.surface_hover, rad)) body.push(@scene.DrawOp::FillRect(@scene.Rect::new(pad, y, 3, bh), p.accent, 1)) for i, l in shown { body.push(@scene.DrawOp::MonoText(pad + 14, y + 8 + i * lh + lh / 2, l, mfs, p.foreground, @scene.TextAlign::Left, false)) } y = y + bh + 14 } // Who authenticates. match d.user() { Some(u) => { let rh = 40 let r = @scene.Rect::new(pad, y, inner, rh) let many = d.req.users.length() > 1 if many { if hover == Some(User) { body.push(@scene.DrawOp::FillRect(r, p.surface_hover, rad)) } hits.push((r, User)) } let initial = if u.real_name != "" { u.real_name[:1].to_string() } else { u.name[:1].to_string() } body.push(@scene.DrawOp::Circle(pad + 20, y + rh / 2, 15, p.accent, true)) body.push(@scene.DrawOp::Text(pad + 20, y + rh / 2, initial.to_upper(), fs, p.accent_foreground, @scene.TextAlign::Center, true)) let name = if u.real_name != "" && u.real_name != u.name { u.real_name } else { u.name } body.push(@scene.DrawOp::Text(pad + 44, y + rh / 2 - 8, name, fs, p.foreground, @scene.TextAlign::Left, true)) let login = if u.uid == 0 { "root" } else { u.name } let sub = if many { login + " · account " + (d.user_sel + 1).to_string() + " of " + d.req.users.length().to_string() } else { login } body.push(@scene.DrawOp::Text(pad + 44, y + rh / 2 + 9, sub, fs - 2, p.foreground_muted, @scene.TextAlign::Left, false)) if many { chevron(pad + inner - 34, y + rh / 2, 5, false, true, p.foreground_muted, body) chevron(pad + inner - 14, y + rh / 2, 5, false, false, p.foreground_muted, body) if d.focus == User { body.push(@scene.DrawOp::StrokeRect(r, p.accent, 2, rad)) } } y = y + rh + 12 } None => () } // Face unlock: what the face module in the PAM stack is doing (docs/face-unlock.md). if d.face != NoFace { let rh = 48 let cx = pad + 25 let cy = y + rh / 2 let tint = match d.face { FaceFailed(_) => warn_c _ => p.accent } body.push(@scene.DrawOp::FillRect(@scene.Rect::new(pad, y, inner, rh), p.surface_hover, rad)) face_glyph(cx, cy, 13, if d.face is FaceFailed(_) { p.foreground_muted } else { p.foreground }, body) match d.face { Scanning => { // A ring of dots sweeping round the face. let t = ((now - d.face_at) / 70UL).to_int() for k = 0; k < 12; k = k + 1 { let a = (k + t) % 12 let (dx, dy) = ring_point(a, 19) let alpha = 40 + k * 18 body.push(@scene.DrawOp::Circle(cx + dx, cy + dy, 2, @scene.Rgba::new(p.accent.r.to_int(), p.accent.g.to_int(), p.accent.b.to_int(), alpha), true)) } } Matched => { body.push(@scene.DrawOp::Circle(cx + 13, cy + 11, 8, p.accent, true)) body.push(@scene.DrawOp::Line(cx + 9, cy + 11, cx + 12, cy + 14, p.accent_foreground, 2)) body.push(@scene.DrawOp::Line(cx + 12, cy + 14, cx + 17, cy + 8, p.accent_foreground, 2)) } FaceFailed(_) => { body.push(@scene.DrawOp::Circle(cx + 13, cy + 11, 8, warn_c, true)) body.push(@scene.DrawOp::Line(cx + 10, cy + 8, cx + 16, cy + 14, p.surface_popup, 2)) body.push(@scene.DrawOp::Line(cx + 16, cy + 8, cx + 10, cy + 14, p.surface_popup, 2)) } NoFace => () } let (title, sub) = match d.face { Scanning => ("Looking for your face…", "Or type your password") Matched => ("Face recognized", "") FaceFailed(why) => (why, "Type your password instead") NoFace => ("", "") } let tx = pad + 54 if sub == "" { body.push(@scene.DrawOp::Text(tx, cy, title, fs, tint, @scene.TextAlign::Left, true)) } else { body.push(@scene.DrawOp::Text(tx, cy - 8, title, fs, tint, @scene.TextAlign::Left, true)) body.push(@scene.DrawOp::Text(tx, cy + 10, sub, fs - 2, p.foreground_muted, @scene.TextAlign::Left, false)) } y = y + rh + 12 } // The prompt and the field. body.push(@scene.DrawOp::Text(pad, y + 7, d.prompt, fs - 1, p.foreground_muted, @scene.TextAlign::Left, true)) y = y + 20 let fh = 40 let field = @scene.Rect::new(pad, y, inner, fh) let enabled = d.phase != Checking body.push(@scene.DrawOp::FillRect(field, p.surface, rad)) let edge = if d.error != "" && d.typed() == 0 { err_c } else if d.focus == Field { p.accent } else { p.border } body.push(@scene.DrawOp::StrokeRect(field, edge, if d.focus == Field || d.error != "" { 2 } else { 1 }, rad)) hits.push((@scene.Rect::new(pad, y, inner - 70, fh), Field)) // Show / Hide (not for prompts that show the answer anyway). let text_right = if d.echo { pad + inner - 12 } else { pad + inner - 76 } if !d.echo { let rr = @scene.Rect::new(pad + inner - 68, y + 6, 60, fh - 12) if hover == Some(Reveal) || d.focus == Reveal { body.push(@scene.DrawOp::FillRect(rr, p.surface_hover, rad - 2)) } if d.focus == Reveal { body.push(@scene.DrawOp::StrokeRect(rr, p.accent, 2, rad - 2)) } body.push(@scene.DrawOp::Text(rr.x + rr.w / 2, rr.y + rr.h / 2, if d.reveal { "Hide" } else { "Show" }, fs - 1, p.accent, @scene.TextAlign::Center, true)) hits.push((rr, Reveal)) } body.push(@scene.DrawOp::PushClip(@scene.Rect::new(pad + 8, y, text_right - pad - 8, fh))) let cy = y + fh / 2 let mut caret_x = pad + 13 if d.typed() == 0 { let hint = match d.phase { Starting => "Preparing…" Waiting => if d.face == Scanning { "Type your password" } else { "Waiting…" } _ => if d.echo { "Type your answer" } else { "Type your password" } } body.push(@scene.DrawOp::Text(pad + 18, cy, hint, fs, @scene.Rgba::new(p.foreground_muted.r.to_int(), p.foreground_muted.g.to_int(), p.foreground_muted.b.to_int(), 150), @scene.TextAlign::Left, false)) } else if d.echo || d.reveal { // Keep the end of long text in view. let t = d.field_text() let tw2 = @renderer.text_width(t, fs + 1, false, false) let avail = text_right - pad - 22 let x0 = if tw2 > avail { pad + 14 - (tw2 - avail) } else { pad + 14 } body.push(@scene.DrawOp::Text(x0, cy, t, fs + 1, if enabled { p.foreground } else { p.foreground_muted }, @scene.TextAlign::Left, false)) caret_x = x0 + tw2 + 2 } else { let n = d.typed() let step = 14 let avail = text_right - pad - 22 let total = n * step let x0 = if total > avail { pad + 14 - (total - avail) } else { pad + 14 } for i = 0; i < n; i = i + 1 { body.push(@scene.DrawOp::Circle(x0 + i * step + 5, cy, 4, if enabled { p.foreground } else { p.foreground_muted }, true)) } caret_x = x0 + total + 2 } if d.focus == Field && enabled { body.push(@scene.DrawOp::FillRect(@scene.Rect::new(caret_x, cy - fs / 2 - 2, 2, fs + 4), p.accent, 0)) } body.push(@scene.DrawOp::PopClip) y = y + fh + 10 // Status: busy, Caps Lock, error, PAM's information. let status : Array[(String, @scene.Rgba, Bool)] = [] if d.phase == Checking { status.push(("Checking…", p.foreground_muted, true)) } if d.caps && !d.echo { status.push(("Caps Lock is on", warn_c, false)) } if d.error != "" { status.push((d.error, err_c, false)) } if d.info != "" { status.push((d.info, p.foreground, false)) } for s in status { let lines = wrap_text(s.0, fs - 1, false, false, inner - 22) // A marker: a spinner while busy, else a dot in the line's colour. if s.2 { let t = ((now - ui.opened) / 90UL).to_int() for k = 0; k < 8; k = k + 1 { let ang = (k + t) % 8 let (dx, dy) = match ang { 0 => (0, -6) 1 => (4, -4) 2 => (6, 0) 3 => (4, 4) 4 => (0, 6) 5 => (-4, 4) 6 => (-6, 0) _ => (-4, -4) } let a = 50 + k * 29 body.push(@scene.DrawOp::Circle(pad + 6 + dx, y + 9 + dy, 2, @scene.Rgba::new(p.accent.r.to_int(), p.accent.g.to_int(), p.accent.b.to_int(), a), true)) } } else { body.push(@scene.DrawOp::Circle(pad + 6, y + 9, 3, s.1, true)) } for l in lines { body.push(@scene.DrawOp::Text(pad + 18, y + 9, l, fs - 1, s.1, @scene.TextAlign::Left, false)) y = y + fs + 5 } y = y + 3 } y = y + 6 // Details. if d.details_open { let rows : Array[(String, String)] = d.desc.details.copy() if d.req.message != "" && d.desc.title != d.req.message { rows.push(("polkit says", d.req.message)) } let lw = 104 let dh0 = y y = y + 10 let rows_ops : Array[@scene.DrawOp] = [] for r in rows { let vl = wrap_text(r.1, fs - 2, false, r.0 == "Action" || r.0 == "Unit", inner - lw - 24) rows_ops.push(@scene.DrawOp::Text(pad + 12, y + 8, r.0, fs - 2, p.foreground_muted, @scene.TextAlign::Left, true)) for l in vl { if r.0 == "Action" || r.0 == "Unit" { rows_ops.push(@scene.DrawOp::MonoText(pad + 12 + lw, y + 8, l, fs - 2, p.foreground, @scene.TextAlign::Left, false)) } else { rows_ops.push(@scene.DrawOp::Text(pad + 12 + lw, y + 8, l, fs - 2, p.foreground, @scene.TextAlign::Left, false)) } y = y + fs + 3 } y = y + 4 } y = y + 6 body.push(@scene.DrawOp::FillRect(@scene.Rect::new(pad, dh0, inner, y - dh0), p.surface_hover, rad)) body.append(rows_ops) y = y + 12 } // Bottom row: Details toggle on the left, Cancel and Authenticate on the right. let bh = 36 let dl = if d.details_open { "Hide details" } else { "Details" } let dw = @renderer.text_width(dl, fs - 1, false, false) + 30 let dr = @scene.Rect::new(pad - 6, y, dw, bh) if hover == Some(Details) { body.push(@scene.DrawOp::FillRect(dr, p.surface_hover, rad)) } chevron(pad + 6, y + bh / 2, 4, d.details_open, false, p.foreground_muted, body) body.push(@scene.DrawOp::Text(pad + 18, y + bh / 2, dl, fs - 1, p.foreground_muted, @scene.TextAlign::Left, false)) if d.focus == Details { body.push(@scene.DrawOp::StrokeRect(dr, p.accent, 2, rad)) } hits.push((dr, Details)) let allow_w = @renderer.text_width("Authenticate", fs, true, false) + 32 let cancel_w = @renderer.text_width("Cancel", fs, false, false) + 32 let ar = @scene.Rect::new(pad + inner - allow_w, y, allow_w, bh) let cr = @scene.Rect::new(ar.x - 10 - cancel_w, y, cancel_w, bh) auth_button(cr, "Cancel", false, d.focus == Cancel, hover == Some(Cancel), true, p, fs, rad, body) auth_button(ar, "Authenticate", true, d.focus == Allow, hover == Some(Allow), enabled && (d.typed() > 0 || d.echo), p, fs, rad, body) hits.push((cr, Cancel)) hits.push((ar, Allow)) y = y + bh + pad // Place the card in the middle (shaking after a failure) with a soft shadow. let h = y let cx = (sw - w) / 2 + d.shake_offset(now) 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)) ui.hits.clear() for hit in hits { let r = hit.0 ui.hits.push((@scene.Rect::new(r.x + cx, r.y + cyy, r.w, r.h), hit.1)) } ops } ///| Move every op by (dx, dy). fn offset_ops_all(ops : Array[@scene.DrawOp], dx : Int, dy : Int) -> Array[@scene.DrawOp] { let mv = fn(r : @scene.Rect) { @scene.Rect::new(r.x + dx, r.y + dy, r.w, r.h) } ops.map(op => match op { FillRect(r, c, rad) => @scene.DrawOp::FillRect(mv(r), c, rad) StrokeRect(r, c, w, rad) => @scene.DrawOp::StrokeRect(mv(r), c, w, rad) Line(x0, y0, x1, y1, c, w) => @scene.DrawOp::Line(x0 + dx, y0 + dy, x1 + dx, y1 + dy, c, w) Circle(x, y, r, c, f) => @scene.DrawOp::Circle(x + dx, y + dy, r, c, f) Text(x, y, t, s, c, a, b) => @scene.DrawOp::Text(x + dx, y + dy, t, s, c, a, b) MonoText(x, y, t, s, c, a, b) => @scene.DrawOp::MonoText(x + dx, y + dy, t, s, c, a, b) PushClip(r) => @scene.DrawOp::PushClip(mv(r)) PopClip => PopClip Image(r, h) => @scene.DrawOp::Image(mv(r), h) }) }