Skip to main content

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::context::JSContext;
7use js::jsapi::Heap;
8use js::jsval::JSVal;
9use js::rust::{HandleObject, MutableHandleValue};
10use script_bindings::reflector::reflect_dom_object_with_proto;
11use script_bindings::root::DomSlice;
12use stylo_atoms::Atom;
13
14use crate::dom::bindings::codegen::Bindings::EventBinding::Event_Binding::EventMethods;
15use crate::dom::bindings::codegen::Bindings::XRInputSourcesChangeEventBinding::{
16    self, XRInputSourcesChangeEventMethods,
17};
18use crate::dom::bindings::inheritance::Castable;
19use crate::dom::bindings::root::{Dom, DomRoot};
20use crate::dom::bindings::str::DOMString;
21use crate::dom::bindings::utils::to_frozen_array;
22use crate::dom::event::Event;
23use crate::dom::window::Window;
24use crate::dom::xrinputsource::XRInputSource;
25use crate::dom::xrsession::XRSession;
26use crate::realms::enter_auto_realm;
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    fn new_inherited(session: &XRSession) -> XRInputSourcesChangeEvent {
40        XRInputSourcesChangeEvent {
41            event: Event::new_inherited(),
42            session: Dom::from_ref(session),
43            added: Heap::default(),
44            removed: Heap::default(),
45        }
46    }
47
48    #[expect(clippy::too_many_arguments)]
49    pub(crate) fn new(
50        cx: &mut JSContext,
51        window: &Window,
52        type_: Atom,
53        bubbles: bool,
54        cancelable: bool,
55        session: &XRSession,
56        added: &[&XRInputSource],
57        removed: &[&XRInputSource],
58    ) -> DomRoot<XRInputSourcesChangeEvent> {
59        Self::new_with_proto(
60            cx, window, None, type_, bubbles, cancelable, session, added, removed,
61        )
62    }
63
64    #[expect(clippy::too_many_arguments)]
65    fn new_with_proto(
66        cx: &mut JSContext,
67        window: &Window,
68        proto: Option<HandleObject>,
69        type_: Atom,
70        bubbles: bool,
71        cancelable: bool,
72        session: &XRSession,
73        added: &[&XRInputSource],
74        removed: &[&XRInputSource],
75    ) -> DomRoot<XRInputSourcesChangeEvent> {
76        let changeevent = reflect_dom_object_with_proto(
77            cx,
78            Box::new(XRInputSourcesChangeEvent::new_inherited(session)),
79            window,
80            proto,
81        );
82        {
83            let event = changeevent.upcast::<Event>();
84            event.init_event(type_, bubbles, cancelable);
85        }
86        let mut realm = enter_auto_realm(cx, window);
87        let cx = &mut realm.current_realm();
88        rooted!(&in(cx) let mut frozen_val: JSVal);
89        to_frozen_array(cx, added, frozen_val.handle_mut());
90        changeevent.added.set(*frozen_val);
91        to_frozen_array(cx, removed, frozen_val.handle_mut());
92        changeevent.removed.set(*frozen_val);
93        changeevent
94    }
95}
96
97impl XRInputSourcesChangeEventMethods<crate::DomTypeHolder> for XRInputSourcesChangeEvent {
98    /// <https://immersive-web.github.io/webxr/#dom-xrinputsourceschangeevent-xrinputsourceschangeevent>
99    fn Constructor(
100        cx: &mut JSContext,
101        window: &Window,
102        proto: Option<HandleObject>,
103        type_: DOMString,
104        init: &XRInputSourcesChangeEventBinding::XRInputSourcesChangeEventInit,
105    ) -> DomRoot<XRInputSourcesChangeEvent> {
106        XRInputSourcesChangeEvent::new_with_proto(
107            cx,
108            window,
109            proto,
110            Atom::from(type_),
111            init.parent.bubbles,
112            init.parent.cancelable,
113            &init.session,
114            init.added.as_ref_slice(),
115            init.removed.as_ref_slice(),
116        )
117    }
118
119    /// <https://immersive-web.github.io/webxr/#dom-xrinputsourceschangeevent-session>
120    fn Session(&self) -> DomRoot<XRSession> {
121        DomRoot::from_ref(&*self.session)
122    }
123
124    /// <https://immersive-web.github.io/webxr/#dom-xrinputsourceschangeevent-added>
125    fn Added(&self, mut retval: MutableHandleValue) {
126        retval.set(self.added.get())
127    }
128
129    /// <https://immersive-web.github.io/webxr/#dom-xrinputsourceschangeevent-removed>
130    fn Removed(&self, mut retval: MutableHandleValue) {
131        retval.set(self.removed.get())
132    }
133
134    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
135    fn IsTrusted(&self) -> bool {
136        self.event.IsTrusted()
137    }
138}