dioxus_motion/
motion.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
pub use instant::Duration;

use crate::{
    animation::{AnimationMode, Tween},
    spring::Spring,
};

/// Configuration for a motion animation
#[derive(Debug, Clone, Copy)]
pub struct Motion {
    pub initial: f32,
    pub target: f32,
    pub duration: Duration,
    pub delay: Duration,
    pub mode: AnimationMode,
    pub on_complete: Option<fn()>,
}

impl Motion {
    /// Create a new Motion with default parameters
    pub fn new(initial: f32) -> Self {
        Self {
            initial,
            target: initial,
            duration: Duration::from_millis(300),
            delay: Duration::from_millis(0),
            mode: AnimationMode::default(),
            on_complete: None,
        }
    }

    /// Set the target value for the animation
    pub fn to(mut self, target: f32) -> Self {
        self.target = target;
        self
    }

    /// Alias for `to` method
    pub fn animate(self, target: f32) -> Self {
        self.to(target)
    }

    /// Set the duration of the animation
    fn validate_duration(duration: Duration) -> Duration {
        let millis = duration.as_millis();
        if millis == 0 || millis > u64::MAX as u128 {
            Duration::from_millis(300) // Default fallback
        } else {
            duration
        }
    }

    pub fn duration(mut self, duration: Duration) -> Self {
        let safe_duration = Self::validate_duration(duration);
        self.duration = safe_duration;
        self.mode = AnimationMode::Tween(Tween::new(safe_duration));
        self
    }

    pub fn spring(mut self, config: Spring) -> Self {
        self.mode = AnimationMode::Spring(config);
        self
    }

    pub fn mode(mut self, mode: AnimationMode) -> Self {
        self.mode = mode;
        self
    }

    // Helper method to update spring physics
    pub fn update_spring(
        current: f32,
        target: f32,
        velocity: &mut f32,
        spring: &Spring,
        dt: f32,
    ) -> f32 {
        let force = spring.stiffness * (target - current);
        let damping = spring.damping * *velocity;
        let acceleration = (force - damping) / spring.mass;

        *velocity += acceleration * dt;
        current + *velocity * dt
    }

    /// Set a custom easing function
    pub fn easing(mut self, easing: fn(f32, f32, f32, f32) -> f32) -> Self {
        self.mode = match self.mode {
            AnimationMode::Tween(tween) => AnimationMode::Tween(tween.with_easing(easing)),
            _ => self.mode,
        };
        self
    }

    /// Set a callback function to be called when animation completes
    pub fn on_complete(mut self, f: fn()) -> Self {
        self.on_complete = Some(f);
        self
    }

    /// Set a delay before the animation starts
    pub fn delay(mut self, delay: Duration) -> Self {
        self.delay = delay;
        self
    }
}