fix(ui-egui): ask about unsaved changes with the shared dialog

Close, Close all, Close to the right, Quit and the window's close now show
craft-libs' UnsavedChangesDialog with document wording only: title
"Unsaved changes", the catalog's question, buttons Don't Save, Cancel,
Save in the platform's dialog order. Drops the app's own title, message,
icon and "Don't save" label. Tests follow the shared words; a new test
pins the button order.

Revert keeps its own question, now with Cancel as the default: at this
craft-libs pin the shared Revert question makes Revert the Enter answer
and the first focus, and nothing that discards edits may be. A new test
checks that Enter and Space on the first focus keep the edits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
arnav-hiwarkar
2026-12-03 20:18:23 +05:30
parent cd5c601357
commit e00bf4eefc
13 changed files with 74 additions and 58 deletions
+1 -1
View File
@@ -358,7 +358,7 @@ the Tools side, the open panels and their widths. Invalid requests return an err
the document or the panels.
`resize` requests a native inner-window size in logical points (each dimension must be between 320 and 8192); the window manager may constrain it. `state` reports the observed `window.width`, `window.height` and `window.pixels_per_point`. `focus` accepts exactly one widget `id` or unique enabled, focusable `label`; subsequent `key` requests traverse or activate the actual widgets. `inspect` includes focusability and accepts a `focused` filter. `quit` takes no parameters: clean documents exit normally, while unsaved edits or typing open the existing Save/Don't save/Cancel prompt and return `quitting: false, needs_confirmation: true`. It never silently discards work. Window resizing and application quit are native-only.
`resize` requests a native inner-window size in logical points (each dimension must be between 320 and 8192); the window manager may constrain it. `state` reports the observed `window.width`, `window.height` and `window.pixels_per_point`. `focus` accepts exactly one widget `id` or unique enabled, focusable `label`; subsequent `key` requests traverse or activate the actual widgets. `inspect` includes focusability and accepts a `focused` filter. `quit` takes no parameters: clean documents exit normally, while unsaved edits or typing open the existing Save/Don't Save/Cancel prompt and return `quitting: false, needs_confirmation: true`. It never silently discards work. Window resizing and application quit are native-only.
---
+3 -1
View File
@@ -64,7 +64,8 @@ impl PdfCraftApp {
self.prompts.full_screen = Some((dialog, id));
}
/// "Revert" over the active document: Proceed runs `revert_active`.
/// "Revert" over the active document: Proceed runs `revert_active`. Reverting throws edits away, so Cancel is the
/// default: it has the focus first and answers Enter, and only a click, its letter or a moved focus reverts.
pub(crate) fn ask_revert(&mut self) {
let name = self.active_ids().and_then(|(_, id)| self.session.get(id)).map(|d| d.name.clone()).unwrap_or_default();
let message = crate::i18n::fmt(
@@ -75,6 +76,7 @@ impl PdfCraftApp {
.title(tl!("Revert"))
.button(PromptChoice::Proceed, tl!("Revert"))
.button(PromptChoice::Cancel, tl!("Cancel"))
.default_choice(PromptChoice::Cancel)
.instance("revert");
self.prompts.revert = Some(self.dialogs.open(prompt));
}
+2 -17
View File
@@ -262,23 +262,8 @@ impl PdfCraftApp {
}
let name = self.session.get(doc).map(|d| d.name.clone()).unwrap_or_default();
let shown = crate::dialogs::shorten_middle(&name, 80);
// Production asks about each document in turn, with no Apply to All box (`remaining(1)` leaves it out).
// The question as production words it, on every platform: the name in the title, the consequence as a note.
let dialog = UnsavedChangesDialog::new(scope, shown.clone()).remaining(1);
let dialog = if scope == CloseScope::Revert {
dialog
} else {
let template = match scope {
CloseScope::Quit => tl!("Save changes to “{name}” before quitting?"),
_ => tl!("Save changes to “{name}” before closing?"),
};
dialog
.document_wording(true)
.title(crate::i18n::fmt(template, &[("name", &shown)]))
.message(tl!("Your changes will be lost if you don't save them."))
.icon(craft::behavior::IconId::new("save"))
.dont_save_label(tl!("Don't save"))
};
// Each document is asked about in turn, with no Apply to All box (`remaining(1)` leaves it out).
let dialog = UnsavedChangesDialog::new(scope, shown).document_wording(true).remaining(1);
self.close_guard_dialog = Some(self.dialogs.open(dialog));
}
+2 -7
View File
@@ -195,17 +195,12 @@ mod tests {
app_on(Arc::new(craft::host::ManualJobs::new()))
}
/// The unsaved-changes dialog's "don't keep the changes" and Cancel answers as the framework words
/// them on this OS.
/// The unsaved-changes dialog's Don't Save and Cancel answers as the framework words them.
fn answers() -> (String, String) {
// PLAN: assumed paths `craft::ui::dialog::DialogWords`, `craft::ui::runtime::words::CommonWords`,
// `craft::i18n::WordSet`; their fields display as their English text.
use craft::i18n::WordSet as _;
let dialog = craft::ui::dialog::DialogWords::english();
let common = craft::ui::runtime::words::CommonWords::english();
// The app words the question as production does on every platform: "Don't save", in lower case.
let _ = dialog;
("Don't save".to_string(), common.cancel.as_str().to_string())
(dialog.unsaved_dont_save.as_str().to_string(), common.cancel.as_str().to_string())
}
#[test]
+26 -15
View File
@@ -57,7 +57,7 @@ fn save_answer() -> &'static str {
/// The unsaved-changes question's Don't Save answer in the platform's words.
fn dont_save() -> &'static str {
"Don't save"
"Don't Save"
}
/// The next Save's picker answers `path`.
@@ -547,7 +547,7 @@ fn closing_a_dirty_tab_asks_and_cancel_keeps_it() {
h.run_steps(3);
h.key_press_modifiers(Modifiers::COMMAND, Key::W);
h.run_steps(3);
assert!(h.query_all_by_label_contains("Save changes to “doc.pdf”").next().is_some());
assert!(h.query_all_by_label_contains("save the changes you made to “doc.pdf”").next().is_some());
h.get_by_label("Cancel").click();
h.run_steps(3);
assert_eq!(h.state().views.len(), 1, "cancel keeps the document open");
@@ -581,7 +581,7 @@ fn closing_a_dirty_tab_can_save_first() {
#[test]
fn the_save_prompt_answers_to_the_keyboard() {
// Issue #8: Enter saves; ⌘D / Ctrl+D and Alt+D (the Don't save button's letter) don't save;
// Issue #8: Enter saves; ⌘D / Ctrl+D and Alt+D (the Don't Save button's letter) don't save;
// Escape cancels. While the prompt is open, ⌘D is not Document properties.
for (m, key) in [(Modifiers::COMMAND, Key::D), (Modifiers::ALT, Key::D)] {
let mut h = organize(2);
@@ -589,7 +589,7 @@ fn the_save_prompt_answers_to_the_keyboard() {
h.run_steps(3);
h.key_press_modifiers(Modifiers::COMMAND, Key::W);
h.run_steps(3);
assert!(h.query_all_by_label_contains("Save changes to “doc.pdf”").next().is_some());
assert!(h.query_all_by_label_contains("save the changes you made to “doc.pdf”").next().is_some());
h.key_press_modifiers(m, key);
h.run_steps(3);
assert!(h.state().views.is_empty(), "{m:?}+{key:?} doesn't save and closes");
@@ -617,14 +617,25 @@ fn the_save_prompt_answers_to_the_keyboard() {
let _ = std::fs::remove_file(out);
}
/// An organize harness with a dirty document and the "Save changes?" prompt open.
/// Every Craft app asks the same question: Save, Don't Save and Cancel, in the order every Craft dialog uses on the
/// platform (Save first on Windows; Don't Save, Cancel, Save elsewhere).
#[test]
fn the_save_prompt_names_the_actions_in_the_platform_order() {
let h = dirty_prompt();
let x = |label: &str| h.query_all_by_label(label).last().unwrap_or_else(|| panic!("no {label} button")).rect().center().x;
let (dont, cancel, save) = (x(dont_save()), x("Cancel"), x(save_answer()));
let platform_order = if cfg!(windows) { save < dont && dont < cancel } else { dont < cancel && cancel < save };
assert!(platform_order, "Don't Save {dont}, Cancel {cancel}, Save {save}");
}
/// An organize harness with a dirty document and the unsaved-changes prompt open.
fn dirty_prompt() -> Harness<'static, PdfCraftApp> {
let mut h = organize(2);
h.get_by_label("Rotate clockwise").click();
h.run_steps(3);
h.key_press_modifiers(Modifiers::COMMAND, Key::W);
h.run_steps(3);
assert!(h.query_all_by_label_contains("Save changes to “doc.pdf”").next().is_some());
assert!(h.query_all_by_label_contains("save the changes you made to “doc.pdf”").next().is_some());
h
}
@@ -643,13 +654,13 @@ fn press_after(keys: &[(Modifiers, Key)], activate: Key) -> Harness<'static, Pdf
#[test]
fn the_save_prompt_escape_still_cancels_whatever_is_chosen() {
let h = press_after(&[(Modifiers::NONE, Key::ArrowLeft), (Modifiers::NONE, Key::ArrowLeft)], Key::Escape);
assert_eq!(h.state().views.len(), 1, "Escape cancels even with Don't save chosen");
assert_eq!(h.state().views.len(), 1, "Escape cancels even with Don't Save chosen");
assert!(dirty(&h));
}
#[test]
fn the_save_prompt_starts_on_save_every_time_it_opens() {
// Move to Don't save, cancel with Escape, and ask again: the choice is back on Save.
// Move to Don't Save, cancel with Escape, and ask again: the choice is back on Save.
let out = temp_path("reopened.pdf");
let mut h = dirty_prompt();
queue_save(&out);
@@ -706,7 +717,7 @@ fn keys_leave_the_document_under_a_dialog_alone() {
assert_eq!(h.state().views[0].target_pages(), [1], "Escape went to the dialog, not the page selection");
h.key_press_modifiers(Modifiers::COMMAND, Key::W);
h.run_steps(3);
assert!(h.query_all_by_label_contains("Save changes to “doc.pdf”").next().is_some()); // with the dialog closed, ⌘W asks to close the file again
assert!(h.query_all_by_label_contains("save the changes you made to “doc.pdf”").next().is_some()); // with the dialog closed, ⌘W asks to close the file again
}
#[test]
@@ -732,10 +743,10 @@ fn quitting_with_unsaved_changes_asks_for_each_document() {
}
h.key_press_modifiers(Modifiers::COMMAND, Key::Q);
h.run_steps(3);
assert!(h.query_all_by_label_contains("Save changes to “doc.pdf”").next().is_some());
assert!(h.query_all_by_label_contains("save the changes you made to “doc.pdf”").next().is_some());
h.get_by_label(dont_save()).click();
h.run_steps(3);
assert!(h.query_all_by_label_contains("Save changes to “second.pdf”").next().is_some());
assert!(h.query_all_by_label_contains("save the changes you made to “second.pdf”").next().is_some());
h.get_by_label(dont_save()).click();
h.run_steps(3);
assert!(h.state().views.is_empty());
@@ -1004,7 +1015,7 @@ fn save_prompt_stays_inside_the_screen_for_a_long_filename() {
h.run_steps(4);
let screen = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1365.0, 719.0));
let inside = |r: egui::Rect| screen.contains(r.min) && screen.contains(r.max);
let title = h.query_all_by_label_contains("Save changes to").last().expect("the prompt's title");
let title = h.query_all_by_label("Unsaved changes").last().expect("the prompt's title");
let title_rect = title.rect();
assert!(inside(title_rect), "the title rect {title_rect:?} leaves the screen");
for button in [save_answer(), "Cancel", dont_save()] {
@@ -1046,10 +1057,10 @@ fn save_prompt_fits_the_smallest_window_whatever_the_name() {
h.run_steps(4);
let screen = egui::Rect::from_min_size(egui::Pos2::ZERO, size);
let inside = |r: egui::Rect| screen.contains(r.min) && screen.contains(r.max);
let title = h.query_all_by_label_contains("Save changes to").last().expect("the prompt's title");
let title = h.query_all_by_label("Unsaved changes").last().expect("the prompt's title");
let title_rect = title.rect();
assert!(inside(title_rect), "{start}…: the title rect {title_rect:?} leaves the screen");
let opening = format!("Save changes to “{start}");
let opening = format!("Do you want to save the changes you made to “{start}");
assert!(
h.query_all_by(|n| n
.label()
@@ -1057,7 +1068,7 @@ fn save_prompt_fits_the_smallest_window_whatever_the_name() {
.is_some_and(|l| l.starts_with(&opening) && l.contains('…') && l.contains(".pdf” before closing?")))
.next()
.is_some(),
"{start}…: the title keeps the start and the extension"
"{start}…: the question keeps the start and the extension"
);
for button in [save_answer(), "Cancel", dont_save()] {
let rect = h.query_all_by_label(button).last().unwrap_or_else(|| panic!("no {button} button")).rect();
+2 -2
View File
@@ -65,9 +65,9 @@ fn card_count(h: &Harness<'static, PdfCraftApp>) -> usize {
toast::cards(&h.ctx, &WindowKey::main()).len()
}
/// Whether the "Save changes?" prompt is on screen: an alert dialog, the framework's role for the question.
/// Whether the unsaved-changes prompt is on screen: an alert dialog, the framework's role for the question.
fn asks_to_save(h: &Harness<'static, PdfCraftApp>) -> bool {
h.query_by(|n| n.role() == egui::accesskit::Role::AlertDialog && n.label().is_some_and(|l| l.starts_with("Save changes to “"))).is_some()
h.query_by(|n| n.role() == egui::accesskit::Role::AlertDialog && n.label().is_some_and(|l| l == "Unsaved changes")).is_some()
}
/// The unsaved-changes question's Save answer in the platform's words (production's wording, on every platform).
+1 -1
View File
@@ -156,7 +156,7 @@ fn save_answer() -> String {
/// The unsaved-changes question's "don't save" answer, in production's words on every platform.
fn dont_save_answer() -> String {
"Don't save".to_string()
"Don't Save".to_string()
}
/// Presses the button called `label` of the dialog on top (the last such node drawn).
+2 -3
View File
@@ -128,8 +128,7 @@ fn leftovers(dir: &Path) -> Vec<craft::platform::autosave::RecoveryEntry> {
/// The label of the question's "don't save" button on this platform.
fn dont_save() -> &'static str {
// PLAN: the words come from `DialogWords` (`unsaved_dont_save` on macOS, `unsaved_no` elsewhere); literal here as the catalog's English.
"Don't save"
"Don't Save"
}
#[test]
@@ -267,7 +266,7 @@ fn quitting_cleanly_leaves_nothing_behind() {
h.state_mut().apply_edit(&ctx_h, Edit::RotatePages { pages: vec![0], degrees: 90 });
h.state_mut().autosave_now();
assert_eq!(files_in(&dir), 2);
// The window's close → the question → "Don't save" → the app closes and the autosave is removed.
// The window's close → the question → "Don't Save" → the app closes and the autosave is removed.
h.input_mut().viewports.entry(egui::ViewportId::ROOT).or_default().events.push(egui::ViewportEvent::Close);
h.run_steps(2);
h.get_by_label(dont_save()).click();
+4 -2
View File
@@ -307,8 +307,10 @@ fn verify_dirty_snapshot_close(h: &mut Harness<'static, PdfCraftApp>) {
h.state_mut().views[1].close_requested = true;
h.run_steps(2);
assert_eq!(h.state().views.len(), 2);
assert!(h.query_all_by_label("Save changes to “contract_signed (signed version).pdf” before closing?").next().is_some());
let discard = "Don't save";
assert!(
h.query_all_by_label("Do you want to save the changes you made to “contract_signed (signed version).pdf” before closing?").next().is_some()
);
let discard = "Don't Save";
h.query_all_by_label(discard).last().expect("the question has a Don't Save / No button").click();
h.run_steps(2);
assert_eq!(h.state().views.len(), 1);
+2 -2
View File
@@ -24,7 +24,7 @@ fn page_size(s: &PdfCraftApp, i: usize) -> Option<egui::Vec2> {
/// The label of the unsaved-changes dialog's "don't keep the changes" answer on this OS, as the
/// framework words it.
fn dont_save() -> String {
"Don't save".to_owned()
"Don't Save".to_owned()
}
/// `pages` portrait pages (300 × 400) with a line of text each.
@@ -246,7 +246,7 @@ fn split_again_or_closing_a_panes_last_tab_ends_split_view() {
assert_eq!(h.state().views.len(), 1);
assert!(h.state().session.get(h.state().views[0].id).unwrap().dirty, "its edit is still there");
// Typing a view holds of its own (not in the document yet) is asked about, even with the
// document open in the other pane; Don't save closes that view, not the other.
// document open in the other pane; Don't Save closes that view, not the other.
h.key_press_modifiers(Modifiers::COMMAND, Key::Backslash);
h.run_steps(3);
let second = h.state().views.iter().position(|v| v.place() == Place::Second).unwrap();
+2 -2
View File
@@ -1568,7 +1568,7 @@ fn closing_tabs_keeps_the_selected_document_or_selects_its_neighbor() {
);
assert_eq!(app.session.docs().len(), 2);
assert!(app.session.get(ids[removed]).is_none());
assert!(h.query_by_label("Don't save").is_none(), "clean tabs close without a prompt");
assert!(h.query_by_label("Don't Save").is_none(), "clean tabs close without a prompt");
for doc in app.session.docs() {
assert_eq!(doc.bytes.as_slice(), FIXTURE);
assert!(!doc.dirty);
@@ -1603,7 +1603,7 @@ fn cancelling_then_discarding_an_earlier_dirty_tab_preserves_the_selected_docume
h.run_steps(3);
h.get_by_label("middle.pdf").click();
h.run_steps(3);
let discard = "Don't save";
let discard = "Don't Save";
close_named_tab(&mut h, "first.pdf, modified");
h.get_by_label(discard);
h.get_by_label("Cancel").click();
+23 -1
View File
@@ -127,7 +127,7 @@ fn close_all_asks_only_for_changed_documents() {
h.run_steps(2);
assert_eq!(h.state().views.len(), 1, "the clean document closed at once");
// b.pdf asks to be saved: the close dialog is on screen.
assert!(h.query_all_by_label_contains("Save changes to “").next().is_some());
assert!(h.query_all_by_label_contains("save the changes you made to “").next().is_some());
}
#[test]
@@ -144,6 +144,28 @@ fn revert_after_confirming() {
assert_eq!((d.info.pages.len(), d.dirty), (2, false));
}
/// Reverting throws the edits away, so no key alone answers it: the question opens on Cancel, and Enter or Space
/// (pressing the button with the focus) keeps the edits.
#[test]
fn revert_is_never_answered_by_enter_or_the_first_focus() {
use egui::Key;
let mut h = harness();
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(&ctx_h, pdfcraft_engine::Edit::DeletePages { pages: vec![0] });
for key in [Key::Enter, Key::Space] {
assert!(h.execute("file.revert"));
h.run_steps(3);
let focused = h.query_by(|n| n.is_focused()).and_then(|n| n.label());
assert_eq!(focused.as_deref(), Some("Cancel"), "the Revert question opens on Cancel");
h.key_press(key);
h.run_steps(3);
let s = h.state();
let d = s.session.get(s.views[1].id).unwrap();
assert_eq!((d.info.pages.len(), d.dirty), (1, true), "{key:?} kept the edit");
assert!(s.dialogs.is_empty(), "{key:?} answered Cancel");
}
}
/// #3632: Alt+← / Alt+→ go back to where a link was followed from and forward again, as in
/// Acrobat and browsers, while ← / → still turn the page.
#[test]
+4 -4
View File
@@ -597,8 +597,8 @@ milestone = "M4"
status = "shipped"
commands = ["file.close"]
tools = ["doc_close"]
evidence = ["crates/ui-egui/tests/it/editing.rs::closing_a_dirty_tab_asks_and_cancel_keeps_it", "crates/ui-egui/tests/it/editing.rs::closing_a_dirty_tab_can_save_first", "crates/ui-egui/tests/it/editing.rs::quitting_with_unsaved_changes_asks_for_each_document", "crates/ui-egui/tests/it/editing.rs::save_prompt_stays_inside_the_screen_for_a_long_filename", "crates/ui-egui/tests/it/editing.rs::the_save_prompt_escape_still_cancels_whatever_is_chosen", "crates/ui-egui/tests/it/editing.rs::the_save_prompt_starts_on_save_every_time_it_opens"]
notes = "Save is chosen when the prompt opens, so Enter saves. Left/Right (and Up/Down) and Tab/Shift+Tab move the choice between Don't save, Cancel and Save, shown with a focus ring; Enter or Space press the chosen button; Escape cancels. Alt+D, Alt+N and Ctrl/Cmd+D still answer Don't save. A prompt that opens while PdfCraft is in the background (Quit in the Dock menu, logging out, an agent's ui.quit) asks once for the user's attention, as a critical request: the Dock icon bounces until PdfCraft is active, the Windows taskbar button flashes until the window is in front, and X11 sets the urgency hint; coming back or answering withdraws it. Not yet: the same keys on the other confirmations (Revert, reload, overwrite); the other confirmations don't ask for attention; on macOS a prompt answered without coming back (over the control channel) leaves the Dock icon bouncing until PdfCraft is activated, because winit can't withdraw the request there. UI tests lost in the craft framework port (#8160), still to replace: `the_save_prompt_moves_between_buttons_with_the_keyboard`, `a_save_prompt_opened_in_the_background_asks_for_attention_until_the_user_returns`."
evidence = ["crates/ui-egui/tests/it/editing.rs::closing_a_dirty_tab_asks_and_cancel_keeps_it", "crates/ui-egui/tests/it/editing.rs::closing_a_dirty_tab_can_save_first", "crates/ui-egui/tests/it/editing.rs::quitting_with_unsaved_changes_asks_for_each_document", "crates/ui-egui/tests/it/editing.rs::save_prompt_stays_inside_the_screen_for_a_long_filename", "crates/ui-egui/tests/it/editing.rs::the_save_prompt_escape_still_cancels_whatever_is_chosen", "crates/ui-egui/tests/it/editing.rs::the_save_prompt_starts_on_save_every_time_it_opens", "crates/ui-egui/tests/it/editing.rs::the_save_prompt_names_the_actions_in_the_platform_order"]
notes = "The prompt is the suite's shared one, titled Unsaved changes, with Save, Don't Save and Cancel in the platform's dialog order (Don't Save, Cancel, Save on macOS and Linux; Save first on Windows). Save is chosen when the prompt opens, so Enter saves. Left/Right (and Up/Down) and Tab/Shift+Tab move the choice between the buttons, shown with a focus ring; Enter or Space press the chosen button; Escape cancels. Alt+D and Ctrl/Cmd+D answer Don't Save. A prompt that opens while PdfCraft is in the background (Quit in the Dock menu, logging out, an agent's ui.quit) asks once for the user's attention, as a critical request: the Dock icon bounces until PdfCraft is active, the Windows taskbar button flashes until the window is in front, and X11 sets the urgency hint; coming back or answering withdraws it. Not yet: the same keys on the other confirmations (Revert, reload, overwrite); the other confirmations don't ask for attention; on macOS a prompt answered without coming back (over the control channel) leaves the Dock icon bouncing until PdfCraft is activated, because winit can't withdraw the request there. UI tests lost in the craft framework port (#8160), still to replace: `the_save_prompt_moves_between_buttons_with_the_keyboard`, `a_save_prompt_opened_in_the_background_asks_for_attention_until_the_user_returns`."
[[feature]]
id = "core.dirty-indicator"
@@ -645,8 +645,8 @@ milestone = "M4"
status = "shipped"
commands = ["file.revert"]
tools = []
evidence = ["crates/ui-egui/tests/it/viewer.rs::revert_after_confirming", "crates/engine/src/tests.rs::revert_goes_back_to_the_saved_version"]
notes = "with confirmation; clears undo, as Acrobat"
evidence = ["crates/ui-egui/tests/it/viewer.rs::revert_after_confirming", "crates/ui-egui/tests/it/viewer.rs::revert_is_never_answered_by_enter_or_the_first_focus", "crates/engine/src/tests.rs::revert_goes_back_to_the_saved_version"]
notes = "with confirmation; clears undo, as Acrobat. The question opens on Cancel: Enter, Escape and Space on the first focus keep the edits; only pressing Revert (click, its letter or a moved focus) reverts."
[[feature]]
id = "core.reload-changed-files"