use super::Context;
use crate::{
animation::{
state::{Control, Pass},
Animated, Animation, AnimationOptions, AnimationStatus, Interpolate, TweenOptions,
},
Id,
};
use std::time::Instant;
impl Context {
pub fn spring_transition<T: crate::SpringValue>(
&mut self,
id: Id,
target: T,
options: crate::SpringOptions,
) -> Animated<crate::SpringState<T>> {
self.animations
.spring(id, None, target, options, self.animation_pass(true))
}
pub fn spring_transition_from<T: crate::SpringValue>(
&mut self,
id: Id,
initial: crate::SpringState<T>,
target: T,
options: crate::SpringOptions,
) -> Animated<crate::SpringState<T>> {
self.animations.spring(
id,
Some(initial),
target,
options,
self.animation_pass(true),
)
}
pub fn remove_animation(&mut self, id: Id) {
self.animations.remove(id);
}
pub fn frame_time(&self) -> Instant {
self.frame_time
}
pub(crate) fn animation_pass(&self, visible: bool) -> Pass {
Pass {
now: self.frame_time,
frame: self.frame,
visible,
reduced: self.style.motion.reduced_motion,
interval: self.style.motion.frame_interval,
}
}
pub fn transition<T: Interpolate>(
&mut self,
id: Id,
target: T,
options: TweenOptions,
) -> Animated<T> {
self.transition_visible(id, None, target, options, true)
}
pub fn transition_from<T: Interpolate>(
&mut self,
id: Id,
initial: T,
target: T,
options: TweenOptions,
) -> Animated<T> {
self.transition_visible(id, Some(initial), target, options, true)
}
pub(crate) fn transition_visible<T: Interpolate>(
&mut self,
id: Id,
initial: Option<T>,
target: T,
options: TweenOptions,
visible: bool,
) -> Animated<T> {
self.animations
.transition(id, initial, target, options, self.animation_pass(visible))
}
pub fn animate<T: Clone + 'static, A: Animation<T>>(
&mut self,
id: Id,
create: impl FnOnce() -> A,
) -> Animated<T> {
self.animate_with(id, AnimationOptions::default(), create)
}
pub fn animate_with<T: Clone + 'static, A: Animation<T>>(
&mut self,
id: Id,
options: AnimationOptions,
create: impl FnOnce() -> A,
) -> Animated<T> {
self.animations
.animate(id, options, create, self.animation_pass(true))
}
pub fn restart_animation<T: Clone + 'static>(&mut self, id: Id, animation: impl Animation<T>) {
self.restart_animation_with(id, animation, AnimationOptions::default());
}
pub fn restart_animation_with<T: Clone + 'static>(
&mut self,
id: Id,
animation: impl Animation<T>,
options: AnimationOptions,
) {
self.animations
.restart(id, animation, options, self.animation_pass(true));
}
pub fn sample_animation<T: Clone + 'static>(&mut self, id: Id) -> Option<Animated<T>> {
self.animations.read(id, self.animation_pass(true))
}
pub fn animation_status(&self, id: Id) -> Option<AnimationStatus> {
self.animations.status(id)
}
pub fn pause_animation(&mut self, id: Id) -> bool {
self.animations.control(id, Control::Pause, self.frame_time)
}
pub fn resume_animation(&mut self, id: Id) -> bool {
self.animations
.control(id, Control::Resume, self.frame_time)
}
pub fn cancel_animation(&mut self, id: Id) -> bool {
self.animations
.control(id, Control::Cancel, self.frame_time)
}
pub fn finish_animation(&mut self, id: Id) -> bool {
self.animations
.control(id, Control::Finish, self.frame_time)
}
}