script/dom/webxr/
xrinputsourceschangeevent.rs1use 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_and_cx;
11use stylo_atoms::Atom;
12
13use crate::dom::bindings::codegen::Bindings::EventBinding::Event_Binding::EventMethods;
14use crate::dom::bindings::codegen::Bindings::XRInputSourcesChangeEventBinding::{
15 self, XRInputSourcesChangeEventMethods,
16};
17use crate::dom::bindings::inheritance::Castable;
18use crate::dom::bindings::root::{Dom, DomRoot};
19use crate::dom::bindings::str::DOMString;
20use crate::dom::bindings::utils::to_frozen_array;
21use crate::dom::event::Event;
22use crate::dom::window::Window;
23use crate::dom::xrinputsource::XRInputSource;
24use crate::dom::xrsession::XRSession;
25use crate::realms::enter_auto_realm;
26
27#[dom_struct]
28pub(crate) struct XRInputSourcesChangeEvent {
29 event: Event,
30 session: Dom<XRSession>,
31 #[ignore_malloc_size_of = "mozjs"]
32 added: Heap<JSVal>,
33 #[ignore_malloc_size_of = "mozjs"]
34 removed: Heap<JSVal>,
35}
36
37impl XRInputSourcesChangeEvent {
38 fn new_inherited(session: &XRSession) -> XRInputSourcesChangeEvent {
39 XRInputSourcesChangeEvent {
40 event: Event::new_inherited(),
41 session: Dom::from_ref(session),
42 added: Heap::default(),
43 removed: Heap::default(),
44 }
45 }
46
47 #[expect(clippy::too_many_arguments)]
48 pub(crate) fn new(
49 cx: &mut JSContext,
50 window: &Window,
51 type_: Atom,
52 bubbles: bool,
53 cancelable: bool,
54 session: &XRSession,
55 added: &[DomRoot<XRInputSource>],
56 removed: &[DomRoot<XRInputSource>],
57 ) -> DomRoot<XRInputSourcesChangeEvent> {
58 Self::new_with_proto(
59 cx, window, None, type_, bubbles, cancelable, session, added, removed,
60 )
61 }
62
63 #[expect(clippy::too_many_arguments)]
64 fn new_with_proto(
65 cx: &mut JSContext,
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 ) -> DomRoot<XRInputSourcesChangeEvent> {
75 let changeevent = reflect_dom_object_with_proto_and_cx(
76 Box::new(XRInputSourcesChangeEvent::new_inherited(session)),
77 window,
78 proto,
79 cx,
80 );
81 {
82 let event = changeevent.upcast::<Event>();
83 event.init_event(type_, bubbles, cancelable);
84 }
85 let mut realm = enter_auto_realm(cx, window);
86 let cx = &mut realm.current_realm();
87 rooted!(&in(cx) let mut frozen_val: JSVal);
88 to_frozen_array(cx, added, frozen_val.handle_mut());
89 changeevent.added.set(*frozen_val);
90 to_frozen_array(cx, removed, frozen_val.handle_mut());
91 changeevent.removed.set(*frozen_val);
92 changeevent
93 }
94}
95
96impl XRInputSourcesChangeEventMethods<crate::DomTypeHolder> for XRInputSourcesChangeEvent {
97 fn Constructor(
99 cx: &mut JSContext,
100 window: &Window,
101 proto: Option<HandleObject>,
102 type_: DOMString,
103 init: &XRInputSourcesChangeEventBinding::XRInputSourcesChangeEventInit,
104 ) -> DomRoot<XRInputSourcesChangeEvent> {
105 XRInputSourcesChangeEvent::new_with_proto(
106 cx,
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 )
116 }
117
118 fn Session(&self) -> DomRoot<XRSession> {
120 DomRoot::from_ref(&*self.session)
121 }
122
123 fn Added(&self, mut retval: MutableHandleValue) {
125 retval.set(self.added.get())
126 }
127
128 fn Removed(&self, mut retval: MutableHandleValue) {
130 retval.set(self.removed.get())
131 }
132
133 fn IsTrusted(&self) -> bool {
135 self.event.IsTrusted()
136 }
137}