Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Winit 0.27 update #5347

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/bevy_window/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ bevy_math = { path = "../bevy_math", version = "0.8.0" }
bevy_utils = { path = "../bevy_utils", version = "0.8.0" }
# Used for close_on_esc
bevy_input = { path = "../bevy_input", version = "0.8.0" }
raw-window-handle = "0.4.2"
raw-window-handle = "0.5"

# other

Expand Down
46 changes: 29 additions & 17 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub struct Window {
decorations: bool,
cursor_icon: CursorIcon,
cursor_visible: bool,
cursor_locked: bool,
cursor_grab_mode: CursorGrabMode,
physical_cursor_position: Option<DVec2>,
raw_window_handle: RawWindowHandleWrapper,
focused: bool,
Expand Down Expand Up @@ -245,9 +245,10 @@ pub enum WindowCommand {
SetDecorations {
decorations: bool,
},
/// Set whether or not the cursor's position is locked.
SetCursorLockMode {
locked: bool,
/// Set whether or not the cursor's postition is locked in place, confined to the window
/// or free to move anywhere
SetCursorGrabMode {
mode: CursorGrabMode,
},
/// Set the cursor's [`CursorIcon`].
SetCursorIcon {
Expand Down Expand Up @@ -282,8 +283,19 @@ pub enum WindowCommand {
Close,
}

/// Defines the way a window is displayed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// Defines if and how the cursor is grabbed.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CursorGrabMode {
/// The cursor can freely leave the window
None,
/// The cursor is confined to the window area.
Confined,
/// The cursor is locked inside the window area to a certain position.
Locked,
}

/// Defines the way a window is displayed
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum WindowMode {
/// Creates a window that uses the given size.
Windowed,
Expand Down Expand Up @@ -323,7 +335,7 @@ impl Window {
resizable: window_descriptor.resizable,
decorations: window_descriptor.decorations,
cursor_visible: window_descriptor.cursor_visible,
cursor_locked: window_descriptor.cursor_locked,
cursor_grab_mode: window_descriptor.cursor_grab_mode,
cursor_icon: CursorIcon::Default,
physical_cursor_position: None,
raw_window_handle: RawWindowHandleWrapper::new(raw_window_handle),
Expand Down Expand Up @@ -584,28 +596,28 @@ impl Window {
self.command_queue
.push(WindowCommand::SetDecorations { decorations });
}
/// Get whether or not the cursor is locked.
/// Get whether or not the cursor is locked in position, confined to the window, or free to move.
///
/// ## Platform-specific
///
/// - **`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.
/// - **`iOS/Android`** don't have cursors.
#[inline]
pub fn cursor_locked(&self) -> bool {
self.cursor_locked
pub fn cursor_grab_mode(&self) -> CursorGrabMode {
self.cursor_grab_mode
}
/// Set whether or not the cursor is locked.
/// Set whether or not the cursor is locked in position, confined to the window, or free to move.
///
/// This doesn't hide the cursor. For that, use [`set_cursor_visibility`](Window::set_cursor_visibility)
///
/// ## Platform-specific
///
/// - **`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.
/// - **`iOS/Android`** don't have cursors.
pub fn set_cursor_lock_mode(&mut self, lock_mode: bool) {
self.cursor_locked = lock_mode;
pub fn set_cursor_grab_mode(&mut self, grab_mode: CursorGrabMode) {
self.cursor_grab_mode = grab_mode;
self.command_queue
.push(WindowCommand::SetCursorLockMode { locked: lock_mode });
.push(WindowCommand::SetCursorGrabMode { mode: grab_mode });
}
/// Get whether or not the cursor is visible.
///
Expand Down Expand Up @@ -813,8 +825,8 @@ pub struct WindowDescriptor {
pub decorations: bool,
/// Sets whether the cursor is visible when the window has focus.
pub cursor_visible: bool,
/// Sets whether the window locks the cursor inside its borders when the window has focus.
pub cursor_locked: bool,
/// Sets the [`CursorGrabMode`](crate::CursorGrabMode) for whether it is confined/won't move to the window area.
pub cursor_grab_mode: CursorGrabMode,
/// Sets the [`WindowMode`](crate::WindowMode).
pub mode: WindowMode,
/// Sets whether the background of the window should be transparent.
Expand Down Expand Up @@ -856,7 +868,7 @@ impl Default for WindowDescriptor {
present_mode: PresentMode::Fifo,
resizable: true,
decorations: true,
cursor_locked: false,
cursor_grab_mode: CursorGrabMode::None,
cursor_visible: true,
mode: WindowMode::Windowed,
transparent: false,
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ bevy_window = { path = "../bevy_window", version = "0.8.0" }
bevy_utils = { path = "../bevy_utils", version = "0.8.0" }

# other
winit = { version = "0.26.0", default-features = false }
approx = { version = "0.5.0", default-features = false }
raw-window-handle = "0.4.2"
winit = { version = "0.27", default-features = false }
approx = { version = "0.5", default-features = false }
raw-window-handle = "0.5"

[target.'cfg(target_arch = "wasm32")'.dependencies]
winit = { version = "0.26.0", default-features = false }
winit = { version = "0.27", default-features = false }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is winit duplicated here?
As wasm doesn't add any new features, the duplicate entry should just be removed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not entirely sure... I'll remove it unless anyone has any concerns about it.

wasm-bindgen = { version = "0.2" }
web-sys = "0.3"
crossbeam-channel = "0.5"
Expand Down
12 changes: 10 additions & 2 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ fn change_window(
let window = winit_windows.get_window(id).unwrap();
window.set_cursor_icon(converters::convert_cursor_icon(icon));
}
bevy_window::WindowCommand::SetCursorLockMode { locked } => {
bevy_window::WindowCommand::SetCursorGrabMode { mode } => {
let window = winit_windows.get_window(id).unwrap();
window
.set_cursor_grab(locked)
.set_cursor_grab(winit_grab_mode(mode))
.unwrap_or_else(|e| error!("Unable to un/grab cursor: {}", e));
}
bevy_window::WindowCommand::SetCursorVisibility { visible } => {
Expand Down Expand Up @@ -710,3 +710,11 @@ fn handle_create_window_events(
}
}
}

fn winit_grab_mode(mode: bevy_window::CursorGrabMode) -> winit::window::CursorGrabMode {
match mode {
bevy_window::CursorGrabMode::None => winit::window::CursorGrabMode::None,
bevy_window::CursorGrabMode::Confined => winit::window::CursorGrabMode::Confined,
bevy_window::CursorGrabMode::Locked => winit::window::CursorGrabMode::Locked,
}
}
18 changes: 11 additions & 7 deletions crates/bevy_winit/src/winit_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use bevy_window::{Window, WindowDescriptor, WindowId, WindowMode};
use raw_window_handle::HasRawWindowHandle;
use winit::dpi::{LogicalPosition, LogicalSize, PhysicalPosition};

use crate::winit_grab_mode;

#[derive(Debug, Default)]
pub struct WinitWindows {
pub windows: HashMap<winit::window::WindowId, winit::window::Window>,
Expand Down Expand Up @@ -155,11 +157,9 @@ impl WinitWindows {

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

if window_descriptor.cursor_locked {
match winit_window.set_cursor_grab(true) {
Ok(_) | Err(winit::error::ExternalError::NotSupported(_)) => {}
Err(err) => Err(err).unwrap(),
}
match winit_window.set_cursor_grab(winit_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);
Expand Down Expand Up @@ -238,7 +238,9 @@ pub fn get_fitting_videomode(
match abs_diff(a.size().width, width).cmp(&abs_diff(b.size().width, width)) {
Equal => {
match abs_diff(a.size().height, height).cmp(&abs_diff(b.size().height, height)) {
Equal => b.refresh_rate().cmp(&a.refresh_rate()),
Equal => b
.refresh_rate_millihertz()
.cmp(&a.refresh_rate_millihertz()),
default => default,
}
}
Expand All @@ -255,7 +257,9 @@ pub fn get_best_videomode(monitor: &winit::monitor::MonitorHandle) -> winit::mon
use std::cmp::Ordering::*;
match b.size().width.cmp(&a.size().width) {
Equal => match b.size().height.cmp(&a.size().height) {
Equal => b.refresh_rate().cmp(&a.refresh_rate()),
Equal => b
.refresh_rate_millihertz()
.cmp(&a.refresh_rate_millihertz()),
default => default,
},
default => default,
Expand Down