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

@ -63,10 +63,10 @@ use super::bindings::trace::HashMapTracedValues;
use crate::dom::bindings::cell::{DomRefCell, RefMut};
use crate::dom::bindings::codegen::Bindings::BroadcastChannelBinding::BroadcastChannelMethods;
use crate::dom::bindings::codegen::Bindings::EventSourceBinding::EventSource_Binding::EventSourceMethods;
use crate::dom::bindings::codegen::Bindings::GamepadListBinding::GamepadList_Binding::GamepadListMethods;
use crate::dom::bindings::codegen::Bindings::ImageBitmapBinding::{
ImageBitmapOptions, ImageBitmapSource,
};
use crate::dom::bindings::codegen::Bindings::NavigatorBinding::NavigatorMethods;
use crate::dom::bindings::codegen::Bindings::PerformanceBinding::Performance_Binding::PerformanceMethods;
use crate::dom::bindings::codegen::Bindings::PermissionStatusBinding::PermissionState;
use crate::dom::bindings::codegen::Bindings::VoidFunctionBinding::VoidFunction;
@ -3133,7 +3133,10 @@ impl GlobalScope {
/// <https://www.w3.org/TR/gamepad/#dfn-gamepadconnected>
pub fn handle_gamepad_connect(
&self,
index: usize,
// As the spec actually defines how to set the gamepad index, the GilRs index
// is currently unused, though in practice it will almost always be the same.
// More infra is currently needed to track gamepads across windows.
_index: usize,
name: String,
axis_bounds: (f64, f64),
button_bounds: (f64, f64),
@ -3144,19 +3147,12 @@ impl GlobalScope {
self.gamepad_task_source().queue_with_canceller(
task!(gamepad_connected: move || {
let global = this.root();
let gamepad = Gamepad::new(&global, index as u32, name, axis_bounds, button_bounds);
if let Some(window) = global.downcast::<Window>() {
let has_gesture = window.Navigator().has_gamepad_gesture();
if has_gesture {
gamepad.set_exposed(true);
if window.Document().is_fully_active() {
gamepad.update_connected(true, has_gesture);
}
}
let gamepad_list = window.Navigator().gamepads();
let gamepad_arr: [DomRoot<Gamepad>; 1] = [gamepad.clone()];
gamepad_list.add_if_not_exists(&gamepad_arr);
let navigator = window.Navigator();
let selected_index = navigator.select_gamepad_index();
let gamepad = Gamepad::new(&global, selected_index, name, axis_bounds, button_bounds);
navigator.set_gamepad(selected_index as usize, &*gamepad);
}
}),
&self.task_canceller(TaskSourceName::Gamepad)
@ -3172,18 +3168,11 @@ impl GlobalScope {
task!(gamepad_disconnected: move || {
let global = this.root();
if let Some(window) = global.downcast::<Window>() {
let gamepad_list = window.Navigator().gamepads();
if let Some(gamepad) = gamepad_list.Item(index as u32) {
let navigator = window.Navigator();
if let Some(gamepad) = navigator.get_gamepad(index) {
if window.Document().is_fully_active() {
gamepad.update_connected(false, gamepad.exposed());
gamepad_list.remove_gamepad(index);
}
}
for i in (0..gamepad_list.Length()).rev() {
if gamepad_list.Item(i).is_none() {
gamepad_list.remove_gamepad(i as usize);
} else {
break;
navigator.remove_gamepad(index);
}
}
}
@ -3203,11 +3192,10 @@ impl GlobalScope {
task!(update_gamepad_state: move || {
let global = this.root();
if let Some(window) = global.downcast::<Window>() {
let gamepad_list = window.Navigator().gamepads();
if let Some(gamepad) = gamepad_list.Item(index as u32) {
let navigator = window.Navigator();
if let Some(gamepad) = navigator.get_gamepad(index) {
let current_time = global.performance().Now();
gamepad.update_timestamp(*current_time);
match update_type {
GamepadUpdateType::Axis(index, value) => {
gamepad.map_and_normalize_axes(index, value);
@ -3216,26 +3204,27 @@ impl GlobalScope {
gamepad.map_and_normalize_buttons(index, value);
}
};
if !window.Navigator().has_gamepad_gesture() && contains_user_gesture(update_type) {
window.Navigator().set_has_gamepad_gesture(true);
for i in 0..gamepad_list.Length() {
if let Some(gamepad) = gamepad_list.Item(i) {
if !navigator.has_gamepad_gesture() && contains_user_gesture(update_type) {
navigator.set_has_gamepad_gesture(true);
navigator.GetGamepads()
.iter()
.filter_map(|g| g.as_ref())
.for_each(|gamepad| {
gamepad.set_exposed(true);
gamepad.update_timestamp(*current_time);
let new_gamepad = Trusted::new(&*gamepad);
let new_gamepad = Trusted::new(&**gamepad);
if window.Document().is_fully_active() {
window.task_manager().gamepad_task_source().queue_with_canceller(
task!(update_gamepad_connect: move || {
let gamepad = new_gamepad.root();
gamepad.notify_event(GamepadEventType::Connected);
}),
&window.upcast::<GlobalScope>().task_canceller(TaskSourceName::Gamepad),
&window.upcast::<GlobalScope>()
.task_canceller(TaskSourceName::Gamepad),
)
.expect("Failed to queue update gamepad connect task.");
}
}
}
});
}
}
}