cushy/examples/animation.rs
Jonathan Johnson 0f6d3838b1
LayoutContext
measure() now is layout(). LayoutContext can either persist layout
information or be used temporarily for measurement. While this caching
is constantly thrown out currently, this is a step towards being able to
only re-layout widgets if they've been invalidated.
2023-11-05 11:50:59 -08:00

49 lines
1.7 KiB
Rust

use std::time::Duration;
use gooey::animation::{AnimationHandle, AnimationTarget, IntoAnimate, Spawn};
use gooey::value::Dynamic;
use gooey::widgets::{Button, Label, Stack};
use gooey::{children, Run, WithClone};
fn main() -> gooey::Result {
let animation = Dynamic::new(AnimationHandle::new());
let value = Dynamic::new(50);
let label = value.map_each(|value| value.to_string());
// Gooey's animation system supports using a `Duration` as a step in
// animation to create a delay. This can also be used to call a function
// after a specified amount of time:
Duration::from_secs(1)
.on_complete(|| println!("Gooey animations are neat!"))
.launch();
Stack::columns(children![
Button::new("To 0").on_click(animate_to(&animation, &value, 0)),
Label::new(label),
Button::new("To 100").on_click(animate_to(&animation, &value, 100)),
])
.run()
}
fn animate_to(
animation: &Dynamic<AnimationHandle>,
value: &Dynamic<u8>,
target: u8,
) -> impl FnMut(()) {
(animation, value).with_clone(|(animation, value)| {
move |_| {
// Here we use spawn to schedule the animation, which returns an
// `AnimationHandle`. When dropped, the animation associated with
// the `AnimationHandle` will be cancelled. The effect is that this
// line of code will ensure we only keep one animation running at
// all times in this example, despite how many times the buttons are
// pressed.
animation.set(
value
.transition_to(target)
.over(Duration::from_secs(1))
.spawn(),
)
}
})
}