-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eframe app creation refactor (#1363)
* Change how eframe apps are created * eframe: re-export epi::* so users don't need to care about what epi is
- Loading branch information
Showing
26 changed files
with
387 additions
and
444 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,61 @@ | ||
use eframe::{egui, epi}; | ||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release | ||
|
||
use eframe::egui; | ||
|
||
fn main() { | ||
let options = eframe::NativeOptions::default(); | ||
eframe::run_native("egui example: custom font", options, |cc| { | ||
Box::new(MyApp::new(cc)) | ||
}); | ||
} | ||
|
||
fn setup_custom_fonts(ctx: &egui::Context) { | ||
// Start with the default fonts (we will be adding to them rather than replacing them). | ||
let mut fonts = egui::FontDefinitions::default(); | ||
|
||
// Install my own font (maybe supporting non-latin characters). | ||
// .ttf and .otf files supported. | ||
fonts.font_data.insert( | ||
"my_font".to_owned(), | ||
egui::FontData::from_static(include_bytes!("../../epaint/fonts/Hack-Regular.ttf")), | ||
); | ||
|
||
// Put my font first (highest priority) for proportional text: | ||
fonts | ||
.families | ||
.entry(egui::FontFamily::Proportional) | ||
.or_default() | ||
.insert(0, "my_font".to_owned()); | ||
|
||
// Put my font as last fallback for monospace: | ||
fonts | ||
.families | ||
.entry(egui::FontFamily::Monospace) | ||
.or_default() | ||
.push("my_font".to_owned()); | ||
|
||
// Tell egui to use these fonts: | ||
ctx.set_fonts(fonts); | ||
} | ||
|
||
struct MyApp { | ||
text: String, | ||
} | ||
|
||
impl Default for MyApp { | ||
fn default() -> Self { | ||
impl MyApp { | ||
fn new(cc: &eframe::CreationContext<'_>) -> Self { | ||
setup_custom_fonts(&cc.egui_ctx); | ||
Self { | ||
text: "Edit this text field if you want".to_owned(), | ||
} | ||
} | ||
} | ||
|
||
impl epi::App for MyApp { | ||
fn name(&self) -> &str { | ||
"egui example: custom font" | ||
} | ||
|
||
fn setup( | ||
&mut self, | ||
ctx: &egui::Context, | ||
_frame: &epi::Frame, | ||
_storage: Option<&dyn epi::Storage>, | ||
_gl: &std::rc::Rc<epi::glow::Context>, | ||
) { | ||
// Start with the default fonts (we will be adding to them rather than replacing them). | ||
let mut fonts = egui::FontDefinitions::default(); | ||
|
||
// Install my own font (maybe supporting non-latin characters). | ||
// .ttf and .otf files supported. | ||
fonts.font_data.insert( | ||
"my_font".to_owned(), | ||
egui::FontData::from_static(include_bytes!("../../epaint/fonts/Hack-Regular.ttf")), | ||
); | ||
|
||
// Put my font first (highest priority) for proportional text: | ||
fonts | ||
.families | ||
.entry(egui::FontFamily::Proportional) | ||
.or_default() | ||
.insert(0, "my_font".to_owned()); | ||
|
||
// Put my font as last fallback for monospace: | ||
fonts | ||
.families | ||
.entry(egui::FontFamily::Monospace) | ||
.or_default() | ||
.push("my_font".to_owned()); | ||
|
||
// Tell egui to use these fonts: | ||
ctx.set_fonts(fonts); | ||
} | ||
|
||
fn update(&mut self, ctx: &egui::Context, _frame: &epi::Frame) { | ||
impl eframe::App for MyApp { | ||
fn update(&mut self, ctx: &egui::Context, _frame: &eframe::Frame) { | ||
egui::CentralPanel::default().show(ctx, |ui| { | ||
ui.heading("egui using custom fonts"); | ||
ui.text_edit_multiline(&mut self.text); | ||
}); | ||
} | ||
} | ||
|
||
fn main() { | ||
let options = eframe::NativeOptions::default(); | ||
eframe::run_native(Box::new(MyApp::default()), options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.