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