script/dom/
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::rust::HandleObject;
9use stylo_atoms::Atom;
10
11use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
12use crate::dom::bindings::codegen::Bindings::PageTransitionEventBinding;
13use crate::dom::bindings::codegen::Bindings::PageTransitionEventBinding::PageTransitionEventMethods;
14use crate::dom::bindings::error::Fallible;
15use crate::dom::bindings::inheritance::Castable;
16use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
17use crate::dom::bindings::root::DomRoot;
18use crate::dom::bindings::str::DOMString;
19use crate::dom::event::Event;
20use crate::dom::window::Window;
21use crate::script_runtime::CanGc;
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        window: &Window,
40        proto: Option<HandleObject>,
41        can_gc: CanGc,
42    ) -> DomRoot<PageTransitionEvent> {
43        reflect_dom_object_with_proto(
44            Box::new(PageTransitionEvent::new_inherited()),
45            window,
46            proto,
47            can_gc,
48        )
49    }
50
51    pub(crate) fn new(
52        window: &Window,
53        type_: Atom,
54        bubbles: bool,
55        cancelable: bool,
56        persisted: bool,
57        can_gc: CanGc,
58    ) -> DomRoot<PageTransitionEvent> {
59        Self::new_with_proto(window, None, type_, bubbles, cancelable, persisted, can_gc)
60    }
61
62    fn new_with_proto(
63        window: &Window,
64        proto: Option<HandleObject>,
65        type_: Atom,
66        bubbles: bool,
67        cancelable: bool,
68        persisted: bool,
69        can_gc: CanGc,
70    ) -> DomRoot<PageTransitionEvent> {
71        let ev = PageTransitionEvent::new_uninitialized(window, proto, can_gc);
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        window: &Window,
85        proto: Option<HandleObject>,
86        can_gc: CanGc,
87        type_: DOMString,
88        init: &PageTransitionEventBinding::PageTransitionEventInit,
89    ) -> Fallible<DomRoot<PageTransitionEvent>> {
90        Ok(PageTransitionEvent::new_with_proto(
91            window,
92            proto,
93            Atom::from(type_),
94            init.parent.bubbles,
95            init.parent.cancelable,
96            init.persisted,
97            can_gc,
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}