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