script/dom/webxr/
xrinputsourceschangeevent.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use dom_struct::dom_struct;
6use js::jsapi::Heap;
7use js::jsval::JSVal;
8use js::rust::{HandleObject, MutableHandleValue};
9use stylo_atoms::Atom;
10
11use crate::dom::bindings::codegen::Bindings::EventBinding::Event_Binding::EventMethods;
12use crate::dom::bindings::codegen::Bindings::XRInputSourcesChangeEventBinding::{
13    self, XRInputSourcesChangeEventMethods,
14};
15use crate::dom::bindings::inheritance::Castable;
16use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
17use crate::dom::bindings::root::{Dom, DomRoot};
18use crate::dom::bindings::str::DOMString;
19use crate::dom::bindings::utils::to_frozen_array;
20use crate::dom::event::Event;
21use crate::dom::globalscope::GlobalScope;
22use crate::dom::window::Window;
23use crate::dom::xrinputsource::XRInputSource;
24use crate::dom::xrsession::XRSession;
25use crate::realms::enter_realm;
26use crate::script_runtime::{CanGc, JSContext};
27
28#[dom_struct]
29pub(crate) struct XRInputSourcesChangeEvent {
30    event: Event,
31    session: Dom<XRSession>,
32    #[ignore_malloc_size_of = "mozjs"]
33    added: Heap<JSVal>,
34    #[ignore_malloc_size_of = "mozjs"]
35    removed: Heap<JSVal>,
36}
37
38impl XRInputSourcesChangeEvent {
39    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
40    fn new_inherited(session: &XRSession) -> XRInputSourcesChangeEvent {
41        XRInputSourcesChangeEvent {
42            event: Event::new_inherited(),
43            session: Dom::from_ref(session),
44            added: Heap::default(),
45            removed: Heap::default(),
46        }
47    }
48
49    #[allow(clippy::too_many_arguments)]
50    pub(crate) fn new(
51        window: &Window,
52        type_: Atom,
53        bubbles: bool,
54        cancelable: bool,
55        session: &XRSession,
56        added: &[DomRoot<XRInputSource>],
57        removed: &[DomRoot<XRInputSource>],
58        can_gc: CanGc,
59    ) -> DomRoot<XRInputSourcesChangeEvent> {
60        Self::new_with_proto(
61            window, None, type_, bubbles, cancelable, session, added, removed, can_gc,
62        )
63    }
64
65    #[allow(clippy::too_many_arguments)]
66    fn new_with_proto(
67        window: &Window,
68        proto: Option<HandleObject>,
69        type_: Atom,
70        bubbles: bool,
71        cancelable: bool,
72        session: &XRSession,
73        added: &[DomRoot<XRInputSource>],
74        removed: &[DomRoot<XRInputSource>],
75        can_gc: CanGc,
76    ) -> DomRoot<XRInputSourcesChangeEvent> {
77        let changeevent = reflect_dom_object_with_proto(
78            Box::new(XRInputSourcesChangeEvent::new_inherited(session)),
79            window,
80            proto,
81            can_gc,
82        );
83        {
84            let event = changeevent.upcast::<Event>();
85            event.init_event(type_, bubbles, cancelable);
86        }
87        let _ac = enter_realm(window);
88        let cx = GlobalScope::get_cx();
89        rooted!(in(*cx) let mut frozen_val: JSVal);
90        to_frozen_array(added, cx, frozen_val.handle_mut(), can_gc);
91        changeevent.added.set(*frozen_val);
92        to_frozen_array(removed, cx, frozen_val.handle_mut(), can_gc);
93        changeevent.removed.set(*frozen_val);
94        changeevent
95    }
96}
97
98impl XRInputSourcesChangeEventMethods<crate::DomTypeHolder> for XRInputSourcesChangeEvent {
99    /// <https://immersive-web.github.io/webxr/#dom-xrinputsourceschangeevent-xrinputsourceschangeevent>
100    fn Constructor(
101        window: &Window,
102        proto: Option<HandleObject>,
103        can_gc: CanGc,
104        type_: DOMString,
105        init: &XRInputSourcesChangeEventBinding::XRInputSourcesChangeEventInit,
106    ) -> DomRoot<XRInputSourcesChangeEvent> {
107        XRInputSourcesChangeEvent::new_with_proto(
108            window,
109            proto,
110            Atom::from(type_),
111            init.parent.bubbles,
112            init.parent.cancelable,
113            &init.session,
114            &init.added,
115            &init.removed,
116            can_gc,
117        )
118    }
119
120    /// <https://immersive-web.github.io/webxr/#dom-xrinputsourceschangeevent-session>
121    fn Session(&self) -> DomRoot<XRSession> {
122        DomRoot::from_ref(&*self.session)
123    }
124
125    /// <https://immersive-web.github.io/webxr/#dom-xrinputsourceschangeevent-added>
126    fn Added(&self, _cx: JSContext, mut retval: MutableHandleValue) {
127        retval.set(self.added.get())
128    }
129
130    /// <https://immersive-web.github.io/webxr/#dom-xrinputsourceschangeevent-removed>
131    fn Removed(&self, _cx: JSContext, mut retval: MutableHandleValue) {
132        retval.set(self.removed.get())
133    }
134
135    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
136    fn IsTrusted(&self) -> bool {
137        self.event.IsTrusted()
138    }
139}