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