Skip to content

Commit

Permalink
Add switch to example
Browse files Browse the repository at this point in the history
  • Loading branch information
giannissc committed Nov 22, 2023
1 parent 2ec05c4 commit 8acc0dd
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions examples/hello.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
use xilem::view::{button, h_stack, v_stack};
use xilem::view::{button, h_stack, switch, v_stack};
use xilem::{view::View, App, AppLauncher};

fn app_logic(data: &mut i32) -> impl View<i32> {
fn app_logic(data: &mut AppData) -> impl View<AppData> {
// 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| {
button(label, |data: &mut AppData| {
println!("clicked");
*data += 1;
data.count += 1;
}),
h_stack((
"Buttons: ",
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.is_on, |data: &mut AppData, value: bool| {
data.is_on = value
}),
)),
))
.with_spacing(20.0)
}

struct AppData {
count: i32,
is_on: bool,
}

fn main() {
/*
let app = Application::new().unwrap();
Expand All @@ -41,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()
}

0 comments on commit 8acc0dd

Please sign in to comment.