Skip to content

Commit 4ad5b92

Browse files
committed
Prep for next version of winit
1 parent 6752c9c commit 4ad5b92

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

crates/bevy_window/src/window.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ pub struct Window {
198198
decorations: bool,
199199
cursor_icon: CursorIcon,
200200
cursor_visible: bool,
201-
cursor_locked: bool,
201+
cursor_grab_mode: CursorGrabMode,
202202
physical_cursor_position: Option<DVec2>,
203203
raw_window_handle: RawWindowHandleWrapper,
204204
focused: bool,
@@ -245,9 +245,10 @@ pub enum WindowCommand {
245245
SetDecorations {
246246
decorations: bool,
247247
},
248-
/// Set whether or not the cursor's position is locked.
249-
SetCursorLockMode {
250-
locked: bool,
248+
/// Set whether or not the cursor's postition is locked in place, confined to the window
249+
/// or free to move anywhere
250+
SetCursorGrabMode {
251+
mode: CursorGrabMode,
251252
},
252253
/// Set the cursor's [`CursorIcon`].
253254
SetCursorIcon {
@@ -282,8 +283,19 @@ pub enum WindowCommand {
282283
Close,
283284
}
284285

285-
/// Defines the way a window is displayed.
286-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
286+
/// Defines if and how the cursor is grabbed.
287+
#[derive(Debug, Clone, Copy, PartialEq)]
288+
pub enum CursorGrabMode {
289+
/// No grabbing of the cursor is performed
290+
None,
291+
/// The cursor is confined to the window area.
292+
Confined,
293+
/// The cursor is locked inside the window area to a certain position.
294+
Locked,
295+
}
296+
297+
/// Defines the way a window is displayed
298+
#[derive(Debug, Clone, Copy, PartialEq)]
287299
pub enum WindowMode {
288300
/// Creates a window that uses the given size.
289301
Windowed,
@@ -323,7 +335,7 @@ impl Window {
323335
resizable: window_descriptor.resizable,
324336
decorations: window_descriptor.decorations,
325337
cursor_visible: window_descriptor.cursor_visible,
326-
cursor_locked: window_descriptor.cursor_locked,
338+
cursor_grab_mode: window_descriptor.cursor_grab_mode,
327339
cursor_icon: CursorIcon::Default,
328340
physical_cursor_position: None,
329341
raw_window_handle: RawWindowHandleWrapper::new(raw_window_handle),
@@ -584,28 +596,28 @@ impl Window {
584596
self.command_queue
585597
.push(WindowCommand::SetDecorations { decorations });
586598
}
587-
/// Get whether or not the cursor is locked.
599+
/// Get whether or not the cursor is locked in position, confined to the window, or free to move.
588600
///
589601
/// ## Platform-specific
590602
///
591603
/// - **`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.
592604
/// - **`iOS/Android`** don't have cursors.
593605
#[inline]
594-
pub fn cursor_locked(&self) -> bool {
595-
self.cursor_locked
606+
pub fn cursor_grab_mode(&self) -> CursorGrabMode {
607+
self.cursor_grab_mode
596608
}
597-
/// Set whether or not the cursor is locked.
609+
/// Set whether or not the cursor is locked in position, confined to the window, or free to move.
598610
///
599611
/// This doesn't hide the cursor. For that, use [`set_cursor_visibility`](Window::set_cursor_visibility)
600612
///
601613
/// ## Platform-specific
602614
///
603615
/// - **`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.
604616
/// - **`iOS/Android`** don't have cursors.
605-
pub fn set_cursor_lock_mode(&mut self, lock_mode: bool) {
606-
self.cursor_locked = lock_mode;
617+
pub fn set_cursor_grab_mode(&mut self, grab_mode: CursorGrabMode) {
618+
self.cursor_grab_mode = grab_mode;
607619
self.command_queue
608-
.push(WindowCommand::SetCursorLockMode { locked: lock_mode });
620+
.push(WindowCommand::SetCursorGrabMode { mode: grab_mode });
609621
}
610622
/// Get whether or not the cursor is visible.
611623
///
@@ -813,8 +825,8 @@ pub struct WindowDescriptor {
813825
pub decorations: bool,
814826
/// Sets whether the cursor is visible when the window has focus.
815827
pub cursor_visible: bool,
816-
/// Sets whether the window locks the cursor inside its borders when the window has focus.
817-
pub cursor_locked: bool,
828+
/// Sets the [`CursorGrabMode`](crate::CursorGrabMode) for whether it is confined/won't move to the window area.
829+
pub cursor_grab_mode: CursorGrabMode,
818830
/// Sets the [`WindowMode`](crate::WindowMode).
819831
pub mode: WindowMode,
820832
/// Sets whether the background of the window should be transparent.
@@ -856,7 +868,7 @@ impl Default for WindowDescriptor {
856868
present_mode: PresentMode::Fifo,
857869
resizable: true,
858870
decorations: true,
859-
cursor_locked: false,
871+
cursor_grab_mode: CursorGrabMode::None,
860872
cursor_visible: true,
861873
mode: WindowMode::Windowed,
862874
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" }
2222
bevy_utils = { path = "../bevy_utils", version = "0.8.0" }
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
@@ -135,10 +135,10 @@ fn change_window(
135135
let window = winit_windows.get_window(id).unwrap();
136136
window.set_cursor_icon(converters::convert_cursor_icon(icon));
137137
}
138-
bevy_window::WindowCommand::SetCursorLockMode { locked } => {
138+
bevy_window::WindowCommand::SetCursorGrabMode { mode } => {
139139
let window = winit_windows.get_window(id).unwrap();
140140
window
141-
.set_cursor_grab(locked)
141+
.set_cursor_grab(winit_grab_mode(mode))
142142
.unwrap_or_else(|e| error!("Unable to un/grab cursor: {}", e));
143143
}
144144
bevy_window::WindowCommand::SetCursorVisibility { visible } => {
@@ -710,3 +710,11 @@ fn handle_create_window_events(
710710
}
711711
}
712712
}
713+
714+
fn winit_grab_mode(mode: bevy_window::CursorGrabMode) -> winit::window::CursorGrabMode {
715+
match mode {
716+
bevy_window::CursorGrabMode::None => winit::window::CursorGrabMode::None,
717+
bevy_window::CursorGrabMode::Confined => winit::window::CursorGrabMode::Confined,
718+
bevy_window::CursorGrabMode::Locked => winit::window::CursorGrabMode::Locked,
719+
}
720+
}

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::{LogicalPosition, LogicalSize, PhysicalPosition};
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>,
@@ -155,11 +157,9 @@ impl WinitWindows {
155157

156158
let winit_window = winit_window_builder.build(event_loop).unwrap();
157159

158-
if window_descriptor.cursor_locked {
159-
match winit_window.set_cursor_grab(true) {
160-
Ok(_) | Err(winit::error::ExternalError::NotSupported(_)) => {}
161-
Err(err) => Err(err).unwrap(),
162-
}
160+
match winit_window.set_cursor_grab(winit_grab_mode(window_descriptor.cursor_grab_mode)) {
161+
Ok(_) | Err(winit::error::ExternalError::NotSupported(_)) => {}
162+
Err(err) => Err(err).unwrap(),
163163
}
164164

165165
winit_window.set_cursor_visible(window_descriptor.cursor_visible);
@@ -238,7 +238,9 @@ pub fn get_fitting_videomode(
238238
match abs_diff(a.size().width, width).cmp(&abs_diff(b.size().width, width)) {
239239
Equal => {
240240
match abs_diff(a.size().height, height).cmp(&abs_diff(b.size().height, height)) {
241-
Equal => b.refresh_rate().cmp(&a.refresh_rate()),
241+
Equal => b
242+
.refresh_rate_millihertz()
243+
.cmp(&a.refresh_rate_millihertz()),
242244
default => default,
243245
}
244246
}
@@ -255,7 +257,9 @@ pub fn get_best_videomode(monitor: &winit::monitor::MonitorHandle) -> winit::mon
255257
use std::cmp::Ordering::*;
256258
match b.size().width.cmp(&a.size().width) {
257259
Equal => match b.size().height.cmp(&a.size().height) {
258-
Equal => b.refresh_rate().cmp(&a.refresh_rate()),
260+
Equal => b
261+
.refresh_rate_millihertz()
262+
.cmp(&a.refresh_rate_millihertz()),
259263
default => default,
260264
},
261265
default => default,

0 commit comments

Comments
 (0)