cushy/examples/debug-window.rs
Jonathan Johnson 9e4e079bf5
WindowLocal + Custom Observers
This cascaded into a lot more work than expected. However, in general,
if one clones a `WidgetInstance` and shares it between two windows, it
should now work. Widget authors must ensure that when they cache
information, they do so with either a `WidgetCacheKey` or use a
`WindowLocal<T>` if per-window state is desired.

This is demonstrated in the debug-window example, where the counter of
open windows is next to a clone of the same button from the main window
that opens a new window.
2023-12-29 13:21:39 -08:00

69 lines
2 KiB
Rust

use cushy::debug::DebugContext;
use cushy::value::Dynamic;
use cushy::widget::MakeWidget;
use cushy::widgets::slider::Slidable;
use cushy::{Application, Open, PendingApp};
const INTRO: &str = "This example demonstrates the DebugContext, which allows observing values easily throughout GUI";
fn main() -> cushy::Result {
let app = PendingApp::default();
let info = DebugContext::default();
let window_count = Dynamic::new(0_usize);
let total_windows = info.dbg("Total Windows", Dynamic::new(0_usize));
let open_window_button = "Open a Window"
.into_button()
.on_click({
let app = app.as_app();
let info = info.clone();
let window_count = window_count.clone();
let total_windows = total_windows.clone();
move |()| open_a_window(&window_count, &total_windows, &info, &app)
})
.make_widget();
info.observe("Open Windows", &window_count, |window_count| {
window_count
.map_each(ToString::to_string)
.and(open_window_button.clone())
.into_columns()
});
info.clone().open(&app)?;
INTRO
.and(open_window_button)
.into_rows()
.centered()
.run_in(app)
}
fn open_a_window(
window_count: &Dynamic<usize>,
total_windows: &Dynamic<usize>,
info: &DebugContext,
app: &dyn Application,
) {
*window_count.lock() += 1;
let window_number = total_windows.map_mut(|total| {
*total += 1;
*total
});
let window_title = format!("Window #{window_number}");
let dbg = info.section(&window_title);
let value = dbg.dbg("Slider", Dynamic::new(0_u8));
let window_count = window_count.clone();
let _ = format!("This is window {window_number}.")
.and(value.slider())
.into_rows()
.centered()
.into_window()
.titled(window_title)
.on_close(move || {
*window_count.lock() -= 1;
})
.open(app);
}