1use std::time::Duration;
2
3use gpui::{Animation, AnimationElement, AnimationExt, Transformation, percentage};
4
5use crate::{prelude::*, traits::transformable::Transformable};
6
7/// An extension trait for adding common animations to animatable components.
8pub trait CommonAnimationExt: AnimationExt {
9 /// Render this component as rotating over the given duration.
10 ///
11 /// NOTE: This method uses the location of the caller to generate an ID for this state.
12 /// If this is not sufficient to identify your state (e.g. you're rendering a list item),
13 /// you can provide a custom ElementID using the `use_keyed_rotate_animation` method.
14 #[track_caller]
15 fn with_rotate_animation(self, duration: u64) -> AnimationElement<Self>
16 where
17 Self: Transformable + Sized,
18 {
19 self.with_keyed_rotate_animation(
20 ElementId::CodeLocation(*std::panic::Location::caller()),
21 duration,
22 )
23 }
24
25 /// Render this component as rotating with the given element ID over the given duration.
26 fn with_keyed_rotate_animation(
27 self,
28 id: impl Into<ElementId>,
29 duration: u64,
30 ) -> AnimationElement<Self>
31 where
32 Self: Transformable + Sized,
33 {
34 self.with_animation(
35 id,
36 Animation::new(Duration::from_secs(duration)).repeat(),
37 |component, delta| component.transform(Transformation::rotate(percentage(delta))),
38 )
39 }
40}
41
42impl<T: AnimationExt> CommonAnimationExt for T {}