rustyle_css/animation/
state.rs

1//! Animation state management
2//!
3//! Provides state-driven animations with automatic transition generation.
4
5/// Animation state
6#[derive(Clone, Debug, PartialEq)]
7pub enum AnimationState {
8    Idle,
9    Hover,
10    Active,
11    Focus,
12    Disabled,
13    Loading,
14    Success,
15    Error,
16}
17
18/// Animation state machine
19#[derive(Clone, Debug)]
20pub struct AnimationStateMachine {
21    pub current: AnimationState,
22    pub transitions: Vec<(AnimationState, AnimationState, String)>,
23}
24
25impl AnimationStateMachine {
26    /// Create a new animation state machine
27    pub fn new(initial: AnimationState) -> Self {
28        Self {
29            current: initial,
30            transitions: Vec::new(),
31        }
32    }
33
34    /// Add a transition
35    pub fn transition(mut self, from: AnimationState, to: AnimationState, css: &str) -> Self {
36        self.transitions.push((from, to, css.to_string()));
37        self
38    }
39
40    /// Transition to a new state
41    pub fn transition_to(&mut self, new_state: AnimationState) {
42        self.current = new_state;
43    }
44
45    /// Get CSS for current state transitions
46    pub fn get_transition_css(&self) -> String {
47        let mut css = String::new();
48
49        for (from, _to, transition_css) in &self.transitions {
50            if *from == self.current {
51                css.push_str(transition_css);
52                css.push_str("\n");
53            }
54        }
55
56        css
57    }
58}
59
60impl Default for AnimationStateMachine {
61    fn default() -> Self {
62        Self::new(AnimationState::Idle)
63    }
64}