Gamepad: Remove GamepadList and fix dropped connection event on startup (#31684)

* Replace GamepadList

* Fix initial gamepad connection event from gilrs getting dropped

* Fix gamepad reconnection issues, use MutNullableDom

* Reduce some repetition in handle_gamepad_events

* Address feedback, move some steps to navigator methods

* Refactor internal navigator gamepad methods

* Add note re: unused gilrs index, adjust navigator gamepad methods
This commit is contained in:
Daniel Adams 2024-04-07 23:43:48 -10:00 committed by GitHub
parent ddbec46e1f
commit e38b34a629
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 91 additions and 146 deletions

View file

@ -407,7 +407,10 @@ impl App {
// Catch some keyboard events, and push the rest onto the WebViewManager event queue.
webviews.handle_window_events(embedder_events);
if pref!(dom.gamepad.enabled) && webviews.webview_id().is_some() {
// If the Gamepad API is enabled, handle gamepad events from GilRs.
// Checking for current_url_string should ensure we'll have a valid browsing context.
if pref!(dom.gamepad.enabled) && webviews.current_url_string().is_some() {
webviews.handle_gamepad_events();
}

View file

@ -155,14 +155,14 @@ where
let gamepad = gilrs.gamepad(event.id);
let name = gamepad.name();
let index = GamepadIndex(event.id.into());
let mut gamepad_event: Option<GamepadEvent> = None;
match event.event {
EventType::ButtonPressed(button, _) => {
let mapped_index = Self::map_gamepad_button(button);
// We only want to send this for a valid digital button, aka on/off only
if !matches!(mapped_index, 6 | 7 | 17) {
let update_type = GamepadUpdateType::Button(mapped_index, 1.0);
let event = GamepadEvent::Updated(index, update_type);
self.event_queue.push(EmbedderEvent::Gamepad(event));
gamepad_event = Some(GamepadEvent::Updated(index, update_type));
}
},
EventType::ButtonReleased(button, _) => {
@ -170,8 +170,7 @@ where
// We only want to send this for a valid digital button, aka on/off only
if !matches!(mapped_index, 6 | 7 | 17) {
let update_type = GamepadUpdateType::Button(mapped_index, 0.0);
let event = GamepadEvent::Updated(index, update_type);
self.event_queue.push(EmbedderEvent::Gamepad(event));
gamepad_event = Some(GamepadEvent::Updated(index, update_type));
}
},
EventType::ButtonChanged(button, value, _) => {
@ -179,8 +178,7 @@ where
// We only want to send this for a valid non-digital button, aka the triggers
if matches!(mapped_index, 6 | 7) {
let update_type = GamepadUpdateType::Button(mapped_index, value as f64);
let event = GamepadEvent::Updated(index, update_type);
self.event_queue.push(EmbedderEvent::Gamepad(event));
gamepad_event = Some(GamepadEvent::Updated(index, update_type));
}
},
EventType::AxisChanged(axis, value, _) => {
@ -203,8 +201,7 @@ where
};
let update_type =
GamepadUpdateType::Axis(mapped_axis, axis_value as f64);
let event = GamepadEvent::Updated(index, update_type);
self.event_queue.push(EmbedderEvent::Gamepad(event));
gamepad_event = Some(GamepadEvent::Updated(index, update_type));
}
},
EventType::Connected => {
@ -213,15 +210,17 @@ where
axis_bounds: (-1.0, 1.0),
button_bounds: (0.0, 1.0),
};
let event = GamepadEvent::Connected(index, name, bounds);
self.event_queue.push(EmbedderEvent::Gamepad(event));
gamepad_event = Some(GamepadEvent::Connected(index, name, bounds));
},
EventType::Disconnected => {
let event = GamepadEvent::Disconnected(index);
self.event_queue.push(EmbedderEvent::Gamepad(event));
gamepad_event = Some(GamepadEvent::Disconnected(index));
},
_ => {},
}
if let Some(event) = gamepad_event {
self.event_queue.push(EmbedderEvent::Gamepad(event));
}
}
}
}