egui/egui_demo_lib/src/apps/demo/font_book.rs

105 lines
3.0 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

pub struct FontBook {
standard: bool,
emojis: bool,
filter: String,
text_style: egui::TextStyle,
}
impl Default for FontBook {
fn default() -> Self {
Self {
standard: false,
emojis: true,
filter: Default::default(),
text_style: egui::TextStyle::Button,
}
}
}
impl FontBook {
fn characters_ui(&self, ui: &mut egui::Ui, characters: &[(u32, char, &str)]) {
use egui::{Button, Label};
for &(_, chr, name) in characters {
if self.filter.is_empty()
|| name.contains(&self.filter)
|| self.filter == chr.to_string()
{
let button = Button::new(chr).text_style(self.text_style).frame(false);
let tooltip_ui = |ui: &mut egui::Ui| {
ui.add(Label::new(chr).text_style(self.text_style));
ui.label(format!("{}\nU+{:X}\n\nClick to copy", name, chr as u32));
};
if ui.add(button).on_hover_ui(tooltip_ui).clicked() {
ui.output().copied_text = chr.to_string();
}
}
}
}
}
impl super::Demo for FontBook {
fn name(&self) -> &'static str {
"🔤 Font Book"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
egui::Window::new(self.name()).open(open).show(ctx, |ui| {
use super::View;
self.ui(ui);
});
}
}
impl super::View for FontBook {
fn ui(&mut self, ui: &mut egui::Ui) {
use super::font_contents_emoji::FULL_EMOJI_LIST;
use super::font_contents_ubuntu::UBUNTU_FONT_CHARACTERS;
ui.label(format!(
"egui supports {} standard characters and {} emojis.\nClick on a character to copy it.",
UBUNTU_FONT_CHARACTERS.len(),
FULL_EMOJI_LIST.len(),
));
ui.separator();
egui::combo_box_with_label(ui, "Text style", format!("{:?}", self.text_style), |ui| {
for style in egui::TextStyle::all() {
ui.selectable_value(&mut self.text_style, style, format!("{:?}", style));
}
});
ui.horizontal(|ui| {
ui.label("Show:");
ui.checkbox(&mut self.standard, "Standard");
ui.checkbox(&mut self.emojis, "Emojis");
});
ui.horizontal(|ui| {
ui.label("Filter:");
ui.text_edit_singleline(&mut self.filter);
self.filter = self.filter.to_lowercase();
if ui.button("").clicked() {
self.filter.clear();
}
});
ui.separator();
egui::ScrollArea::auto_sized().show(ui, |ui| {
ui.horizontal_wrapped(|ui| {
ui.spacing_mut().item_spacing = egui::Vec2::splat(2.0);
if self.standard {
self.characters_ui(ui, UBUNTU_FONT_CHARACTERS);
}
if self.emojis {
self.characters_ui(ui, FULL_EMOJI_LIST);
}
});
});
}
}