1use std::sync::Arc;
7
8use waft_embedder::valo::Path;
9use waft_embedder::{
10 Canvas, Color, FillRule, FontStyle, FontWeight, Paint, Size, TextAlign, TextDirection,
11};
12use waft_foundation::App;
13use waft_painting::{PaintingBinding, TextScaler, TextStyle};
14use waft_rendering::CustomPainter;
15use waft_widgets::*;
16
17pub const ICON_FONT_SIZE: f64 = 20.0;
20
21const DEFAULT_ICON_FOREGROUND: Color = Color::from_argb(255, 0, 0, 0);
24
25pub const FLUENT_ICON_FONT_FAMILY: &str = "FluentSystemIcons-Regular";
27
28pub fn install_icon_font(app: &mut App) {
30 PaintingBinding::instance(app).register_font(
31 app,
32 FLUENT_ICON_FONT_FAMILY,
33 include_bytes!("../../assets/fluent-system-icons/FluentSystemIcons-Regular.ttf").to_vec(),
34 );
35}
36
37#[derive(Clone, Copy, Debug, PartialEq, Eq)]
40pub enum FluentSymbol {
41 MenuList,
43 Checkmark,
45 Info,
47 CheckmarkCircle,
49 DismissCircle,
51 Warning,
53 ErrorCircle,
55 Timer,
57 ChevronDownUp,
59
60 CursorClick,
62 ToggleLeft,
64 CheckboxChecked,
66 CheckboxIndeterminate,
68 RadioButton,
70 Grid,
72 Options,
74 PanelLeft,
76 Tab,
78 Tabs,
80 TooltipQuote,
82 Layer,
84 Link,
86 ArrowRepeatAll,
88 TextFont,
90 Apps,
92 WeatherMoon,
94 WeatherSunny,
96 Add,
98 Dismiss,
100 ChevronLeft,
102 ChevronRight,
104 ChevronDown,
106 ChevronUp,
108 ChevronUpDown,
110 Navigation,
112 Search,
114 Back,
116 Settings,
118 More,
120 Eye,
122 TextField,
124 LockClosed,
126 Circle,
128 Color,
130 TextBulletListTree,
132 SpinnerIos,
134 Document,
136 List,
138 Table,
140 Toolbox,
142 Camera,
144 Mic,
146 Folder,
148 Globe,
150 Shield,
152 Clipboard,
154 Alert,
156 WindowConsole,
158 PlugConnected,
160 Live,
162 Question,
164 Desktop,
166 Pulse,
168 Play,
170 Stop,
172}
173
174impl FluentSymbol {
175 pub fn glyph(self) -> char {
177 char::from_u32(match self {
178 Self::MenuList => 60642,
179 Self::Checkmark => 62099,
180 Self::Info => 62626,
181 Self::CheckmarkCircle => 62103,
182 Self::DismissCircle => 62316,
183 Self::Warning => 63592,
184 Self::ErrorCircle => 62448,
185 Self::Timer => 60808,
186 Self::ChevronDownUp => 983404,
187 Self::CursorClick => 58437,
188 Self::ToggleLeft => 60816,
189 Self::CheckboxChecked => 62093,
190 Self::CheckboxIndeterminate => 58110,
191 Self::RadioButton => 63044,
192 Self::Grid => 62562,
193 Self::Options => 62855,
194 Self::PanelLeft => 59568,
195 Self::Tab => 63300,
196 Self::Tabs => 60527,
197 Self::TooltipQuote => 63419,
198 Self::Layer => 62667,
199 Self::Link => 62692,
200 Self::ArrowRepeatAll => 61809,
201 Self::TextFont => 63460,
202 Self::Apps => 61747,
203 Self::WeatherMoon => 63613,
204 Self::WeatherSunny => 63649,
205 Self::Add => 61704,
206 Self::Dismiss => 62312,
207 Self::ChevronLeft => 62121,
208 Self::ChevronRight => 62127,
209 Self::ChevronDown => 62114,
210 Self::ChevronUp => 62134,
211 Self::ChevronUpDown => 58158,
212 Self::Search => 63119,
213 Self::Navigation => 62816,
214 Self::Back => 61787,
215 Self::Settings => 63145,
216 Self::More => 59428,
217 Self::Eye => 58866,
218 Self::TextField => 63455,
219 Self::LockClosed => 59279,
220 Self::Circle => 62140,
221 Self::Color => 62197,
222 Self::TextBulletListTree => 63406,
223 Self::SpinnerIos => 63241,
224 Self::Document => 62329,
225 Self::List => 62702,
226 Self::Table => 63326,
227 Self::Toolbox => 63534,
228 Self::Camera => 62036,
229 Self::Mic => 59385,
230 Self::Folder => 62488,
231 Self::Globe => 62554,
232 Self::Shield => 63166,
233 Self::Clipboard => 62153,
234 Self::Alert => 61716,
235 Self::WindowConsole => 61039,
236 Self::PlugConnected => 59801,
237 Self::Live => 62704,
238 Self::Question => 63032,
239 Self::Desktop => 62297,
240 Self::Pulse => 59868,
241 Self::Play => 62981,
242 Self::Stop => 63274,
243 })
244 .unwrap()
245 }
246}
247
248#[derive(Clone, Debug)]
252pub struct FontIcon {
253 pub glyph: String,
255 pub font_family: String,
257 pub font_size: f64,
259 pub font_weight: FontWeight,
261 pub font_style: FontStyle,
263 pub mirrored_when_right_to_left: bool,
266 pub foreground: Option<Color>,
268 pub key: Option<KeyRef>,
270}
271
272impl FontIcon {
273 pub fn new(glyph: impl Into<String>) -> Self {
275 Self {
276 glyph: glyph.into(),
277 font_family: FLUENT_ICON_FONT_FAMILY.to_string(),
278 font_size: ICON_FONT_SIZE,
279 font_weight: FontWeight::NORMAL,
280 font_style: FontStyle::Normal,
281 mirrored_when_right_to_left: false,
282 foreground: None,
283 key: None,
284 }
285 }
286
287 pub fn symbol(symbol: FluentSymbol) -> Self {
289 Self::new(symbol.glyph().to_string())
290 }
291
292 pub fn font_family(mut self, value: impl Into<String>) -> Self {
294 self.font_family = value.into();
295 self
296 }
297
298 pub fn font_size(mut self, value: f64) -> Self {
300 self.font_size = value;
301 self
302 }
303
304 pub fn font_weight(mut self, value: FontWeight) -> Self {
306 self.font_weight = value;
307 self
308 }
309
310 pub fn font_style(mut self, value: FontStyle) -> Self {
312 self.font_style = value;
313 self
314 }
315
316 pub fn mirrored_when_right_to_left(mut self, value: bool) -> Self {
318 self.mirrored_when_right_to_left = value;
319 self
320 }
321
322 pub fn foreground(mut self, value: Color) -> Self {
324 self.foreground = Some(value);
325 self
326 }
327
328 pub fn key(mut self, value: KeyRef) -> Self {
330 self.key = Some(value);
331 self
332 }
333}
334
335impl StatelessWidget for FontIcon {
336 fn key(&self) -> Option<&KeyRef> {
337 self.key.as_ref()
338 }
339
340 fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
341 let mut style = TextStyle::new()
342 .font_family(self.font_family.clone())
343 .font_size(self.font_size)
344 .font_weight(self.font_weight)
345 .font_style(self.font_style);
346 if let Some(color) = self.foreground {
347 style = style.color(color);
348 }
349 let glyph = Text::new(self.glyph.clone())
351 .style(style)
352 .text_align(TextAlign::Center)
353 .text_scaler(TextScaler::NO_SCALING);
354 let glyph = Center::new()
355 .width_factor(1.0)
356 .height_factor(1.0)
357 .child(glyph);
358 if self.mirrored_when_right_to_left
359 && Directionality::of(app, context) == TextDirection::Rtl
360 {
361 Transform::scale(None, Some(-1.0), Some(1.0))
362 .child(glyph)
363 .into_widget()
364 } else {
365 glyph.into_widget()
366 }
367 }
368}
369
370#[derive(Clone, Debug)]
374pub struct PathIcon {
375 pub data: Arc<Path>,
377 pub fill_rule: FillRule,
380 pub foreground: Option<Color>,
382 pub key: Option<KeyRef>,
384}
385
386impl PathIcon {
387 pub fn new(data: impl Into<Arc<Path>>) -> Self {
389 Self {
390 data: data.into(),
391 fill_rule: FillRule::EvenOdd,
392 foreground: None,
393 key: None,
394 }
395 }
396
397 pub fn fill_rule(mut self, value: FillRule) -> Self {
399 self.fill_rule = value;
400 self
401 }
402
403 pub fn foreground(mut self, value: Color) -> Self {
405 self.foreground = Some(value);
406 self
407 }
408
409 pub fn key(mut self, value: KeyRef) -> Self {
411 self.key = Some(value);
412 self
413 }
414}
415
416impl StatelessWidget for PathIcon {
417 fn key(&self) -> Option<&KeyRef> {
418 self.key.as_ref()
419 }
420
421 fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
422 let fill = self.foreground.unwrap_or_else(|| {
423 DefaultTextStyle::of(app, context)
424 .style
425 .color
426 .map_or(DEFAULT_ICON_FOREGROUND, |color| color.color())
427 });
428 let bounds = self.data.tight_bounds();
430 let size = Size::new(
431 f64::from(bounds.x + bounds.width).max(0.0),
432 f64::from(bounds.y + bounds.height).max(0.0),
433 );
434 CustomPaint::new()
435 .size(size)
436 .painter(PathIconPainter {
437 data: self.data.clone(),
438 fill_rule: self.fill_rule,
439 fill,
440 })
441 .into_widget()
442 }
443}
444
445#[derive(Clone, Debug)]
447struct PathIconPainter {
448 data: Arc<Path>,
449 fill_rule: FillRule,
450 fill: Color,
451}
452
453impl CustomPainter for PathIconPainter {
454 fn paint(&self, _app: &mut App, canvas: &mut Canvas, _size: Size) {
455 canvas.draw_path(
456 &self.data,
457 self.fill_rule,
458 &Paint::from_color(self.fill.into()),
459 );
460 }
461
462 fn should_repaint(&self, _app: &App, old_delegate: &dyn CustomPainter) -> bool {
463 match old_delegate.as_any().downcast_ref::<Self>() {
464 Some(old) => {
465 old.fill != self.fill
466 || old.fill_rule != self.fill_rule
467 || !Arc::ptr_eq(&old.data, &self.data)
468 && !old.data.elements().eq(self.data.elements())
469 }
470 None => true,
471 }
472 }
473
474 fn as_any(&self) -> &dyn std::any::Any {
475 self
476 }
477}
478
479#[cfg(test)]
480mod tests {
481 use super::FluentSymbol;
482
483 #[test]
485 fn the_inspectors_symbols_are_the_fonts_named_glyphs() {
486 let codepoints = include_str!("../../assets/fluent-system-icons/codepoints.json");
487 for (symbol, name) in [
488 (FluentSymbol::Camera, "ic_fluent_camera_20_regular"),
489 (FluentSymbol::Mic, "ic_fluent_mic_20_regular"),
490 (FluentSymbol::Folder, "ic_fluent_folder_20_regular"),
491 (FluentSymbol::Globe, "ic_fluent_globe_20_regular"),
492 (FluentSymbol::Shield, "ic_fluent_shield_20_regular"),
493 (FluentSymbol::Clipboard, "ic_fluent_clipboard_20_regular"),
494 (FluentSymbol::Alert, "ic_fluent_alert_20_regular"),
495 (
496 FluentSymbol::WindowConsole,
497 "ic_fluent_window_console_20_regular",
498 ),
499 (
500 FluentSymbol::PlugConnected,
501 "ic_fluent_plug_connected_20_regular",
502 ),
503 (FluentSymbol::Live, "ic_fluent_live_20_regular"),
504 (FluentSymbol::Question, "ic_fluent_question_20_regular"),
505 (FluentSymbol::Desktop, "ic_fluent_desktop_20_regular"),
506 (FluentSymbol::Pulse, "ic_fluent_pulse_20_regular"),
507 (FluentSymbol::Play, "ic_fluent_play_20_regular"),
508 (FluentSymbol::Stop, "ic_fluent_stop_20_regular"),
509 ] {
510 let entry = format!("\"{name}\": {}", symbol.glyph() as u32);
511 assert!(codepoints.contains(&entry), "{symbol:?} is {name}");
512 }
513 }
514}