1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 05:10:03 -04:00

Color emojis 🦀

This commit is contained in:
lucasmerlin
2025-07-10 20:34:45 +02:00
parent fdcaff8465
commit d808ca8f9a
2 changed files with 84 additions and 16 deletions

View File

@@ -2,6 +2,10 @@
#![allow(rustdoc::missing_crate_level_docs)] // it's an example
use eframe::egui;
use eframe::egui::{
Color32, ColorImage, FontFamily, FontId, FontSelection, RichText, TextEdit,
global_theme_preference_switch, include_image,
};
fn main() -> eframe::Result {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
@@ -26,10 +30,14 @@ struct MyApp {
age: u32,
}
fn font_id() -> FontId {
FontId::new(30.0, FontFamily::Proportional)
}
impl Default for MyApp {
fn default() -> Self {
Self {
name: "Arthur".to_owned(),
name: "Ferris :crab:".to_owned(),
age: 42,
}
}
@@ -38,21 +46,28 @@ impl Default for MyApp {
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("My egui Application");
ui.horizontal(|ui| {
let name_label = ui.label("Your name: ");
ui.text_edit_singleline(&mut self.name)
.labelled_by(name_label.id);
});
ui.add(egui::Slider::new(&mut self.age, 0..=120).text("age"));
if ui.button("Increment").clicked() {
self.age += 1;
}
ui.label(format!("Hello '{}', age {}", self.name, self.age));
global_theme_preference_switch(ui);
ui.image(egui::include_image!(
"../../../crates/egui/assets/ferris.png"
));
ctx.fonts(|f| {
let mut fonts = f.lock();
let font = fonts.fonts.font(&font_id());
if !font.has_glyph('🦀') {
let image = include_bytes!("../../../crates/egui/assets/ferris.png");
let image = egui_extras::image::load_image_bytes(image).unwrap();
font.allocate_custom_glyph('🦀', image);
}
});
TextEdit::singleline(&mut self.name)
.font(FontSelection::FontId(font_id()))
.text_color(Color32::WHITE)
.show(ui);
self.name = self.name.replace(":crab:", "🦀");
ui.label(RichText::new(&self.name).font(font_id()).strong());
});
}
}