From 3359fe0d05ec3b305c955a761f9fc6cb9cd2d5cd Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Sun, 22 Sep 2024 10:38:46 -0700 Subject: [PATCH] SharedCallback animation --- src/animation.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/animation.rs b/src/animation.rs index 135749f..e9d7510 100644 --- a/src/animation.rs +++ b/src/animation.rs @@ -58,6 +58,7 @@ use crate::animation::easings::Linear; use crate::styles::{Component, RequireInvalidation}; use crate::utils::run_in_bg; use crate::value::{Destination, Dynamic, Source}; +use crate::widget::SharedCallback; use crate::Cushy; static ANIMATIONS: Mutex = Mutex::new(Animating::new()); @@ -778,6 +779,48 @@ impl Animate for Duration { } } +impl IntoAnimate for SharedCallback { + type Animate = Self; + + /// Invokes this callback when the animation progresses. Consumes no + /// animation time. + fn into_animate(self) -> Self::Animate { + self + } +} + +impl Animate for SharedCallback { + /// Invokes the callback and consumes no animation time. + fn animate(&mut self, elapsed: Duration) -> ControlFlow { + self.invoke(()); + ControlFlow::Break(elapsed) + } +} + +impl IntoAnimate for SharedCallback> { + type Animate = Self; + + /// Invokes this callback to implement a custom animation. + /// + /// Returning `ControlFlow::Continue` consumes the entire elapsed duration. + /// Returning `ControlFlow::Break` completes the animation and yields the + /// provided duration to the next animation. + fn into_animate(self) -> Self::Animate { + self + } +} + +impl Animate for SharedCallback> { + /// Invokes this callback to implement a custom animation. + /// + /// Returning `ControlFlow::Continue` consumes the entire elapsed duration. + /// Returning `ControlFlow::Break` completes the animation and yields the + /// provided duration to the next animation. + fn animate(&mut self, elapsed: Duration) -> ControlFlow { + self.invoke(elapsed) + } +} + /// Performs a linear interpolation between two values. /// /// This trait can be derived for structs and fieldless enums.