Skip to main content

glassy_ui/
icon.rs

1use crate::motion::StyledSlot;
2use gpui::{
3    px, svg, App, Hsla, IntoElement, Pixels, RenderOnce, SharedString, StyleRefinement, Styled,
4    Window,
5};
6
7/// Icons used on the Paper Buttons page.
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
9pub enum IconName {
10    Plus,
11    Download,
12    Info,
13    ChevronRight,
14    ChevronDown,
15    Search,
16    Spinner,
17    Check,
18}
19
20impl IconName {
21    pub fn path(self) -> SharedString {
22        SharedString::from(match self {
23            Self::Plus => "icons/plus.svg",
24            Self::Download => "icons/download.svg",
25            Self::Info => "icons/info.svg",
26            Self::ChevronRight => "icons/chevron-right.svg",
27            Self::ChevronDown => "icons/chevron-down.svg",
28            Self::Search => "icons/search.svg",
29            Self::Spinner => "icons/spinner.svg",
30            Self::Check => "icons/check.svg",
31        })
32    }
33}
34
35/// SVG icon painted with `text_color`.
36#[derive(IntoElement)]
37pub struct Icon {
38    name: IconName,
39    style: StyleRefinement,
40}
41
42impl Icon {
43    pub fn new(name: IconName) -> Self {
44        Self {
45            name,
46            style: StyleRefinement::default().size(px(16.)).flex_shrink_0(),
47        }
48    }
49
50    pub fn px(self, size: impl Into<Pixels>) -> Self {
51        self.size(size.into())
52    }
53
54    pub fn color(self, color: Hsla) -> Self {
55        self.text_color(color)
56    }
57
58    pub fn name(&self) -> IconName {
59        self.name
60    }
61}
62
63impl Styled for Icon {
64    fn style(&mut self) -> &mut StyleRefinement {
65        &mut self.style
66    }
67}
68
69impl RenderOnce for Icon {
70    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
71        svg().path(self.name.path()).refine_style(&self.style)
72    }
73}