egui_pop_core/kit/
glyph.rs1use crate::icon::Icon;
8use crate::theme::Tokens;
9
10macro_rules! glyphs {
11 ($($(#[$doc:meta])* $role:ident: $name:literal => $default:ident,)*) => {
12 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
17 pub enum Glyph {
18 $($(#[$doc])* $role,)*
19 }
20
21 impl Glyph {
22 pub const ALL: &'static [Glyph] = &[$(Glyph::$role,)*];
24
25 pub fn name(self) -> &'static str {
27 match self {
28 $(Glyph::$role => $name,)*
29 }
30 }
31
32 pub fn default_icon(self) -> Icon {
34 match self {
35 $(Glyph::$role => Icon::$default,)*
36 }
37 }
38 }
39 };
40}
41
42glyphs! {
43 DisclosureOpen: "disclosure-open" => TriangleDown,
45 DisclosureClosed: "disclosure-closed" => TriangleRight,
47 SortAscending: "sort-ascending" => TriangleUp,
49 SortDescending: "sort-descending" => TriangleDown,
51 Check: "check" => Check,
53 Radio: "radio" => Dot,
55 Indeterminate: "indeterminate" => Minus,
57 Bullet: "bullet" => Dot,
59 Submenu: "submenu" => ChevronRight,
61 ChevronStart: "chevron-start" => ChevronLeft,
63 ChevronEnd: "chevron-end" => ChevronRight,
65 Close: "close" => Close,
67 More: "more" => More,
69 DragHandle: "drag-handle" => Grip,
71 Dropdown: "dropdown" => TriangleDown,
73 Expand: "expand" => Plus,
75 Collapse: "collapse" => Minus,
77 WindowMinimize: "window-minimize" => WindowMinimize,
79 WindowMaximize: "window-maximize" => WindowMaximize,
81 WindowRestore: "window-restore" => WindowRestore,
83 WindowClose: "window-close" => WindowClose,
85 WindowExitFullscreen: "window-exit-fullscreen" => ExitFullscreen,
88 LedOff: "led-off" => Circle,
90 LedOn: "led-on" => Disc,
92 LedBusy: "led-busy" => Disc,
94 LedWarning: "led-warning" => Triangle,
96 LedError: "led-error" => Square,
98 FlagModified: "flag-modified" => Corner,
100 FlagPinned: "flag-pinned" => Diamond,
102 FlagRequired: "flag-required" => Asterisk,
104 FlagInvalid: "flag-invalid" => Triangle,
106}
107
108#[derive(Clone, Debug, PartialEq)]
111pub struct Glyphs(Vec<Icon>);
112
113impl Default for Glyphs {
114 fn default() -> Self {
116 Glyphs(Glyph::ALL.iter().map(|g| g.default_icon()).collect())
117 }
118}
119
120impl Glyphs {
121 pub(super) fn compile(t: &Tokens, problems: &mut Vec<String>) -> Glyphs {
122 let mut glyphs = Glyphs::read(t, problems);
123 glyphs.keep_shapes_distinct(problems);
124 glyphs
125 }
126
127 fn keep_shapes_distinct(&mut self, problems: &mut Vec<String>) {
131 let role = |name: &str| Glyph::ALL.iter().copied().find(|g| g.name() == name);
132 loop {
133 let clash = egui_pop_tokens::kit::DISTINCT_GLYPHS
134 .iter()
135 .filter_map(|(a, b)| Some((role(a)?, role(b)?)))
136 .find(|(a, b)| {
137 self.get(*a) == self.get(*b)
138 && (self.get(*a) != a.default_icon() || self.get(*b) != b.default_icon())
139 });
140 let Some((a, b)) = clash else { break };
141 problems.push(format!(
142 "glyph.{} and glyph.{} both draw `{}`: their states would differ by colour alone; `{}` and `{}` are drawn",
143 a.name(),
144 b.name(),
145 self.get(a).name(),
146 a.default_icon().name(),
147 b.default_icon().name()
148 ));
149 self.0[a as usize] = a.default_icon();
150 self.0[b as usize] = b.default_icon();
151 }
152 }
153
154 fn read(t: &Tokens, problems: &mut Vec<String>) -> Glyphs {
155 Glyphs(
156 Glyph::ALL
157 .iter()
158 .map(|g| {
159 let path = format!("glyph.{}", g.name());
160 match super::string(t, &path) {
161 None => g.default_icon(),
162 Some(name) => Icon::by_name(&name).unwrap_or_else(|| {
163 problems.push(format!(
164 "{path}: no icon named `{name}`, `{}` is drawn",
165 g.default_icon().name()
166 ));
167 g.default_icon()
168 }),
169 }
170 })
171 .collect(),
172 )
173 }
174
175 pub fn get(&self, glyph: Glyph) -> Icon {
177 self.0[glyph as usize]
178 }
179}