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::rust::HandleObject;
7use stylo_atoms::Atom;
8
9use crate::dom::bindings::codegen::Bindings::EventBinding::Event_Binding::EventMethods;
10use crate::dom::bindings::codegen::Bindings::XRSessionEventBinding::{self, XRSessionEventMethods};
11use crate::dom::bindings::error::Fallible;
12use crate::dom::bindings::inheritance::Castable;
13use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
14use crate::dom::bindings::root::{Dom, DomRoot};
15use crate::dom::bindings::str::DOMString;
16use crate::dom::event::Event;
17use crate::dom::window::Window;
18use crate::dom::xrsession::XRSession;
19use crate::script_runtime::CanGc;
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        window: &Window,
37        type_: Atom,
38        bubbles: bool,
39        cancelable: bool,
40        session: &XRSession,
41        can_gc: CanGc,
42    ) -> DomRoot<XRSessionEvent> {
43        Self::new_with_proto(window, None, type_, bubbles, cancelable, session, can_gc)
44    }
45
46    fn new_with_proto(
47        window: &Window,
48        proto: Option<HandleObject>,
49        type_: Atom,
50        bubbles: bool,
51        cancelable: bool,
52        session: &XRSession,
53        can_gc: CanGc,
54    ) -> DomRoot<XRSessionEvent> {
55        let trackevent = reflect_dom_object_with_proto(
56            Box::new(XRSessionEvent::new_inherited(session)),
57            window,
58            proto,
59            can_gc,
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        window: &Window,
73        proto: Option<HandleObject>,
74        can_gc: CanGc,
75        type_: DOMString,
76        init: &XRSessionEventBinding::XRSessionEventInit,
77    ) -> Fallible<DomRoot<XRSessionEvent>> {
78        Ok(XRSessionEvent::new_with_proto(
79            window,
80            proto,
81            Atom::from(type_),
82            init.parent.bubbles,
83            init.parent.cancelable,
84            &init.session,
85            can_gc,
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}