Skip to content

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

fix(macos): close the empty main window with Command-W (#930)

Nigel Ihza Firdausy88869196bec9
diff --git a/crates/ui-egui/src/commands.rs b/crates/ui-egui/src/commands.rs
index f4886f94d..546ea74c3 100644
--- a/crates/ui-egui/src/commands.rs
+++ b/crates/ui-egui/src/commands.rs
@@ -649,6 +649,11 @@ impl PdfCraftApp {
// this document only.
self.deferred_commands.push((spec.id, active.map(|(_, id)| id)));
ctx.request_repaint();
+ } else if spec.id == "file.close" && active.is_none() && crate::commands::mac_shortcuts(ctx) && !cfg!(target_arch = "wasm32") {
+ // macOS ⌘W closes the last window when no document tabs remain (#869).
+ // Route through the normal viewport-close path (and its quit guard),
+ // not the disabled document-only `file.close` command.
+ ctx.send_viewport_cmd(egui::ViewportCommand::Close);
} else {
self.execute(spec.id);
}
diff --git a/crates/ui-egui/tests/commands.rs b/crates/ui-egui/tests/commands.rs
index 08d023778..48a18eb42 100644
--- a/crates/ui-egui/tests/commands.rs
+++ b/crates/ui-egui/tests/commands.rs
@@ -452,3 +452,27 @@ fn the_about_dialog_shows_the_running_version() {
h.run_steps(3);
h.get_by_label_contains(&format!("Version {}", env!("CARGO_PKG_VERSION")));
}
+
+#[test]
+fn command_w_closes_empty_macos_window_but_not_an_empty_windows_window() {
+ for (os, should_close) in [(OperatingSystem::Mac, true), (OperatingSystem::Windows, false)] {
+ let mut h = Harness::builder().with_size(egui::vec2(1000.0, 700.0)).build_eframe(|_cc| PdfCraftApp::new());
+ h.ctx.set_os(os);
+ h.run_steps(4);
+ assert!(h.state().views.is_empty());
+ h.event(egui::Event::Key { key: Key::W, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::COMMAND });
+ h.step();
+ let closes = h.output().viewport_output.values().any(|v| v.commands.iter().any(|c| matches!(c, egui::ViewportCommand::Close)));
+ assert_eq!(closes, should_close, "{os:?}: only an empty macOS window should close");
+ }
+}
+
+#[test]
+fn command_w_still_closes_the_document_tab_on_macos() {
+ let mut h = harness();
+ h.ctx.set_os(OperatingSystem::Mac);
+ h.run_steps(2);
+ h.key_press_modifiers(Modifiers::COMMAND, Key::W);
+ h.run_steps(2);
+ assert!(h.state().views.is_empty(), "⌘W closes the clean document tab");
+}