icon.rs

  1use gpui::{rems, svg, RenderOnce, Svg};
  2use strum::EnumIter;
  3
  4use crate::prelude::*;
  5
  6#[derive(Default, PartialEq, Copy, Clone)]
  7pub enum IconSize {
  8    Small,
  9    #[default]
 10    Medium,
 11}
 12
 13#[derive(Debug, PartialEq, Copy, Clone, EnumIter)]
 14pub enum Icon {
 15    Ai,
 16    ArrowLeft,
 17    ArrowRight,
 18    ArrowUpRight,
 19    AtSign,
 20    AudioOff,
 21    AudioOn,
 22    Bell,
 23    BellOff,
 24    BellRing,
 25    Bolt,
 26    Check,
 27    ChevronDown,
 28    ChevronLeft,
 29    ChevronRight,
 30    ChevronUp,
 31    Close,
 32    Collab,
 33    Copilot,
 34    Dash,
 35    Envelope,
 36    ExclamationTriangle,
 37    Exit,
 38    File,
 39    FileDoc,
 40    FileGeneric,
 41    FileGit,
 42    FileLock,
 43    FileRust,
 44    FileToml,
 45    FileTree,
 46    Folder,
 47    FolderOpen,
 48    FolderX,
 49    Hash,
 50    InlayHint,
 51    MagicWand,
 52    MagnifyingGlass,
 53    MailOpen,
 54    Maximize,
 55    Menu,
 56    MessageBubbles,
 57    Mic,
 58    MicMute,
 59    Plus,
 60    Quote,
 61    Replace,
 62    ReplaceAll,
 63    Screen,
 64    SelectAll,
 65    Split,
 66    SplitMessage,
 67    Terminal,
 68    XCircle,
 69}
 70
 71impl Icon {
 72    pub fn path(self) -> &'static str {
 73        match self {
 74            Icon::Ai => "icons/ai.svg",
 75            Icon::ArrowLeft => "icons/arrow_left.svg",
 76            Icon::ArrowRight => "icons/arrow_right.svg",
 77            Icon::ArrowUpRight => "icons/arrow_up_right.svg",
 78            Icon::AtSign => "icons/at-sign.svg",
 79            Icon::AudioOff => "icons/speaker-off.svg",
 80            Icon::AudioOn => "icons/speaker-loud.svg",
 81            Icon::Bell => "icons/bell.svg",
 82            Icon::BellOff => "icons/bell-off.svg",
 83            Icon::BellRing => "icons/bell-ring.svg",
 84            Icon::Bolt => "icons/bolt.svg",
 85            Icon::Check => "icons/check.svg",
 86            Icon::ChevronDown => "icons/chevron_down.svg",
 87            Icon::ChevronLeft => "icons/chevron_left.svg",
 88            Icon::ChevronRight => "icons/chevron_right.svg",
 89            Icon::ChevronUp => "icons/chevron_up.svg",
 90            Icon::Close => "icons/x.svg",
 91            Icon::Collab => "icons/user_group_16.svg",
 92            Icon::Copilot => "icons/copilot.svg",
 93            Icon::Dash => "icons/dash.svg",
 94            Icon::Envelope => "icons/feedback.svg",
 95            Icon::ExclamationTriangle => "icons/warning.svg",
 96            Icon::Exit => "icons/exit.svg",
 97            Icon::File => "icons/file.svg",
 98            Icon::FileDoc => "icons/file_icons/book.svg",
 99            Icon::FileGeneric => "icons/file_icons/file.svg",
100            Icon::FileGit => "icons/file_icons/git.svg",
101            Icon::FileLock => "icons/file_icons/lock.svg",
102            Icon::FileRust => "icons/file_icons/rust.svg",
103            Icon::FileToml => "icons/file_icons/toml.svg",
104            Icon::FileTree => "icons/project.svg",
105            Icon::Folder => "icons/file_icons/folder.svg",
106            Icon::FolderOpen => "icons/file_icons/folder_open.svg",
107            Icon::FolderX => "icons/stop_sharing.svg",
108            Icon::Hash => "icons/hash.svg",
109            Icon::InlayHint => "icons/inlay_hint.svg",
110            Icon::MagicWand => "icons/magic-wand.svg",
111            Icon::MagnifyingGlass => "icons/magnifying_glass.svg",
112            Icon::MailOpen => "icons/mail-open.svg",
113            Icon::Maximize => "icons/maximize.svg",
114            Icon::Menu => "icons/menu.svg",
115            Icon::MessageBubbles => "icons/conversations.svg",
116            Icon::Mic => "icons/mic.svg",
117            Icon::MicMute => "icons/mic-mute.svg",
118            Icon::Plus => "icons/plus.svg",
119            Icon::Quote => "icons/quote.svg",
120            Icon::Replace => "icons/replace.svg",
121            Icon::ReplaceAll => "icons/replace_all.svg",
122            Icon::Screen => "icons/desktop.svg",
123            Icon::SelectAll => "icons/select-all.svg",
124            Icon::Split => "icons/split.svg",
125            Icon::SplitMessage => "icons/split_message.svg",
126            Icon::Terminal => "icons/terminal.svg",
127            Icon::XCircle => "icons/error.svg",
128        }
129    }
130}
131
132#[derive(RenderOnce)]
133pub struct IconElement {
134    path: SharedString,
135    color: TextColor,
136    size: IconSize,
137}
138
139impl<V: 'static> Component<V> for IconElement {
140    type Rendered = Svg<V>;
141
142    fn render(self, _view: &mut V, cx: &mut ViewContext<V>) -> Self::Rendered {
143        let svg_size = match self.size {
144            IconSize::Small => rems(0.75),
145            IconSize::Medium => rems(0.9375),
146        };
147
148        svg()
149            .size(svg_size)
150            .flex_none()
151            .path(self.path)
152            .text_color(self.color.color(cx))
153    }
154}
155
156impl IconElement {
157    pub fn new(icon: Icon) -> Self {
158        Self {
159            path: icon.path().into(),
160            color: TextColor::default(),
161            size: IconSize::default(),
162        }
163    }
164
165    pub fn from_path(path: impl Into<SharedString>) -> Self {
166        Self {
167            path: path.into(),
168            color: TextColor::default(),
169            size: IconSize::default(),
170        }
171    }
172
173    pub fn color(mut self, color: TextColor) -> Self {
174        self.color = color;
175        self
176    }
177
178    pub fn size(mut self, size: IconSize) -> Self {
179        self.size = size;
180        self
181    }
182
183    fn render<V: 'static>(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Element<V> {
184        let svg_size = match self.size {
185            IconSize::Small => rems(0.75),
186            IconSize::Medium => rems(0.9375),
187        };
188
189        svg()
190            .size(svg_size)
191            .flex_none()
192            .path(self.path)
193            .text_color(self.color.color(cx))
194    }
195}
196
197#[cfg(feature = "stories")]
198pub use stories::*;
199
200#[cfg(feature = "stories")]
201mod stories {
202    use gpui::{Div, Render};
203    use strum::IntoEnumIterator;
204
205    use crate::Story;
206
207    use super::*;
208
209    pub struct IconStory;
210
211    impl Render<Self> for IconStory {
212        type Element = Div<Self>;
213
214        fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
215            let icons = Icon::iter();
216
217            Story::container(cx)
218                .child(Story::title_for::<_, IconElement>(cx))
219                .child(Story::label(cx, "All Icons"))
220                .child(div().flex().gap_3().children(icons.map(IconElement::new)))
221        }
222    }
223}