use crate::style::easing;
use std::time::Duration;
#[derive(Clone)]
pub struct Animation {
preset: AnimationPreset,
duration: Duration,
easing: fn(f32) -> f32,
delay: Duration,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum AnimationPreset {
Fade,
SlideLeft,
SlideRight,
SlideUp,
SlideDown,
Scale,
Custom {
opacity: Option<f32>,
offset_x: Option<i16>,
offset_y: Option<i16>,
scale: Option<f32>,
},
}
impl Animation {
pub fn fade() -> Self {
Self {
preset: AnimationPreset::Fade,
duration: Duration::from_millis(300),
easing: easing::ease_in_out,
delay: Duration::ZERO,
}
}
pub fn fade_in() -> Self {
Self::fade()
}
pub fn fade_out() -> Self {
Self::fade()
}
pub fn slide_left() -> Self {
Self {
preset: AnimationPreset::SlideLeft,
duration: Duration::from_millis(300),
easing: easing::ease_out,
delay: Duration::ZERO,
}
}
pub fn slide_in_left() -> Self {
Self::slide_left()
}
pub fn slide_out_left() -> Self {
Self::slide_left()
}
pub fn slide_right() -> Self {
Self {
preset: AnimationPreset::SlideRight,
duration: Duration::from_millis(300),
easing: easing::ease_out,
delay: Duration::ZERO,
}
}
pub fn slide_in_right() -> Self {
Self::slide_right()
}
pub fn slide_out_right() -> Self {
Self::slide_right()
}
pub fn slide_up() -> Self {
Self {
preset: AnimationPreset::SlideUp,
duration: Duration::from_millis(300),
easing: easing::ease_out,
delay: Duration::ZERO,
}
}
pub fn slide_in_up() -> Self {
Self::slide_up()
}
pub fn slide_out_up() -> Self {
Self::slide_up()
}
pub fn slide_down() -> Self {
Self {
preset: AnimationPreset::SlideDown,
duration: Duration::from_millis(300),
easing: easing::ease_out,
delay: Duration::ZERO,
}
}
pub fn slide_in_down() -> Self {
Self::slide_down()
}
pub fn slide_out_down() -> Self {
Self::slide_down()
}
pub fn scale() -> Self {
Self {
preset: AnimationPreset::Scale,
duration: Duration::from_millis(300),
easing: easing::back_out,
delay: Duration::ZERO,
}
}
pub fn scale_up() -> Self {
Self::scale()
}
pub fn scale_down() -> Self {
Self::scale()
}
pub fn custom(
opacity: Option<f32>,
offset_x: Option<i16>,
offset_y: Option<i16>,
scale: Option<f32>,
) -> Self {
Self {
preset: AnimationPreset::Custom {
opacity,
offset_x,
offset_y,
scale,
},
duration: Duration::from_millis(300),
easing: easing::ease_in_out,
delay: Duration::ZERO,
}
}
pub fn duration(mut self, duration_ms: u64) -> Self {
self.duration = Duration::from_millis(duration_ms);
self
}
pub fn easing(mut self, easing: fn(f32) -> f32) -> Self {
self.easing = easing;
self
}
pub fn delay(mut self, delay_ms: u64) -> Self {
self.delay = Duration::from_millis(delay_ms);
self
}
pub fn preset(&self) -> AnimationPreset {
self.preset
}
pub fn get_duration(&self) -> Duration {
self.duration
}
pub fn get_easing(&self) -> fn(f32) -> f32 {
self.easing
}
pub fn get_delay(&self) -> Duration {
self.delay
}
}
impl Default for Animation {
fn default() -> Self {
Self::fade()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TransitionPhase {
Hidden,
Entering,
Visible,
Leaving,
}