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

120 lines
3.6 KiB
Rust
Raw Normal View History

2020-12-12 18:53:04 +00:00
pub struct FontBook {
standard: bool,
emojis: bool,
filter: String,
text_style: egui::TextStyle,
2020-12-12 18:53:04 +00:00
}
impl Default for FontBook {
fn default() -> Self {
Self {
standard: false,
emojis: true,
filter: Default::default(),
text_style: egui::TextStyle::Button,
2020-12-12 18:53:04 +00:00
}
}
}
impl FontBook {
fn characters_ui(&self, ui: &mut egui::Ui, characters: &[(u32, char, &str)]) {
use egui::{Button, Label};
2020-12-12 18:53:04 +00:00
for &(_, chr, name) in characters {
2020-12-13 19:37:44 +00:00
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);
2020-12-12 18:53:04 +00:00
let tooltip_ui = |ui: &mut egui::Ui| {
2020-12-13 19:37:44 +00:00
ui.add(Label::new(chr).text_style(self.text_style));
ui.label(format!("{}\nU+{:X}\n\nClick to copy", name, chr as u32));
};
2020-12-12 18:53:04 +00:00
if ui.add(button).on_hover_ui(tooltip_ui).clicked() {
2020-12-13 19:37:44 +00:00
ui.output().copied_text = chr.to_string();
}
2020-12-12 18:53:04 +00:00
}
}
}
}
2021-01-01 16:11:05 +00:00
impl super::Demo for FontBook {
fn name(&self) -> &'static str {
2020-12-12 18:53:04 +00:00
"🔤 Font Book"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
egui::Window::new(self.name()).open(open).show(ctx, |ui| {
2021-01-01 16:11:05 +00:00
use super::View;
2020-12-12 18:53:04 +00:00
self.ui(ui);
});
}
}
2021-01-01 16:11:05 +00:00
impl super::View for FontBook {
fn ui(&mut self, ui: &mut egui::Ui) {
2021-01-01 16:11:05 +00:00
use super::font_contents_emoji::FULL_EMOJI_LIST;
use super::font_contents_ubuntu::UBUNTU_FONT_CHARACTERS;
2020-12-12 18:53:04 +00:00
ui.label(format!(
"The default egui fonts supports {} standard characters and {} emojis.",
2020-12-12 18:53:04 +00:00
UBUNTU_FONT_CHARACTERS.len(),
FULL_EMOJI_LIST.len(),
));
ui.horizontal_wrapped(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
ui.label("You can add more characters by installing additional fonts with ");
ui.add(
egui::Hyperlink::from_label_and_url(
"Context::set_fonts",
"https://docs.rs/egui/latest/egui/struct.Context.html#method.set_fonts",
)
.text_style(egui::TextStyle::Monospace),
);
ui.label(".");
});
2020-12-12 18:53:04 +00:00
ui.separator();
egui::ComboBox::from_label("Text style")
.selected_text(format!("{:?}", self.text_style))
.show_ui(ui, |ui| {
for style in egui::TextStyle::all() {
ui.selectable_value(&mut self.text_style, style, format!("{:?}", style));
}
});
2020-12-12 18:53:04 +00:00
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() {
2020-12-12 18:53:04 +00:00
self.filter.clear();
}
});
ui.separator();
egui::ScrollArea::auto_sized().show(ui, |ui| {
2020-12-12 18:53:04 +00:00
ui.horizontal_wrapped(|ui| {
ui.spacing_mut().item_spacing = egui::Vec2::splat(2.0);
2020-12-12 18:53:04 +00:00
if self.standard {
self.characters_ui(ui, UBUNTU_FONT_CHARACTERS);
}
if self.emojis {
self.characters_ui(ui, FULL_EMOJI_LIST);
}
});
});
}
}