Skip to main content

aura_anim_iced/runtime/
entry.rs

1use super::AnimationHandle;
2use crate::{
3    property::PropertySnapshot, runtime::source::AnimationSource,
4    runtime::target::AnimationTargetId, timing::Duration,
5};
6
7/// Playback state tracked for an active runtime entry.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
9pub enum AnimationPlaybackState {
10    /// The animation can advance on runtime ticks.
11    #[default]
12    Playing,
13    /// The animation is held at its current elapsed position.
14    Paused,
15    /// The animation has been canceled and should stop producing snapshots.
16    Canceled,
17    /// The animation reached completion.
18    Completed,
19}
20
21/// Internal runtime registry entry for one animation source.
22#[derive(Debug, Clone, PartialEq)]
23pub(crate) struct ActiveAnimation {
24    handle: AnimationHandle,
25    target: AnimationTargetId,
26    source: AnimationSource,
27    position: Duration,
28    last_tick: Duration,
29    state: AnimationPlaybackState,
30    last_snapshot: Option<PropertySnapshot>,
31    completed_at: Option<Duration>,
32}
33
34impl ActiveAnimation {
35    #[must_use]
36    pub(crate) fn new(
37        handle: AnimationHandle,
38        target: AnimationTargetId,
39        source: impl Into<AnimationSource>,
40        now: impl Into<Duration>,
41    ) -> Self {
42        Self {
43            handle,
44            target,
45            source: source.into(),
46            position: Duration::ZERO,
47            last_tick: now.into(),
48            state: AnimationPlaybackState::Playing,
49            last_snapshot: None,
50            completed_at: None,
51        }
52    }
53
54    #[must_use]
55    pub(crate) const fn handle(&self) -> AnimationHandle {
56        self.handle
57    }
58
59    #[must_use]
60    pub(crate) const fn source(&self) -> &AnimationSource {
61        &self.source
62    }
63
64    #[must_use]
65    pub(crate) const fn target(&self) -> AnimationTargetId {
66        self.target
67    }
68
69    #[must_use]
70    pub(crate) const fn last_tick(&self) -> Duration {
71        self.last_tick
72    }
73
74    #[must_use]
75    pub(crate) const fn position(&self) -> Duration {
76        self.position
77    }
78
79    #[must_use]
80    pub(crate) const fn state(&self) -> AnimationPlaybackState {
81        self.state
82    }
83
84    pub(crate) const fn set_state(&mut self, state: AnimationPlaybackState) {
85        self.state = state;
86    }
87
88    #[must_use]
89    pub(crate) const fn last_snapshot(&self) -> Option<&PropertySnapshot> {
90        self.last_snapshot.as_ref()
91    }
92
93    pub(crate) fn set_last_snapshot(&mut self, snapshot: Option<PropertySnapshot>) {
94        self.last_snapshot = snapshot;
95    }
96
97    pub(crate) fn clear_last_snapshot(&mut self) {
98        self.last_snapshot = None;
99    }
100
101    pub(crate) fn replace_last_snapshot(&mut self, snapshot: &PropertySnapshot) {
102        if let Some(existing) = &mut self.last_snapshot {
103            existing.replace_from(snapshot);
104        } else {
105            self.last_snapshot = Some(snapshot.clone());
106        }
107    }
108
109    #[must_use]
110    pub(crate) const fn is_active(&self) -> bool {
111        matches!(
112            self.state,
113            AnimationPlaybackState::Playing | AnimationPlaybackState::Paused
114        )
115    }
116
117    #[must_use]
118    pub(crate) const fn needs_tick(&self) -> bool {
119        matches!(self.state, AnimationPlaybackState::Playing)
120    }
121
122    #[must_use]
123    pub(crate) const fn completed_at(&self) -> Option<Duration> {
124        self.completed_at
125    }
126
127    pub(crate) const fn mark_completed(&mut self, completed_at: Duration) {
128        self.state = AnimationPlaybackState::Completed;
129        self.completed_at = Some(completed_at);
130    }
131
132    pub(super) fn update_position(&mut self, delta: Duration) {
133        self.position += delta;
134    }
135
136    pub(super) fn set_last_tick(&mut self, last_tick: Duration) {
137        self.last_tick = last_tick;
138    }
139
140    pub(super) fn set_position(&mut self, position: Duration) {
141        self.position = position;
142    }
143}