pub fn add_timeout<F: FnMut(TimeoutHandle) + 'static>(
tm: f64,
cb: F,
) -> TimeoutHandleExpand description
Adds a one-shot timeout callback. The timeout duration tm is indicated in seconds
This function returns a handle that can be use for future interaction with the timeout
Example:
use fltk::{prelude::*, *};
fn main() {
let callback = |_handle| {
println!("FIRED");
};
let app = app::App::default();
let mut wind = window::Window::new(100, 100, 400, 300, "");
wind.show();
let _handle = app::add_timeout(1.0, callback);
app.run().unwrap();
}Examples found in repository?
examples/animations.rs (lines 68-71)
56fn main() {
57 let app = app::App::default();
58 let mut wind = Window::default().with_label("timeout").with_size(720, 486);
59 wind.set_center_screen();
60 let mut frame = Frame::new(-200, 150, 200, 200, "");
61 let mut pxm = Pixmap::new(PXM).unwrap();
62 pxm.scale(200, 200, true, true);
63 frame.set_image_scaled(Some(pxm));
64 wind.set_color(enums::Color::White);
65 wind.end();
66 wind.show_with_env_args();
67
68 app::add_timeout(0.016, move |handle| {
69 let frame = frame.clone();
70 move_image(frame, handle);
71 });
72 app.run().unwrap();
73}More examples
examples/editor.rs (lines 162-170)
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}examples/editor2.rs (lines 264-272)
199 pub fn new(args: Vec<String>) -> Self {
200 let app = app::App::default().with_scheme(app::Scheme::Gtk);
201 app::background(211, 211, 211);
202 let (s, r) = app::channel::<Message>();
203 let mut buf = text::TextBuffer::default();
204 buf.set_tab_distance(4);
205 let mut main_win = window::Window::default()
206 .with_size(800, 600)
207 .with_label("RustyEd");
208 main_win.set_center_screen();
209 let menu = MyMenu::new(&s);
210 let modified = false;
211 menu.menu.find_item("&File/&Save\t").unwrap().deactivate();
212 let mut editor = MyEditor::new(buf.clone());
213 editor.emit(s, Message::Changed);
214 main_win.make_resizable(true);
215 // only resize editor, not the menu bar
216 main_win.resizable(&*editor);
217 main_win.end();
218 main_win.show();
219 main_win.set_callback(move |_| {
220 if app::event() == Event::Close {
221 s.send(Message::Quit);
222 }
223 });
224 let filename = if args.len() > 1 {
225 let file = path::Path::new(&args[1]);
226 assert!(
227 file.exists() && file.is_file(),
228 "An error occurred while opening the file!"
229 );
230 match buf.load_file(&args[1]) {
231 Ok(_) => Some(PathBuf::from(args[1].clone())),
232 Err(e) => {
233 dialog::alert(&format!("An issue occured while loading the file: {e}"));
234 None
235 }
236 }
237 } else {
238 None
239 };
240
241 // Handle drag and drop
242 editor.handle({
243 let mut dnd = false;
244 let mut released = false;
245 let buf = buf.clone();
246 move |_, ev| match ev {
247 Event::DndEnter => {
248 dnd = true;
249 true
250 }
251 Event::DndDrag => true,
252 Event::DndRelease => {
253 released = true;
254 true
255 }
256 Event::Paste => {
257 if dnd && released {
258 let path = app::event_text().unwrap();
259 let path = path.trim();
260 let path = path.replace("file://", "");
261 let path = std::path::PathBuf::from(&path);
262 if path.exists() {
263 // we use a timeout to avoid pasting the path into the buffer
264 app::add_timeout(0.0, {
265 let mut buf = buf.clone();
266 move |_| match buf.load_file(&path) {
267 Ok(_) => (),
268 Err(e) => dialog::alert(&format!(
269 "An issue occured while loading the file: {e}"
270 )),
271 }
272 });
273 }
274 dnd = false;
275 released = false;
276 true
277 } else {
278 false
279 }
280 }
281 Event::DndLeave => {
282 dnd = false;
283 released = false;
284 true
285 }
286 _ => false,
287 }
288 });
289
290 // What shows when we attempt to print
291 let mut printable = text::TextDisplay::default();
292 printable.set_frame(FrameType::NoBox);
293 printable.set_scrollbar_size(0);
294 printable.set_buffer(Some(buf.clone()));
295
296 Self {
297 app,
298 modified,
299 filename,
300 r,
301 main_win,
302 menu,
303 buf,
304 editor,
305 printable,
306 }
307 }