From d7ef5280c5619a3a64c0d40f3960778d69f7568e Mon Sep 17 00:00:00 2001 From: Frank <58932224+plexi32@users.noreply.github.com> Date: Wed, 28 Oct 2026 20:15:33 +0000 Subject: [PATCH] Ctrl+W closes the design shown, not the browser tab (#5366) (#5466) Ctrl+W (Cmd+W) closes the active design, asking first when it is unsaved; closing the last one shows the start page. In the web app a capture-phase keydown listener keeps the browser from closing the tab while a design is shown; on the start page the browser closes the tab. Ctrl+W joins the keys the app reserves, and the tab menu's Close shows it. Co-authored-by: Claude Opus 5.5 --- apps/solidcraft-web/src/main.rs | 40 +++++++++++++++++++++++++++++++++ crates/ui-egui/src/documents.rs | 34 ++++++++++++++++++++++++++++ crates/ui-egui/src/keymap.rs | 1 + crates/ui-egui/src/toolbar.rs | 3 +++ 4 files changed, 78 insertions(+) diff --git a/apps/solidcraft-web/src/main.rs b/apps/solidcraft-web/src/main.rs index 5da7a567e..93c3d4d43 100644 --- a/apps/solidcraft-web/src/main.rs +++ b/apps/solidcraft-web/src/main.rs @@ -112,6 +112,13 @@ fn check_download_size(current: usize, incoming: u64) -> Result<(), String> { Ok(()) } +/// Ctrl+W (⌘W, without Shift or Alt) while a design is shown closes it in the app, so the +/// browser must not close the tab (#5366). On the start page the browser closes the tab. +#[cfg(any(target_arch = "wasm32", test))] +fn closes_design(key: &str, command: bool, shift: bool, alt: bool, design_shown: bool) -> bool { + design_shown && command && !shift && !alt && key.eq_ignore_ascii_case("w") +} + #[cfg(target_arch = "wasm32")] mod web { use std::rc::Rc; @@ -163,6 +170,23 @@ mod web { thread_local! { /// An open design has unsaved changes: leaving the page asks first. static UNSAVED: std::cell::Cell = const { std::cell::Cell::new(false) }; + /// A design is shown (not the start page): Ctrl+W closes it, not the browser tab. + static DESIGN_SHOWN: std::cell::Cell = const { std::cell::Cell::new(false) }; + } + + /// Ctrl+W (⌘W) closes the design shown, as on the desktop; on the start page it is left to + /// the browser, which closes the tab (#5366). Browsers that keep Ctrl+W to themselves still + /// close the tab, after asking when a design is unsaved ([`ask_before_unload`]). + fn keep_ctrl_w_for_the_design(window: &web_sys::Window) { + let on_key = wasm_bindgen::closure::Closure::::new(|e: web_sys::KeyboardEvent| { + let command = e.ctrl_key() || e.meta_key(); + if crate::closes_design(&e.key(), command, e.shift_key(), e.alt_key(), DESIGN_SHOWN.with(std::cell::Cell::get)) { + e.prevent_default(); + } + }); + let _ = window.add_event_listener_with_callback_and_bool("keydown", on_key.as_ref().unchecked_ref(), true); + // The listener lives as long as the page. + on_key.forget(); } /// The browser's "leave site? changes you made may not be saved" prompt while a design has @@ -625,6 +649,7 @@ mod web { self.space_mouse.logic(&mut self.app, ctx); self.files_tick(ctx); UNSAVED.with(|u| u.set(solidcraft_ui_egui::documents::any_dirty(&self.app))); + DESIGN_SHOWN.with(|d| d.set(solidcraft_ui_egui::toolbar::design_shown(&self.app))); } fn raw_input_hook(&mut self, ctx: &egui::Context, raw: &mut egui::RawInput) { let now = raw.time.unwrap_or_else(|| ctx.input(|i| i.time)); @@ -724,6 +749,7 @@ mod web { ask_before_unload(&window); } leave_tab_switching_to_browser(&window); + keep_ctrl_w_for_the_design(&window); let Some(document) = window.document() else { return }; let Some(canvas) = document.get_element_by_id(CANVAS_ID).and_then(|e| e.dyn_into::().ok()) else { log::error!("missing "); @@ -956,4 +982,18 @@ mod tests { assert!(!browser_switches_tabs("Tab", false)); assert!(!browser_switches_tabs("s", true)); } + + #[test] + fn ctrl_w_closes_the_design_not_the_tab() { + // Ctrl+W and ⌘W (either case of the key) close the design shown. + assert!(closes_design("w", true, false, false, true)); + assert!(closes_design("W", true, false, false, true)); + // On the start page the browser closes the tab. + assert!(!closes_design("w", true, false, false, false)); + // Ctrl+Shift+W (close the window), plain W and other keys stay the browser's or the app's. + assert!(!closes_design("W", true, true, false, true)); + assert!(!closes_design("w", true, false, true, true)); + assert!(!closes_design("w", false, false, false, true)); + assert!(!closes_design("s", true, false, false, true)); + } } diff --git a/crates/ui-egui/src/documents.rs b/crates/ui-egui/src/documents.rs index 1c5d2c458..b029dce91 100644 --- a/crates/ui-egui/src/documents.rs +++ b/crates/ui-egui/src/documents.rs @@ -877,6 +877,9 @@ pub fn tabs(app: &mut SolveApp, ui: &mut egui::Ui, r: Rect, x0: f32, x1: f32) { let mut chosen = None; for (id, label) in items { let mut item = crate::context_menu::Item::action(id, label, ""); + if id == "ui.closeTab" && i == app.docs.active { + item = item.key("Ctrl+W"); + } item.enabled = id != "ui.closeOtherTabs" || n > 1; let top = ui.cursor().top(); let row = crate::context_menu::list_row(ui, &item); @@ -1430,6 +1433,37 @@ mod tests { assert_eq!(h.app.docs.active, 2, "Escape put it back"); } + /// #5366: Ctrl+W closes the design shown: a saved one at once (the last one leaves the start + /// page), an unsaved one after "save changes?", and on the start page it does nothing (the + /// browser closes its tab there). + #[test] + fn ctrl_w_closes_the_design_shown() { + let mut h = crate::scenario::Harness::new(); + h.app.home.open = false; + h.app.run("solid.box", json!({"length": 10, "width": 10, "height": 10})).unwrap(); + new_design(&mut h.app); + h.app.run("solid.sphere", json!({"diameter": 8})).unwrap(); + h.app.session.mark_saved(); + assert_eq!(h.app.docs.count(), 2); + h.frames(2); + h.step(&json!({"key": "W", "cmd": true})).unwrap(); + h.frames(2); + assert_eq!((h.app.docs.count(), h.app.docs.closing), (1, None), "the saved design closed without asking"); + assert!(!h.app.home.open, "the other design is shown"); + + h.step(&json!({"key": "W", "cmd": true})).unwrap(); + h.frames(2); + assert_eq!((h.app.docs.count(), h.app.docs.closing), (1, Some(0)), "the unsaved one asks first"); + answer_prompt(&mut h.app, Answer::DontSave); + assert!(active_is_blank(&h.app) && h.app.home.open, "the last one closed: the start page"); + + let revision = h.app.session.revision; + h.step(&json!({"key": "W", "cmd": true})).unwrap(); + h.frames(2); + assert_eq!((h.app.docs.closing, h.app.session.revision), (None, revision), "the start page has nothing to close"); + assert!(h.app.home.open); + } + #[test] fn tabs_keep_their_own_design() { let mut a = app(); diff --git a/crates/ui-egui/src/keymap.rs b/crates/ui-egui/src/keymap.rs index fa3d1a1c1..971541226 100644 --- a/crates/ui-egui/src/keymap.rs +++ b/crates/ui-egui/src/keymap.rs @@ -26,6 +26,7 @@ pub const RESERVED: &[&str] = &[ "Ctrl+Shift+S", "Ctrl+N", "Ctrl+O", + "Ctrl+W", "Ctrl+Comma", "Ctrl+Shift+A", "Ctrl+C", diff --git a/crates/ui-egui/src/toolbar.rs b/crates/ui-egui/src/toolbar.rs index 93058b031..d190d5958 100644 --- a/crates/ui-egui/src/toolbar.rs +++ b/crates/ui-egui/src/toolbar.rs @@ -2248,6 +2248,9 @@ pub fn shortcuts(app: &mut SolveApp, ctx: &egui::Context) { }); } _ if !shown => {} + // Close the design shown (asking first when it is unsaved); closing the last one + // shows the start page (#5366). + Key::W if !mods.shift && !mods.alt => crate::documents::close(app, app.docs.active, false), Key::Z => { if !crate::tools::undo_spline_point(app) { drop(app.run("edit.undo", json!({})));