Skip to main content

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