From 929fb9d4a30d4abd7e2e69ae7ede0fc259b80976 Mon Sep 17 00:00:00 2001 From: Sylvain <67423638+Le-Syl21@users.noreply.github.com> Date: Tue, 6 Oct 2026 09:11:22 +0200 Subject: [PATCH] eframe: deliver raw mouse motion to the viewport under the pointer, even if unfocused (#8405) * [x] I have followed the instructions in the PR template `DeviceEvent::MouseMotion` is device-level relative motion with no window attached. eframe used to deliver it only to the focused viewport and dropped it when no viewport was focused. That made raw mouse deltas unusable in multi-viewport apps on Wayland, where another viewport can steal focus while the pointer stays over the window the user is interacting with. Now eframe delivers it to the focused viewport if that viewport has the pointer or an active drag, and otherwise to the first viewport that does. Focus is no longer required. Only that window is repainted. glow and wgpu behave the same. egui-winit's guard from #7866 is kept: motion is still ignored unless the pointer is in the window or a button is held, so moving the mouse elsewhere on screen doesn't cause repaints. --------- Co-authored-by: Claude Opus 4.7 (1M context) Co-authored-by: Emil Ernerfeldt --- crates/eframe/src/native/glow_integration.rs | 43 +++++++++++--------- crates/eframe/src/native/wgpu_integration.rs | 43 +++++++++++--------- crates/egui-winit/src/lib.rs | 6 +++ 3 files changed, 54 insertions(+), 38 deletions(-) diff --git a/crates/eframe/src/native/glow_integration.rs b/crates/eframe/src/native/glow_integration.rs index 82560e2e..4d4b417f 100644 --- a/crates/eframe/src/native/glow_integration.rs +++ b/crates/eframe/src/native/glow_integration.rs @@ -141,6 +141,15 @@ struct Viewport { } impl Viewport { + /// Forward raw mouse motion to egui. + /// + /// Returns the window to repaint if the event was used. + fn on_mouse_motion(&mut self, delta: (f64, f64)) -> Option { + let window = self.window.as_ref()?; + let egui_winit = self.egui_winit.as_mut()?; + egui_winit.on_mouse_motion(delta).then(|| window.id()) + } + /// Apply the commands, or defer them until we have a window. fn process_commands( &mut self, @@ -543,27 +552,23 @@ impl WinitApp for GlowWinitApp<'_> { if let winit::event::DeviceEvent::MouseMotion { delta } = event && let Some(running) = &mut self.running { + // `MouseMotion` is not associated with any window, so we deliver it to + // the viewport that has the pointer (or an ongoing drag), preferring the focused one. + // We don't require that viewport to have focus, since another viewport may have stolen it + // (e.g. on Wayland, where focus-stealing prevention can refuse `ViewportCommand::Focus`). let mut glutin = running.glutin.borrow_mut(); - if let Some(viewport) = glutin - .focused_viewport - .and_then(|viewport| glutin.viewports.get_mut(&viewport)) - && let Some(window) = viewport.window.as_ref() + let focused_viewport = glutin.focused_viewport; + let viewports = &mut glutin.viewports; + if let Some(window_id) = focused_viewport + .and_then(|id| viewports.get_mut(&id)) + .and_then(|viewport| viewport.on_mouse_motion(delta)) + .or_else(|| { + viewports + .values_mut() + .find_map(|viewport| viewport.on_mouse_motion(delta)) + }) { - if !window.has_focus() - && !viewport - .egui_winit - .as_ref() - .map(|state| state.is_any_pointer_button_down()) - .unwrap_or(false) - { - return Ok(EventResult::Wait); - } - - if let Some(egui_winit) = viewport.egui_winit.as_mut() - && egui_winit.on_mouse_motion(delta) - { - return Ok(EventResult::RepaintNext(window.id())); - } + return Ok(EventResult::RepaintNext(window_id)); } } diff --git a/crates/eframe/src/native/wgpu_integration.rs b/crates/eframe/src/native/wgpu_integration.rs index 868fe3a6..88f1236a 100644 --- a/crates/eframe/src/native/wgpu_integration.rs +++ b/crates/eframe/src/native/wgpu_integration.rs @@ -497,27 +497,23 @@ impl WinitApp for WgpuWinitApp<'_> { if let winit::event::DeviceEvent::MouseMotion { delta } = event && let Some(running) = &mut self.running { + // `MouseMotion` is not associated with any window, so we deliver it to + // the viewport that has the pointer (or an ongoing drag), preferring the focused one. + // We don't require that viewport to have focus, since another viewport may have stolen it + // (e.g. on Wayland, where focus-stealing prevention can refuse `ViewportCommand::Focus`). let mut shared = running.shared.borrow_mut(); - if let Some(viewport) = shared - .focused_viewport - .and_then(|viewport| shared.viewports.get_mut(&viewport)) - && let Some(window) = viewport.window.as_ref() + let focused_viewport = shared.focused_viewport; + let viewports = &mut shared.viewports; + if let Some(window_id) = focused_viewport + .and_then(|id| viewports.get_mut(&id)) + .and_then(|viewport| viewport.on_mouse_motion(delta)) + .or_else(|| { + viewports + .values_mut() + .find_map(|viewport| viewport.on_mouse_motion(delta)) + }) { - if !window.has_focus() - && !viewport - .egui_winit - .as_ref() - .map(|state| state.is_any_pointer_button_down()) - .unwrap_or(false) - { - return Ok(EventResult::Wait); - } - - if let Some(egui_winit) = viewport.egui_winit.as_mut() - && egui_winit.on_mouse_motion(delta) - { - return Ok(EventResult::RepaintNext(window.id())); - } + return Ok(EventResult::RepaintNext(window_id)); } } @@ -1045,6 +1041,15 @@ impl WgpuWinitRunning<'_> { } impl Viewport { + /// Forward raw mouse motion to egui. + /// + /// Returns the window to repaint if the event was used. + fn on_mouse_motion(&mut self, delta: (f64, f64)) -> Option { + let window = self.window.as_ref()?; + let egui_winit = self.egui_winit.as_mut()?; + egui_winit.on_mouse_motion(delta).then(|| window.id()) + } + /// Apply the commands, or defer them until we have a window. fn process_commands( &mut self, diff --git a/crates/egui-winit/src/lib.rs b/crates/egui-winit/src/lib.rs index 4e824ffa..30599ae5 100644 --- a/crates/egui-winit/src/lib.rs +++ b/crates/egui-winit/src/lib.rs @@ -796,6 +796,12 @@ impl State { } } + /// Forward raw mouse motion (e.g. `winit::event::DeviceEvent::MouseMotion`) to egui + /// as [`egui::Event::MouseMoved`]. + /// + /// The motion is ignored unless the pointer is inside the window or a pointer button is held down, + /// so that mouse motion elsewhere on the screen doesn't cause repaints. + /// /// Returns `true` if the event was sent to egui. pub fn on_mouse_motion(&mut self, delta: (f64, f64)) -> bool { if !self.is_pointer_in_window() && !self.any_pointer_button_down {