App

Struct App 

Source
pub struct App {}
Expand description

Basic Application struct, used to instantiate, set the scheme and run the event loop

Implementations§

Source§

impl App

Source

pub fn set_scheme(&mut self, scheme: Scheme)

Sets the scheme of the application

Source

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
Hide additional 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()
46        .with_size(500, 450)
47        .with_label("Tabs")
48        .center_screen();
49
50    draw_gallery();
51
52    wind.make_resizable(true);
53    wind.end();
54    wind.show();
55
56    app.run().unwrap();
57}
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/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    let mut image = SharedImage::load("screenshots/calc.jpg")?;
10    image.scale(200, 200, true, true);
11
12    frame.set_image(Some(image));
13
14    // // To remove an image
15    // frame.set_image(None::<SharedImage>);
16
17    wind.end();
18    wind.make_resizable(true);
19    wind.show();
20
21    app.run()?;
22    Ok(())
23}
examples/hello_svg.rs (line 10)
9fn main() {
10    let app = app::App::default().with_scheme(app::Scheme::Gleam);
11
12    let mut wind = Window::new(100, 100, 400, 300, "Hello from rust");
13
14    let mut frame = Frame::default().with_size(360, 260).center_of_parent();
15    frame.set_frame(FrameType::EngravedBox);
16    let mut image1 = SvgImage::load("screenshots/RustLogo.svg").unwrap();
17    image1.scale(200, 200, true, true);
18    frame.set_image(Some(image1));
19
20    wind.make_resizable(true);
21    wind.end();
22    wind.show();
23    wind.set_icon(Some(SvgImage::from_data(IMAGE2).unwrap()));
24
25    app.run().unwrap();
26}
Source

pub fn scheme(self) -> Scheme

Gets the scheme of the application

Source

pub fn run(self) -> Result<(), FltkError>

Runs the event loop

§Errors

Can error on failure to run the application

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
Hide additional examples
examples/tabs.rs (line 56)
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()
46        .with_size(500, 450)
47        .with_label("Tabs")
48        .center_screen();
49
50    draw_gallery();
51
52    wind.make_resizable(true);
53    wind.end();
54    wind.show();
55
56    app.run().unwrap();
57}
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}
Source

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 30)
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 || loop {
26        app::sleep(1.);
27        s.send(Message::Increment(2));
28    });
29
30    while app.wait() {
31        if let Some(Message::Increment(step)) = r.recv() {
32            inc_frame(&mut frame, &mut val, step)
33        }
34    }
35}
More examples
Hide additional 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().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().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}
Source

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}
Source

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

Source

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

Source

pub fn redraw(self)

Redraws the app

Source

pub fn quit(self)

Quit the application

Examples found in repository?
examples/shapedwindow_taskbar.rs (line 75)
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        .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        .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::OFlatFrame);
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.set_pos(app::event_x_root() - x, app::event_y_root() - y);
68
69                // Changing dock window position so it's close enough to the center of the application (not "visible" to user)
70                dock_win.set_pos(wself.x() + (wself.w() / 2), wself.y() + (wself.w() / 2));
71
72                true
73            }
74            enums::Event::Close => {
75                app.quit();
76
77                true
78            }
79            enums::Event::Hide => {
80                app.quit();
81
82                true
83            }
84            _ => false,
85        }
86    });
87
88    // Make main window appear when "opened" via Alt+Tab or Taskbar
89    dock_win.handle({
90        let mut win = win.clone();
91        move |_wself, event| match event {
92            enums::Event::Focus => {
93                let win_shape = prep_shape(win.w(), win.h());
94
95                win.show();
96                win.set_shape(Some(win_shape));
97
98                true
99            }
100            enums::Event::Hide => {
101                win.hide();
102
103                true
104            }
105            enums::Event::Close => {
106                app.quit();
107
108                true
109            }
110            _ => false,
111        }
112    });
113
114    app.run().unwrap();
115}
More examples
Hide additional examples
examples/editor2.rs (line 440)
375    pub fn launch(&mut self) {
376        while self.app.wait() {
377            use Message::*;
378            if let Some(msg) = self.r.recv() {
379                match msg {
380                    Changed => {
381                        if !self.modified {
382                            self.modified = true;
383                            self.menu.menu.find_item("&File/&Save\t").unwrap().activate();
384                            self.menu.menu.find_item("&File/&Quit\t").unwrap().set_label_color(Color::Red);
385                            let name = match &self.filename {
386                                Some(f) => f.to_string_lossy().to_string(),
387                                None => "(Untitled)".to_string(),
388                            };
389                            self.main_win.set_label(&format!("* {name} - RustyEd"));
390                        }
391                    }
392                    New => {
393                        if self.buf.text() != "" {
394                            let clear = if let Some(x) = dialog::choice2(center().0 - 200, center().1 - 100, "File unsaved, Do you wish to continue?", "&Yes", "&No!", "") {
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(center().0 - 200, center().1 - 100, &format!("An issue occured while loading the file: {e}")),
410                                }
411                            } else {
412                                dialog::alert(center().0 - 200, center().1 - 100, "File does not exist!")
413                            }
414                        }
415                    },
416                    Save => { self.save_file().unwrap(); },
417                    SaveAs => { self.save_file_as().unwrap(); },
418                    Print => {
419                        let mut printer = printer::Printer::default();
420                        if printer.begin_job(0).is_ok() {
421                            let (w, h) = printer.printable_rect();
422                            self.printable.set_size(w - 40, h - 40);
423                            // Needs cleanup
424                            let line_count = self.printable.count_lines(0, self.printable.buffer().unwrap().length(), true) / 45;
425                            for i in 0..=line_count {
426                                self.printable.scroll(45 * i, 0);
427                                printer.begin_page().ok();
428                                printer.print_widget(&self.printable, 20, 20);
429                                printer.end_page().ok();
430                            }
431                            printer.end_job();
432                        }
433                    },
434                    Quit => {
435                        if self.modified {
436                            match dialog::choice2(center().0 - 200, center().1 - 100,
437                                "Would you like to save your work?", "&Yes", "&No", "") {
438                                Some(0) => {
439                                    if self.save_file().unwrap() {
440                                        self.app.quit();
441                                    }
442                                },
443                                Some(1) => { self.app.quit() },
444                                Some(_) | None  => (),
445                            }
446                        } else {
447                            self.app.quit();
448                        }
449                    },
450                    Cut => self.editor.cut(),
451                    Copy => self.editor.copy(),
452                    Paste => self.editor.paste(),
453                    About => dialog::message(center().0 - 300, center().1 - 100, "This is an example application written in Rust and using the FLTK Gui library."),
454                }
455            }
456        }
457    }

Trait Implementations§

Source§

impl Clone for App

Source§

fn clone(&self) -> App

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for App

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for App

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.