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