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(unsafe_code)]
66    #[allow(clippy::too_many_arguments)]
67    fn new_with_proto(
68        window: &Window,
69        proto: Option<HandleObject>,
70        type_: Atom,
71        bubbles: bool,
72        cancelable: bool,
73        session: &XRSession,
74        added: &[DomRoot<XRInputSource>],
75        removed: &[DomRoot<XRInputSource>],
76        can_gc: CanGc,
77    ) -> DomRoot<XRInputSourcesChangeEvent> {
78        let changeevent = reflect_dom_object_with_proto(
79            Box::new(XRInputSourcesChangeEvent::new_inherited(session)),
80            window,
81            proto,
82            can_gc,
83        );
84        {
85            let event = changeevent.upcast::<Event>();
86            event.init_event(type_, bubbles, cancelable);
87        }
88        let _ac = enter_realm(window);
89        let cx = GlobalScope::get_cx();
90        rooted!(in(*cx) let mut frozen_val: JSVal);
91        to_frozen_array(added, cx, frozen_val.handle_mut(), can_gc);
92        changeevent.added.set(*frozen_val);
93        to_frozen_array(removed, cx, frozen_val.handle_mut(), can_gc);
94        changeevent.removed.set(*frozen_val);
95        changeevent
96    }
97}
98
99impl XRInputSourcesChangeEventMethods<crate::DomTypeHolder> for XRInputSourcesChangeEvent {
100    // https://immersive-web.github.io/webxr/#dom-xrinputsourceschangeevent-xrinputsourceschangeevent
101    fn Constructor(
102        window: &Window,
103        proto: Option<HandleObject>,
104        can_gc: CanGc,
105        type_: DOMString,
106        init: &XRInputSourcesChangeEventBinding::XRInputSourcesChangeEventInit,
107    ) -> DomRoot<XRInputSourcesChangeEvent> {
108        XRInputSourcesChangeEvent::new_with_proto(
109            window,
110            proto,
111            Atom::from(type_),
112            init.parent.bubbles,
113            init.parent.cancelable,
114            &init.session,
115            &init.added,
116            &init.removed,
117            can_gc,
118        )
119    }
120
121    // https://immersive-web.github.io/webxr/#dom-xrinputsourceschangeevent-session
122    fn Session(&self) -> DomRoot<XRSession> {
123        DomRoot::from_ref(&*self.session)
124    }
125
126    // https://immersive-web.github.io/webxr/#dom-xrinputsourceschangeevent-added
127    fn Added(&self, _cx: JSContext, mut retval: MutableHandleValue) {
128        retval.set(self.added.get())
129    }
130
131    // https://immersive-web.github.io/webxr/#dom-xrinputsourceschangeevent-removed
132    fn Removed(&self, _cx: JSContext, mut retval: MutableHandleValue) {
133        retval.set(self.removed.get())
134    }
135
136    // https://dom.spec.whatwg.org/#dom-event-istrusted
137    fn IsTrusted(&self) -> bool {
138        self.event.IsTrusted()
139    }
140}