script/dom/animations/
animationtimeline.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 script_bindings::num::Finite;
9use script_bindings::reflector::Reflector;
10use time::Duration;
11
12use crate::dom::bindings::codegen::Bindings::AnimationTimelineBinding::AnimationTimelineMethods;
13
14/// <https://drafts.csswg.org/web-animations-1/#animationtimeline>
15#[dom_struct]
16pub(crate) struct AnimationTimeline {
17    reflector_: Reflector,
18    /// <https://drafts.csswg.org/web-animations-1/#dom-animationtimeline-currenttime>
19    ///
20    /// The current time of this [`AnimationTimeline`] expressed as a [`time::Duration`] since the
21    /// Document's "time origin." Note that this Duration may be negative.
22    #[no_trace]
23    current_time: Cell<Duration>,
24}
25
26impl AnimationTimeline {
27    pub(crate) fn new_inherited(current_time: Duration) -> Self {
28        Self {
29            reflector_: Reflector::new(),
30            current_time: Cell::new(current_time),
31        }
32    }
33
34    pub(crate) fn current_time_in_seconds(&self) -> f64 {
35        self.current_time.get().as_seconds_f64()
36    }
37
38    pub(crate) fn set_current_time(&self, duration: Duration) {
39        self.current_time.set(duration);
40    }
41
42    pub(crate) fn advance_specific(&self, by: Duration) {
43        self.current_time.set(self.current_time.get() + by);
44    }
45}
46
47impl AnimationTimelineMethods<crate::DomTypeHolder> for AnimationTimeline {
48    /// <https://drafts.csswg.org/web-animations-1/#dom-animationtimeline-currenttime>
49    fn GetCurrentTime(&self) -> Option<Finite<f64>> {
50        Finite::new(self.current_time.get().as_seconds_f64() * 1000.0)
51    }
52}