Fit the identities window and its add/import dialogs

The identities window is a fixed size and GTK never resizes a mapped window,
so its default has to fit the first-run empty state. At 420x480 it did not:
the `AdwStatusPage` overflowed (its buttons were cut off), and so did the
"Create an identity key" / "Import an identity key" dialogs, which are
presented as overlays inside the same window.

- Size the window 480x560. 480 wide also drops the empty state's description
  from four lines to three.
- Rebuild create/import as plain `AdwDialog`s in the "Connect an app" shape: a
  header bar with Cancel and the accept button over a scrolling body. An
  `AdwAlertDialog`'s message area only *propagates* its natural height
  (`adw-alert-dialog.c`), so a form taller than the window grows past it and is
  clipped instead of scrolling. The new dialog's body scrolls and its buttons
  stay pinned.
- Keep the dialog open when creating/importing fails, so a passphrase mismatch
  can be corrected (the old alert closed before reporting the error).

Rename/export/remove stay `AdwAlertDialog`s: they are single-field or one-line
and fit.
This commit is contained in:
raucao committed 2026-11-28 15:01:12 +04:00
1 parent a8dd45b890
commit ab09201a2e
3 files changed
+137 -39

No files matched your search

+10 -3
View File
@@ -190,7 +190,10 @@ data/ desktop, metainfo, gschema (all `.in` templates), icons, re
`Windows` registry; the identities window is chrome
(`data/resources/ui/keys.blp`) around the existing `KeysView`. A second
`AppState` would mean a second registry and a second signer on one device key
(see M5), so the registry is created once in `application.rs` and shared.
(see M5), so the registry is created once in `application.rs` and shared. The
window is sized (480×560) for the **first-run empty state**, the tallest thing
it shows — GTK never resizes a mapped window, so the default has to fit it —
and is wide enough for the add/import form dialogs (`keys_view::FORM_WIDTH`).
- **The invariant: the main window is presentable exactly when the responder can
serve prompts.** The approval dialogs are parented to the main window, so a
request must never arrive without it. With no keys there is nothing to sign
@@ -534,8 +537,12 @@ data/ desktop, metainfo, gschema (all `.in` templates), icons, re
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`).
(with a confirmation only when that call *sets* the passphrase). They are plain
`AdwDialog`s — a header bar with Cancel and the accept button over a scrolling
body (`keys_view::form_dialog`) — **not** `AdwAlertDialog`s: an alert's message
area only propagates its natural height, so a form taller than the window is
clipped instead of scrolling. Export needs the vault open first
(`with_unlocked`).
- **Never log, print or put the passphrase in argv/env.** The passphrase is
`Zeroizing` where it crosses the vault and the headless path, and is dropped as
soon as the unlock lands; the GTK dialog fields hand it over as a plain
+2 -2
View File
@@ -3,8 +3,8 @@ using Adw 1;
Adw.ApplicationWindow keys_window {
title: "Identities";
default-width: 420;
default-height: 480;
default-width: 480;
default-height: 560;
content: Adw.ToolbarView toolbar_view {
[top]
+125 -34
View File
@@ -319,6 +319,100 @@ impl PassphraseFields {
const NEW_VAULT_NOTE: &str = "Choose a passphrase for the key vault. Every identity key is \
stored encrypted under it, and it is needed to sign. There is no recovery if you forget it.";
/// The width of the add/import form dialogs.
///
/// A floating `AdwDialog` is clipped by the window it is presented in, and these
/// live in the identities window (480 px), so they must be narrower than that.
/// The approval and "Connect an app" dialogs use `plain::DIALOG_WIDTH` (480),
/// but they live in the wider main window.
const FORM_WIDTH: i32 = 420;
/// A form dialog: a header bar with Cancel and an accept button over a body that
/// scrolls rather than growing past the window.
///
/// Deliberately **not** an `AdwAlertDialog`: its message area only *propagates*
/// its natural height (see `adw-alert-dialog.c`), so a form taller than the
/// window is clipped instead of scrolling — the confirm row and the buttons were
/// cut off in the 480 px identities window. This is the same shape as "Connect
/// an app": an `AdwToolbarView` whose content is a scroller, with
/// `follows_content_size` so the dialog hugs its content.
///
/// Returns the dialog and its accept button; the caller wires the button up.
/// Cancel just closes the dialog.
fn form_dialog(
title: &str,
note: &str,
content: &impl IsA<gtk::Widget>,
accept: &str,
) -> (adw::Dialog, gtk::Button) {
let note = gtk::Label::builder()
.label(note)
.use_markup(false)
.wrap(true)
.wrap_mode(gtk::pango::WrapMode::WordChar)
.xalign(0.0)
.css_classes(["dim-label"])
.build();
let body = gtk::Box::builder()
.orientation(gtk::Orientation::Vertical)
.spacing(18)
.margin_top(18)
.margin_bottom(18)
.margin_start(12)
.margin_end(12)
.build();
body.append(&note);
body.append(content);
// A scroller's natural height is 0 unless it propagates, and the dialog sizes
// to its child's natural height (trap 15). The cap keeps a long body from
// making the dialog taller than the window.
let scrolled = gtk::ScrolledWindow::builder()
.child(&body)
.vexpand(true)
.hscrollbar_policy(gtk::PolicyType::Never)
.propagate_natural_height(true)
.max_content_height(620)
.build();
let header = adw::HeaderBar::new();
header.set_title_widget(Some(&adw::WindowTitle::new(title, "")));
let cancel = gtk::Button::with_label("Cancel");
let accept_button = gtk::Button::builder()
.label(accept)
.css_classes(["suggested-action"])
.build();
header.pack_start(&cancel);
header.pack_end(&accept_button);
let toolbar = adw::ToolbarView::new();
toolbar.add_top_bar(&header);
toolbar.set_content(Some(&scrolled));
// Pin the width on the dialog's child: with `follows_content_size` below,
// libadwaita ignores `content_width` and uses the content's natural width
// instead (trap 16).
toolbar.set_size_request(FORM_WIDTH, -1);
let dialog = adw::Dialog::builder()
.title(title)
.follows_content_size(true)
.child(&toolbar)
.build();
// The user is typing the secret themselves, so a default action is fine here
// (unlike the approval dialog, which deliberately has none).
dialog.set_default_widget(Some(&accept_button));
{
let dialog = dialog.clone();
cancel.connect_clicked(move |_| {
dialog.close();
});
}
(dialog, accept_button)
}
fn present_create(handles: Handles, state: Rc<AppState>) {
let store = state.store();
let first = !store.has_vault();
@@ -336,28 +430,21 @@ fn present_create(handles: Handles, state: Rc<AppState>) {
list.append(&name);
fields.append_to(&list);
let dialog = adw::AlertDialog::builder()
.heading("Create an identity key")
.body(if first {
let (dialog, create) = form_dialog(
"Create an identity key",
if first {
NEW_VAULT_NOTE
} else {
"The new key is encrypted under your vault passphrase."
})
.extra_child(&list)
.build();
plain::dialog(&dialog);
dialog.add_response("cancel", "Cancel");
dialog.add_response("create", "Create");
dialog.set_default_response(Some("create"));
dialog.set_response_appearance("create", adw::ResponseAppearance::Suggested);
dialog.set_close_response("cancel");
},
&list,
"Create",
);
{
let (handles, state) = (handles.clone(), Rc::clone(&state));
dialog.connect_response(None, move |_, response| {
if response != "create" {
return;
}
let dialog_to_close = dialog.clone();
create.connect_clicked(move |_| {
let passphrase = match fields.value() {
Ok(passphrase) => passphrase,
Err(error) => return report_error(&handles, &error),
@@ -366,10 +453,16 @@ fn present_create(handles: Handles, state: Rc<AppState>) {
let after = Rc::clone(&state);
let handles = handles.clone();
let store = Arc::clone(&store);
let dialog = dialog_to_close.clone();
runtime::spawn(
async move { store.generate_user_key(&name, &passphrase).await },
move |result| match result {
Ok(_) => after.refresh_identities(),
Ok(_) => {
after.refresh_identities();
dialog.force_close();
}
// Keep the dialog open so a passphrase that does not match
// can be corrected, unlike the old alert that closed first.
Err(error) => report_error(&handles, &error),
},
);
@@ -397,28 +490,21 @@ fn present_import(handles: Handles, state: Rc<AppState>) {
list.append(&import_password);
fields.append_to(&list);
let dialog = adw::AlertDialog::builder()
.heading("Import an identity key")
.body(if first {
let (dialog, import) = form_dialog(
"Import an identity key",
if first {
NEW_VAULT_NOTE
} else {
"The imported key is re-encrypted under your vault passphrase."
})
.extra_child(&list)
.build();
plain::dialog(&dialog);
dialog.add_response("cancel", "Cancel");
dialog.add_response("import", "Import");
dialog.set_default_response(Some("import"));
dialog.set_response_appearance("import", adw::ResponseAppearance::Suggested);
dialog.set_close_response("cancel");
},
&list,
"Import",
);
{
let (handles, state) = (handles.clone(), Rc::clone(&state));
dialog.connect_response(None, move |_, response| {
if response != "import" {
return;
}
let dialog_to_close = dialog.clone();
import.connect_clicked(move |_| {
let passphrase = match fields.value() {
Ok(passphrase) => passphrase,
Err(error) => return report_error(&handles, &error),
@@ -429,6 +515,7 @@ fn present_import(handles: Handles, state: Rc<AppState>) {
let after = Rc::clone(&state);
let handles = handles.clone();
let store = Arc::clone(&store);
let dialog = dialog_to_close.clone();
runtime::spawn(
async move {
if text.starts_with("ncryptsec1") {
@@ -440,7 +527,11 @@ fn present_import(handles: Handles, state: Rc<AppState>) {
}
},
move |result| match result {
Ok(_) => after.refresh_identities(),
Ok(_) => {
after.refresh_identities();
dialog.force_close();
}
// Keep the dialog open so the input can be corrected.
Err(error) => report_error(&handles, &error),
},
);