Skip to main content

script/dom/webxr/
xrsessionevent.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::XRSessionEventBinding::{self, XRSessionEventMethods};
13use crate::dom::bindings::error::Fallible;
14use crate::dom::bindings::inheritance::Castable;
15use crate::dom::bindings::root::{Dom, DomRoot};
16use crate::dom::bindings::str::DOMString;
17use crate::dom::event::Event;
18use crate::dom::window::Window;
19use crate::dom::xrsession::XRSession;
20
21#[dom_struct]
22pub(crate) struct XRSessionEvent {
23    event: Event,
24    session: Dom<XRSession>,
25}
26
27impl XRSessionEvent {
28    fn new_inherited(session: &XRSession) -> XRSessionEvent {
29        XRSessionEvent {
30            event: Event::new_inherited(),
31            session: Dom::from_ref(session),
32        }
33    }
34
35    pub(crate) fn new(
36        cx: &mut JSContext,
37        window: &Window,
38        type_: Atom,
39        bubbles: bool,
40        cancelable: bool,
41        session: &XRSession,
42    ) -> DomRoot<XRSessionEvent> {
43        Self::new_with_proto(cx, window, None, type_, bubbles, cancelable, session)
44    }
45
46    fn new_with_proto(
47        cx: &mut JSContext,
48        window: &Window,
49        proto: Option<HandleObject>,
50        type_: Atom,
51        bubbles: bool,
52        cancelable: bool,
53        session: &XRSession,
54    ) -> DomRoot<XRSessionEvent> {
55        let trackevent = reflect_dom_object_with_proto_and_cx(
56            Box::new(XRSessionEvent::new_inherited(session)),
57            window,
58            proto,
59            cx,
60        );
61        {
62            let event = trackevent.upcast::<Event>();
63            event.init_event(type_, bubbles, cancelable);
64        }
65        trackevent
66    }
67}
68
69impl XRSessionEventMethods<crate::DomTypeHolder> for XRSessionEvent {
70    /// <https://immersive-web.github.io/webxr/#dom-xrsessionevent-xrsessionevent>
71    fn Constructor(
72        cx: &mut JSContext,
73        window: &Window,
74        proto: Option<HandleObject>,
75        type_: DOMString,
76        init: &XRSessionEventBinding::XRSessionEventInit,
77    ) -> Fallible<DomRoot<XRSessionEvent>> {
78        Ok(XRSessionEvent::new_with_proto(
79            cx,
80            window,
81            proto,
82            Atom::from(type_),
83            init.parent.bubbles,
84            init.parent.cancelable,
85            &init.session,
86        ))
87    }
88
89    /// <https://immersive-web.github.io/webxr/#dom-xrsessioneventinit-session>
90    fn Session(&self) -> DomRoot<XRSession> {
91        DomRoot::from_ref(&*self.session)
92    }
93
94    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
95    fn IsTrusted(&self) -> bool {
96        self.event.IsTrusted()
97    }
98}