Skip to content

Commit

Permalink
Fix set_cursor_grab on Linux. Closes #714 (#717)
Browse files Browse the repository at this point in the history
The glutin/winit update in #700 changed set_cursor_grab from a bool to
an enum, but always used CursorGrabMode::Locked, which is only available
on macOS. Conditionally compile to use CursorGrabMode::Confined otherwise.
Fixes #714 and fixes #713 

* set_cursor_grab failed with NotSupported(NotSupportedError) on Linux
Patch from #714

* cargo fmt

Co-authored-by: inferno <[email protected]>
  • Loading branch information
iceiix and inferrna authored Aug 28, 2022
1 parent bbbe58a commit fe8d8ec
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,11 @@ fn handle_window_event<T>(
event: winit::event::Event<T>,
) -> bool {
use winit::event::*;
let cursor_grab_mode = if cfg!(target_os = "macos") {
winit::window::CursorGrabMode::Locked
} else {
winit::window::CursorGrabMode::Confined
};
match event {
Event::MainEventsCleared => return true,
Event::DeviceEvent {
Expand Down Expand Up @@ -659,9 +664,7 @@ fn handle_window_event<T>(
use std::f64::consts::PI;

if game.focused {
window
.set_cursor_grab(winit::window::CursorGrabMode::Locked)
.unwrap();
window.set_cursor_grab(cursor_grab_mode).unwrap();
window.set_cursor_visible(false);
if let Some(player) = game.server.player {
let rotation = game
Expand Down Expand Up @@ -719,9 +722,7 @@ fn handle_window_event<T>(
&& !game.screen_sys.is_current_closable()
{
game.focused = true;
window
.set_cursor_grab(winit::window::CursorGrabMode::Locked)
.unwrap();
window.set_cursor_grab(cursor_grab_mode).unwrap();
window.set_cursor_visible(false);
} else if !game.focused {
#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -783,9 +784,7 @@ fn handle_window_event<T>(
screen::SettingsMenu::new(game.vars.clone(), true),
));
} else if game.screen_sys.is_current_closable() {
window
.set_cursor_grab(winit::window::CursorGrabMode::Locked)
.unwrap();
window.set_cursor_grab(cursor_grab_mode).unwrap();
window.set_cursor_visible(false);
game.focused = true;
game.screen_sys.pop_screen();
Expand Down

0 comments on commit fe8d8ec

Please sign in to comment.