From 3c12363ec4af1d4d9e0e7314bec07bc8165c1fea Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Sun, 22 Sep 2024 10:41:30 -0700 Subject: [PATCH] Animate for callbacks on_complete fires once. Callbacks can be used in a cycling animation. --- CHANGELOG.md | 5 +++++ src/animation.rs | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1817daa..6e71ddb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -180,6 +180,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `PendingApp::with_tracing` and `PendingApp::initialize_tracing` install Cushy's tracing Subscriber. The default `PendingApp` has tracing initialized, but `PendingApp::new` does not. +- `Animate` and `IntoAnimate` are now implemented for: + + - `impl FnMut(Duration) -> ControlFlow + Send + Sync + 'static` + - `SharedCallback>` + - `SharedCallback` [139]: https://github.com/khonsulabs/cushy/issues/139 diff --git a/src/animation.rs b/src/animation.rs index e9d7510..23c6206 100644 --- a/src/animation.rs +++ b/src/animation.rs @@ -821,6 +821,31 @@ impl Animate for SharedCallback> { } } +impl IntoAnimate for F +where + F: FnMut(Duration) -> ControlFlow + Send + Sync + 'static, +{ + type Animate = Self; + + fn into_animate(self) -> Self::Animate { + self + } +} + +impl Animate for F +where + F: FnMut(Duration) -> ControlFlow + Send + Sync + 'static, +{ + /// 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(elapsed) + } +} + /// Performs a linear interpolation between two values. /// /// This trait can be derived for structs and fieldless enums.