aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/navigator.rs
diff options
context:
space:
mode:
authorDaniel Adams <70986246+msub2@users.noreply.github.com>2024-03-12 08:32:30 -0400
committerGitHub <noreply@github.com>2024-03-12 12:32:30 +0000
commit48fa77df6710a89e156c8cebb7dec10c8cda4ae6 (patch)
tree20cd807a4ba546a9c9060096615364c8588b7ed1 /components/script/dom/navigator.rs
parent31a50feb4a61707d661c6b72fe6479666a5c9832 (diff)
downloadservo-48fa77df6710a89e156c8cebb7dec10c8cda4ae6.tar.gz
servo-48fa77df6710a89e156c8cebb7dec10c8cda4ae6.zip
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
Diffstat (limited to 'components/script/dom/navigator.rs')
-rw-r--r--components/script/dom/navigator.rs42
1 files changed, 35 insertions, 7 deletions
diff --git a/components/script/dom/navigator.rs b/components/script/dom/navigator.rs
index 612d2e68ff4..d16b4f263ae 100644
--- a/components/script/dom/navigator.rs
+++ b/components/script/dom/navigator.rs
@@ -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: 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, &[]));
- // TODO: Add gamepads
- root
+ root.list()
}
// https://w3c.github.io/permissions/#navigator-and-workernavigator-extension
fn Permissions(&self) -> DomRoot<Permissions> {