Skip to main content

script/dom/webxr/
xrinputsourceevent.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::rust::HandleObject;
8use script_bindings::reflector::reflect_dom_object_with_proto_and_cx;
9use stylo_atoms::Atom;
10
11use crate::dom::bindings::codegen::Bindings::EventBinding::Event_Binding::EventMethods;
12use crate::dom::bindings::codegen::Bindings::XRInputSourceEventBinding::{
13    self, XRInputSourceEventMethods,
14};
15use crate::dom::bindings::error::Fallible;
16use crate::dom::bindings::inheritance::Castable;
17use crate::dom::bindings::root::{Dom, DomRoot};
18use crate::dom::bindings::str::DOMString;
19use crate::dom::event::Event;
20use crate::dom::window::Window;
21use crate::dom::xrframe::XRFrame;
22use crate::dom::xrinputsource::XRInputSource;
23
24#[dom_struct]
25pub(crate) struct XRInputSourceEvent {
26    event: Event,
27    frame: Dom<XRFrame>,
28    source: Dom<XRInputSource>,
29}
30
31impl XRInputSourceEvent {
32    fn new_inherited(frame: &XRFrame, source: &XRInputSource) -> XRInputSourceEvent {
33        XRInputSourceEvent {
34            event: Event::new_inherited(),
35            frame: Dom::from_ref(frame),
36            source: Dom::from_ref(source),
37        }
38    }
39
40    pub(crate) fn new(
41        cx: &mut JSContext,
42        window: &Window,
43        type_: Atom,
44        bubbles: bool,
45        cancelable: bool,
46        frame: &XRFrame,
47        source: &XRInputSource,
48    ) -> DomRoot<XRInputSourceEvent> {
49        Self::new_with_proto(cx, window, None, type_, bubbles, cancelable, frame, source)
50    }
51
52    #[expect(clippy::too_many_arguments)]
53    fn new_with_proto(
54        cx: &mut JSContext,
55        window: &Window,
56        proto: Option<HandleObject>,
57        type_: Atom,
58        bubbles: bool,
59        cancelable: bool,
60        frame: &XRFrame,
61        source: &XRInputSource,
62    ) -> DomRoot<XRInputSourceEvent> {
63        let trackevent = reflect_dom_object_with_proto_and_cx(
64            Box::new(XRInputSourceEvent::new_inherited(frame, source)),
65            window,
66            proto,
67            cx,
68        );
69        {
70            let event = trackevent.upcast::<Event>();
71            event.init_event(type_, bubbles, cancelable);
72        }
73        trackevent
74    }
75}
76
77impl XRInputSourceEventMethods<crate::DomTypeHolder> for XRInputSourceEvent {
78    /// <https://immersive-web.github.io/webxr/#dom-xrinputsourceevent-xrinputsourceevent>
79    fn Constructor(
80        cx: &mut JSContext,
81        window: &Window,
82        proto: Option<HandleObject>,
83        type_: DOMString,
84        init: &XRInputSourceEventBinding::XRInputSourceEventInit,
85    ) -> Fallible<DomRoot<XRInputSourceEvent>> {
86        Ok(XRInputSourceEvent::new_with_proto(
87            cx,
88            window,
89            proto,
90            Atom::from(type_),
91            init.parent.bubbles,
92            init.parent.cancelable,
93            &init.frame,
94            &init.inputSource,
95        ))
96    }
97
98    /// <https://immersive-web.github.io/webxr/#dom-xrinputsourceeventinit-frame>
99    fn Frame(&self) -> DomRoot<XRFrame> {
100        DomRoot::from_ref(&*self.frame)
101    }
102
103    /// <https://immersive-web.github.io/webxr/#dom-xrinputsourceeventinit-inputsource>
104    fn InputSource(&self) -> DomRoot<XRInputSource> {
105        DomRoot::from_ref(&*self.source)
106    }
107
108    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
109    fn IsTrusted(&self) -> bool {
110        self.event.IsTrusted()
111    }
112}