Make the preferences lock row a toggle

Preferences could lock the vault but not open it, so a locked user had no
way to change the passphrase without leaving the dialog. The row now
mirrors the header lock button: unlocked it offers "Lock", locked it
offers "Unlock…" through the same present_unlock dialog every other route
uses, and it is inert until a key exists. The state-to-labels mapping is a
pure helper with its own tests.
This commit is contained in:
raucao committed 2026-11-26 11:56:47 +04:00
1 parent 8800ccf1c4
commit e0eec2df63
2 files changed
+101 -6

No files matched your search

+6 -2
View File
@@ -464,8 +464,12 @@ data/ desktop, metainfo, gschema, icons, resources (ui/*.blp, sty
- UI: a header lock/unlock button (`app.lock`, `<primary>l`) whose icon and
tooltip show the **state** — a closed padlock and "Locked", an open one and
"Unlocked" — not what clicking does; the button itself still toggles.
Preferences owns the passphrase ("Change…"), the auto-lock `ComboRow` and "Lock
now", and the identities window's add/import dialogs carry the passphrase field
Preferences owns the passphrase ("Change…"), the auto-lock `ComboRow` and a
lock row that **toggles with the vault's state**: unlocked it offers "Lock",
locked it offers "Unlock…" through the same `present_unlock` dialog the header
button uses — which is what lets a locked user reach "Change…" without leaving
Preferences. It is inert until a key (and so a passphrase) exists. The
identities window's add/import dialogs carry the passphrase field
(with a confirmation only when that call *sets* the passphrase). Export needs
the vault open first (`with_unlocked`).
- **Never log, print or put the passphrase in argv/env.** The passphrase is
+95 -4
View File
@@ -38,6 +38,36 @@ const AUTO_LOCK: [(u32, &str); 6] = [
/// answers silently.
const AUTO_APPROVE_MAX: f64 = 600.0;
/// What the lock row shows and whether its button is live, as
/// `(title, subtitle, button label, sensitive)`.
///
/// The row is a **toggle** mirroring the header lock button: while locked it
/// offers to open the vault — which is the only route to "Change…" from
/// Preferences — and while unlocked it offers to close it. With no vault (no
/// identity key) there is neither, so the row is inert.
fn lock_row_state(locked: bool, has_vault: bool) -> (&'static str, &'static str, &'static str, bool) {
match (has_vault, locked) {
(false, _) => (
"Lock now",
"Require the passphrase before the next signature",
"Lock",
false,
),
(true, true) => (
"Unlock",
"Locked — the passphrase is needed before the next signature",
"Unlock…",
true,
),
(true, false) => (
"Lock now",
"Require the passphrase before the next signature",
"Lock",
true,
),
}
}
pub struct Preferences {
dialog: adw::PreferencesDialog,
state: Rc<AppState>,
@@ -46,6 +76,7 @@ pub struct Preferences {
toggle: gtk::Button,
passphrase: adw::ActionRow,
passphrase_button: gtk::Button,
lock_row: adw::ActionRow,
lock_button: gtk::Button,
auto_lock: adw::ComboRow,
auto_approve: adw::SpinRow,
@@ -95,6 +126,7 @@ impl Preferences {
.model(&gtk::StringList::new(&labels))
.build();
// Filled by `refresh`: the row toggles between locking and unlocking.
let lock_row = plain::row(
"Lock now",
"Require the passphrase before the next signature",
@@ -149,6 +181,7 @@ impl Preferences {
toggle,
passphrase,
passphrase_button,
lock_row,
lock_button,
auto_lock,
auto_approve,
@@ -177,9 +210,25 @@ impl Preferences {
let button = preferences.lock_button.clone();
let preferences = Rc::clone(&preferences);
button.connect_clicked(move |_| {
preferences.state.store().lock();
eprintln!("regent: the key vault is locked");
preferences.state.notify();
let store = preferences.state.store();
// The row is a toggle: locked offers to open the vault (the same
// dialog every other route uses — the header button, the
// identities window, an approval), unlocked closes it.
if !store.is_locked() {
store.lock();
eprintln!("regent: the key vault is locked");
preferences.state.notify();
return;
}
let widget: gtk::Widget = preferences.lock_row.clone().upcast();
let state = Rc::clone(&preferences.state);
glib::MainContext::default().spawn_local(async move {
// User-initiated, so no deadline; cancel or a wrong
// passphrase leaves the vault locked.
if crate::ui::approval::present_unlock(&widget, store, None).await {
state.notify();
}
});
});
}
{
@@ -254,8 +303,21 @@ impl Preferences {
}
let locked = self.state.is_locked();
// The lock row is a **toggle**: while locked it offers to open the vault
// — so the passphrase can be changed without leaving Preferences — and
// while unlocked it offers to close it. It is the same action as the
// header button, presented as a row.
let (title, subtitle, label, sensitive) =
lock_row_state(locked, self.state.store().has_vault());
self.lock_row.set_title(title);
self.lock_row.set_subtitle(subtitle);
self.lock_button.set_label(label);
// Nothing to lock or unlock until a key — and so a passphrase — exists.
self.lock_button.set_sensitive(sensitive);
// Changing the passphrase needs the decrypted keys, so it stays gated on
// an open vault; unlocking from the row above is what opens it.
self.passphrase_button.set_sensitive(!locked);
self.lock_button.set_sensitive(!locked);
self.passphrase.set_subtitle(if locked {
"Locked — unlock to change it"
} else {
@@ -350,3 +412,32 @@ impl Preferences {
dialog.present(self.window().as_ref());
}
}
#[cfg(test)]
mod tests {
use super::*;
/// The row is a toggle: locked offers to open the vault, unlocked offers to
/// close it. That is what lets a locked user reach "Change…" without
/// leaving Preferences.
#[test]
fn the_lock_row_offers_the_other_state() {
let (unlocked_title, _, unlocked_label, unlocked_sensitive) = lock_row_state(false, true);
let (locked_title, _, locked_label, locked_sensitive) = lock_row_state(true, true);
assert_eq!(unlocked_title, "Lock now");
assert_eq!(unlocked_label, "Lock");
assert_eq!(locked_title, "Unlock");
assert_eq!(locked_label, "Unlock…");
assert!(unlocked_sensitive && locked_sensitive);
assert_ne!(unlocked_label, locked_label);
}
/// No vault means no passphrase: there is neither something to lock nor to
/// unlock, so the row is inert — the same rule the header button follows.
#[test]
fn the_lock_row_is_inert_without_a_vault() {
assert!(!lock_row_state(true, false).3);
assert!(!lock_row_state(false, false).3);
}
}