use std::{cell::Cell, rc::Rc, time::Duration};
use gpui::{Animation, AnimationElement, AnimationExt, ElementId, IntoElement, Styled};
mod easing;
mod keyframes;
pub mod presets;
mod transition;
pub use easing::{Easing, Interpolate};
pub use keyframes::{Frame, Keyframes};
pub use transition::{use_presence, use_transition, Presence, Transition};
thread_local! {
static REDUCED_MOTION: Cell<bool> = const { Cell::new(false) };
}
pub fn set_reduced_motion(reduced: bool) {
REDUCED_MOTION.with(|flag| flag.set(reduced));
}
pub fn reduced_motion() -> bool {
REDUCED_MOTION.with(Cell::get)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Iterations {
Count(u32),
Infinite,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum MotionDirection {
#[default]
Normal,
Reverse,
Alternate,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Motion {
keyframes: Rc<Keyframes>,
duration: Duration,
delay: Duration,
easing: Easing,
iterations: Iterations,
direction: MotionDirection,
}
impl Motion {
#[must_use]
pub fn new(keyframes: &Keyframes) -> Self {
Self {
keyframes: Rc::new(keyframes.clone()),
duration: Duration::from_millis(200),
delay: Duration::ZERO,
easing: Easing::EaseOut,
iterations: Iterations::Count(1),
direction: MotionDirection::Normal,
}
}
#[must_use]
pub fn duration(mut self, duration: Duration) -> Self {
self.duration = duration;
self
}
#[must_use]
pub fn duration_ms(self, milliseconds: u64) -> Self {
self.duration(Duration::from_millis(milliseconds))
}
#[must_use]
pub fn delay(mut self, delay: Duration) -> Self {
self.delay = delay;
self
}
#[must_use]
pub fn delay_ms(self, milliseconds: u64) -> Self {
self.delay(Duration::from_millis(milliseconds))
}
#[must_use]
pub fn easing(mut self, easing: Easing) -> Self {
self.easing = easing;
self
}
#[must_use]
pub fn iterations(mut self, count: u32) -> Self {
self.iterations = Iterations::Count(count.max(1));
self
}
#[must_use]
pub fn infinite(mut self) -> Self {
self.iterations = Iterations::Infinite;
self
}
#[must_use]
pub fn direction(mut self, direction: MotionDirection) -> Self {
self.direction = direction;
self
}
#[must_use]
pub fn alternate(self) -> Self {
self.direction(MotionDirection::Alternate)
}
#[must_use]
pub fn reverse(self) -> Self {
self.direction(MotionDirection::Reverse)
}
#[must_use]
pub fn progress_at(&self, elapsed: f32) -> f32 {
let iteration_length = self.delay.as_secs_f32() + self.duration.as_secs_f32();
let iteration_length = iteration_length.max(f32::EPSILON);
let iterations = match self.iterations {
Iterations::Count(count) => count as f32,
Iterations::Infinite => 1.,
};
let total = iteration_length * iterations;
let time = elapsed.clamp(0., 1.) * total;
let iteration = ((time / iteration_length).floor() as u32).min(iterations as u32 - 1);
let within = time - iteration as f32 * iteration_length - self.delay.as_secs_f32();
let t = (within / self.duration.as_secs_f32().max(f32::EPSILON)).clamp(0., 1.);
let t = match self.direction {
MotionDirection::Normal => t,
MotionDirection::Reverse => 1. - t,
MotionDirection::Alternate if iteration % 2 == 1 => 1. - t,
MotionDirection::Alternate => t,
};
self.easing.apply(t)
}
fn animation(&self) -> Animation {
let iteration_length = self.delay + self.duration;
let total = match self.iterations {
Iterations::Count(count) => iteration_length * count,
Iterations::Infinite => match self.direction {
MotionDirection::Alternate => iteration_length * 2,
_ => iteration_length,
},
};
let animation = Animation::new(total.max(Duration::from_millis(1)));
match self.iterations {
Iterations::Infinite => animation.repeat(),
Iterations::Count(_) => animation,
}
}
}
pub trait MotionExt: IntoElement + Styled + 'static {
fn motion(self, id: impl Into<ElementId>, motion: Motion) -> AnimationElement<Self> {
let reduced = reduced_motion();
let mut motion = motion;
if matches!(motion.iterations, Iterations::Infinite)
&& motion.direction == MotionDirection::Alternate
{
motion.iterations = Iterations::Count(2);
let animation = motion.animation().repeat();
return self.with_animation(id, animation, move |element, elapsed| {
let progress = if reduced {
1.
} else {
motion.progress_at(elapsed)
};
motion.keyframes.apply(element, progress)
});
}
let animation = motion.animation();
self.with_animation(id, animation, move |element, elapsed| {
let progress = if reduced {
1.
} else {
motion.progress_at(elapsed)
};
motion.keyframes.apply(element, progress)
})
}
}
impl<E: IntoElement + Styled + 'static> MotionExt for E {}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MotionSide {
Top,
Bottom,
Left,
Right,
}
#[cfg(test)]
#[allow(clippy::float_cmp)] mod tests {
use super::*;
#[test]
fn motion_timing_handles_delay_iterations_and_alternation() {
let keyframes = Keyframes::new();
let motion = Motion::new(&keyframes)
.duration_ms(100)
.delay_ms(100)
.easing(Easing::Linear);
assert_eq!(motion.progress_at(0.25), 0.);
assert!((motion.progress_at(0.75) - 0.5).abs() < 1e-4);
let alternating = Motion::new(&keyframes)
.duration_ms(100)
.easing(Easing::Linear)
.iterations(2)
.alternate();
assert!((alternating.progress_at(0.25) - 0.5).abs() < 1e-4);
assert!((alternating.progress_at(0.75) - 0.5).abs() < 1e-4);
assert!(alternating.progress_at(0.95) < 0.2);
}
}