mirror of
https://github.com/danbulant/cushy
synced 2026-06-15 04:21:06 +00:00
Refs #98 This refactor overhauls the reactive system to move all the reactive methods to traits. The side effect of this change is that now DynamicReader's API is the same as Dynamic's API, but because it only implements Source<T>, DynamicReader does not offer any mutation functions. While it's unfortunate to have more traits to include to use Cushy, this seems like the best option, and it offers a path to try to integrate this into the tuple ForEach/MapEach traits. Unfortunately, my attempt at doing those in this set of changes led to issues specifying generic associated lifetimes for the DynamicGuard. But, I was also in the middle of this larger refactoring, so it might be that a fresh attempt will succeed.
56 lines
1.7 KiB
Rust
56 lines
1.7 KiB
Rust
//! This example shows how dynamic values make it easy to communicate state back
|
|
//! to widgets from multiple threads.
|
|
|
|
use std::time::Duration;
|
|
|
|
use cushy::animation::ZeroToOne;
|
|
use cushy::value::{Destination, Dynamic, Switchable};
|
|
use cushy::widget::MakeWidget;
|
|
use cushy::widgets::progress::{Progress, Progressable};
|
|
use cushy::Run;
|
|
|
|
#[derive(Debug, Default, Eq, PartialEq)]
|
|
struct Task {
|
|
progress: Dynamic<Progress>,
|
|
}
|
|
|
|
fn main() -> cushy::Result {
|
|
let task = Dynamic::new(None::<Task>);
|
|
|
|
task.switcher(|task, dynamic| {
|
|
if let Some(task) = task {
|
|
// A background thread is running, show a progress bar.
|
|
task.progress.clone().progress_bar().make_widget()
|
|
} else {
|
|
// There is no background task. Show a button that will start one.
|
|
"Start"
|
|
.into_button()
|
|
.on_click({
|
|
let task = dynamic.clone();
|
|
move |()| {
|
|
let background_task = Task::default();
|
|
spawn_background_thread(&background_task.progress, &task);
|
|
task.set(Some(background_task));
|
|
}
|
|
})
|
|
.make_widget()
|
|
}
|
|
})
|
|
.contain()
|
|
.centered()
|
|
.run()
|
|
}
|
|
|
|
fn spawn_background_thread(progress: &Dynamic<Progress>, task: &Dynamic<Option<Task>>) {
|
|
let progress = progress.clone();
|
|
let task = task.clone();
|
|
std::thread::spawn(move || background_task(&progress, &task));
|
|
}
|
|
|
|
fn background_task(progress: &Dynamic<Progress>, task: &Dynamic<Option<Task>>) {
|
|
for i in 0_u8..=10 {
|
|
progress.set(Progress::Percent(ZeroToOne::new(f32::from(i) / 10.)));
|
|
std::thread::sleep(Duration::from_millis(100));
|
|
}
|
|
task.set(None);
|
|
}
|