script/animation_timeline.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
5#![deny(missing_docs)]
6
7//! A timeline module, used to specify an `AnimationTimeline` which determines
8//! the time used for synchronizing animations in the script thread.
9
10use std::time::{SystemTime, UNIX_EPOCH};
11
12use jstraceable_derive::JSTraceable;
13
14/// A `AnimationTimeline` which is used to synchronize animations during the script
15/// event loop.
16#[derive(Clone, Copy, Debug, JSTraceable, MallocSizeOf)]
17pub(crate) struct AnimationTimeline {
18 current_value: f64,
19}
20
21impl AnimationTimeline {
22 /// Creates a new "normal" timeline, i.e., a "Current" mode timer.
23 #[inline]
24 pub(crate) fn new() -> Self {
25 Self {
26 current_value: SystemTime::now()
27 .duration_since(UNIX_EPOCH)
28 .unwrap_or_default()
29 .as_secs_f64(),
30 }
31 }
32
33 /// Creates a new "test mode" timeline, with initial time 0.
34 #[inline]
35 pub(crate) fn new_for_testing() -> Self {
36 Self { current_value: 0. }
37 }
38
39 /// Returns the current value of the timeline in seconds.
40 pub(crate) fn current_value(&self) -> f64 {
41 self.current_value
42 }
43
44 /// Updates the value of the `AnimationTimeline` to the current clock time.
45 pub(crate) fn update(&mut self) {
46 self.current_value = SystemTime::now()
47 .duration_since(UNIX_EPOCH)
48 .unwrap_or_default()
49 .as_secs_f64();
50 }
51
52 /// Increments the current value of the timeline by a specific number of seconds.
53 /// This is used for testing.
54 pub(crate) fn advance_specific(&mut self, by: f64) {
55 self.current_value += by;
56 }
57}