use std::sync::Arc;
use waft_embedder::valo::Path;
use waft_embedder::{
Canvas, Color, FillRule, FontStyle, FontWeight, Paint, Size, TextAlign, TextDirection,
};
use waft_foundation::App;
use waft_painting::{PaintingBinding, TextScaler, TextStyle};
use waft_rendering::CustomPainter;
use waft_widgets::*;
pub const ICON_FONT_SIZE: f64 = 20.0;
const DEFAULT_ICON_FOREGROUND: Color = Color::from_argb(255, 0, 0, 0);
pub const FLUENT_ICON_FONT_FAMILY: &str = "FluentSystemIcons-Regular";
pub fn install_icon_font(app: &mut App) {
PaintingBinding::instance(app).register_font(
app,
FLUENT_ICON_FONT_FAMILY,
include_bytes!("../../assets/fluent-system-icons/FluentSystemIcons-Regular.ttf").to_vec(),
);
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FluentSymbol {
MenuList,
Checkmark,
Info,
CheckmarkCircle,
DismissCircle,
Warning,
ErrorCircle,
Timer,
ChevronDownUp,
CursorClick,
ToggleLeft,
CheckboxChecked,
CheckboxIndeterminate,
RadioButton,
Grid,
Options,
PanelLeft,
Tab,
Tabs,
TooltipQuote,
Layer,
Link,
ArrowRepeatAll,
TextFont,
Apps,
WeatherMoon,
WeatherSunny,
Add,
Dismiss,
ChevronLeft,
ChevronRight,
ChevronDown,
ChevronUp,
ChevronUpDown,
Navigation,
Search,
Back,
Settings,
More,
Eye,
TextField,
LockClosed,
Circle,
Color,
TextBulletListTree,
SpinnerIos,
Document,
List,
Table,
Toolbox,
Camera,
Mic,
Folder,
Globe,
Shield,
Clipboard,
Alert,
WindowConsole,
PlugConnected,
Live,
Question,
Desktop,
Pulse,
Play,
Stop,
}
impl FluentSymbol {
pub fn glyph(self) -> char {
char::from_u32(match self {
Self::MenuList => 60642,
Self::Checkmark => 62099,
Self::Info => 62626,
Self::CheckmarkCircle => 62103,
Self::DismissCircle => 62316,
Self::Warning => 63592,
Self::ErrorCircle => 62448,
Self::Timer => 60808,
Self::ChevronDownUp => 983404,
Self::CursorClick => 58437,
Self::ToggleLeft => 60816,
Self::CheckboxChecked => 62093,
Self::CheckboxIndeterminate => 58110,
Self::RadioButton => 63044,
Self::Grid => 62562,
Self::Options => 62855,
Self::PanelLeft => 59568,
Self::Tab => 63300,
Self::Tabs => 60527,
Self::TooltipQuote => 63419,
Self::Layer => 62667,
Self::Link => 62692,
Self::ArrowRepeatAll => 61809,
Self::TextFont => 63460,
Self::Apps => 61747,
Self::WeatherMoon => 63613,
Self::WeatherSunny => 63649,
Self::Add => 61704,
Self::Dismiss => 62312,
Self::ChevronLeft => 62121,
Self::ChevronRight => 62127,
Self::ChevronDown => 62114,
Self::ChevronUp => 62134,
Self::ChevronUpDown => 58158,
Self::Search => 63119,
Self::Navigation => 62816,
Self::Back => 61787,
Self::Settings => 63145,
Self::More => 59428,
Self::Eye => 58866,
Self::TextField => 63455,
Self::LockClosed => 59279,
Self::Circle => 62140,
Self::Color => 62197,
Self::TextBulletListTree => 63406,
Self::SpinnerIos => 63241,
Self::Document => 62329,
Self::List => 62702,
Self::Table => 63326,
Self::Toolbox => 63534,
Self::Camera => 62036,
Self::Mic => 59385,
Self::Folder => 62488,
Self::Globe => 62554,
Self::Shield => 63166,
Self::Clipboard => 62153,
Self::Alert => 61716,
Self::WindowConsole => 61039,
Self::PlugConnected => 59801,
Self::Live => 62704,
Self::Question => 63032,
Self::Desktop => 62297,
Self::Pulse => 59868,
Self::Play => 62981,
Self::Stop => 63274,
})
.unwrap()
}
}
#[derive(Clone, Debug)]
pub struct FontIcon {
pub glyph: String,
pub font_family: String,
pub font_size: f64,
pub font_weight: FontWeight,
pub font_style: FontStyle,
pub mirrored_when_right_to_left: bool,
pub foreground: Option<Color>,
pub key: Option<KeyRef>,
}
impl FontIcon {
pub fn new(glyph: impl Into<String>) -> Self {
Self {
glyph: glyph.into(),
font_family: FLUENT_ICON_FONT_FAMILY.to_string(),
font_size: ICON_FONT_SIZE,
font_weight: FontWeight::NORMAL,
font_style: FontStyle::Normal,
mirrored_when_right_to_left: false,
foreground: None,
key: None,
}
}
pub fn symbol(symbol: FluentSymbol) -> Self {
Self::new(symbol.glyph().to_string())
}
pub fn font_family(mut self, value: impl Into<String>) -> Self {
self.font_family = value.into();
self
}
pub fn font_size(mut self, value: f64) -> Self {
self.font_size = value;
self
}
pub fn font_weight(mut self, value: FontWeight) -> Self {
self.font_weight = value;
self
}
pub fn font_style(mut self, value: FontStyle) -> Self {
self.font_style = value;
self
}
pub fn mirrored_when_right_to_left(mut self, value: bool) -> Self {
self.mirrored_when_right_to_left = value;
self
}
pub fn foreground(mut self, value: Color) -> Self {
self.foreground = Some(value);
self
}
pub fn key(mut self, value: KeyRef) -> Self {
self.key = Some(value);
self
}
}
impl StatelessWidget for FontIcon {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
let mut style = TextStyle::new()
.font_family(self.font_family.clone())
.font_size(self.font_size)
.font_weight(self.font_weight)
.font_style(self.font_style);
if let Some(color) = self.foreground {
style = style.color(color);
}
let glyph = Text::new(self.glyph.clone())
.style(style)
.text_align(TextAlign::Center)
.text_scaler(TextScaler::NO_SCALING);
let glyph = Center::new()
.width_factor(1.0)
.height_factor(1.0)
.child(glyph);
if self.mirrored_when_right_to_left
&& Directionality::of(app, context) == TextDirection::Rtl
{
Transform::scale(None, Some(-1.0), Some(1.0))
.child(glyph)
.into_widget()
} else {
glyph.into_widget()
}
}
}
#[derive(Clone, Debug)]
pub struct PathIcon {
pub data: Arc<Path>,
pub fill_rule: FillRule,
pub foreground: Option<Color>,
pub key: Option<KeyRef>,
}
impl PathIcon {
pub fn new(data: impl Into<Arc<Path>>) -> Self {
Self {
data: data.into(),
fill_rule: FillRule::EvenOdd,
foreground: None,
key: None,
}
}
pub fn fill_rule(mut self, value: FillRule) -> Self {
self.fill_rule = value;
self
}
pub fn foreground(mut self, value: Color) -> Self {
self.foreground = Some(value);
self
}
pub fn key(mut self, value: KeyRef) -> Self {
self.key = Some(value);
self
}
}
impl StatelessWidget for PathIcon {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
let fill = self.foreground.unwrap_or_else(|| {
DefaultTextStyle::of(app, context)
.style
.color
.map_or(DEFAULT_ICON_FOREGROUND, |color| color.color())
});
let bounds = self.data.tight_bounds();
let size = Size::new(
f64::from(bounds.x + bounds.width).max(0.0),
f64::from(bounds.y + bounds.height).max(0.0),
);
CustomPaint::new()
.size(size)
.painter(PathIconPainter {
data: self.data.clone(),
fill_rule: self.fill_rule,
fill,
})
.into_widget()
}
}
#[derive(Clone, Debug)]
struct PathIconPainter {
data: Arc<Path>,
fill_rule: FillRule,
fill: Color,
}
impl CustomPainter for PathIconPainter {
fn paint(&self, _app: &mut App, canvas: &mut Canvas, _size: Size) {
canvas.draw_path(
&self.data,
self.fill_rule,
&Paint::from_color(self.fill.into()),
);
}
fn should_repaint(&self, _app: &App, old_delegate: &dyn CustomPainter) -> bool {
match old_delegate.as_any().downcast_ref::<Self>() {
Some(old) => {
old.fill != self.fill
|| old.fill_rule != self.fill_rule
|| !Arc::ptr_eq(&old.data, &self.data)
&& !old.data.elements().eq(self.data.elements())
}
None => true,
}
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
#[cfg(test)]
mod tests {
use super::FluentSymbol;
#[test]
fn the_inspectors_symbols_are_the_fonts_named_glyphs() {
let codepoints = include_str!("../../assets/fluent-system-icons/codepoints.json");
for (symbol, name) in [
(FluentSymbol::Camera, "ic_fluent_camera_20_regular"),
(FluentSymbol::Mic, "ic_fluent_mic_20_regular"),
(FluentSymbol::Folder, "ic_fluent_folder_20_regular"),
(FluentSymbol::Globe, "ic_fluent_globe_20_regular"),
(FluentSymbol::Shield, "ic_fluent_shield_20_regular"),
(FluentSymbol::Clipboard, "ic_fluent_clipboard_20_regular"),
(FluentSymbol::Alert, "ic_fluent_alert_20_regular"),
(
FluentSymbol::WindowConsole,
"ic_fluent_window_console_20_regular",
),
(
FluentSymbol::PlugConnected,
"ic_fluent_plug_connected_20_regular",
),
(FluentSymbol::Live, "ic_fluent_live_20_regular"),
(FluentSymbol::Question, "ic_fluent_question_20_regular"),
(FluentSymbol::Desktop, "ic_fluent_desktop_20_regular"),
(FluentSymbol::Pulse, "ic_fluent_pulse_20_regular"),
(FluentSymbol::Play, "ic_fluent_play_20_regular"),
(FluentSymbol::Stop, "ic_fluent_stop_20_regular"),
] {
let entry = format!("\"{name}\": {}", symbol.glyph() as u32);
assert!(codepoints.contains(&entry), "{symbol:?} is {name}");
}
}
}