Skip to content

Commit

Permalink
fix cursor grab issue (bevyengine#7010)
Browse files Browse the repository at this point in the history
# Objective

- Set the cursor grab mode after the window is built, fix bevyengine#7007, clean some conversion code.

## Solution

- Set the cursor grab mode after the window is built.
  • Loading branch information
VitalyAnkh authored and ItsDoot committed Feb 1, 2023
1 parent af1bb0f commit 7197cff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 23 deletions.
11 changes: 1 addition & 10 deletions crates/bevy_winit/src/converters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy_input::{
ButtonState,
};
use bevy_math::Vec2;
use bevy_window::{CursorGrabMode, CursorIcon};
use bevy_window::CursorIcon;

pub fn convert_keyboard_input(keyboard_input: &winit::event::KeyboardInput) -> KeyboardInput {
KeyboardInput {
Expand Down Expand Up @@ -266,12 +266,3 @@ pub fn convert_cursor_icon(cursor_icon: CursorIcon) -> winit::window::CursorIcon
CursorIcon::RowResize => winit::window::CursorIcon::RowResize,
}
}

/// Map [`bevy_window::CursorGrabMode`] to [`winit::window::CursorGrabMode`].
pub fn convert_cursor_grab_mode(mode: CursorGrabMode) -> winit::window::CursorGrabMode {
match mode {
CursorGrabMode::None => winit::window::CursorGrabMode::None,
CursorGrabMode::Confined => winit::window::CursorGrabMode::Confined,
CursorGrabMode::Locked => winit::window::CursorGrabMode::Locked,
}
}
20 changes: 7 additions & 13 deletions crates/bevy_winit/src/winit_windows.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::converters::convert_cursor_grab_mode;
use bevy_math::{DVec2, IVec2};
use bevy_utils::HashMap;
use bevy_window::{
Expand Down Expand Up @@ -165,16 +164,6 @@ impl WinitWindows {
}
}

// Do not set the grab mode on window creation if it's none, this can fail on mobile
if window_descriptor.cursor_grab_mode != CursorGrabMode::None {
match winit_window
.set_cursor_grab(convert_cursor_grab_mode(window_descriptor.cursor_grab_mode))
{
Ok(_) | Err(winit::error::ExternalError::NotSupported(_)) => {}
Err(err) => Err(err).unwrap(),
}
}

winit_window.set_cursor_visible(window_descriptor.cursor_visible);

self.window_id_to_winit.insert(window_id, winit_window.id());
Expand Down Expand Up @@ -207,15 +196,20 @@ impl WinitWindows {
display_handle: winit_window.raw_display_handle(),
};
self.windows.insert(winit_window.id(), winit_window);
Window::new(
let mut window = Window::new(
window_id,
window_descriptor,
inner_size.width,
inner_size.height,
scale_factor,
position,
Some(raw_handle),
)
);
// Do not set the grab mode on window creation if it's none, this can fail on mobile
if window_descriptor.cursor_grab_mode != CursorGrabMode::None {
window.set_cursor_grab_mode(window_descriptor.cursor_grab_mode);
}
window
}

pub fn get_window(&self, id: WindowId) -> Option<&winit::window::Window> {
Expand Down

0 comments on commit 7197cff

Please sign in to comment.