diff --git a/examples/hello.rs b/examples/hello.rs index f1c78fe88..b66f5dfc1 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -1,34 +1,43 @@ -use xilem::view::{button, h_stack, v_stack}; +use xilem::view::{button, h_stack, v_stack, Adapt, switch}; use xilem::{view::View, App, AppLauncher}; -fn app_logic(data: &mut i32) -> impl View { +fn app_logic(data: &mut AppData) -> impl View { // here's some logic, deriving state for the view from our state - let label = if *data == 1 { + let count = data.count; + let label = if count == 1 { "clicked 1 time".to_string() } else { - format!("clicked {data} times") + format!("clicked {count} times") }; // The actual UI Code starts here v_stack(( - button(label, |data| { - println!("clicked"); - *data += 1; - }), + button(label, |data: &mut AppData| { + println!("clicked"); + data.count += 1; + }), h_stack(( - button("decrease", |data| { + button("decrease", |data: &mut AppData| { println!("clicked decrease"); - *data -= 1; + data.count -= 1; }), - button("reset", |data| { + button("reset", |data: &mut AppData| { println!("clicked reset"); - *data = 0; + data.count = 0; }), + switch(data, |data: &mut AppData| { + &mut data.is_on + }) )), )) .with_spacing(20.0) } +struct AppData { + count: u32, + is_on:bool, +} + fn main() { /* let app = Application::new().unwrap(); @@ -40,6 +49,11 @@ fn main() { window_handle.show(); app.run(None); */ - let app = App::new(0, app_logic); + let data = AppData { + count: 0, + is_on: false, + }; + + let app = App::new(data, app_logic); AppLauncher::new(app).run() }