PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
M3: the title marks drafts not yet applied, and the web page title names the file (#3640)
Adrian4b7385067122
diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rsindex 352178bb7..54f871796 100644--- a/crates/ui-egui/src/app.rs+++ b/crates/ui-egui/src/app.rs@@ -198,6 +198,7 @@ impl PdfCraftApp {field_table_draft: None,form_preview: false,window_title: String::new(),+ page_title: String::new(),recovery: None,recoverable: Vec::new(),recovery_keys: Default::default(),@@ -547,23 +548,29 @@ impl eframe::App for PdfCraftApp {view.begin_render_frame();}// The window shows the active document's name (or title, if it asks for that), with •- // while it has unsaved changes, as the other Craft apps write it (#3640).- let view = self.active.and_then(|i| self.views.get(i));- let dirty = view.and_then(|v| self.session.get(v.id)).is_some_and(|d| d.dirty);+ // while it has unsaved changes, as the other Craft apps write it (#3640). Unsaved means+ // what closing would ask about, so a draft not yet applied counts too.+ let dirty = self.active.is_some_and(|i| self.has_unsaved_work(i));let mark = if dirty { " •" } else { "" };- let title = self- .active- .and_then(|i| crate::canvas::tab_label(self, i))- .or_else(|| self.combine_showing().then(|| tl!("Combine files").to_owned()))- .map_or_else(|| "PdfCraft".to_owned(), |name| format!("{name}{mark} — PdfCraft"));+ let titled = |name: Option<String>| {+ name.or_else(|| self.combine_showing().then(|| tl!("Combine files").to_owned()))+ .map_or_else(|| "PdfCraft".to_owned(), |name| format!("{name}{mark} — PdfCraft"))+ };+ let title = titled(self.active.and_then(|i| crate::canvas::tab_label(self, i)));+ // The web page's title is what an embedding host reads to tell which file opened, so it+ // names the file even when the document asks to show its own title.+ let page_title = titled(self.active.and_then(|i| self.views.get(i)).and_then(|v| self.session.get(v.id)).map(|d| d.name.clone()));if title != self.window_title {ctx.send_viewport_cmd(egui::ViewportCommand::Title(title.clone()));- // eframe's web runner ignores Title: set the page's own, which an embedding host reads.+ self.window_title = title;+ }+ if page_title != self.page_title {+ // eframe's web runner ignores Title: set the page's own.#[cfg(target_arch = "wasm32")]if let Some(document) = web_sys::window().and_then(|w| w.document()) {- document.set_title(&title);+ document.set_title(&page_title);}- self.window_title = title;+ self.page_title = page_title;}// The accessible window takes the same name, so a screen reader (AT-SPI on Linux, UIA on// Windows) can tell it apart (#4614). egui builds the root node afresh every pass.diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 8a8c51775..f8237f7fa 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -566,6 +566,8 @@ pub struct PdfCraftApp {fonts_cjk: theme::CjkPreference,/// The window title last sent to the platform.pub window_title: String,+ /// The web page's title last set: the window title, but always with the file's name.+ pub page_title: String,/// The UI control channel, when enabled (`--control`; off by default).control: Option<control::Control>,/// A bookmark being renamed in the Bookmarks panel: (path, text so far).diff --git a/crates/ui-egui/tests/it/viewer.rs b/crates/ui-egui/tests/it/viewer.rsindex e151f4cfc..b08c7bd70 100644--- a/crates/ui-egui/tests/it/viewer.rs+++ b/crates/ui-egui/tests/it/viewer.rs@@ -1010,6 +1010,42 @@ fn the_window_title_marks_unsaved_changes_until_saved() {assert_eq!(s.window_title, "b.pdf — PdfCraft", "saved");}+/// #3640: text typed but not yet applied is unsaved work too, as closing the tab says, so the+/// title marks it.+#[test]+fn the_window_title_marks_a_draft_that_is_not_applied_yet() {+ use pdfcraft_ui_egui::fill_sign::TypeBox;+ let mut h = harness();+ h.run_steps(2);+ assert_eq!(h.state().window_title, "b.pdf — PdfCraft");+ let i = h.state().active.unwrap();+ h.state_mut().views[i].fill_text = Some(TypeBox { page: 0, at: [40.0, 200.0], text: "Typed".into(), size: 10.0, focus: false });+ h.run_steps(2);+ let s = h.state();+ assert!(!s.session.get(s.views[i].id).unwrap().dirty, "nothing applied to the document yet");+ assert_eq!(s.window_title, "b.pdf • — PdfCraft");+ assert_eq!(s.page_title, "b.pdf • — PdfCraft");+}++/// #3640: a document that asks to show its own title shows it in the window, but the web page's+/// title, which an embedding host reads to tell which file opened, keeps the file's name.+#[test]+fn the_web_page_title_names_the_file_when_the_document_shows_its_title() {+ use pdfcraft_engine::Edit;+ let mut h = harness();+ {+ let s = h.state_mut();+ let id = s.views[s.active.unwrap()].id;+ s.session.apply(id, Edit::SetInfo { key: "Title".into(), value: "Budget 2026".into() }).unwrap();+ let mut v = s.session.get(id).unwrap().initial_view();+ v.display_title = true;+ s.session.apply(id, Edit::SetInitialView(Box::new(v))).unwrap();+ }+ h.run_steps(2);+ assert_eq!(h.state().window_title, "Budget 2026 • — PdfCraft");+ assert_eq!(h.state().page_title, "b.pdf • — PdfCraft");+}+#[test]fn a_placeholder_title_leaves_the_file_name_on_the_tab_and_window() {// A browser's "Save as PDF" of a blank page or pop-up titles the document "about:blank"