Skip to main content

script/dom/event/
pagetransitionevent.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 std::cell::Cell;
6
7use dom_struct::dom_struct;
8use js::context::JSContext;
9use js::rust::HandleObject;
10use script_bindings::reflector::reflect_dom_object_with_proto_and_cx;
11use stylo_atoms::Atom;
12
13use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
14use crate::dom::bindings::codegen::Bindings::PageTransitionEventBinding;
15use crate::dom::bindings::codegen::Bindings::PageTransitionEventBinding::PageTransitionEventMethods;
16use crate::dom::bindings::error::Fallible;
17use crate::dom::bindings::inheritance::Castable;
18use crate::dom::bindings::root::DomRoot;
19use crate::dom::bindings::str::DOMString;
20use crate::dom::event::Event;
21use crate::dom::window::Window;
22
23// https://html.spec.whatwg.org/multipage/#pagetransitionevent
24#[dom_struct]
25pub(crate) struct PageTransitionEvent {
26    event: Event,
27    persisted: Cell<bool>,
28}
29
30impl PageTransitionEvent {
31    fn new_inherited() -> PageTransitionEvent {
32        PageTransitionEvent {
33            event: Event::new_inherited(),
34            persisted: Cell::new(false),
35        }
36    }
37
38    fn new_uninitialized(
39        cx: &mut JSContext,
40        window: &Window,
41        proto: Option<HandleObject>,
42    ) -> DomRoot<PageTransitionEvent> {
43        reflect_dom_object_with_proto_and_cx(
44            Box::new(PageTransitionEvent::new_inherited()),
45            window,
46            proto,
47            cx,
48        )
49    }
50
51    pub(crate) fn new(
52        cx: &mut JSContext,
53        window: &Window,
54        type_: Atom,
55        bubbles: bool,
56        cancelable: bool,
57        persisted: bool,
58    ) -> DomRoot<PageTransitionEvent> {
59        Self::new_with_proto(cx, window, None, type_, bubbles, cancelable, persisted)
60    }
61
62    fn new_with_proto(
63        cx: &mut JSContext,
64        window: &Window,
65        proto: Option<HandleObject>,
66        type_: Atom,
67        bubbles: bool,
68        cancelable: bool,
69        persisted: bool,
70    ) -> DomRoot<PageTransitionEvent> {
71        let ev = PageTransitionEvent::new_uninitialized(cx, window, proto);
72        ev.persisted.set(persisted);
73        {
74            let event = ev.upcast::<Event>();
75            event.init_event(type_, bubbles, cancelable);
76        }
77        ev
78    }
79}
80
81impl PageTransitionEventMethods<crate::DomTypeHolder> for PageTransitionEvent {
82    /// <https://html.spec.whatwg.org/multipage/#pagetransitionevent>
83    fn Constructor(
84        cx: &mut JSContext,
85        window: &Window,
86        proto: Option<HandleObject>,
87        type_: DOMString,
88        init: &PageTransitionEventBinding::PageTransitionEventInit,
89    ) -> Fallible<DomRoot<PageTransitionEvent>> {
90        Ok(PageTransitionEvent::new_with_proto(
91            cx,
92            window,
93            proto,
94            Atom::from(type_),
95            init.parent.bubbles,
96            init.parent.cancelable,
97            init.persisted,
98        ))
99    }
100
101    /// <https://html.spec.whatwg.org/multipage/#dom-pagetransitionevent-persisted>
102    fn Persisted(&self) -> bool {
103        self.persisted.get()
104    }
105
106    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
107    fn IsTrusted(&self) -> bool {
108        self.event.IsTrusted()
109    }
110}