rustyle_css/animation/
state.rs1#[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#[derive(Clone, Debug)]
20pub struct AnimationStateMachine {
21 pub current: AnimationState,
22 pub transitions: Vec<(AnimationState, AnimationState, String)>,
23}
24
25impl AnimationStateMachine {
26 pub fn new(initial: AnimationState) -> Self {
28 Self {
29 current: initial,
30 transitions: Vec::new(),
31 }
32 }
33
34 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 pub fn transition_to(&mut self, new_state: AnimationState) {
42 self.current = new_state;
43 }
44
45 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}