cushy/examples/animation.rs
2023-11-02 07:48:30 -07:00

35 lines
1,009 B
Rust

use std::time::Duration;
use gooey::animation::{AnimationHandle, AnimationTarget, Spawn};
use gooey::value::Dynamic;
use gooey::widgets::{Button, Label, Stack};
use gooey::{widgets, 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());
Stack::columns(widgets![
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 |_| {
animation.set(
value
.transition_to(target)
.over(Duration::from_secs(1))
.spawn(),
)
}
})
}