Skip to content

Commit 150d8ff

Browse files
committed
Prep for next version of winit
1 parent e28b88b commit 150d8ff

File tree

4 files changed

+55
-31
lines changed

4 files changed

+55
-31
lines changed

crates/bevy_window/src/window.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ pub struct Window {
184184
decorations: bool,
185185
cursor_icon: CursorIcon,
186186
cursor_visible: bool,
187-
cursor_locked: bool,
187+
cursor_grab_mode: CursorGrabMode,
188188
physical_cursor_position: Option<DVec2>,
189189
raw_window_handle: RawWindowHandleWrapper,
190190
focused: bool,
@@ -231,9 +231,10 @@ pub enum WindowCommand {
231231
SetDecorations {
232232
decorations: bool,
233233
},
234-
/// Set whether or not the cursor's postition is locked.
235-
SetCursorLockMode {
236-
locked: bool,
234+
/// Set whether or not the cursor's postition is locked in place, confined to the window
235+
/// or free to move anywhere
236+
SetCursorGrabMode {
237+
mode: CursorGrabMode,
237238
},
238239
/// Set the cursor's [`CursorIcon`].
239240
SetCursorIcon {
@@ -266,7 +267,18 @@ pub enum WindowCommand {
266267
Close,
267268
}
268269

269-
/// Defines the way a window is displayed.
270+
/// Defines how the cursor is grabbed.
271+
#[derive(Debug, Clone, Copy, PartialEq)]
272+
pub enum CursorGrabMode {
273+
/// No grabbing of the cursor is performed
274+
None,
275+
/// The cursor is confined to the window area.
276+
Confined,
277+
/// The cursor is locked inside the window area to a certain position.
278+
Locked,
279+
}
280+
281+
/// Defines the way a window is displayed
270282
#[derive(Debug, Clone, Copy, PartialEq)]
271283
pub enum WindowMode {
272284
/// Creates a window that uses the given size.
@@ -307,7 +319,7 @@ impl Window {
307319
resizable: window_descriptor.resizable,
308320
decorations: window_descriptor.decorations,
309321
cursor_visible: window_descriptor.cursor_visible,
310-
cursor_locked: window_descriptor.cursor_locked,
322+
cursor_grab_mode: window_descriptor.cursor_grab_mode,
311323
cursor_icon: CursorIcon::Default,
312324
physical_cursor_position: None,
313325
raw_window_handle: RawWindowHandleWrapper::new(raw_window_handle),
@@ -557,28 +569,28 @@ impl Window {
557569
self.command_queue
558570
.push(WindowCommand::SetDecorations { decorations });
559571
}
560-
/// Get whether or not the cursor is locked.
572+
/// Get whether or not the cursor is locked in position, confined to the window, or free to move.
561573
///
562574
/// ## Platform-specific
563575
///
564576
/// - **`macOS`** doesn't support cursor lock, but most windowing plugins can emulate it. See [issue #4875](https://github.com/bevyengine/bevy/issues/4875#issuecomment-1153977546) for more information.
565577
/// - **`iOS/Android`** don't have cursors.
566578
#[inline]
567-
pub fn cursor_locked(&self) -> bool {
568-
self.cursor_locked
579+
pub fn cursor_grab_mode(&self) -> CursorGrabMode {
580+
self.cursor_grab_mode
569581
}
570-
/// Set whether or not the cursor is locked.
582+
/// Set whether or not the cursor is locked in position, confined to the window, or free to move.
571583
///
572584
/// This doesn't hide the cursor. For that, use [`set_cursor_visibility`](Window::set_cursor_visibility)
573585
///
574586
/// ## Platform-specific
575587
///
576588
/// - **`macOS`** doesn't support cursor lock, but most windowing plugins can emulate it. See [issue #4875](https://github.com/bevyengine/bevy/issues/4875#issuecomment-1153977546) for more information.
577589
/// - **`iOS/Android`** don't have cursors.
578-
pub fn set_cursor_lock_mode(&mut self, lock_mode: bool) {
579-
self.cursor_locked = lock_mode;
590+
pub fn set_cursor_grab_mode(&mut self, grab_mode: CursorGrabMode) {
591+
self.cursor_grab_mode = grab_mode;
580592
self.command_queue
581-
.push(WindowCommand::SetCursorLockMode { locked: lock_mode });
593+
.push(WindowCommand::SetCursorGrabMode { mode: grab_mode });
582594
}
583595
/// Get whether or not the cursor is visible.
584596
///
@@ -598,10 +610,10 @@ impl Window {
598610
/// - **`Windows`**, **`X11`**, and **`Wayland`**: The cursor is hidden only when inside the window. To stop the cursor from leaving the window, use [`set_cursor_lock_mode`](Window::set_cursor_lock_mode).
599611
/// - **`macOS`**: The cursor is hidden only when the window is focused.
600612
/// - **`iOS`** and **`Android`** do not have cursors
601-
pub fn set_cursor_visibility(&mut self, visibile_mode: bool) {
602-
self.cursor_visible = visibile_mode;
613+
pub fn set_cursor_visibility(&mut self, visible_mode: bool) {
614+
self.cursor_visible = visible_mode;
603615
self.command_queue.push(WindowCommand::SetCursorVisibility {
604-
visible: visibile_mode,
616+
visible: visible_mode,
605617
});
606618
}
607619
/// Get the current [`CursorIcon`]
@@ -762,8 +774,8 @@ pub struct WindowDescriptor {
762774
pub decorations: bool,
763775
/// Sets whether the cursor is visible when the window has focus.
764776
pub cursor_visible: bool,
765-
/// Sets whether the window locks the cursor inside its borders when the window has focus.
766-
pub cursor_locked: bool,
777+
/// Sets the [`CursorGrabMode`](crate::CursorGrabMode) for whether it is confined/won't move to the window area.
778+
pub cursor_grab_mode: CursorGrabMode,
767779
/// Sets the [`WindowMode`](crate::WindowMode).
768780
pub mode: WindowMode,
769781
/// Sets whether the background of the window should be transparent.
@@ -805,7 +817,7 @@ impl Default for WindowDescriptor {
805817
present_mode: PresentMode::Fifo,
806818
resizable: true,
807819
decorations: true,
808-
cursor_locked: false,
820+
cursor_grab_mode: CursorGrabMode::None,
809821
cursor_visible: true,
810822
mode: WindowMode::Windowed,
811823
transparent: false,

crates/bevy_winit/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ bevy_window = { path = "../bevy_window", version = "0.8.0-dev" }
2222
bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" }
2323

2424
# other
25-
winit = { version = "0.26.0", default-features = false }
26-
approx = { version = "0.5.0", default-features = false }
25+
winit = { version = "0.27", default-features = false }
26+
approx = { version = "0.5", default-features = false }
2727
raw-window-handle = "0.4.2"
2828

2929
[target.'cfg(target_arch = "wasm32")'.dependencies]
30-
winit = { version = "0.26.0", default-features = false }
30+
winit = { version = "0.27", default-features = false }
3131
wasm-bindgen = { version = "0.2" }
3232
web-sys = "0.3"
3333
crossbeam-channel = "0.5"

crates/bevy_winit/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ fn change_window(
123123
let window = winit_windows.get_window(id).unwrap();
124124
window.set_cursor_icon(converters::convert_cursor_icon(icon));
125125
}
126-
bevy_window::WindowCommand::SetCursorLockMode { locked } => {
126+
bevy_window::WindowCommand::SetCursorGrabMode { mode } => {
127127
let window = winit_windows.get_window(id).unwrap();
128128
window
129-
.set_cursor_grab(locked)
129+
.set_cursor_grab(winit_grab_mode(mode))
130130
.unwrap_or_else(|e| error!("Unable to un/grab cursor: {}", e));
131131
}
132132
bevy_window::WindowCommand::SetCursorVisibility { visible } => {
@@ -673,3 +673,11 @@ fn handle_create_window_events(
673673
}
674674
}
675675
}
676+
677+
fn winit_grab_mode(mode: bevy_window::CursorGrabMode) -> winit::window::CursorGrabMode {
678+
match mode {
679+
bevy_window::CursorGrabMode::None => winit::window::CursorGrabMode::None,
680+
bevy_window::CursorGrabMode::Confined => winit::window::CursorGrabMode::Confined,
681+
bevy_window::CursorGrabMode::Locked => winit::window::CursorGrabMode::Locked,
682+
}
683+
}

crates/bevy_winit/src/winit_windows.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use bevy_window::{Window, WindowDescriptor, WindowId, WindowMode};
44
use raw_window_handle::HasRawWindowHandle;
55
use winit::dpi::LogicalSize;
66

7+
use crate::winit_grab_mode;
8+
79
#[derive(Debug, Default)]
810
pub struct WinitWindows {
911
pub windows: HashMap<winit::window::WindowId, winit::window::Window>,
@@ -124,11 +126,9 @@ impl WinitWindows {
124126

125127
let winit_window = winit_window_builder.build(event_loop).unwrap();
126128

127-
if window_descriptor.cursor_locked {
128-
match winit_window.set_cursor_grab(true) {
129-
Ok(_) | Err(winit::error::ExternalError::NotSupported(_)) => {}
130-
Err(err) => Err(err).unwrap(),
131-
}
129+
match winit_window.set_cursor_grab(winit_grab_mode(window_descriptor.cursor_grab_mode)) {
130+
Ok(_) | Err(winit::error::ExternalError::NotSupported(_)) => {}
131+
Err(err) => Err(err).unwrap(),
132132
}
133133

134134
winit_window.set_cursor_visible(window_descriptor.cursor_visible);
@@ -207,7 +207,9 @@ pub fn get_fitting_videomode(
207207
match abs_diff(a.size().width, width).cmp(&abs_diff(b.size().width, width)) {
208208
Equal => {
209209
match abs_diff(a.size().height, height).cmp(&abs_diff(b.size().height, height)) {
210-
Equal => b.refresh_rate().cmp(&a.refresh_rate()),
210+
Equal => b
211+
.refresh_rate_millihertz()
212+
.cmp(&a.refresh_rate_millihertz()),
211213
default => default,
212214
}
213215
}
@@ -224,7 +226,9 @@ pub fn get_best_videomode(monitor: &winit::monitor::MonitorHandle) -> winit::mon
224226
use std::cmp::Ordering::*;
225227
match b.size().width.cmp(&a.size().width) {
226228
Equal => match b.size().height.cmp(&a.size().height) {
227-
Equal => b.refresh_rate().cmp(&a.refresh_rate()),
229+
Equal => b
230+
.refresh_rate_millihertz()
231+
.cmp(&a.refresh_rate_millihertz()),
228232
default => default,
229233
},
230234
default => default,

0 commit comments

Comments
 (0)