fc_ui/capabilities/motion/
animate.rs1#![allow(deprecated)]
4
5use gpui::*;
6use std::time::Duration;
7
8use crate::capabilities::motion::animations::easings;
9
10#[derive(Clone)]
11#[deprecated(
12 note = "unused within the crate and examples; superseded by gpui::Animation + motion::animations helpers, will be removed in 0.10"
13)]
14pub struct AnimationPreset {
15 duration: Duration,
16 easing: fn(f32) -> f32,
17 delay: Duration,
18}
19
20impl AnimationPreset {
21 pub fn new(duration: Duration, easing: fn(f32) -> f32) -> Self {
22 Self {
23 duration,
24 easing,
25 delay: Duration::ZERO,
26 }
27 }
28
29 #[deprecated(note = "use with_duration; will be removed in 0.10")]
31 pub fn duration(self, duration: Duration) -> Self {
32 self.with_duration(duration)
33 }
34
35 #[deprecated(note = "use with_easing; will be removed in 0.10")]
37 pub fn easing(self, easing: fn(f32) -> f32) -> Self {
38 self.with_easing(easing)
39 }
40
41 #[deprecated(note = "use with_delay; will be removed in 0.10")]
44 pub fn delay(self, delay: Duration) -> Self {
45 self.with_delay(delay)
46 }
47
48 pub fn with_delay(mut self, delay: Duration) -> Self {
51 self.delay = delay;
52 self
53 }
54
55 pub fn with_duration(mut self, duration: Duration) -> Self {
56 self.duration = duration;
57 self
58 }
59
60 pub fn with_easing(mut self, easing: fn(f32) -> f32) -> Self {
61 self.easing = easing;
62 self
63 }
64
65 pub fn get_duration(&self) -> Duration {
66 self.duration
67 }
68
69 pub fn get_easing(&self) -> fn(f32) -> f32 {
70 self.easing
71 }
72
73 pub fn get_delay(&self) -> Duration {
75 self.delay
76 }
77
78 pub fn to_animation(&self) -> Animation {
79 Animation::new(self.duration)
80 .with_easing(self.easing)
81 .with_delay(self.delay)
82 }
83}
84
85pub fn fade_in() -> AnimationPreset {
86 AnimationPreset::new(Duration::from_millis(200), easings::ease_out_cubic)
87}
88
89pub fn fade_out() -> AnimationPreset {
90 AnimationPreset::new(Duration::from_millis(200), easings::ease_in_cubic)
91}
92
93pub fn slide_up() -> AnimationPreset {
94 AnimationPreset::new(Duration::from_millis(250), easings::ease_out_cubic)
95}
96
97pub fn slide_down() -> AnimationPreset {
98 AnimationPreset::new(Duration::from_millis(250), easings::ease_out_cubic)
99}
100
101pub fn scale_in() -> AnimationPreset {
102 AnimationPreset::new(Duration::from_millis(200), easings::ease_out_back)
103}
104
105pub fn bounce_in() -> AnimationPreset {
106 AnimationPreset::new(Duration::from_millis(400), easings::elastic)
107}
108
109pub fn slide_in_left() -> AnimationPreset {
110 AnimationPreset::new(Duration::from_millis(250), easings::ease_out_cubic)
111}
112
113pub fn slide_in_right() -> AnimationPreset {
114 AnimationPreset::new(Duration::from_millis(250), easings::ease_out_cubic)
115}
116
117#[derive(Clone, Debug, PartialEq)]
118#[deprecated(note = "unused within the crate and examples; will be removed in 0.10")]
119pub enum AnimationRepeat {
120 Once,
121 Count(u32),
123 Infinite,
124}
125
126#[deprecated(note = "unused within the crate and examples; will be removed in 0.10")]
127pub struct KeyframeAnimation {
128 id: SharedString,
129 keyframes: Vec<(f32, f32)>,
130 duration: Duration,
131 repeat: AnimationRepeat,
132 easing: fn(f32) -> f32,
133}
134
135impl KeyframeAnimation {
136 pub fn new(id: impl Into<SharedString>) -> Self {
137 Self {
138 id: id.into(),
139 keyframes: vec![(0.0, 0.0), (1.0, 1.0)],
140 duration: Duration::from_millis(300),
141 repeat: AnimationRepeat::Once,
142 easing: easings::linear,
143 }
144 }
145
146 pub fn at(mut self, pct: f32, value: f32) -> Self {
148 if !pct.is_finite() || !value.is_finite() {
149 return self;
150 }
151
152 let pct = pct.clamp(0.0, 1.0);
153 if let Some(pos) = self
154 .keyframes
155 .iter()
156 .position(|(p, _)| (*p - pct).abs() < f32::EPSILON)
157 {
158 self.keyframes[pos] = (pct, value);
159 } else {
160 self.keyframes.push((pct, value));
161 self.keyframes
162 .sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
163 }
164 self
165 }
166
167 pub fn duration(mut self, duration: Duration) -> Self {
168 self.duration = duration;
169 self
170 }
171
172 pub fn repeat(mut self, repeat: AnimationRepeat) -> Self {
173 self.repeat = repeat;
174 self
175 }
176
177 pub fn easing(mut self, easing: fn(f32) -> f32) -> Self {
178 self.easing = easing;
179 self
180 }
181
182 pub fn id(&self) -> &SharedString {
183 &self.id
184 }
185
186 pub fn get_duration(&self) -> Duration {
187 self.duration
188 }
189
190 pub fn get_repeat(&self) -> &AnimationRepeat {
191 &self.repeat
192 }
193
194 pub fn interpolate(&self, progress: f32) -> f32 {
195 let progress = progress.clamp(0.0, 1.0);
196
197 if self.keyframes.len() < 2 {
198 return if self.keyframes.is_empty() {
199 0.0
200 } else {
201 self.keyframes[0].1
202 };
203 }
204
205 let eased = (self.easing)(progress);
206
207 let mut prev = &self.keyframes[0];
208 for kf in &self.keyframes[1..] {
209 if eased <= kf.0 {
210 let range = kf.0 - prev.0;
211 if range <= f32::EPSILON {
212 return kf.1;
213 }
214 let local_t = (eased - prev.0) / range;
215 return prev.1 + (kf.1 - prev.1) * local_t;
216 }
217 prev = kf;
218 }
219
220 self.keyframes.last().map(|kf| kf.1).unwrap_or(1.0)
221 }
222
223 pub fn to_animation(&self) -> Animation {
224 let anim = Animation::new(self.duration).with_easing(self.easing);
225 match &self.repeat {
226 AnimationRepeat::Once => anim,
227 AnimationRepeat::Infinite => anim.repeat(),
228 AnimationRepeat::Count(n) => anim.repeat_n(*n),
229 }
230 }
231}
232
233#[derive(Clone)]
234#[deprecated(note = "unused within the crate and examples; will be removed in 0.10")]
235pub struct StaggerConfig {
236 delay_per_child: Duration,
237 preset: AnimationPreset,
238}
239
240impl StaggerConfig {
241 pub fn new() -> Self {
242 Self {
243 delay_per_child: Duration::from_millis(50),
244 preset: fade_in(),
245 }
246 }
247
248 pub fn delay_per_child(mut self, delay: Duration) -> Self {
249 self.delay_per_child = delay;
250 self
251 }
252
253 pub fn animation(mut self, preset: AnimationPreset) -> Self {
254 self.preset = preset;
255 self
256 }
257
258 pub fn delay_for_index(&self, index: usize) -> Duration {
259 self.preset.delay + self.delay_per_child * index as u32
260 }
261
262 pub fn preset_for_index(&self, index: usize) -> AnimationPreset {
263 let mut preset = self.preset.clone();
264 preset.delay = self.delay_for_index(index);
265 preset
266 }
267
268 pub fn get_preset(&self) -> &AnimationPreset {
269 &self.preset
270 }
271}
272
273impl Default for StaggerConfig {
274 fn default() -> Self {
275 Self::new()
276 }
277}
278
279#[cfg(test)]
280mod tests {
281 #![allow(deprecated)]
282 use super::{easings, AnimationPreset, KeyframeAnimation};
283 use std::time::Duration;
284
285 #[test]
286 fn deprecated_animation_preset_builders_remain_compatible() {
287 let preset = AnimationPreset::new(Duration::from_millis(100), easings::linear)
288 .duration(Duration::from_millis(200))
289 .easing(easings::ease_in_cubic)
290 .delay(Duration::from_millis(25));
291
292 assert_eq!(preset.get_duration(), Duration::from_millis(200));
293 assert_eq!(preset.get_delay(), Duration::from_millis(25));
294 }
295
296 #[test]
297 fn non_finite_keyframes_are_ignored() {
298 let animation = KeyframeAnimation::new("finite-keyframes")
299 .at(f32::NAN, 0.5)
300 .at(0.5, f32::INFINITY);
301
302 assert_eq!(animation.keyframes, vec![(0.0, 0.0), (1.0, 1.0)]);
303 }
304}
305
306#[derive(Clone)]
307#[deprecated(note = "unused within the crate and examples; will be removed in 0.10")]
308pub struct Transition {
309 property: SharedString,
310 duration: Duration,
311 easing: fn(f32) -> f32,
312 delay: Duration,
313}
314
315impl Transition {
316 pub fn new(property: impl Into<SharedString>) -> Self {
317 Self {
318 property: property.into(),
319 duration: Duration::from_millis(200),
320 easing: easings::ease_out_cubic,
321 delay: Duration::ZERO,
322 }
323 }
324
325 pub fn duration(mut self, duration: Duration) -> Self {
326 self.duration = duration;
327 self
328 }
329
330 pub fn easing(mut self, easing: fn(f32) -> f32) -> Self {
331 self.easing = easing;
332 self
333 }
334
335 pub fn delay(mut self, delay: Duration) -> Self {
338 self.delay = delay;
339 self
340 }
341
342 pub fn get_property(&self) -> &SharedString {
343 &self.property
344 }
345
346 pub fn get_duration(&self) -> Duration {
347 self.duration
348 }
349
350 pub fn get_easing(&self) -> fn(f32) -> f32 {
351 self.easing
352 }
353
354 pub fn get_delay(&self) -> Duration {
356 self.delay
357 }
358
359 pub fn to_animation(&self) -> Animation {
360 Animation::new(self.duration)
361 .with_easing(self.easing)
362 .with_delay(self.delay)
363 }
364}