cushy/examples/buttonception.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

60 lines
1.8 KiB
Rust

//! This example shows off how buttons are able to use any widget, including
//! buttons, as their "label". The widget hierarchy constructed for this example
//! is:
//!
//! ```text
//! ┏ Align
//! ┃ ┏ Stack
//! ┃ ┃ ┏ Button
//! ┃ ┃ ┃ ┏ Stack
//! ┃ ┃ ┃ ┃ ┏ "Yo dawg!"
//! ┃ ┃ ┃ ┃ ┣ Button
//! ┃ ┃ ┃ ┃ ┃ ┏ Stack
//! ┃ ┃ ┃ ┃ ┃ ┃ ┏ "I heard you like buttons"
//! ┃ ┃ ┃ ┃ ┃ ┃ ┣ Button
//! ┃ ┃ ┃ ┃ ┃ ┃ ┃ ╺ "So I put buttons in your buttons"
//! ┃ ┃ ┣ clicked_button Label
//! ```
use cushy::value::{Destination, Dynamic};
use cushy::widget::MakeWidget;
use cushy::widgets::button::{ButtonHoverBackground, ButtonHoverForeground};
use cushy::Run;
use kludgine::Color;
fn main() -> cushy::Result {
let clicked_button = Dynamic::<&'static str>::default();
let inner_button = "So I put buttons in your buttons"
.into_button()
.on_click({
let clicked_button = clicked_button.clone();
move |()| clicked_button.set("inner button clicked")
})
.with(&ButtonHoverBackground, Color::RED)
.with(&ButtonHoverForeground, Color::WHITE);
let middle_button = "I heard you like buttons"
.and(inner_button)
.into_rows()
.into_button()
.on_click({
let clicked_button = clicked_button.clone();
move |()| clicked_button.set("middle button clicked")
});
let outer_button = "Yo dawg!"
.and(middle_button)
.into_rows()
.into_button()
.on_click({
let clicked_button = clicked_button.clone();
move |()| clicked_button.set("outer button clicked")
});
outer_button
.and(clicked_button)
.into_rows()
.centered()
.run()
}