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) <noreply@anthropic.com>
Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
authored and GitHub committed 2026-10-06 08:11:22 +01:00
1 parent 74b94e6fb7
commit 929fb9d4a3
3 files changed
+54 -38

No files matched your search

+24 -19
View File
@@ -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<winit::window::WindowId> {
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));
}
}
+24 -19
View File
@@ -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<winit::window::WindowId> {
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,
+6
View File
@@ -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 {