mod composition;
mod curves;
mod decay;
mod easing;
mod effects;
mod hermite;
mod interpolate;
mod path;
mod spring;
pub(crate) mod state;
pub mod testing;
mod timeline;
pub use composition::{Delay, Parallel, Sequence, Stagger};
pub use decay::{Decay, DecayOptions};
pub use easing::Easing;
pub use effects::{Pulse, Rotation};
pub use interpolate::{Interpolate, Oklab};
pub use path::{Path, PathFollow, PathPose};
pub use spring::{Spring, SpringOptions, SpringState, SpringValue};
pub use timeline::{Keyframe, Keyframes, Repeat, Tween, TweenOptions};
use std::time::Duration;
pub trait Animation<T>: 'static {
fn sample(&self, elapsed: Duration) -> AnimationSample<T>;
fn finish(&self) -> T;
fn duration(&self) -> Option<Duration> {
None
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Wake {
NextFrame,
After(Duration),
}
#[derive(Clone, Debug, PartialEq)]
pub struct AnimationSample<T> {
pub value: T,
pub completed: bool,
pub wake: Wake,
}
impl<T> AnimationSample<T> {
pub fn running(value: T) -> Self {
Self {
value,
completed: false,
wake: Wake::NextFrame,
}
}
pub fn completed(value: T) -> Self {
Self {
value,
completed: true,
wake: Wake::NextFrame,
}
}
pub fn after(value: T, delay: Duration) -> Self {
Self {
value,
completed: false,
wake: Wake::After(delay),
}
}
}
pub struct Procedural<T, F> {
final_value: T,
sample: F,
}
impl<T, F> Procedural<T, F> {
pub fn new(final_value: T, sample: F) -> Self {
Self {
final_value,
sample,
}
}
}
impl<T: Clone + 'static, F: Fn(Duration) -> AnimationSample<T> + 'static> Animation<T>
for Procedural<T, F>
{
fn sample(&self, elapsed: Duration) -> AnimationSample<T> {
(self.sample)(elapsed)
}
fn finish(&self) -> T {
self.final_value.clone()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AnimationStatus {
Running,
Paused,
Completed,
Cancelled,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Animated<T> {
pub value: T,
pub status: AnimationStatus,
pub just_completed: bool,
}
impl<T> Animated<T> {
pub fn running(&self) -> bool {
self.status == AnimationStatus::Running
}
pub fn completed(&self) -> bool {
self.status == AnimationStatus::Completed
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct AnimationOptions {
pub decorative: bool,
}
impl Default for AnimationOptions {
fn default() -> Self {
Self { decorative: true }
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct MotionStyle {
pub reduced_motion: bool,
pub hover: TweenOptions,
pub expand: TweenOptions,
pub page: TweenOptions,
pub frame_interval: Duration,
pub spring: SpringOptions,
pub presence: TweenOptions,
pub reorder: TweenOptions,
pub highlight: TweenOptions,
pub slide_distance: f32,
pub loader_size: f32,
pub loader_stroke: f32,
pub cycle_period: Duration,
pub indicator_thickness: f32,
pub time_scale: f32,
}
impl Default for MotionStyle {
fn default() -> Self {
Self {
reduced_motion: false,
hover: TweenOptions::new(Duration::from_millis(160)).easing(Easing::QuadOut),
expand: TweenOptions::new(Duration::from_millis(160)).easing(Easing::QuadOut),
page: TweenOptions::new(Duration::from_millis(280)).easing(Easing::QuintOut),
frame_interval: Duration::from_millis(16),
spring: SpringOptions::default(),
presence: TweenOptions::new(Duration::from_millis(240)).easing(Easing::CubicOut),
reorder: TweenOptions::new(Duration::from_millis(200)).easing(Easing::CubicOut),
highlight: TweenOptions::new(Duration::from_millis(500)).easing(Easing::SineOut),
slide_distance: 8.0,
loader_size: 24.0,
loader_stroke: 2.0,
cycle_period: Duration::from_millis(1200),
indicator_thickness: 2.0,
time_scale: 1.0,
}
}
}