pub struct GlobalState<T> { /* private fields */ }
Expand description
Represents global state
Implementations§
Source§impl<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 51)
49fn main() {
50 let counter = Counter { count: 0 };
51 let _state = app::GlobalState::new(counter);
52 let app = app::App::default().with_scheme(app::Scheme::Gtk);
53 let mut wind = window::Window::default()
54 .with_size(160, 200)
55 .with_label("Counter");
56 let col = group::Flex::default_fill().column();
57 button::Button::default()
58 .with_label("+")
59 .on_trigger(increment); // passed by function object
60 frame::Frame::default().with_label("0").with_id("my_frame"); // pass id here
61 button::Button::default()
62 .with_label("-")
63 .on_trigger(|_| increment_by(-1)); // called in closure
64 col.end();
65 wind.make_resizable(true);
66 wind.end();
67 wind.show();
68
69 app.run().unwrap();
70}
More examples
examples/editor.rs (line 254)
246fn main() {
247 let a = app::App::default().with_scheme(app::Scheme::Oxy);
248 app::get_system_colors();
249
250 let mut buf = text::TextBuffer::default();
251 buf.set_tab_distance(4);
252
253 let state = State::new(buf.clone());
254 app::GlobalState::new(state);
255
256 let mut w = window::Window::default()
257 .with_size(WIDTH, HEIGHT)
258 .with_label("Ted");
259 w.set_xclass("ted");
260 {
261 let mut col = group::Flex::default_fill().column();
262 col.set_pad(0);
263 let mut m = menu::SysMenuBar::default();
264 init_menu(&mut m);
265 let mut ed = text::TextEditor::default().with_id("ed");
266 ed.set_buffer(buf);
267 ed.set_linenumber_width(40);
268 ed.set_text_font(Font::Courier);
269 ed.set_when(When::Changed);
270 ed.set_callback(editor_cb);
271 handle_drag_drop(&mut ed);
272 w.resizable(&col);
273 col.fixed(&m, 30);
274 col.end();
275 }
276 w.end();
277 w.show();
278 w.set_callback(win_cb);
279 a.run().unwrap();
280}
Sourcepub fn with<V: Clone, F: 'static + FnMut(&mut T) -> V>(&self, cb: F) -> V
pub fn with<V: Clone, F: 'static + FnMut(&mut T) -> V>(&self, cb: F) -> V
Examples found in repository?
examples/widget_id.rs (lines 31-34)
28fn increment_by(step: i32) {
29 let mut frame: frame::Frame = app::widget_from_id("my_frame").unwrap();
30 let state = app::GlobalState::<Counter>::get();
31 let count = state.with(move |c| {
32 c.count += step;
33 c.count
34 });
35 frame.set_label(&count.to_string());
36}
37
38// To pass a function object directly!
39fn increment(_w: &mut impl WidgetExt) {
40 let mut frame: frame::Frame = app::widget_from_id("my_frame").unwrap();
41 let state = app::GlobalState::<Counter>::get();
42 let count = state.with(|c| {
43 c.count += 1;
44 c.count
45 });
46 frame.set_label(&count.to_string());
47}
More examples
examples/editor.rs (lines 112-126)
111fn quit_cb() {
112 STATE.with(|s| {
113 if s.saved {
114 app::quit();
115 } else {
116 let c = dialog::choice(
117 "Are you sure you want to exit without saving?",
118 "&Yes",
119 "&No",
120 "",
121 );
122 if c == Some(0) {
123 app::quit();
124 }
125 }
126 });
127}
128
129fn win_cb(_w: &mut window::Window) {
130 if app::event() == Event::Close {
131 quit_cb();
132 }
133}
134
135fn editor_cb(_e: &mut text::TextEditor) {
136 STATE.with(|s| s.saved = false);
137}
138
139fn handle_drag_drop(editor: &mut text::TextEditor) {
140 editor.handle({
141 let mut dnd = false;
142 let mut released = false;
143 let buf = editor.buffer().unwrap();
144 move |_, ev| match ev {
145 Event::DndEnter => {
146 dnd = true;
147 true
148 }
149 Event::DndDrag => true,
150 Event::DndRelease => {
151 released = true;
152 true
153 }
154 Event::Paste => {
155 if dnd && released {
156 let path = app::event_text().unwrap();
157 let path = path.trim();
158 let path = path.replace("file://", "");
159 let path = std::path::PathBuf::from(&path);
160 if path.exists() {
161 // we use a timeout to avoid pasting the path into the buffer
162 app::add_timeout(0.0, {
163 let mut buf = buf.clone();
164 move |_| match buf.load_file(&path) {
165 Ok(_) => (),
166 Err(e) => dialog::alert(&format!(
167 "An issue occured while loading the file: {e}"
168 )),
169 }
170 });
171 }
172 dnd = false;
173 released = false;
174 true
175 } else {
176 false
177 }
178 }
179 Event::DndLeave => {
180 dnd = false;
181 released = false;
182 true
183 }
184 _ => false,
185 }
186 });
187}
188
189fn menu_cb(m: &mut impl MenuExt) {
190 if let Ok(mpath) = m.item_pathname(None) {
191 let ed: text::TextEditor = app::widget_from_id("ed").unwrap();
192 match mpath.as_str() {
193 "&File/&New...\t" => {
194 STATE.with(|s| {
195 if !s.buf.text().is_empty() {
196 let c = dialog::choice(
197 "Are you sure you want to clear the buffer?",
198 "&Yes",
199 "&No",
200 "",
201 );
202 if c == Some(0) {
203 s.buf.set_text("");
204 s.saved = false;
205 }
206 }
207 });
208 }
209 "&File/&Open...\t" => {
210 if let Some(c) = nfc_get_file(dialog::NativeFileChooserType::BrowseFile) {
211 if let Ok(text) = std::fs::read_to_string(&c) {
212 STATE.with(move |s| {
213 s.buf.set_text(&text);
214 s.saved = false;
215 s.current_file = c.clone();
216 });
217 }
218 }
219 }
220 "&File/&Save\t" => {
221 STATE.with(|s| {
222 if !s.saved && s.current_file.exists() {
223 std::fs::write(&s.current_file, s.buf.text()).ok();
224 }
225 });
226 }
227 "&File/Save &as...\t" => {
228 if let Some(c) = nfc_get_file(dialog::NativeFileChooserType::BrowseSaveFile) {
229 STATE.with(move |s| {
230 std::fs::write(&c, s.buf.text()).ok();
231 s.saved = true;
232 s.current_file = c.clone();
233 });
234 }
235 }
236 "&File/&Quit\t" => quit_cb(),
237 "&Edit/Cu&t\t" => ed.cut(),
238 "&Edit/&Copy\t" => ed.copy(),
239 "&Edit/&Paste\t" => ed.paste(),
240 "&Help/&About\t" => dialog::message("A minimal text editor written using fltk-rs!"),
241 _ => unreachable!(),
242 }
243 }
244}
Sourcepub fn get() -> Self
pub fn get() -> Self
Gets the already initialized global state
Examples found in repository?
examples/widget_id.rs (line 30)
28fn increment_by(step: i32) {
29 let mut frame: frame::Frame = app::widget_from_id("my_frame").unwrap();
30 let state = app::GlobalState::<Counter>::get();
31 let count = state.with(move |c| {
32 c.count += step;
33 c.count
34 });
35 frame.set_label(&count.to_string());
36}
37
38// To pass a function object directly!
39fn increment(_w: &mut impl WidgetExt) {
40 let mut frame: frame::Frame = app::widget_from_id("my_frame").unwrap();
41 let state = app::GlobalState::<Counter>::get();
42 let count = state.with(|c| {
43 c.count += 1;
44 c.count
45 });
46 frame.set_label(&count.to_string());
47}
Trait Implementations§
Source§impl<T> Clone for GlobalState<T>
impl<T> Clone for GlobalState<T>
Source§impl<T: Debug> Debug for GlobalState<T>
impl<T: Debug> Debug for GlobalState<T>
impl<T: Copy> Copy for GlobalState<T>
Auto Trait Implementations§
impl<T> Freeze for GlobalState<T>
impl<T> RefUnwindSafe for GlobalState<T>where
T: RefUnwindSafe,
impl<T> Send for GlobalState<T>where
T: Send,
impl<T> Sync for GlobalState<T>where
T: Sync,
impl<T> Unpin for GlobalState<T>where
T: Unpin,
impl<T> UnwindSafe for GlobalState<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more