script/dom/
animationevent.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::AnimationEventBinding::{
10    AnimationEventInit, AnimationEventMethods,
11};
12use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
13use crate::dom::bindings::inheritance::Castable;
14use crate::dom::bindings::num::Finite;
15use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::str::DOMString;
18use crate::dom::event::Event;
19use crate::dom::window::Window;
20use crate::script_runtime::CanGc;
21
22#[dom_struct]
23pub(crate) struct AnimationEvent {
24    event: Event,
25    #[no_trace]
26    animation_name: Atom,
27    elapsed_time: Finite<f32>,
28    pseudo_element: DOMString,
29}
30
31impl AnimationEvent {
32    fn new_inherited(init: &AnimationEventInit) -> AnimationEvent {
33        AnimationEvent {
34            event: Event::new_inherited(),
35            animation_name: Atom::from(init.animationName.clone()),
36            elapsed_time: init.elapsedTime,
37            pseudo_element: init.pseudoElement.clone(),
38        }
39    }
40
41    pub(crate) fn new(
42        window: &Window,
43        type_: Atom,
44        init: &AnimationEventInit,
45        can_gc: CanGc,
46    ) -> DomRoot<AnimationEvent> {
47        Self::new_with_proto(window, None, type_, init, can_gc)
48    }
49
50    fn new_with_proto(
51        window: &Window,
52        proto: Option<HandleObject>,
53        type_: Atom,
54        init: &AnimationEventInit,
55        can_gc: CanGc,
56    ) -> DomRoot<AnimationEvent> {
57        let ev = reflect_dom_object_with_proto(
58            Box::new(AnimationEvent::new_inherited(init)),
59            window,
60            proto,
61            can_gc,
62        );
63        {
64            let event = ev.upcast::<Event>();
65            event.init_event(type_, init.parent.bubbles, init.parent.cancelable);
66        }
67        ev
68    }
69}
70
71impl AnimationEventMethods<crate::DomTypeHolder> for AnimationEvent {
72    // https://drafts.csswg.org/css-animations/#dom-animationevent-animationevent
73    fn Constructor(
74        window: &Window,
75        proto: Option<HandleObject>,
76        can_gc: CanGc,
77        type_: DOMString,
78        init: &AnimationEventInit,
79    ) -> DomRoot<AnimationEvent> {
80        AnimationEvent::new_with_proto(window, proto, Atom::from(type_), init, can_gc)
81    }
82
83    // https://drafts.csswg.org/css-animations/#interface-animationevent-attributes
84    fn AnimationName(&self) -> DOMString {
85        DOMString::from(&*self.animation_name)
86    }
87
88    // https://drafts.csswg.org/css-animations/#interface-animationevent-attributes
89    fn ElapsedTime(&self) -> Finite<f32> {
90        self.elapsed_time
91    }
92
93    // https://drafts.csswg.org/css-animations/#interface-animationevent-attributes
94    fn PseudoElement(&self) -> DOMString {
95        self.pseudo_element.clone()
96    }
97
98    // https://dom.spec.whatwg.org/#dom-event-istrusted
99    fn IsTrusted(&self) -> bool {
100        self.upcast::<Event>().IsTrusted()
101    }
102}