fltk/app/mod.rs
1use crate::enums::Mode;
2use crate::prelude::*;
3use std::path;
4
5mod channel;
6pub use channel::*;
7mod event;
8pub use event::*;
9mod font;
10pub use font::*;
11mod init;
12pub use init::*;
13mod rt;
14pub use rt::*;
15mod screen;
16pub use screen::*;
17mod state;
18pub use state::*;
19mod version;
20pub use version::*;
21mod visual;
22pub use visual::*;
23mod widget;
24pub use widget::*;
25mod opts;
26pub use opts::*;
27/// Enables the use of the cairo context with Cairo-rs. Requires the cairoext build flag.
28pub mod cairo;
29
30/// Provids bindings to Preferences which provides methods to store user settings between application starts
31pub mod prefs;
32
33/// Basic Application struct, used to instantiate, set the scheme and run the event loop
34#[derive(Debug, Copy, Clone)]
35pub struct App {}
36
37impl Default for App {
38 fn default() -> Self {
39 init_all();
40 App {}
41 }
42}
43
44impl App {
45 /// Sets the scheme of the application
46 pub fn set_scheme(&mut self, scheme: Scheme) {
47 set_scheme(scheme);
48 }
49
50 /// Sets the scheme of the application
51 #[must_use]
52 pub fn with_scheme(self, scheme: Scheme) -> App {
53 set_scheme(scheme);
54 self
55 }
56
57 /// Gets the scheme of the application
58 pub fn scheme(self) -> Scheme {
59 scheme()
60 }
61
62 /// Runs the event loop
63 /// # Errors
64 /// Can error on failure to run the application
65 pub fn run(self) -> Result<(), FltkError> {
66 run()
67 }
68
69 /// Wait for incoming messages.
70 /// Calls to redraw within wait require an explicit sleep
71 pub fn wait(self) -> bool {
72 wait()
73 }
74
75 /// Loads system fonts
76 #[must_use]
77 pub fn load_system_fonts(self) -> Self {
78 *FONTS.lock().unwrap() = get_font_names();
79 self
80 }
81
82 /**
83 Loads a font from a path.
84 On success, returns a String with the ttf Font Family name. The font's index is always 16.
85 As such only one font can be loaded at a time.
86 The font name can be used with [`Font::by_name`](`crate::enums::Font::by_name`), and index with [`Font::by_index`](`crate::enums::Font::by_index`).
87 # Examples
88 ```rust,no_run
89 use fltk::{prelude::*, *};
90 let app = app::App::default();
91 let font = app.load_font("font.ttf").unwrap();
92 let mut frame = frame::Frame::new(0, 0, 400, 100, "Hello");
93 frame.set_label_font(enums::Font::by_name(&font));
94 ```
95 # Errors
96 Returns `ResourceNotFound` if the Font file was not found
97 */
98 pub fn load_font<P: AsRef<path::Path>>(self, path: P) -> Result<String, FltkError> {
99 Self::load_font_(path.as_ref())
100 }
101
102 fn load_font_(path: &path::Path) -> Result<String, FltkError> {
103 if !path.exists() {
104 return Err::<String, FltkError>(FltkError::Internal(FltkErrorKind::ResourceNotFound));
105 }
106 if let Some(p) = path.to_str() {
107 let name = load_font(p)?;
108 Ok(name)
109 } else {
110 Err(FltkError::Internal(FltkErrorKind::ResourceNotFound))
111 }
112 }
113
114 /// Set the visual of the application
115 /// # Errors
116 /// Returns `FailedOperation` if FLTK failed to set the visual mode
117 pub fn set_visual(self, mode: Mode) -> Result<(), FltkError> {
118 set_visual(mode)
119 }
120
121 /// Redraws the app
122 pub fn redraw(self) {
123 redraw();
124 }
125
126 /// Quit the application
127 pub fn quit(self) {
128 quit();
129 }
130}