Skip to content

Commit 6abcace

Browse files
rectalogicmockersf
authored andcommitted
Do not attempt to grab pointer on web if unsupported. (#21534)
# Objective Fixes #21497 ## Solution Mobile web does not support pointer APIs and panics if they are used. Check if they exist before using them. ## Testing Tested on iOS Safari using https://github.com/rectalogic/pointerlock
1 parent 0fed9d1 commit 6abcace

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

crates/bevy_winit/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ bevy_android = { path = "../bevy_android", version = "0.17.2", default-features
7878
[target.'cfg(target_arch = "wasm32")'.dependencies]
7979
wasm-bindgen = { version = "0.2" }
8080
web-sys = "0.3"
81+
js-sys = "0.3"
8182
# TODO: Assuming all wasm builds are for the browser. Require `no_std` support to break assumption.
8283
bevy_app = { path = "../bevy_app", version = "0.17.2", default-features = false, features = [
8384
"web",

crates/bevy_winit/src/winit_windows.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,29 @@ fn get_current_videomode(monitor: &MonitorHandle) -> Option<VideoModeHandle> {
397397
.max_by_key(VideoModeHandle::bit_depth)
398398
}
399399

400+
#[cfg(target_arch = "wasm32")]
401+
fn pointer_supported() -> Result<bool, ExternalError> {
402+
Ok(js_sys::Reflect::has(
403+
web_sys::window()
404+
.ok_or(ExternalError::Ignored)?
405+
.document()
406+
.ok_or(ExternalError::Ignored)?
407+
.as_ref(),
408+
&"exitPointerLock".into(),
409+
)
410+
.unwrap_or(false))
411+
}
412+
400413
pub(crate) fn attempt_grab(
401414
winit_window: &WinitWindow,
402415
grab_mode: CursorGrabMode,
403416
) -> Result<(), ExternalError> {
417+
// Do not attempt to grab on web if unsupported (e.g. mobile)
418+
#[cfg(target_arch = "wasm32")]
419+
if !pointer_supported()? {
420+
return Err(ExternalError::Ignored);
421+
}
422+
404423
let grab_result = match grab_mode {
405424
CursorGrabMode::None => winit_window.set_cursor_grab(WinitCursorGrabMode::None),
406425
CursorGrabMode::Confined => winit_window

0 commit comments

Comments
 (0)