Gamepad: Align closer to spec and implement missing slots (#31385)

* Implement missing gamepad slots, align to spec more

- Fixes TODO's from initial gamepad implementation
- Adds some missing spec steps

* Only handle gamepad events when pref is enabled

* Return empty list in getGamepads if document not active

* ./mach fmt

* Update getGamepads to return an array instead of GamepadList

* Add spec link for [[exposed]] slot

* Remove failing test expectations for not-fully-active

* A few fixes

- Change should_notify to has_gesture
- Add spec links and TODO to navigator
- Remove unneeded clone from GamepadList::list
- Move gamepadconnected event firing into has_gesture block

* Use queue_with_canceller for tasks and add expects

* Explicitly check for gamepad user gesture

* Move user gesture check into separate function

* Change contains_user_gesture to be a gamepad function

* mach fmt

* Change axis/button threshold constants to be private to module
This commit is contained in:
Daniel Adams 2024-03-12 08:32:30 -04:00 committed by GitHub
parent 31a50feb4a
commit 48fa77df67
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 124 additions and 45 deletions

View file

@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::Cell;
use std::convert::TryInto;
use dom_struct::dom_struct;
@ -9,11 +10,13 @@ use js::jsval::JSVal;
use lazy_static::lazy_static;
use crate::dom::bindings::codegen::Bindings::NavigatorBinding::NavigatorMethods;
use crate::dom::bindings::codegen::Bindings::WindowBinding::Window_Binding::WindowMethods;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::utils::to_frozen_array;
use crate::dom::bluetooth::Bluetooth;
use crate::dom::gamepad::Gamepad;
use crate::dom::gamepadlist::GamepadList;
use crate::dom::gpu::GPU;
use crate::dom::mediadevices::MediaDevices;
@ -43,10 +46,13 @@ pub struct Navigator {
service_worker: MutNullableDom<ServiceWorkerContainer>,
xr: MutNullableDom<XRSystem>,
mediadevices: MutNullableDom<MediaDevices>,
/// <https://www.w3.org/TR/gamepad/#dfn-gamepads>
gamepads: MutNullableDom<GamepadList>,
permissions: MutNullableDom<Permissions>,
mediasession: MutNullableDom<MediaSession>,
gpu: MutNullableDom<GPU>,
/// <https://www.w3.org/TR/gamepad/#dfn-hasgamepadgesture>
has_gamepad_gesture: Cell<bool>,
}
impl Navigator {
@ -63,6 +69,7 @@ impl Navigator {
permissions: Default::default(),
mediasession: Default::default(),
gpu: Default::default(),
has_gamepad_gesture: Cell::new(false),
}
}
@ -73,6 +80,21 @@ impl Navigator {
pub fn xr(&self) -> Option<DomRoot<XRSystem>> {
self.xr.get()
}
pub fn gamepads(&self) -> DomRoot<GamepadList> {
let gamepads = self
.gamepads
.or_init(|| GamepadList::new(&self.global(), &[]));
gamepads
}
pub fn has_gamepad_gesture(&self) -> bool {
self.has_gamepad_gesture.get()
}
pub fn set_has_gamepad_gesture(&self, has_gamepad_gesture: bool) {
self.has_gamepad_gesture.set(has_gamepad_gesture);
}
}
impl NavigatorMethods for Navigator {
@ -169,14 +191,20 @@ impl NavigatorMethods for Navigator {
true
}
// https://www.w3.org/TR/gamepad/#navigator-interface-extension
fn GetGamepads(&self) -> DomRoot<GamepadList> {
let root = self
.gamepads
.or_init(|| GamepadList::new(&self.global(), &[]));
/// <https://www.w3.org/TR/gamepad/#dom-navigator-getgamepads>
fn GetGamepads(&self) -> Vec<Option<DomRoot<Gamepad>>> {
let global = self.global();
let window = global.as_window();
let doc = window.Document();
// TODO: Add gamepads
root
// TODO: Handle permissions policy once implemented
if !doc.is_fully_active() || !self.has_gamepad_gesture.get() {
return Vec::new();
}
let root = self.gamepads.or_init(|| GamepadList::new(&global, &[]));
root.list()
}
// https://w3c.github.io/permissions/#navigator-and-workernavigator-extension
fn Permissions(&self) -> DomRoot<Permissions> {