use fltk::{prelude::*, *};
trait OnTrigger<W>
where
W: WidgetExt,
{
fn on_trigger<F: 'static + FnMut(&mut Self)>(self, cb: F) -> Self
where
Self: Sized;
}
impl<W> OnTrigger<W> for W
where
W: WidgetExt,
{
fn on_trigger<F: 'static + FnMut(&mut Self)>(mut self, mut cb: F) -> Self {
self.set_callback(move |s| cb(s));
self
}
}
struct Counter {
count: i32,
}
fn increment_by(step: i32) {
let mut frame: frame::Frame = app::widget_from_id("my_frame").unwrap();
let state = app::GlobalState::<Counter>::get();
let count = state.with(move |c| {
c.count += step;
c.count
});
frame.set_label(&count.to_string());
}
fn increment(_w: &mut impl WidgetExt) {
let mut frame: frame::Frame = app::widget_from_id("my_frame").unwrap();
let state = app::GlobalState::<Counter>::get();
let count = state.with(|c| {
c.count += 1;
c.count
});
frame.set_label(&count.to_string());
}
fn main() {
let counter = Counter { count: 0 };
let _state = app::GlobalState::new(counter);
let app = app::App::default().with_scheme(app::Scheme::Gtk);
let mut wind = window::Window::default()
.with_size(160, 200)
.with_label("Counter");
let col = group::Flex::default_fill().column();
button::Button::default()
.with_label("+")
.on_trigger(increment); frame::Frame::default().with_label("0").with_id("my_frame"); button::Button::default()
.with_label("-")
.on_trigger(|_| increment_by(-1)); col.end();
wind.make_resizable(true);
wind.end();
wind.show();
app.run().unwrap();
}