icon.rs

  1use gpui::{rems, svg, IntoElement, Rems, Svg};
  2use strum::EnumIter;
  3
  4use crate::prelude::*;
  5
  6#[derive(Default, PartialEq, Copy, Clone)]
  7pub enum IconSize {
  8    XSmall,
  9    Small,
 10    #[default]
 11    Medium,
 12}
 13
 14impl IconSize {
 15    pub fn rems(self) -> Rems {
 16        match self {
 17            IconSize::XSmall => rems(12. / 16.),
 18            IconSize::Small => rems(14. / 16.),
 19            IconSize::Medium => rems(16. / 16.),
 20        }
 21    }
 22}
 23
 24#[derive(Debug, PartialEq, Copy, Clone, EnumIter)]
 25pub enum Icon {
 26    Ai,
 27    ArrowDown,
 28    ArrowLeft,
 29    ArrowRight,
 30    ArrowUp,
 31    ArrowUpRight,
 32    AtSign,
 33    AudioOff,
 34    AudioOn,
 35    Backspace,
 36    Bell,
 37    BellOff,
 38    BellRing,
 39    Bolt,
 40    CaseSensitive,
 41    Check,
 42    ChevronDown,
 43    ChevronLeft,
 44    ChevronRight,
 45    ChevronUp,
 46    Close,
 47    Collab,
 48    Command,
 49    Control,
 50    Copilot,
 51    CopilotDisabled,
 52    CopilotError,
 53    CopilotInit,
 54    Copy,
 55    Dash,
 56    Delete,
 57    Disconnected,
 58    Ellipsis,
 59    Envelope,
 60    Escape,
 61    ExclamationTriangle,
 62    Exit,
 63    ExternalLink,
 64    File,
 65    FileDoc,
 66    FileGeneric,
 67    FileGit,
 68    FileLock,
 69    FileRust,
 70    FileToml,
 71    FileTree,
 72    Filter,
 73    Folder,
 74    FolderOpen,
 75    FolderX,
 76    Github,
 77    Hash,
 78    InlayHint,
 79    Link,
 80    MagicWand,
 81    MagnifyingGlass,
 82    MailOpen,
 83    Maximize,
 84    Menu,
 85    MessageBubbles,
 86    Mic,
 87    MicMute,
 88    Minimize,
 89    Option,
 90    Plus,
 91    Public,
 92    Quote,
 93    Replace,
 94    ReplaceAll,
 95    ReplaceNext,
 96    Return,
 97    Screen,
 98    SelectAll,
 99    Shift,
100    Snip,
101    Space,
102    Split,
103    Tab,
104    Terminal,
105    Update,
106    WholeWord,
107    XCircle,
108    ZedXCopilot,
109}
110
111impl Icon {
112    pub fn path(self) -> &'static str {
113        match self {
114            Icon::Ai => "icons/ai.svg",
115            Icon::ArrowDown => "icons/arrow_down.svg",
116            Icon::ArrowLeft => "icons/arrow_left.svg",
117            Icon::ArrowRight => "icons/arrow_right.svg",
118            Icon::ArrowUp => "icons/arrow_up.svg",
119            Icon::ArrowUpRight => "icons/arrow_up_right.svg",
120            Icon::AtSign => "icons/at-sign.svg",
121            Icon::AudioOff => "icons/speaker-off.svg",
122            Icon::AudioOn => "icons/speaker-loud.svg",
123            Icon::Backspace => "icons/backspace.svg",
124            Icon::Bell => "icons/bell.svg",
125            Icon::BellOff => "icons/bell-off.svg",
126            Icon::BellRing => "icons/bell-ring.svg",
127            Icon::Bolt => "icons/bolt.svg",
128            Icon::CaseSensitive => "icons/case_insensitive.svg",
129            Icon::Check => "icons/check.svg",
130            Icon::ChevronDown => "icons/chevron_down.svg",
131            Icon::ChevronLeft => "icons/chevron_left.svg",
132            Icon::ChevronRight => "icons/chevron_right.svg",
133            Icon::ChevronUp => "icons/chevron_up.svg",
134            Icon::Close => "icons/x.svg",
135            Icon::Collab => "icons/user_group_16.svg",
136            Icon::Command => "icons/command.svg",
137            Icon::Control => "icons/control.svg",
138            Icon::Copilot => "icons/copilot.svg",
139            Icon::CopilotDisabled => "icons/copilot_disabled.svg",
140            Icon::CopilotError => "icons/copilot_error.svg",
141            Icon::CopilotInit => "icons/copilot_init.svg",
142            Icon::Copy => "icons/copy.svg",
143            Icon::Dash => "icons/dash.svg",
144            Icon::Delete => "icons/delete.svg",
145            Icon::Disconnected => "icons/disconnected.svg",
146            Icon::Ellipsis => "icons/ellipsis.svg",
147            Icon::Envelope => "icons/feedback.svg",
148            Icon::Escape => "icons/escape.svg",
149            Icon::ExclamationTriangle => "icons/warning.svg",
150            Icon::Exit => "icons/exit.svg",
151            Icon::ExternalLink => "icons/external_link.svg",
152            Icon::File => "icons/file.svg",
153            Icon::FileDoc => "icons/file_icons/book.svg",
154            Icon::FileGeneric => "icons/file_icons/file.svg",
155            Icon::FileGit => "icons/file_icons/git.svg",
156            Icon::FileLock => "icons/file_icons/lock.svg",
157            Icon::FileRust => "icons/file_icons/rust.svg",
158            Icon::FileToml => "icons/file_icons/toml.svg",
159            Icon::FileTree => "icons/project.svg",
160            Icon::Filter => "icons/filter.svg",
161            Icon::Folder => "icons/file_icons/folder.svg",
162            Icon::FolderOpen => "icons/file_icons/folder_open.svg",
163            Icon::FolderX => "icons/stop_sharing.svg",
164            Icon::Github => "icons/github.svg",
165            Icon::Hash => "icons/hash.svg",
166            Icon::InlayHint => "icons/inlay_hint.svg",
167            Icon::Link => "icons/link.svg",
168            Icon::MagicWand => "icons/magic-wand.svg",
169            Icon::MagnifyingGlass => "icons/magnifying_glass.svg",
170            Icon::MailOpen => "icons/mail-open.svg",
171            Icon::Maximize => "icons/maximize.svg",
172            Icon::Menu => "icons/menu.svg",
173            Icon::MessageBubbles => "icons/conversations.svg",
174            Icon::Mic => "icons/mic.svg",
175            Icon::MicMute => "icons/mic-mute.svg",
176            Icon::Minimize => "icons/minimize.svg",
177            Icon::Option => "icons/option.svg",
178            Icon::Plus => "icons/plus.svg",
179            Icon::Public => "icons/public.svg",
180            Icon::Quote => "icons/quote.svg",
181            Icon::Replace => "icons/replace.svg",
182            Icon::ReplaceAll => "icons/replace_all.svg",
183            Icon::ReplaceNext => "icons/replace_next.svg",
184            Icon::Return => "icons/return.svg",
185            Icon::Screen => "icons/desktop.svg",
186            Icon::SelectAll => "icons/select-all.svg",
187            Icon::Shift => "icons/shift.svg",
188            Icon::Snip => "icons/snip.svg",
189            Icon::Space => "icons/space.svg",
190            Icon::Split => "icons/split.svg",
191            Icon::Tab => "icons/tab.svg",
192            Icon::Terminal => "icons/terminal.svg",
193            Icon::Update => "icons/update.svg",
194            Icon::WholeWord => "icons/word_search.svg",
195            Icon::XCircle => "icons/error.svg",
196            Icon::ZedXCopilot => "icons/zed_x_copilot.svg",
197        }
198    }
199}
200
201#[derive(IntoElement)]
202pub struct IconElement {
203    path: SharedString,
204    color: Color,
205    size: IconSize,
206}
207
208impl RenderOnce for IconElement {
209    type Output = Svg;
210
211    fn render(self, cx: &mut WindowContext) -> Self::Output {
212        svg()
213            .size(self.size.rems())
214            .flex_none()
215            .path(self.path)
216            .text_color(self.color.color(cx))
217    }
218}
219
220impl IconElement {
221    pub fn new(icon: Icon) -> Self {
222        Self {
223            path: icon.path().into(),
224            color: Color::default(),
225            size: IconSize::default(),
226        }
227    }
228
229    pub fn from_path(path: impl Into<SharedString>) -> Self {
230        Self {
231            path: path.into(),
232            color: Color::default(),
233            size: IconSize::default(),
234        }
235    }
236
237    pub fn color(mut self, color: Color) -> Self {
238        self.color = color;
239        self
240    }
241
242    pub fn size(mut self, size: IconSize) -> Self {
243        self.size = size;
244        self
245    }
246}