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::reflector::reflect_dom_object_with_proto_and_cx;
9use stylo_atoms::Atom;
10
11use crate::dom::bindings::codegen::Bindings::AnimationEventBinding::{
12    AnimationEventInit, AnimationEventMethods,
13};
14use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
15use crate::dom::bindings::inheritance::Castable;
16use crate::dom::bindings::num::Finite;
17use crate::dom::bindings::root::DomRoot;
18use crate::dom::bindings::str::DOMString;
19use crate::dom::event::Event;
20use crate::dom::window::Window;
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        cx: &mut JSContext,
43        window: &Window,
44        type_: Atom,
45        init: &AnimationEventInit,
46    ) -> DomRoot<AnimationEvent> {
47        Self::new_with_proto(cx, window, None, type_, init)
48    }
49
50    fn new_with_proto(
51        cx: &mut JSContext,
52        window: &Window,
53        proto: Option<HandleObject>,
54        type_: Atom,
55        init: &AnimationEventInit,
56    ) -> DomRoot<AnimationEvent> {
57        let ev = reflect_dom_object_with_proto_and_cx(
58            Box::new(AnimationEvent::new_inherited(init)),
59            window,
60            proto,
61            cx,
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        cx: &mut JSContext,
75        window: &Window,
76        proto: Option<HandleObject>,
77        type_: DOMString,
78        init: &AnimationEventInit,
79    ) -> DomRoot<AnimationEvent> {
80        AnimationEvent::new_with_proto(cx, window, proto, Atom::from(type_), init)
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}