pub struct App {}
Expand description
Basic Application struct, used to instantiate, set the scheme and run the event loop
Implementations§
Source§impl App
impl App
Sourcepub fn set_scheme(&mut self, scheme: Scheme)
pub fn set_scheme(&mut self, scheme: Scheme)
Sets the scheme of the application
Sourcepub fn with_scheme(self, scheme: Scheme) -> App
pub fn with_scheme(self, scheme: Scheme) -> App
Sets the scheme of the application
Examples found in repository?
examples/paint.rs (line 89)
88fn main() {
89 let app = app::App::default().with_scheme(app::Scheme::Gtk);
90
91 let mut wind = Window::default()
92 .with_size(WIDTH, HEIGHT)
93 .with_label("RustyPainter");
94
95 Canvas::new(WIDTH - 10, HEIGHT - 10);
96
97 wind.end();
98 wind.show();
99
100 app.run().unwrap();
101}
More examples
examples/tabs.rs (line 42)
41fn main() {
42 let app = app::App::default().with_scheme(app::Scheme::Gtk);
43 app::background(221, 221, 221);
44
45 let mut wind = Window::default().with_size(500, 450).with_label("Tabs");
46 wind.set_center_screen();
47
48 draw_gallery();
49
50 wind.make_resizable(true);
51 wind.end();
52 wind.show();
53
54 app.run().unwrap();
55}
examples/flex.rs (line 4)
3fn main() {
4 let a = app::App::default().with_scheme(app::Scheme::Gtk);
5 let mut win = window::Window::default().with_size(640, 480);
6 let mut col = group::Flex::default_fill().column();
7 main_panel(&mut col);
8 col.end();
9 win.resizable(&col);
10 win.set_color(enums::Color::from_rgb(250, 250, 250));
11 win.end();
12 win.show();
13 win.size_range(600, 400, 0, 0);
14 a.run().unwrap();
15}
examples/wizard.rs (line 56)
55fn main() {
56 let app = app::App::default().with_scheme(app::Scheme::Gtk);
57 let mut win = window::Window::default().with_size(400, 300);
58 let _but = {
59 let mut b = button::Button::default()
60 .with_size(160, 40)
61 .with_label("Show wizard")
62 .center_of(&win);
63 b.set_callback(show_wizard);
64 b
65 };
66 win.end();
67 win.show();
68 app.run().unwrap();
69}
examples/hello_svg.rs (line 6)
5fn main() {
6 let app = app::App::default().with_scheme(app::Scheme::Gleam);
7
8 let mut wind = Window::new(100, 100, 400, 300, "Hello from rust");
9
10 let mut frame = Frame::default().with_size(360, 260).center_of_parent();
11 frame.set_frame(FrameType::EngravedBox);
12 let mut image1 = SvgImage::load("screenshots/RustLogo.svg").unwrap();
13 image1.scale(200, 200, true, true);
14 frame.set_image(Some(image1));
15
16 wind.make_resizable(true);
17 wind.end();
18 wind.show();
19
20 app.run().unwrap();
21}
examples/image.rs (line 5)
4fn main() -> Result<(), Box<dyn Error>> {
5 let app = app::App::default().with_scheme(app::Scheme::Gleam);
6 let mut wind = Window::default().with_size(400, 300);
7 let mut frame = Frame::default_fill();
8
9 {
10 let mut image = JpegImage::load("screenshots/calc.jpg")?;
11 frame.set_image(Some(image.clone()));
12 image.scale(200, 200, true, true);
13 }
14
15 // // To remove an image
16 // frame.set_image(None::<SharedImage>);
17
18 wind.end();
19 wind.make_resizable(true);
20 wind.show();
21
22 app.run()?;
23 Ok(())
24}
Sourcepub fn run(self) -> Result<(), FltkError>
pub fn run(self) -> Result<(), FltkError>
Examples found in repository?
examples/paint.rs (line 100)
88fn main() {
89 let app = app::App::default().with_scheme(app::Scheme::Gtk);
90
91 let mut wind = Window::default()
92 .with_size(WIDTH, HEIGHT)
93 .with_label("RustyPainter");
94
95 Canvas::new(WIDTH - 10, HEIGHT - 10);
96
97 wind.end();
98 wind.show();
99
100 app.run().unwrap();
101}
More examples
examples/tabs.rs (line 54)
41fn main() {
42 let app = app::App::default().with_scheme(app::Scheme::Gtk);
43 app::background(221, 221, 221);
44
45 let mut wind = Window::default().with_size(500, 450).with_label("Tabs");
46 wind.set_center_screen();
47
48 draw_gallery();
49
50 wind.make_resizable(true);
51 wind.end();
52 wind.show();
53
54 app.run().unwrap();
55}
examples/hello_button.rs (line 13)
3fn main() {
4 let app = app::App::default();
5 let mut wind = Window::default().with_size(400, 300);
6 let mut frame = Frame::default().with_size(200, 100).center_of(&wind);
7 let mut but = Button::new(160, 210, 80, 40, "Click me!");
8 wind.end();
9 wind.show();
10
11 but.set_callback(move |_| frame.set_label("Hello world"));
12
13 app.run().unwrap();
14}
examples/flex.rs (line 14)
3fn main() {
4 let a = app::App::default().with_scheme(app::Scheme::Gtk);
5 let mut win = window::Window::default().with_size(640, 480);
6 let mut col = group::Flex::default_fill().column();
7 main_panel(&mut col);
8 col.end();
9 win.resizable(&col);
10 win.set_color(enums::Color::from_rgb(250, 250, 250));
11 win.end();
12 win.show();
13 win.size_range(600, 400, 0, 0);
14 a.run().unwrap();
15}
examples/wizard.rs (line 68)
55fn main() {
56 let app = app::App::default().with_scheme(app::Scheme::Gtk);
57 let mut win = window::Window::default().with_size(400, 300);
58 let _but = {
59 let mut b = button::Button::default()
60 .with_size(160, 40)
61 .with_label("Show wizard")
62 .center_of(&win);
63 b.set_callback(show_wizard);
64 b
65 };
66 win.end();
67 win.show();
68 app.run().unwrap();
69}
examples/custom_choice.rs (line 179)
169fn main() {
170 let app = app::App::default();
171 let mut win = window::Window::default().with_size(400, 300);
172 let mut choice = MyChoice::new(160, 200, 100, 30, None);
173 choice.add_choices(&["choice1", "choice2", "choice3"]);
174 choice.set_current_choice(1);
175 choice.button().set_frame(FrameType::BorderBox);
176 choice.frame().set_frame(FrameType::BorderBox);
177 win.end();
178 win.show();
179 app.run().unwrap();
180}
Additional examples can be found in:
- examples/hello_flex.rs
- examples/custom_dial.rs
- examples/hello_svg.rs
- examples/image.rs
- examples/animations.rs
- examples/composite_widgets.rs
- examples/widget_table.rs
- examples/menubutton.rs
- examples/rounded_images.rs
- examples/widget_id.rs
- examples/gradients.rs
- examples/custom_popup.rs
- examples/fb.rs
- examples/charts.rs
- examples/closable_tab.rs
- examples/rgb.rs
- examples/editor.rs
- examples/counter4.rs
- examples/spreadsheet.rs
- examples/table.rs
- examples/defaults.rs
- examples/frames.rs
- examples/tree.rs
- examples/temp_converter.rs
- examples/counter.rs
- examples/popup_browser.rs
- examples/counter3.rs
- examples/tile.rs
- examples/pong.rs
- examples/shapedwindow_taskbar.rs
- examples/format_text.rs
- examples/terminal.rs
Sourcepub fn wait(self) -> bool
pub fn wait(self) -> bool
Wait for incoming messages. Calls to redraw within wait require an explicit sleep
Examples found in repository?
examples/messages.rs (line 32)
14fn main() {
15 let app = app::App::default();
16 let mut wind = Window::default().with_size(400, 300);
17 let mut frame = Frame::default().size_of(&wind).with_label("0");
18
19 let mut val = 0;
20
21 wind.show();
22
23 let (s, r) = app::channel::<Message>();
24
25 std::thread::spawn(move || {
26 loop {
27 app::sleep(1.);
28 s.send(Message::Increment(2));
29 }
30 });
31
32 while app.wait() {
33 if let Some(Message::Increment(step)) = r.recv() {
34 inc_frame(&mut frame, &mut val, step)
35 }
36 }
37}
More examples
examples/temp_converter2.rs (line 84)
83 pub fn run(&mut self) {
84 while self.a.wait() {
85 if let Some(msg) = self.r.recv() {
86 match msg {
87 Message::CelciusChanged => {
88 self.inp2.set_value(&format!(
89 "{:.4}",
90 c_to_f(self.inp1.value().parse().unwrap_or(0.0))
91 ));
92 }
93 Message::FahrenheitChanged => {
94 self.inp1.set_value(&format!(
95 "{:.4}",
96 f_to_c(self.inp2.value().parse().unwrap_or(0.0))
97 ));
98 }
99 }
100 }
101 }
102 }
examples/system_fonts.rs (line 41)
28fn main() {
29 let app = app::App::default().load_system_fonts();
30 // To load a font by path, check the App::load_font() method
31 let fonts = app::fonts();
32 // println!("{:?}", fonts);
33 let mut wind = window::Window::default().with_size(400, 300);
34 let mut frame = frame::Frame::default().size_of(&wind);
35 frame.set_label_size(30);
36 wind.set_color(enums::Color::White);
37 wind.end();
38 wind.show();
39 println!("The system has {} fonts!\nStarting slideshow!", fonts.len());
40 let mut i = 0;
41 while app.wait() {
42 if i == fonts.len() {
43 i = 0;
44 }
45 frame.set_label(&format!("[{}]", fonts[i]));
46 frame.set_label_font(enums::Font::by_index(i));
47 app::sleep(0.5);
48 i += 1;
49 }
50}
examples/counter2.rs (line 27)
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10 let app = app::App::default();
11 let mut wind = Window::default().with_size(160, 200).with_label("Counter");
12 let mut flex = Flex::default_fill().column();
13 flex.set_margins(30, 40, 30, 40);
14 flex.set_pad(10);
15 let mut but_inc = Button::default().with_label("+");
16 let mut frame = Frame::default().with_label("0");
17 let mut but_dec = Button::default().with_label("-");
18 flex.end();
19 wind.end();
20 wind.show();
21
22 let (s, r) = app::channel::<Message>();
23
24 but_inc.emit(s, Message::Increment);
25 but_dec.emit(s, Message::Decrement);
26
27 while app.wait() {
28 let label: i32 = frame.label().unwrap().parse()?;
29
30 if let Some(msg) = r.recv() {
31 match msg {
32 Message::Increment => frame.set_label(&(label + 1).to_string()),
33 Message::Decrement => frame.set_label(&(label - 1).to_string()),
34 }
35 }
36 }
37 Ok(())
38}
examples/closable_tabs2.rs (line 182)
152fn main() {
153 use fltk::{prelude::*, *};
154 // Create groups to be used as content for tabs
155 pub fn create_tab(from: i32, to: i32) -> group::Group {
156 let grp = group::Group::new(0, 0, 800, 600, None);
157 for idx in from..to {
158 button::Button::new(
159 idx * 10 + (idx - from) * 42,
160 idx * 10 + (idx - from) * 42,
161 80,
162 40,
163 None,
164 )
165 .with_label(&format!("button {idx}"));
166 }
167 grp.end();
168 grp
169 }
170 let app = app::App::default();
171 let mut win = window::Window::default().with_size(800, 600);
172 let (s, r) = app::channel::<closable_tab::Message>();
173 let mut tabs = closable_tab::ClosableTab::new(0, 0, 800, 600, &s);
174 win.end();
175 win.show();
176 tabs.add(&mut create_tab(1, 3), "tab 1");
177 tabs.add(&mut create_tab(4, 7), "tab 2");
178 tabs.add(&mut create_tab(8, 11), "tab 3");
179 tabs.add(&mut create_tab(12, 15), "tab 4");
180 tabs.add(&mut create_tab(16, 22), "tab 5");
181 tabs.set_foreground(2);
182 while app.wait() {
183 use closable_tab::Message::*;
184 if let Some(msg) = r.recv() {
185 match msg {
186 Foreground(idx) => {
187 tabs.set_foreground(idx);
188 }
189 Delete(idx) => {
190 tabs.remove(idx);
191 }
192 InsertNew(_) => {}
193 }
194 }
195 }
196}
examples/threads_windows.rs (line 62)
17fn main() -> Result<(), Box<dyn std::error::Error>> {
18 let app = app::App::default();
19 let mut wind = Window::default().with_size(160, 200).with_label("Counter");
20 let mut col = Flex::default()
21 .with_size(120, 140)
22 .center_of(&wind)
23 .column();
24 col.set_spacing(10);
25 let mut but_inc = Button::default().with_label("+");
26 let mut frame = Frame::default().with_label("0");
27 let mut but_dec = Button::default().with_label("-");
28 col.end();
29 wind.end();
30 wind.show();
31
32 let mut msg_wind = Window::default().with_size(120, 100).with_label("Message");
33 let mut msgview = HelpView::default().with_size(120, 100);
34 msgview.set_align(Align::Center);
35 msg_wind.end();
36
37 let (s, r) = app::channel::<Message>();
38
39 but_inc.set_callback({
40 move |_| {
41 s.send(Message::Deactivate);
42 thread::spawn(move || {
43 thread::sleep(Duration::from_secs(1));
44 s.send(Message::Increment);
45 s.send(Message::Message("Incremented"));
46 s.send(Message::Activate);
47 });
48 }
49 });
50 but_dec.set_callback({
51 move |_| {
52 s.send(Message::Deactivate);
53 thread::spawn(move || {
54 thread::sleep(Duration::from_secs(1));
55 s.send(Message::Decrement);
56 s.send(Message::Message("Decremented"));
57 s.send(Message::Activate);
58 });
59 }
60 });
61
62 while app.wait() {
63 if let Some(msg) = r.recv() {
64 let label: i32 = frame.label().unwrap().parse()?;
65 match msg {
66 Message::Increment => frame.set_label(&(label + 1).to_string()),
67 Message::Decrement => frame.set_label(&(label - 1).to_string()),
68 Message::Activate => {
69 but_inc.activate();
70 but_dec.activate();
71 }
72 Message::Deactivate => {
73 but_inc.deactivate();
74 but_dec.deactivate();
75 }
76 Message::Message(s) => {
77 msgview.set_value(s);
78 msg_wind.show();
79 }
80 }
81 }
82 }
83 Ok(())
84}
Additional examples can be found in:
Sourcepub fn load_system_fonts(self) -> Self
pub fn load_system_fonts(self) -> Self
Loads system fonts
Examples found in repository?
examples/system_fonts.rs (line 29)
28fn main() {
29 let app = app::App::default().load_system_fonts();
30 // To load a font by path, check the App::load_font() method
31 let fonts = app::fonts();
32 // println!("{:?}", fonts);
33 let mut wind = window::Window::default().with_size(400, 300);
34 let mut frame = frame::Frame::default().size_of(&wind);
35 frame.set_label_size(30);
36 wind.set_color(enums::Color::White);
37 wind.end();
38 wind.show();
39 println!("The system has {} fonts!\nStarting slideshow!", fonts.len());
40 let mut i = 0;
41 while app.wait() {
42 if i == fonts.len() {
43 i = 0;
44 }
45 frame.set_label(&format!("[{}]", fonts[i]));
46 frame.set_label_font(enums::Font::by_index(i));
47 app::sleep(0.5);
48 i += 1;
49 }
50}
Sourcepub fn load_font<P: AsRef<Path>>(self, path: P) -> Result<String, FltkError>
pub fn load_font<P: AsRef<Path>>(self, path: P) -> Result<String, FltkError>
Loads a font from a path.
On success, returns a String with the ttf Font Family name. The font’s index is always 16.
As such only one font can be loaded at a time.
The font name can be used with Font::by_name
, and index with Font::by_index
.
§Examples
use fltk::{prelude::*, *};
let app = app::App::default();
let font = app.load_font("font.ttf").unwrap();
let mut frame = frame::Frame::new(0, 0, 400, 100, "Hello");
frame.set_label_font(enums::Font::by_name(&font));
§Errors
Returns ResourceNotFound
if the Font file was not found
Sourcepub fn set_visual(self, mode: Mode) -> Result<(), FltkError>
pub fn set_visual(self, mode: Mode) -> Result<(), FltkError>
Set the visual of the application
§Errors
Returns FailedOperation
if FLTK failed to set the visual mode
Sourcepub fn quit(self)
pub fn quit(self)
Quit the application
Examples found in repository?
examples/shapedwindow_taskbar.rs (line 85)
17fn main() {
18 let app = app::App::default();
19
20 // Act as the application in the taskbar (scroll to event handling)
21 let mut dock_win = window::Window::default()
22 .with_size(1, 1) // So we can place it at the center of the screen (needs a size >0 to be centered)
23 .with_label("TestApplication");
24 dock_win.set_center_screen();
25 dock_win.size_range(0, 0, 0, 0);
26 dock_win.make_resizable(false);
27
28 dock_win.show();
29 dock_win.end();
30
31 let mut win = window::Window::default()
32 .with_size(900, 500)
33 .with_label("TestApplication");
34 win.set_center_screen();
35 win.set_color(enums::Color::from_rgb(26, 25, 55));
36
37 let mut but = button::Button::default()
38 .with_label("Button")
39 .with_size(80, 80)
40 .center_of_parent();
41 but.set_frame(enums::FrameType::OFlatBox);
42 but.set_color(enums::Color::Cyan);
43 but.clear_visible_focus();
44 but.set_callback(|_| println!("Clicked"));
45
46 win.show();
47 win.end();
48
49 let win_shape = prep_shape(win.w(), win.h());
50
51 // Called after showing window
52 win.set_shape(Some(win_shape));
53
54 win.handle({
55 let mut x = 0;
56 let mut y = 0;
57 let mut dock_win = dock_win.clone();
58 move |wself, event| match event {
59 enums::Event::Push => {
60 let coords = app::event_coords();
61 x = coords.0;
62 y = coords.1;
63
64 true
65 }
66 enums::Event::Drag => {
67 wself.resize(
68 app::event_x_root() - x,
69 app::event_y_root() - y,
70 wself.w(),
71 wself.h(),
72 );
73
74 // Changing dock window position so it's close enough to the center of the application (not "visible" to user)
75 dock_win.resize(
76 wself.x() + (wself.w() / 2),
77 wself.y() + (wself.w() / 2),
78 dock_win.w(),
79 dock_win.h(),
80 );
81
82 true
83 }
84 enums::Event::Close => {
85 app.quit();
86
87 true
88 }
89 enums::Event::Hide => {
90 app.quit();
91
92 true
93 }
94 _ => false,
95 }
96 });
97
98 // Make main window appear when "opened" via Alt+Tab or Taskbar
99 dock_win.handle({
100 let mut win = win.clone();
101 move |_wself, event| match event {
102 enums::Event::Focus => {
103 let win_shape = prep_shape(win.w(), win.h());
104
105 win.show();
106 win.set_shape(Some(win_shape));
107
108 true
109 }
110 enums::Event::Hide => {
111 win.hide();
112
113 true
114 }
115 enums::Event::Close => {
116 app.quit();
117
118 true
119 }
120 _ => false,
121 }
122 });
123
124 app.run().unwrap();
125}
More examples
examples/editor2.rs (line 459)
362 pub fn launch(&mut self) {
363 while self.app.wait() {
364 use Message::*;
365 if let Some(msg) = self.r.recv() {
366 match msg {
367 Changed => {
368 if !self.modified {
369 self.modified = true;
370 self.menu
371 .menu
372 .find_item("&File/&Save\t")
373 .unwrap()
374 .activate();
375 self.menu
376 .menu
377 .find_item("&File/&Quit\t")
378 .unwrap()
379 .set_label_color(Color::Red);
380 let name = match &self.filename {
381 Some(f) => f.to_string_lossy().to_string(),
382 None => "(Untitled)".to_string(),
383 };
384 self.main_win.set_label(&format!("* {name} - RustyEd"));
385 }
386 }
387 New => {
388 if self.buf.text() != "" {
389 let clear = if let Some(x) = dialog::choice(
390 "File unsaved, Do you wish to continue?",
391 "&Yes",
392 "&No!",
393 "",
394 ) {
395 x == 0
396 } else {
397 false
398 };
399 if clear {
400 self.buf.set_text("");
401 }
402 }
403 }
404 Open => {
405 if let Some(c) = nfc_get_file(dialog::NativeFileChooserType::BrowseFile) {
406 if c.exists() {
407 match self.buf.load_file(&c) {
408 Ok(_) => self.filename = Some(c),
409 Err(e) => dialog::alert(&format!(
410 "An issue occured while loading the file: {e}"
411 )),
412 }
413 } else {
414 dialog::alert("File does not exist!")
415 }
416 }
417 }
418 Save => {
419 self.save_file().unwrap();
420 }
421 SaveAs => {
422 self.save_file_as().unwrap();
423 }
424 Print => {
425 let mut printer = printer::Printer::default();
426 if printer.begin_job(0).is_ok() {
427 let (w, h) = printer.printable_rect();
428 self.printable.resize(
429 self.printable.x(),
430 self.printable.y(),
431 w - 40,
432 h - 40,
433 );
434 // Needs cleanup
435 let line_count = self.printable.count_lines(
436 0,
437 self.printable.buffer().unwrap().length(),
438 true,
439 ) / 45;
440 for i in 0..=line_count {
441 self.printable.scroll(45 * i, 0);
442 printer.begin_page().ok();
443 printer.print_widget(&self.printable, 20, 20);
444 printer.end_page().ok();
445 }
446 printer.end_job();
447 }
448 }
449 Quit => {
450 if self.modified {
451 match dialog::choice(
452 "Would you like to save your work?",
453 "&Yes",
454 "&No",
455 "",
456 ) {
457 Some(0) => {
458 if self.save_file().unwrap() {
459 self.app.quit();
460 }
461 }
462 Some(1) => self.app.quit(),
463 Some(_) | None => (),
464 }
465 } else {
466 self.app.quit();
467 }
468 }
469 Cut => self.editor.cut(),
470 Copy => self.editor.copy(),
471 Paste => self.editor.paste(),
472 About => dialog::message(
473 "This is an example application written in Rust and using the FLTK Gui library.",
474 ),
475 }
476 }
477 }
478 }
Trait Implementations§
impl Copy for App
Auto Trait Implementations§
impl Freeze for App
impl RefUnwindSafe for App
impl Send for App
impl Sync for App
impl Unpin for App
impl UnwindSafe for App
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