use super::{Animation, BaseAnimation};
use crate::core::DURATION_ZERO;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct Delay<T: Animation> {
src: T,
delay: Duration,
}
impl<T: Animation> Delay<T> {
#[inline]
pub(super) fn new(src: T, delay: Duration) -> Self {
Self { src, delay }
}
#[inline]
fn delay(&self) -> Duration {
if self.delay > DURATION_ZERO {
self.delay
} else {
DURATION_ZERO
}
}
}
impl<T: Animation> BaseAnimation for Delay<T> {
type Item = T::Item;
#[inline]
fn duration(&self) -> Option<Duration> {
self.src.duration().map(|d| self.delay() + d)
}
#[inline]
fn animate(&self, elapsed: Duration) -> Self::Item {
let delay = self.delay();
let elapsed = if elapsed > delay {
elapsed - delay
} else {
DURATION_ZERO
};
self.src.animate(elapsed)
}
}