mirror of
https://github.com/emilk/egui.git
synced 2026-06-26 22:53:14 -04:00
Move examples out of eframe/examples into examples/ (#1486)
* Move examples out of eframe/examples into examples/ Give each example a `Cargo.toml` and `src/main.rs`. This makes it easier for people to use as templates. * Update README.md with more deps needed on vanilla Ubuntu * Install libgtk-3-dev on CI, hoping that will fix something
This commit is contained in:
12
examples/custom_font/Cargo.toml
Normal file
12
examples/custom_font/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "custom_font"
|
||||
version = "0.1.0"
|
||||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.60"
|
||||
publish = false
|
||||
|
||||
|
||||
[dependencies]
|
||||
eframe = { path = "../../eframe" }
|
||||
3
examples/custom_font/README.md
Normal file
3
examples/custom_font/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
```sh
|
||||
cargo run -p custom_font
|
||||
```
|
||||
63
examples/custom_font/src/main.rs
Normal file
63
examples/custom_font/src/main.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
#![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,
|
||||
Box::new(|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 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 eframe::App for MyApp {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.heading("egui using custom fonts");
|
||||
ui.text_edit_multiline(&mut self.text);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user