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