fc_ui/capabilities/motion/
spring.rs1use std::time::Duration;
21
22#[deprecated(note = "use gpui::SpringAnimation / with_spring instead")]
23#[derive(Clone, Debug)]
24pub struct Spring {
25 pub position: f32,
26 pub velocity: f32,
27 pub target: f32,
28 stiffness: f32,
29 damping: f32,
30 mass: f32,
31 rest_threshold: f32,
32}
33
34#[allow(deprecated)]
35impl Default for Spring {
36 fn default() -> Self {
37 Self::gentle()
38 }
39}
40
41#[allow(deprecated)]
42impl Spring {
43 pub fn new(stiffness: f32, damping: f32, mass: f32) -> Self {
44 Self {
45 position: 0.0,
46 velocity: 0.0,
47 target: 0.0,
48 stiffness: stiffness.max(0.1),
49 damping: damping.max(0.0),
50 mass: mass.max(0.01),
51 rest_threshold: 0.001,
52 }
53 }
54
55 pub fn gentle() -> Self {
56 Self::new(120.0, 14.0, 1.0)
57 }
58
59 pub fn wobbly() -> Self {
60 Self::new(180.0, 12.0, 1.0)
61 }
62
63 pub fn stiff() -> Self {
64 Self::new(210.0, 20.0, 1.0)
65 }
66
67 pub fn slow() -> Self {
68 Self::new(280.0, 60.0, 1.0)
69 }
70
71 pub fn snappy() -> Self {
72 Self::new(400.0, 30.0, 1.0)
73 }
74
75 pub fn with_position(mut self, position: f32) -> Self {
76 self.position = position;
77 self
78 }
79
80 pub fn with_target(mut self, target: f32) -> Self {
81 self.target = target;
82 self
83 }
84
85 pub fn with_velocity(mut self, velocity: f32) -> Self {
86 self.velocity = velocity;
87 self
88 }
89
90 pub fn with_rest_threshold(mut self, threshold: f32) -> Self {
91 self.rest_threshold = threshold.max(0.0001);
92 self
93 }
94
95 pub fn set_target(&mut self, target: f32) {
96 self.target = target;
97 }
98
99 pub fn set_position(&mut self, position: f32) {
100 self.position = position;
101 }
102
103 pub fn impulse(&mut self, velocity: f32) {
104 self.velocity += velocity;
105 }
106
107 pub fn tick(&mut self, dt: f32) -> bool {
108 let dt = dt.min(0.064);
109
110 let displacement = self.position - self.target;
111 let spring_force = -self.stiffness * displacement;
112 let damping_force = -self.damping * self.velocity;
113 let acceleration = (spring_force + damping_force) / self.mass;
114
115 self.velocity += acceleration * dt;
116 self.position += self.velocity * dt;
117
118 let is_moving = self.velocity.abs() > self.rest_threshold
119 || (self.position - self.target).abs() > self.rest_threshold;
120
121 if !is_moving {
122 self.position = self.target;
123 self.velocity = 0.0;
124 }
125
126 is_moving
127 }
128
129 pub fn tick_duration(&mut self, duration: Duration) -> bool {
130 self.tick(duration.as_secs_f32())
131 }
132
133 pub fn is_at_rest(&self) -> bool {
134 self.velocity.abs() <= self.rest_threshold
135 && (self.position - self.target).abs() <= self.rest_threshold
136 }
137
138 pub fn progress(&self) -> f32 {
139 if (self.target - self.position).abs() < self.rest_threshold {
140 return 1.0;
141 }
142 let start = 0.0_f32;
143 let total = self.target - start;
144 if total.abs() < f32::EPSILON {
145 return 1.0;
146 }
147 ((self.position - start) / total).clamp(0.0, 1.5)
148 }
149
150 pub fn reset(&mut self) {
151 self.position = 0.0;
152 self.velocity = 0.0;
153 }
154
155 pub fn snap_to_target(&mut self) {
156 self.position = self.target;
157 self.velocity = 0.0;
158 }
159}