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    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        window: &Window,
51        type_: Atom,
52        bubbles: bool,
53        cancelable: bool,
54        session: &XRSession,
55        added: &[DomRoot<XRInputSource>],
56        removed: &[DomRoot<XRInputSource>],
57        can_gc: CanGc,
58    ) -> DomRoot<XRInputSourcesChangeEvent> {
59        Self::new_with_proto(
60            window, None, type_, bubbles, cancelable, session, added, removed, can_gc,
61        )
62    }
63
64    #[expect(clippy::too_many_arguments)]
65    fn new_with_proto(
66        window: &Window,
67        proto: Option<HandleObject>,
68        type_: Atom,
69        bubbles: bool,
70        cancelable: bool,
71        session: &XRSession,
72        added: &[DomRoot<XRInputSource>],
73        removed: &[DomRoot<XRInputSource>],
74        can_gc: CanGc,
75    ) -> DomRoot<XRInputSourcesChangeEvent> {
76        let changeevent = reflect_dom_object_with_proto(
77            Box::new(XRInputSourcesChangeEvent::new_inherited(session)),
78            window,
79            proto,
80            can_gc,
81        );
82        {
83            let event = changeevent.upcast::<Event>();
84            event.init_event(type_, bubbles, cancelable);
85        }
86        let _ac = enter_realm(window);
87        let cx = GlobalScope::get_cx();
88        rooted!(in(*cx) let mut frozen_val: JSVal);
89        to_frozen_array(added, cx, frozen_val.handle_mut(), can_gc);
90        changeevent.added.set(*frozen_val);
91        to_frozen_array(removed, cx, frozen_val.handle_mut(), can_gc);
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        window: &Window,
101        proto: Option<HandleObject>,
102        can_gc: CanGc,
103        type_: DOMString,
104        init: &XRInputSourcesChangeEventBinding::XRInputSourcesChangeEventInit,
105    ) -> DomRoot<XRInputSourcesChangeEvent> {
106        XRInputSourcesChangeEvent::new_with_proto(
107            window,
108            proto,
109            Atom::from(type_),
110            init.parent.bubbles,
111            init.parent.cancelable,
112            &init.session,
113            &init.added,
114            &init.removed,
115            can_gc,
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, _cx: JSContext, 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, _cx: JSContext, 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}