mirror of
https://github.com/danbulant/cushy
synced 2026-05-21 21:28:42 +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.
60 lines
1.8 KiB
Rust
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()
|
|
}
|