Do not attempt to grab pointer on web if unsupported. (#21534)

# Objective

Fixes https://github.com/bevyengine/bevy/issues/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
This commit is contained in:
Andrew Wason
2025-10-14 17:18:36 -04:00
committed by GitHub
parent 7939097435
commit 52b4a2d5a7
2 changed files with 20 additions and 0 deletions
+1
View File
@@ -78,6 +78,7 @@ bevy_android = { path = "../bevy_android", version = "0.18.0-dev", default-featu
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = { version = "0.2" }
web-sys = "0.3"
js-sys = "0.3"
# TODO: Assuming all wasm builds are for the browser. Require `no_std` support to break assumption.
bevy_app = { path = "../bevy_app", version = "0.18.0-dev", default-features = false, features = [
"web",
+19
View File
@@ -397,10 +397,29 @@ fn get_current_videomode(monitor: &MonitorHandle) -> Option<VideoModeHandle> {
.max_by_key(VideoModeHandle::bit_depth)
}
#[cfg(target_arch = "wasm32")]
fn pointer_supported() -> Result<bool, ExternalError> {
Ok(js_sys::Reflect::has(
web_sys::window()
.ok_or(ExternalError::Ignored)?
.document()
.ok_or(ExternalError::Ignored)?
.as_ref(),
&"exitPointerLock".into(),
)
.unwrap_or(false))
}
pub(crate) fn attempt_grab(
winit_window: &WinitWindow,
grab_mode: CursorGrabMode,
) -> Result<(), ExternalError> {
// Do not attempt to grab on web if unsupported (e.g. mobile)
#[cfg(target_arch = "wasm32")]
if !pointer_supported()? {
return Err(ExternalError::Ignored);
}
let grab_result = match grab_mode {
CursorGrabMode::None => winit_window.set_cursor_grab(WinitCursorGrabMode::None),
CursorGrabMode::Confined => winit_window