Skip to content

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

M3: the titles follow a document opened while the window or tab is hidden (#3640)

Adrian3850c9f52e07
diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs
index 54f871796..583b55a6b 100644
--- a/crates/ui-egui/src/app.rs
+++ b/crates/ui-egui/src/app.rs
@@ -31,6 +31,37 @@ impl Default for PdfCraftApp {
}
impl PdfCraftApp {
+ /// The window's title and, on the web, the page's, from the active document (#3640). Done in
+ /// `logic` too, which also runs while the window or browser tab is hidden: a document opened
+ /// then (a `?file=` download finishing) still names the page an embedding host reads.
+ fn sync_titles(&mut self, ctx: &egui::Context) {
+ // 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). 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 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()));
+ 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(&page_title);
+ }
+ self.page_title = page_title;
+ }
+ }
+
/// Construct an application with the default session, panels, theme and control state.
pub fn new() -> Self {
Self {
@@ -528,6 +559,7 @@ impl eframe::App for PdfCraftApp {
// Last, after everything that can open or answer the "save changes?" question.
self.attend_save_prompt(ctx);
self.sync_native_menu(ctx);
+ self.sync_titles(ctx);
}
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
@@ -547,31 +579,7 @@ impl eframe::App for PdfCraftApp {
for view in &mut self.views {
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). 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 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()));
- 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(&page_title);
- }
- self.page_title = page_title;
- }
+ self.sync_titles(&ctx);
// 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.
ctx.accesskit_node_builder(egui::accesskit_root_id(), |root| root.set_label(self.window_title.as_str()));
diff --git a/crates/ui-egui/tests/it/viewer.rs b/crates/ui-egui/tests/it/viewer.rs
index b08c7bd70..187a8057f 100644
--- a/crates/ui-egui/tests/it/viewer.rs
+++ b/crates/ui-egui/tests/it/viewer.rs
@@ -1027,6 +1027,31 @@ fn the_window_title_marks_a_draft_that_is_not_applied_yet() {
assert_eq!(s.page_title, "b.pdf • — PdfCraft");
}
+/// #3640: a document that arrives while the window or browser tab is hidden, when eframe runs
+/// only `App::logic` and no frame is drawn, still gives the titles its name.
+#[test]
+fn a_document_opened_while_hidden_names_the_titles() {
+ let mut h = harness();
+ h.run_steps(2);
+ let before = h.state().views.len();
+ let bytes = pdfcraft_engine::Session::new().create_from_text("Arrived", "Arrived while hidden").unwrap();
+ h.state().inbox.lock().unwrap().push(("arrived.pdf".into(), bytes.to_vec()));
+ let ctx = h.ctx.clone();
+ let mut frame = eframe::Frame::_new_kittest();
+ // The open runs on a worker and is taken in by a later `logic`, as eframe keeps calling it.
+ for _ in 0..500 {
+ let _ = ctx.run_logic(&egui::RawInput::default(), |ctx| eframe::App::logic(h.state_mut(), ctx, &mut frame));
+ if h.state().views.len() > before {
+ break;
+ }
+ std::thread::sleep(std::time::Duration::from_millis(10));
+ }
+ let s = h.state();
+ assert_eq!(s.views.len(), before + 1, "the document opened in logic");
+ assert_eq!(s.page_title, "arrived.pdf — PdfCraft");
+ assert_eq!(s.window_title, "arrived.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]