Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

M4: each document's save question asks for attention on its own

Eric Kaden95feaad2692e
diff --git a/crates/ui-egui/src/editing.rs b/crates/ui-egui/src/editing.rs
index 7b3c1c765..d9736c343 100644
--- a/crates/ui-egui/src/editing.rs
+++ b/crates/ui-egui/src/editing.rs
@@ -900,6 +900,8 @@ impl PdfCraftApp {
/// Close tab `index` for a close request, then ask about the next dirty document, or quit.
fn close_and_continue(&mut self, ctx: &egui::Context, index: usize, req: CloseRequest) {
self.close_tab(index);
+ // Its question is answered: a question about the next document is a new one.
+ self.save_prompt_seen = false;
// A newer prompt wins: the user may have started another close while a save was pending.
if (req == CloseRequest::Quit || req == CloseRequest::All) && self.close_request.is_none() {
match self.first_dirty() {
@@ -969,18 +971,22 @@ impl PdfCraftApp {
/// makes it for a modal panel that an inactive app presents: the Dock icon bounces until
/// PdfCraft is active, Windows flashes the taskbar button until the window is in front, and
/// X11 sets the urgency hint, which stays until it is reset. A question that opens in front
- /// asks nothing, and neither does leaving it open: the user has seen it.
+ /// asks nothing, and neither does leaving it open: the user has seen it. Each document asked
+ /// about when quitting is a new question, so one that opens in the background asks even after
+ /// a question the user saw; a request that still stands carries over to it.
pub(crate) fn attend_save_prompt(&mut self, ctx: &egui::Context) {
let open = self.close_request.is_some();
let focused = ctx.input(|i| i.focused);
- if open && !self.save_prompt_was_open && !focused {
+ if self.save_prompt_attention {
+ if focused || !open {
+ ctx.send_viewport_cmd(egui::ViewportCommand::RequestUserAttention(egui::UserAttentionType::Reset));
+ self.save_prompt_attention = false;
+ }
+ } else if open && !self.save_prompt_seen && !focused {
ctx.send_viewport_cmd(egui::ViewportCommand::RequestUserAttention(egui::UserAttentionType::Critical));
self.save_prompt_attention = true;
- } else if self.save_prompt_attention && (focused || !open) {
- ctx.send_viewport_cmd(egui::ViewportCommand::RequestUserAttention(egui::UserAttentionType::Reset));
- self.save_prompt_attention = false;
}
- self.save_prompt_was_open = open;
+ self.save_prompt_seen = open;
}
}
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 0db6e1fef..edfcb3b71 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -642,8 +642,9 @@ pub struct PdfCraftApp {
pub close_request: Option<CloseRequest>,
/// The button of the "save changes?" question the keyboard has chosen; Save when it opens.
pub(crate) save_prompt_button: dialogs::PromptButton,
- /// Whether the "save changes?" question was pending last frame, to tell the frame it opens.
- save_prompt_was_open: bool,
+ /// `attend_save_prompt` has seen the pending "save changes?" question. It is false again once
+ /// the question is answered, so the next document's question is told apart from it.
+ save_prompt_seen: bool,
/// The "save changes?" question asked for the user's attention and the request still stands
/// (see `attend_save_prompt`).
save_prompt_attention: bool,
@@ -1023,7 +1024,7 @@ impl PdfCraftApp {
host_dirty: None,
close_request: None,
save_prompt_button: Default::default(),
- save_prompt_was_open: false,
+ save_prompt_seen: false,
save_prompt_attention: false,
#[cfg(not(target_arch = "wasm32"))]
pending_overwrite: None,
diff --git a/crates/ui-egui/tests/it/control.rs b/crates/ui-egui/tests/it/control.rs
index dc62fa2a7..8bd7b5dd4 100644
--- a/crates/ui-egui/tests/it/control.rs
+++ b/crates/ui-egui/tests/it/control.rs
@@ -276,17 +276,41 @@ fn attention_during(h: &mut Harness<'static, PdfCraftApp>, c: &ControlClient, me
panic!("{method}: no reply after 30 frames");
}
-/// An agent quits while another app is in front, then answers the prompt itself: the request is
-/// withdrawn, so the taskbar or the window list stops flagging a window with nothing to ask.
+/// Open `name` with an unsaved change, in a tab of its own.
+fn open_unsaved(h: &mut Harness<'static, PdfCraftApp>, name: &str) {
+ let app = h.state_mut();
+ app.open_bytes(name, None, fixture(1)).unwrap();
+ app.active = Some(app.views.len() - 1);
+ app.apply_edit(pdfcraft_engine::Edit::RotatePages { pages: vec![0], degrees: 90 });
+}
+
+/// An agent quits while another app is in front, then answers the prompts itself. Quitting asks
+/// about each unsaved document in turn; the request made for the first question stands for the
+/// next, and answering the last withdraws it, so the taskbar or the window list stops flagging a
+/// window with nothing to ask. Each question is a new one: when the first opened in front, the
+/// next one asks when it opens in the background.
#[test]
fn a_save_prompt_answered_in_the_background_withdraws_its_attention_request() {
use egui::UserAttentionType::{Critical, Reset};
let (mut h, c) = harness();
ok(&mut h, &c, "ui.command", json!({"id": "page.insert_blank"}));
+ open_unsaved(&mut h, "second.pdf");
h.input_mut().focused = false;
assert_eq!(attention_during(&mut h, &c, "ui.quit", json!({})), [Critical]);
+ assert!(attention_during(&mut h, &c, "ui.click", json!({"label": "Don't save"})).is_empty(), "the request stands");
+ assert_eq!(attention_during(&mut h, &c, "ui.click", json!({"label": "Cancel"})), [Reset]);
+
+ open_unsaved(&mut h, "third.pdf");
+ h.input_mut().focused = true;
+ assert!(attention_during(&mut h, &c, "ui.quit", json!({})).is_empty(), "the first question opens in front");
+ h.input_mut().focused = false;
+ assert!(attention_during(&mut h, &c, "ui.state", json!({})).is_empty(), "leaving it open asks nothing");
+ assert_eq!(attention_during(&mut h, &c, "ui.click", json!({"label": "Don't save"})), [Critical], "the next opens in the background");
assert_eq!(attention_during(&mut h, &c, "ui.click", json!({"label": "Cancel"})), [Reset]);
- assert_eq!(ok(&mut h, &c, "ui.state", json!({}))["close_prompt"], false);
+ let state = ok(&mut h, &c, "ui.state", json!({}));
+ assert_eq!(state["close_prompt"], false);
+ let left: Vec<_> = state["documents"].as_array().unwrap().iter().map(|d| (d["name"].clone(), d["dirty"].clone())).collect();
+ assert_eq!(left, [(json!("third.pdf"), json!(true))], "Don't save closed doc.pdf and second.pdf; Cancel kept third.pdf");
}
#[test]