Skip to content

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

Bring the window forward when the running app is handed files

manchitmr38e5c4bb5b1e
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 7b421263d..bdc3444c3 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -3047,7 +3047,13 @@ impl eframe::App for PdfCraftApp {
for e in os_events {
match e {
#[cfg(not(target_arch = "wasm32"))]
- OsEvent::Open(paths) => paths.iter().for_each(|p| self.open_path(p)),
+ OsEvent::Open(paths) => {
+ paths.iter().for_each(|p| self.open_path(p));
+ // Files handed to the running app (Finder, or another launch): bring its
+ // window to the front, also when minimized or full screen on another Space.
+ ctx.send_viewport_cmd(egui::ViewportCommand::Minimized(false));
+ ctx.send_viewport_cmd(egui::ViewportCommand::Focus);
+ }
#[cfg(target_arch = "wasm32")]
OsEvent::Open(_) => {}
// Like closing the window: `guard_quit` asks about unsaved changes.
diff --git a/crates/ui-egui/tests/ui.rs b/crates/ui-egui/tests/ui.rs
index b86f7699f..eacc46015 100644
--- a/crates/ui-egui/tests/ui.rs
+++ b/crates/ui-egui/tests/ui.rs
@@ -878,6 +878,15 @@ fn files_and_quit_from_the_operating_system() {
std::fs::remove_file(&path).ok();
assert_eq!(h.state().views.len(), 1, "the file opens in a tab");
h.get_by_label_contains(&name);
+ // A file opened while the app runs brings its window forward, even minimized or full screen
+ // on another Space.
+ std::fs::write(&path, FIXTURE).unwrap();
+ queue.borrow_mut().push(OsEvent::Open(vec![path.to_string_lossy().into_owned()]));
+ h.step();
+ std::fs::remove_file(&path).ok();
+ let commands: Vec<_> = h.output().viewport_output.values().flat_map(|v| v.commands.clone()).collect();
+ assert!(commands.contains(&egui::ViewportCommand::Minimized(false)), "{commands:?}");
+ assert!(commands.contains(&egui::ViewportCommand::Focus), "{commands:?}");
// Quit closes the window like the close button, so unsaved changes are asked about.
queue.borrow_mut().push(OsEvent::Quit);
h.step();