Skip to main content

script/dom/animations/
animation.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::codegen::GenericBindings::DocumentBinding::DocumentMethods;
9use script_bindings::codegen::GenericBindings::WindowBinding::WindowMethods;
10use script_bindings::inheritance::Castable;
11use script_bindings::reflector::reflect_dom_object_with_proto_and_cx;
12use script_bindings::root::DomRoot;
13
14use crate::dom::animationeffect::AnimationEffect;
15use crate::dom::bindings::codegen::Bindings::AnimationBinding::AnimationMethods;
16use crate::dom::bindings::root::MutNullableDom;
17use crate::dom::eventtarget::EventTarget;
18use crate::dom::globalscope::GlobalScope;
19use crate::dom::types::AnimationTimeline;
20use crate::dom::window::Window;
21
22/// <https://drafts.csswg.org/web-animations-1/#animation>
23#[dom_struct]
24pub(crate) struct Animation {
25    event_target: EventTarget,
26
27    /// <https://drafts.csswg.org/web-animations-1/#timeline>
28    timeline: MutNullableDom<AnimationTimeline>,
29
30    /// <https://drafts.csswg.org/web-animations-1/#animation-associated-effect>
31    associated_effect: MutNullableDom<AnimationEffect>,
32}
33
34impl Animation {
35    pub(crate) fn new_inherited() -> Self {
36        Self {
37            event_target: EventTarget::new_inherited(),
38            timeline: Default::default(),
39            associated_effect: Default::default(),
40        }
41    }
42
43    fn new_with_proto_and_cx(
44        cx: &mut JSContext,
45        global: &GlobalScope,
46        proto: Option<HandleObject>,
47    ) -> DomRoot<Self> {
48        reflect_dom_object_with_proto_and_cx(Box::new(Self::new_inherited()), global, proto, cx)
49    }
50
51    pub(crate) fn new(cx: &mut JSContext, global: &GlobalScope) -> DomRoot<Self> {
52        Self::new_with_proto_and_cx(cx, global, None)
53    }
54
55    /// <https://drafts.csswg.org/web-animations-1/#animation-set-the-timeline-of-an-animation>
56    fn set_the_timeline(&self, timeline: &AnimationTimeline) {
57        // FIXME: Implement this fully
58        self.timeline.set(Some(timeline));
59    }
60
61    /// <https://drafts.csswg.org/web-animations-1/#animation-set-the-associated-effect-of-an-animation>
62    fn set_the_associated_effect(&self, effect: Option<&AnimationEffect>) {
63        // FIXME: Implement this fully
64        self.associated_effect.set(effect);
65    }
66}
67
68impl AnimationMethods<crate::DomTypeHolder> for Animation {
69    /// <https://drafts.csswg.org/web-animations-1/#dom-animation-animation>
70    fn Constructor(
71        cx: &mut JSContext,
72        window: &Window,
73        _object: Option<HandleObject>,
74        effect: Option<&AnimationEffect>,
75    ) -> DomRoot<Self> {
76        // Step 1. Let animation be a new Animation object.
77        let animation = Animation::new(cx, window.upcast());
78
79        // Step 2. Run the procedure to set the timeline of an animation on animation passing timeline
80        // as the new timeline; or, if the timeline argument is missing, passing the default document
81        // timeline of the Document associated with the Window that is the current global object.
82        // TODO: We don't suppor the timeline argument yet.
83        animation.set_the_timeline(window.Document().Timeline().upcast());
84
85        // Step 3. Run the procedure to set the associated effect of an animation on animation passing
86        // source as the new effect.
87        animation.set_the_associated_effect(effect);
88
89        animation
90    }
91}