diff --git a/crates/ui-egui/src/windows.rs b/crates/ui-egui/src/windows.rs index eddc966597..d3b2c16c13 100644 --- a/crates/ui-egui/src/windows.rs +++ b/crates/ui-egui/src/windows.rs @@ -678,6 +678,18 @@ impl WordApp { return; } let ctx = ui.ctx().clone(); + // A press or a key in this window makes it the active one, as gaining focus does: a desktop that keeps focus + // on this window when another opens (GNOME on Wayland) sends it no focus change when it is used again, and the + // commands it fires would run in the other window (#9128). + let used = ui.input(|i| { + i.events.iter().any(|e| matches!(e, egui::Event::PointerButton { pressed: true, .. } | egui::Event::Key { pressed: true, .. })) + }); + if self.windows.native + && used + && let Some(id) = self.windows.id_of(key) + { + self.windows.list.focus(id); + } let queued = self.windows.get_mut(key).map(|w| std::mem::take(&mut w.requests)).unwrap_or_default(); for req in queued { self.handle_request(&req); diff --git a/crates/ui-egui/src/windows/tests.rs b/crates/ui-egui/src/windows/tests.rs index 7be77732d8..bd425c37d4 100644 --- a/crates/ui-egui/src/windows/tests.rs +++ b/crates/ui-egui/src/windows/tests.rs @@ -1423,3 +1423,32 @@ fn ui_closewindow_reports_not_requested_while_a_prompt_is_up() { let r = ctl(&mut a, &rig, "ui.closeWindow", json!({})); assert_eq!(r["result"]["requested"], false, "{r}"); } + +/// #9128: a desktop that keeps focus on the first window when New opens a second (GNOME on Wayland) sends the first +/// no focus change when it is clicked again. The click still makes it the active window, so the command it fires +/// (View › New Window) runs there and copies its document, not the blank one. +#[test] +fn a_click_in_a_window_that_kept_focus_runs_its_command_there() { + use craft::ui::runtime::commands::{CommandRef, FirePhase, FireSource, Fired}; + use craft::ui::runtime::frame::FrameOutput; + let mut rig = Rig::new(); + let mut a = app("Typed"); + rig.focus(ViewportId::ROOT); + rig.frames(&mut a, 2); + let typed = a.windows.shown(); + a.run("file.new", json!({})).unwrap(); + rig.frames(&mut a, 2); + let blank = a.windows.shown(); + assert_ne!(blank, typed); + assert_eq!(a.windows.active(), Some(blank), "New makes the blank window active while the desktop keeps focus on the first"); + + let at = egui::pos2(100.0, 100.0); + let press = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }; + rig.frame_events(&mut a, vec![egui::Event::PointerMoved(at), press(true), press(false)], Vec::new()); + let mut out = FrameOutput::default(); + out.fired.push(Fired::new(CommandRef::new("view.newWindow"), FireSource::Pointer, FirePhase::Press, 0.0)); + a.run_fired(&rig.ctx(), &out); + rig.frames(&mut a, 2); + assert_eq!(a.window_count(), 3); + assert_eq!(text(&a), "Typed", "the new window shows the clicked window's document"); +}