-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16f9660
commit f08684c
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use rui::*; | ||
|
||
// This example shows how to mix the redux style, with state local | ||
// to a view. | ||
|
||
#[derive(Clone)] | ||
enum Action { | ||
Increment, | ||
None, | ||
} | ||
|
||
struct AppState { | ||
count: i32, | ||
} | ||
|
||
impl AppState { | ||
fn new() -> Self { | ||
AppState { count: 1 } | ||
} | ||
} | ||
|
||
fn reduce(state: &mut AppState, action: &Action) { | ||
match action { | ||
Action::Increment => state.count += 1, | ||
Action::None => (), | ||
} | ||
} | ||
|
||
fn main() { | ||
rui(redux(AppState::new, reduce, |app_state| { | ||
vstack(( | ||
format!("{}", app_state.count).padding(Auto), | ||
state(|| 0, |handle, _| { | ||
button("increment every 5 clicks", move|cx| { | ||
cx[handle] += 1; | ||
if cx[handle] == 5 { | ||
cx[handle] = 0; | ||
Action::Increment | ||
} else { | ||
Action::None | ||
} | ||
}) | ||
}), | ||
)) | ||
})); | ||
} |