diff options
| author | HampusM <hampus@hampusmat.com> | 2026-11-28 15:31:11 +0100 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-11-28 15:31:11 +0100 |
| commit | f514c4ec94a33447ab1ccb45b11346f0f0a2446d (patch) | |
| tree | 26532e944199c7f817d79039c38482548ee74726 /engine | |
| parent | 5411ac376b64c34b0e2c57b86edd859bcd196d26 (diff) | |
feat(engine): add cursor grab mode for confining cursor to window
Diffstat (limited to 'engine')
| -rw-r--r-- | engine/src/windowing.rs | 19 | ||||
| -rw-r--r-- | engine/src/windowing/window.rs | 3 |
2 files changed, 22 insertions, 0 deletions
diff --git a/engine/src/windowing.rs b/engine/src/windowing.rs index 54947eb..d81b57c 100644 --- a/engine/src/windowing.rs +++ b/engine/src/windowing.rs @@ -1266,6 +1266,24 @@ impl ApplicationHandler for App ), )); } + WindowEvent::Focused(is_window_focused) => { + let Some((winit_window, window_settings)) = + self.windows.get(WindowId::from_inner(window_id)) + else { + cold_path(); + return; + }; + + // For some reason, on X11 (XFCE and AwesomeWM, might be more) the cursor + // is confined even after the window loses focus. + if window_settings.cursor_grab_mode == CursorGrabMode::Confined { + let _ = winit_window.set_cursor_grab(if is_window_focused { + winit::window::CursorGrabMode::Confined + } else { + winit::window::CursorGrabMode::None + }); + } + } _ => {} } } @@ -1330,6 +1348,7 @@ impl WindowSettings winit::window::CursorGrabMode::None } CursorGrabMode::Locked => winit::window::CursorGrabMode::Locked, + CursorGrabMode::Confined => winit::window::CursorGrabMode::Confined, }); if let Err(err) = res { diff --git a/engine/src/windowing/window.rs b/engine/src/windowing/window.rs index c601487..b935657 100644 --- a/engine/src/windowing/window.rs +++ b/engine/src/windowing/window.rs @@ -288,6 +288,9 @@ pub enum CursorGrabMode /// The cursor is locked to a specific position in the window. Locked, + + /// The cursor is confined to the inside of the window + Confined, } /// A unique identifier for an X11 visual. |
