cushy/examples/switcher.rs
Jonathan Johnson e70e92726c
Source<T> + Destination<T> (breaking)
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.
2024-01-02 09:00:29 -08:00

47 lines
1.3 KiB
Rust

use cushy::value::{Destination, Dynamic, Switchable};
use cushy::widget::{MakeWidget, WidgetInstance};
use cushy::Run;
#[derive(Debug, Eq, PartialEq)]
enum ActiveContent {
Intro,
Success,
}
fn main() -> cushy::Result {
let active = Dynamic::new(ActiveContent::Intro);
active
.switcher(|current, active| match current {
ActiveContent::Intro => intro(active.clone()),
ActiveContent::Success => success(active.clone()),
})
.contain()
.centered()
.run()
}
fn intro(active: Dynamic<ActiveContent>) -> WidgetInstance {
const INTRO: &str = "This example demonstrates the Switcher<T> widget, which uses a mapping function to convert from a generic type to the widget it uses for its contents.";
INTRO
.and(
"Switch!"
.into_button()
.on_click(move |_| active.set(ActiveContent::Success))
.centered(),
)
.into_rows()
.make_widget()
}
fn success(active: Dynamic<ActiveContent>) -> WidgetInstance {
"The value changed to `ActiveContent::Success`!"
.and(
"Start Over"
.into_button()
.on_click(move |_| active.set(ActiveContent::Intro))
.centered(),
)
.into_rows()
.make_widget()
}