icon.rs

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