use masonry::properties::types::AsUnit;
use winit::error::EventLoopError;
use xilem::core::one_of::{OneOf, OneOf3};
use xilem::style::Style as _;
use xilem::view::{flex_col, label, prose, sized_box, spinner, text_button};
use xilem::{EventLoop, WidgetView, WindowOptions, Xilem};
struct StateMachine {
state: IsEven,
history: String,
}
#[derive(Copy, Clone, Debug)]
enum IsEven {
Initial,
Odd,
Even,
Halt,
Success,
}
fn state_machine(app_data: &mut StateMachine) -> impl WidgetView<StateMachine> + use<> {
match app_data.state {
IsEven::Initial | IsEven::Even => OneOf3::A(flex_col((
sequence_button("1", IsEven::Odd),
sequence_button("_", IsEven::Success),
))),
IsEven::Odd => OneOf::B(flex_col((
sequence_button("1", IsEven::Even),
sequence_button("_", IsEven::Halt),
))),
IsEven::Halt => OneOf::C(label("Failure! Tally total was odd.")),
IsEven::Success => OneOf::C(label("Success! Tally total was even.")),
}
}
fn sequence_button(value: &'static str, target_state: IsEven) -> impl WidgetView<StateMachine> {
text_button(value, move |app_data: &mut StateMachine| {
app_data.state = target_state;
app_data.history.push_str(value);
})
}
fn app_logic(app_data: &mut StateMachine) -> impl WidgetView<StateMachine> + use<> {
flex_col((
text_button("Reset", |app_data: &mut StateMachine| {
app_data.history.clear();
app_data.state = IsEven::Initial;
}),
prose(&*app_data.history),
label(format!("Current state: {:?}", app_data.state)),
sized_box(spinner()).height(40.px()).width(40.px()),
state_machine(app_data),
))
.padding(15.0)
}
fn main() -> Result<(), EventLoopError> {
let app_data = StateMachine {
state: IsEven::Initial,
history: String::new(),
};
let app = Xilem::new_simple(app_data, app_logic, WindowOptions::new("Centered Flex"));
app.run_in(EventLoop::with_user_event())?;
Ok(())
}