Struct fltk::app::GlobalState
source · Expand description
Represents global state
Implementations
sourceimpl<T: Sync + Send + 'static> GlobalState<T>
impl<T: Sync + Send + 'static> GlobalState<T>
sourcepub fn new(val: T) -> Self
pub fn new(val: T) -> Self
Creates a new global state
Examples found in repository?
examples/widget_id.rs (line 94)
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
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); // passed by function object
frame::Frame::default().with_label("0").with_id("my_frame"); // pass id here
button::Button::default()
.with_label("-")
.on_trigger(|_| increment_by(-1)); // called in closure
col.end();
wind.make_resizable(true);
wind.end();
wind.show();
app.run().unwrap();
}
sourcepub fn with<V: Clone, F: 'static + Fn(&mut T) -> V>(&self, cb: F) -> V
pub fn with<V: Clone, F: 'static + Fn(&mut T) -> V>(&self, cb: F) -> V
Examples found in repository?
examples/widget_id.rs (lines 74-77)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
fn increment_by(step: i32) {
let mut frame: frame::Frame = 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());
}
// To pass a function object directly!
fn increment(_w: &mut impl WidgetExt) {
let mut frame: frame::Frame = 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());
}
sourcepub fn get() -> Self
pub fn get() -> Self
Gets the already initialized global state
Examples found in repository?
examples/widget_id.rs (line 73)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
fn increment_by(step: i32) {
let mut frame: frame::Frame = 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());
}
// To pass a function object directly!
fn increment(_w: &mut impl WidgetExt) {
let mut frame: frame::Frame = 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());
}
Trait Implementations
sourceimpl<T: Send + Sync> Clone for GlobalState<T>
impl<T: Send + Sync> Clone for GlobalState<T>
impl<T: Copy + Send + Sync> Copy for GlobalState<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for GlobalState<T>where
T: RefUnwindSafe,
impl<T> Send for GlobalState<T>
impl<T> Sync for GlobalState<T>
impl<T> Unpin for GlobalState<T>where
T: Unpin,
impl<T> UnwindSafe for GlobalState<T>where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more