use std::sync::Arc;
use gpui::{
AnyElement, App, ElementId, InteractiveElement, IntoElement, ParentElement, Pixels, Radians,
RenderOnce, Role, SharedString, StatefulInteractiveElement, Styled, Transformation, Window,
div, px, radians, size, svg,
};
use gpui_base::TestSupportExt;
use gpui_material_foundation::{color, current_material_theme};
use gpui_material_theme::{ColorRole, Direction};
macro_rules! symbols {
($($variant:ident => $name:literal),+ $(,)?) => {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum IconName { $($variant),+ }
impl IconName {
pub const ALL: &'static [Self] = &[$(Self::$variant),+];
pub fn svg_data(self, filled: bool) -> &'static [u8] {
match (self, filled) {
$((Self::$variant, false) => include_bytes!(concat!("../assets/material-symbols/", $name, "_24px.svg")),
(Self::$variant, true) => include_bytes!(concat!("../assets/material-symbols/", $name, "_fill1_24px.svg"))),+
}
}
}
};
}
symbols! {
Add => "add",
Remove => "remove",
Check => "check",
Close => "close",
ChevronRight => "chevron_right",
ArrowForward => "arrow_forward",
ArrowDropDown => "arrow_drop_down",
Favorite => "favorite",
Archive => "archive",
Inbox => "inbox",
Description => "description",
Notifications => "notifications",
MotionPhotosOn => "motion_photos_on",
Contrast => "contrast",
DarkMode => "dark_mode",
LightMode => "light_mode",
LeftPanelOpen => "left_panel_open",
LeftPanelClose => "left_panel_close",
Search => "search",
Error => "error",
Info => "info",
Edit => "edit",
Exposure => "exposure",
Visibility => "visibility",
VisibilityOff => "visibility_off",
}
#[derive(IntoElement)]
pub struct Icon {
content: IconContent,
name: Option<(ElementId, SharedString)>,
side: Option<Pixels>,
color: Option<ColorRole>,
mirror: bool,
rotation: Radians,
filled: bool,
}
enum IconContent {
Symbol(IconName),
Svg(SharedString),
SvgData(Arc<[u8]>),
Element(AnyElement),
}
impl Icon {
pub fn new(symbol: IconName) -> Self {
Self {
content: IconContent::Symbol(symbol),
name: None,
side: None,
color: None,
mirror: false,
rotation: radians(0.),
filled: false,
}
}
pub fn svg(path: impl Into<SharedString>) -> Self {
Self {
content: IconContent::Svg(path.into()),
side: Some(px(24.)),
color: Some(ColorRole::OnSurfaceVariant),
..Self::new(IconName::Info)
}
}
pub fn svg_data(data: impl Into<Arc<[u8]>>) -> Self {
Self {
content: IconContent::SvgData(data.into()),
..Self::new(IconName::Info)
}
}
pub fn custom(element: impl IntoElement) -> Self {
Self {
content: IconContent::Element(element.into_any_element()),
..Self::svg("")
}
}
pub fn filled(mut self, filled: bool) -> Self {
self.filled = filled;
self
}
pub fn label(mut self, id: impl Into<ElementId>, name: impl Into<SharedString>) -> Self {
self.name = Some((id.into(), name.into()));
self
}
pub fn size(mut self, side: Pixels) -> Self {
self.side = Some(side.max(px(0.)));
self
}
pub fn color(mut self, color: ColorRole) -> Self {
self.color = Some(color);
self
}
#[cfg_attr(not(feature = "full"), allow(dead_code))]
pub(crate) fn inherit_color(mut self) -> Self {
self.color = None;
self
}
pub fn mirror_in_rtl(mut self, mirror: bool) -> Self {
self.mirror = mirror;
self
}
pub fn rotate(mut self, angle: Radians) -> Self {
self.rotation = angle;
self
}
}
impl RenderOnce for Icon {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = current_material_theme(window, cx);
let tint = self.color.map_or_else(
|| window.text_style().color,
|role| color(theme.colors.color(role)),
);
let side = self
.side
.unwrap_or_else(|| window.text_style().font_size.to_pixels(window.rem_size()));
let rtl = self.mirror && theme.layout.direction == Direction::Rtl;
let transform = Transformation::rotate(self.rotation)
.with_scaling(size(if rtl { -1. } else { 1. }, 1.));
let icon_svg = || {
svg()
.size_full()
.text_color(tint)
.with_transformation(transform)
};
let icon = match self.content {
IconContent::Symbol(symbol) => icon_svg()
.data(symbol.svg_data(self.filled))
.into_any_element(),
IconContent::Svg(path) => icon_svg().path(path).into_any_element(),
IconContent::SvgData(data) => icon_svg().data(&data).into_any_element(),
IconContent::Element(element) => element,
};
let icon = div()
.size(side)
.flex_none()
.flex()
.items_center()
.justify_center()
.text_color(tint)
.child(icon);
match self.name {
Some((id, label)) => icon
.id(id)
.test_support()
.role(Role::Image)
.aria_label(label)
.into_any_element(),
None => icon.into_any_element(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_bundled_symbol_rasterizes_without_fonts_or_application_assets() {
let renderer = gpui::SvgRenderer::new(Arc::new(()));
for name in IconName::ALL {
for filled in [false, true] {
let data = name.svg_data(filled);
let source = std::str::from_utf8(data).unwrap();
assert!(!source.contains("<text") && !source.contains("<image"));
assert!(!source.contains("href="));
for scale in [1., 2.] {
let image = renderer
.render_single_frame(data, scale)
.unwrap_or_else(|error| panic!("{name:?} fill={filled}: {error}"));
assert!(
image
.as_bytes(0)
.unwrap()
.as_chunks::<4>()
.0
.iter()
.any(|pixel| pixel[3] != 0),
"{name:?} fill={filled} must paint visible pixels"
);
}
}
}
assert_ne!(
IconName::Favorite.svg_data(false),
IconName::Favorite.svg_data(true)
);
}
}