diff --git a/examples/taffy.rs b/examples/taffy.rs index edfe7faeb..089a54d71 100644 --- a/examples/taffy.rs +++ b/examples/taffy.rs @@ -12,12 +12,23 @@ const COLORS: [Color; 4] = [ Color::HOT_PINK, ]; -fn app_logic(data: &mut i32) -> impl View { +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +struct AppState { + count: u32, +} + +impl AppState { + fn new() -> Self { + Self { count: 1 } + } +} + +fn app_logic(state: &mut AppState) -> impl View { // here's some logic, deriving state for the view from our state - let label = if *data == 1 { + let label = if state.count == 1 { "Square count: 1".to_string() } else { - format!("Square count: {data}") + format!("Square count: {}", state.count) }; // The actual UI Code starts here @@ -30,19 +41,19 @@ fn app_logic(data: &mut i32) -> impl View { flex_row(( label, - button("increase", |data| { + button("increase", |state: &mut AppState| { println!("clicked increase"); - *data += 1; + state.count += 1; }), - button("decrease", |data| { + button("decrease", |state: &mut AppState| { println!("clicked decrease"); - if *data > 0 { - *data -= 1; + if state.count > 0 { + state.count -= 1; } }), - button("reset", |data| { + button("reset", |state: &mut AppState| { println!("clicked reset"); - *data = 1; + state.count = 1; }), )) .with_background_color(Color::BLUE_VIOLET) @@ -61,7 +72,7 @@ fn app_logic(data: &mut i32) -> impl View { .with_background_color(Color::RED) .with_style(|s| s.padding = length(20.0)), - flex_row((0..*data).map(|i| { + flex_row((0..state.count).map(|i| { div(()).with_background_color(COLORS[(i % 4) as usize]).with_style(|s| { s.size.width = length(200.0); s.size.height = length(200.0); @@ -94,6 +105,6 @@ fn app_logic(data: &mut i32) -> impl View { } fn main() { - let app = App::new(1, app_logic); + let app = App::new(AppState::new(), app_logic); AppLauncher::new(app).run() }