Skip to main content

aura_anim_iced/timing/
duration.rs

1use std::{
2    ops::{Add, AddAssign},
3    time::Duration as StdDuration,
4};
5
6use crate::timing::utils::std_duration_from_secs;
7
8/// A non-negative animation duration.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
10pub struct Duration(StdDuration);
11
12impl Duration {
13    /// A zero-length duration.
14    pub const ZERO: Self = Self(StdDuration::ZERO);
15
16    /// Creates a duration from milliseconds.
17    #[must_use]
18    pub fn from_millis(millis: f64) -> Self {
19        Self(std_duration_from_secs(millis / 1000.0))
20    }
21
22    /// Creates a duration from seconds.
23    #[must_use]
24    pub fn from_secs(seconds: f64) -> Self {
25        Self(std_duration_from_secs(seconds))
26    }
27
28    /// Returns this duration in milliseconds.
29    #[must_use]
30    pub const fn as_millis(self) -> f64 {
31        self.0.as_secs_f64() * 1000.0
32    }
33
34    pub(crate) fn checked_mul(self, rhs: u32) -> Option<Self> {
35        self.0.checked_mul(rhs).map(Self)
36    }
37
38    pub(crate) fn checked_add_delay(self, rhs: Delay) -> Option<Self> {
39        self.0.checked_add(rhs.0).map(Self)
40    }
41
42    pub(crate) fn checked_sub(self, rhs: Self) -> Option<Self> {
43        self.0.checked_sub(rhs.0).map(Self)
44    }
45
46    pub(crate) fn max(self, rhs: Self) -> Self {
47        Self(self.0.max(rhs.0))
48    }
49}
50
51impl AddAssign for Duration {
52    fn add_assign(&mut self, rhs: Self) {
53        self.0 += rhs.0;
54    }
55}
56
57impl Add for Duration {
58    type Output = Self;
59
60    fn add(self, rhs: Self) -> Self::Output {
61        Self(self.0 + rhs.0)
62    }
63}
64
65impl From<StdDuration> for Duration {
66    fn from(value: StdDuration) -> Self {
67        Self(value)
68    }
69}
70
71/// A non-negative animation start delay.
72#[derive(Debug, Clone, Copy, PartialEq, Default)]
73pub struct Delay(StdDuration);
74
75impl Delay {
76    /// No start delay.
77    pub const ZERO: Self = Self(StdDuration::ZERO);
78
79    /// Creates a delay from milliseconds.
80    #[must_use]
81    pub fn from_millis(millis: f64) -> Self {
82        Self(std_duration_from_secs(millis / 1000.0))
83    }
84
85    /// Creates a delay from seconds.
86    #[must_use]
87    pub fn from_secs(seconds: f64) -> Self {
88        Self(std_duration_from_secs(seconds))
89    }
90
91    /// Returns this delay in milliseconds.
92    #[must_use]
93    pub const fn as_millis(self) -> f64 {
94        self.0.as_secs_f64() * 1000.0
95    }
96}
97
98impl From<StdDuration> for Delay {
99    fn from(value: StdDuration) -> Self {
100        Self(value)
101    }
102}