From c4ab2fea705176e9bb3234ab4801b1a8b56a895d Mon Sep 17 00:00:00 2001 From: bmh129 <11478682+bmh129@users.noreply.github.com> Date: Fri, 4 Dec 2026 23:45:22 -0600 Subject: [PATCH] =?UTF-8?q?Windows:=20a=20click=20makes=20its=20window=20a?= =?UTF-8?q?ctive,=20so=20View=20=E2=80=BA=20New=20Window=20copies=20its=20?= =?UTF-8?q?document=20(#9128)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A window became active only when it gained focus. GNOME can keep focus on the first window when New opens another, so clicking the first window again sent no focus change, and the commands it fired ran in the new window. A press or key in a window now makes it the active one. --- crates/ui-egui/src/windows.rs | 12 ++++++++++++ crates/ui-egui/src/windows/tests.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) 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"); +}