pub fn with_state<State: 'static, F: FnOnce(&State) + Clone>(f: F)
Expand description
Provides read-only access to the global state for the given closure.
Examples found in repository?
examples/handle.rs (lines 59-62)
38fn main() {
39 let a = app::App::default().use_state(Counter::new).unwrap();
40
41 let mut window = Window::default().with_size(320, 240).with_label("Add data");
42 let col = Flex::default_fill().column();
43 let mut inc = Button::default().with_label("+");
44 inc.set_action(Counter::increment);
45 let mut f = Frame::default().with_label("0");
46 f.set_frame(FrameType::FlatBox);
47 f.handle(|f, ev| match ev {
48 Event::Enter => {
49 f.set_color(Color::Red);
50 f.redraw();
51 true
52 }
53 Event::Leave => {
54 f.set_color(Color::Background);
55 f.redraw();
56 true
57 }
58 fltk_observe::STATE_CHANGED => {
59 fltk_observe::with_state({
60 let mut f = f.clone();
61 move |s: &Counter| s.update_output(&mut f)
62 });
63 false
64 }
65 _ => false,
66 });
67 let mut dec = Button::default().with_label("-");
68 dec.set_action(|s: &mut Counter, b| s.decrement(b));
69 col.end();
70 window.end();
71 window.show();
72
73 a.run().unwrap();
74}