1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
use crate::*;

pub struct FontBook {
    standard: bool,
    emojis: bool,
    filter: String,
    text_style: TextStyle,
}

impl Default for FontBook {
    fn default() -> Self {
        Self {
            standard: false,
            emojis: true,
            filter: Default::default(),
            text_style: TextStyle::Heading,
        }
    }
}

impl FontBook {
    fn characters_ui(&self, ui: &mut Ui, characters: &[(u32, char, &str)]) {
        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 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 demos::Demo for FontBook {
    fn name(&self) -> &str {
        "🔤 Font Book"
    }

    fn show(&mut self, ctx: &std::sync::Arc<crate::Context>, open: &mut bool) {
        Window::new(self.name()).open(open).show(ctx, |ui| {
            use demos::View;
            self.ui(ui);
        });
    }
}

impl demos::View for FontBook {
    fn ui(&mut self, ui: &mut Ui) {
        use crate::demos::font_contents_emoji::FULL_EMOJI_LIST;
        use crate::demos::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();

        ui.horizontal(|ui| {
            ui.label("Text style:");
            for style in TextStyle::all() {
                ui.radio_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("x").clicked {
                self.filter.clear();
            }
        });

        ui.separator();

        crate::ScrollArea::auto_sized().show(ui, |ui| {
            ui.horizontal_wrapped(|ui| {
                ui.style_mut().spacing.item_spacing = Vec2::splat(2.0);

                if self.standard {
                    self.characters_ui(ui, UBUNTU_FONT_CHARACTERS);
                }
                if self.emojis {
                    self.characters_ui(ui, FULL_EMOJI_LIST);
                }
            });
        });
    }
}