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

[Merged by Bors] - fix cursor grab issue #7010

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
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 {
Copy link
Contributor

@nicopap nicopap Dec 23, 2022

Choose a reason for hiding this comment

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

This is non-blocking but I'm going to Chesterton-fence you here: What purpose did this serve? I notice it is marked pub so technically this is breaking change. Though such a conversion method defined as a free function is really weird.

Copy link
Contributor Author

@VitalyAnkh VitalyAnkh Dec 23, 2022

Choose a reason for hiding this comment

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

This is only for bevy crates' internal usage. I introduced this function for bevy_window abstracts its own CursorGrabMode struct because bevy_window doesn't want to depend on winit, then bevy_winit should convert bevy_window's CursorGrabMode to winit::window::CursorGrabMode. I think this function shouldn't be pub but I just followed these conversion functions' signature above. Anyway, we could remove it now and it shouldn't break much code since it's not intended for end users in the beginning. If this breaks the semantic version, please help us find a way to get of it... Sorry for this mistake.

Copy link
Contributor

Choose a reason for hiding this comment

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

Don't worry. This is perfectly fine.

Copy link
Member

Choose a reason for hiding this comment

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

This isn't a public module so this isn't a breaking change.

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);
Copy link
Member

Choose a reason for hiding this comment

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

I think I would prefer if this still logged an error here, so the user gets some feedback rather than silently failing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the late reply. I'm experiencing covid-19 this week.

Like the fallback will be done in bevy_winit::change_window, the errors, if any, will also be logged there:

.unwrap_or_else(|e| error!("Unable to un/grab cursor: {}", e));
.

Copy link
Member

Choose a reason for hiding this comment

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

oh gotcha nvm, my brain was in windows as entities mode and I forgot we had window commands

}
window
}

pub fn get_window(&self, id: WindowId) -> Option<&winit::window::Window> {
Expand Down
1 change: 1 addition & 0 deletions examples/window/window_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fn main() {
height: 300.,
present_mode: PresentMode::AutoVsync,
always_on_top: true,
cursor_grab_mode: CursorGrabMode::Confined,
Copy link
Member

Choose a reason for hiding this comment

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

Does the example here need to be changed? Feels like this just adds noise to the example without an explanation why users should set this. If it should, we should document what this is actually doing so that people learning from the example can improve their understanding.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just want to show users they could set cursor grab mode directly. But they will do this without guidance since this field is already in WindowDescriptor. I will remove this change. Thanks!

..default()
},
..default()
Expand Down