icon.rs

  1use gpui::{svg, Hsla, IntoElement, Rems, Transformation};
  2use strum::EnumIter;
  3
  4use crate::{prelude::*, Indicator};
  5
  6#[derive(Default, PartialEq, Copy, Clone)]
  7pub enum IconSize {
  8    Indicator,
  9    XSmall,
 10    Small,
 11    #[default]
 12    Medium,
 13}
 14
 15impl IconSize {
 16    pub fn rems(self) -> Rems {
 17        match self {
 18            IconSize::Indicator => rems_from_px(10.),
 19            IconSize::XSmall => rems_from_px(12.),
 20            IconSize::Small => rems_from_px(14.),
 21            IconSize::Medium => rems_from_px(16.),
 22        }
 23    }
 24}
 25
 26#[derive(Debug, PartialEq, Copy, Clone, EnumIter)]
 27pub enum IconName {
 28    Ai,
 29    ArrowDown,
 30    ArrowLeft,
 31    ArrowRight,
 32    ArrowUp,
 33    ArrowUpRight,
 34    ArrowCircle,
 35    AtSign,
 36    AudioOff,
 37    AudioOn,
 38    Backspace,
 39    Bell,
 40    BellOff,
 41    BellRing,
 42    BellDot,
 43    Bolt,
 44    CaseSensitive,
 45    Check,
 46    ChevronDown,
 47    ChevronLeft,
 48    ChevronRight,
 49    ChevronUp,
 50    ExpandVertical,
 51    Close,
 52    Collab,
 53    Command,
 54    Control,
 55    Copilot,
 56    CopilotDisabled,
 57    CopilotError,
 58    CopilotInit,
 59    Copy,
 60    Dash,
 61    Delete,
 62    Disconnected,
 63    Ellipsis,
 64    Envelope,
 65    Escape,
 66    ExclamationTriangle,
 67    Exit,
 68    ExternalLink,
 69    File,
 70    FileDoc,
 71    FileGeneric,
 72    FileGit,
 73    FileLock,
 74    FileRust,
 75    FileToml,
 76    FileTree,
 77    Filter,
 78    Folder,
 79    FolderOpen,
 80    FolderX,
 81    Github,
 82    Hash,
 83    InlayHint,
 84    Link,
 85    MagicWand,
 86    MagnifyingGlass,
 87    MailOpen,
 88    Maximize,
 89    Menu,
 90    MessageBubbles,
 91    Mic,
 92    MicMute,
 93    Minimize,
 94    Option,
 95    PageDown,
 96    PageUp,
 97    Pencil,
 98    Person,
 99    Play,
100    Plus,
101    Public,
102    Quote,
103    Regex,
104    Replace,
105    ReplaceAll,
106    ReplaceNext,
107    Return,
108    ReplyArrowRight,
109    Settings,
110    Sliders,
111    Screen,
112    SelectAll,
113    Server,
114    Shift,
115    Snip,
116    Space,
117    Split,
118    Tab,
119    Terminal,
120    Trash,
121    Update,
122    WholeWord,
123    XCircle,
124    ZedXCopilot,
125    PullRequest,
126}
127
128impl IconName {
129    pub fn path(self) -> &'static str {
130        match self {
131            IconName::Ai => "icons/ai.svg",
132            IconName::ArrowDown => "icons/arrow_down.svg",
133            IconName::ArrowLeft => "icons/arrow_left.svg",
134            IconName::ArrowRight => "icons/arrow_right.svg",
135            IconName::ArrowUp => "icons/arrow_up.svg",
136            IconName::ArrowUpRight => "icons/arrow_up_right.svg",
137            IconName::ArrowCircle => "icons/arrow_circle.svg",
138            IconName::AtSign => "icons/at_sign.svg",
139            IconName::AudioOff => "icons/speaker_off.svg",
140            IconName::AudioOn => "icons/speaker_loud.svg",
141            IconName::Backspace => "icons/backspace.svg",
142            IconName::Bell => "icons/bell.svg",
143            IconName::BellOff => "icons/bell_off.svg",
144            IconName::BellRing => "icons/bell_ring.svg",
145            IconName::BellDot => "icons/bell_dot.svg",
146            IconName::Bolt => "icons/bolt.svg",
147            IconName::CaseSensitive => "icons/case_insensitive.svg",
148            IconName::Check => "icons/check.svg",
149            IconName::ChevronDown => "icons/chevron_down.svg",
150            IconName::ChevronLeft => "icons/chevron_left.svg",
151            IconName::ChevronRight => "icons/chevron_right.svg",
152            IconName::ChevronUp => "icons/chevron_up.svg",
153            IconName::ExpandVertical => "icons/expand_vertical.svg",
154            IconName::Close => "icons/x.svg",
155            IconName::Collab => "icons/user_group_16.svg",
156            IconName::Command => "icons/command.svg",
157            IconName::Control => "icons/control.svg",
158            IconName::Copilot => "icons/copilot.svg",
159            IconName::CopilotDisabled => "icons/copilot_disabled.svg",
160            IconName::CopilotError => "icons/copilot_error.svg",
161            IconName::CopilotInit => "icons/copilot_init.svg",
162            IconName::Copy => "icons/copy.svg",
163            IconName::Dash => "icons/dash.svg",
164            IconName::Delete => "icons/delete.svg",
165            IconName::Disconnected => "icons/disconnected.svg",
166            IconName::Ellipsis => "icons/ellipsis.svg",
167            IconName::Envelope => "icons/feedback.svg",
168            IconName::Escape => "icons/escape.svg",
169            IconName::ExclamationTriangle => "icons/warning.svg",
170            IconName::Exit => "icons/exit.svg",
171            IconName::ExternalLink => "icons/external_link.svg",
172            IconName::File => "icons/file.svg",
173            IconName::FileDoc => "icons/file_icons/book.svg",
174            IconName::FileGeneric => "icons/file_icons/file.svg",
175            IconName::FileGit => "icons/file_icons/git.svg",
176            IconName::FileLock => "icons/file_icons/lock.svg",
177            IconName::FileRust => "icons/file_icons/rust.svg",
178            IconName::FileToml => "icons/file_icons/toml.svg",
179            IconName::FileTree => "icons/project.svg",
180            IconName::Filter => "icons/filter.svg",
181            IconName::Folder => "icons/file_icons/folder.svg",
182            IconName::FolderOpen => "icons/file_icons/folder_open.svg",
183            IconName::FolderX => "icons/stop_sharing.svg",
184            IconName::Github => "icons/github.svg",
185            IconName::Hash => "icons/hash.svg",
186            IconName::InlayHint => "icons/inlay_hint.svg",
187            IconName::Link => "icons/link.svg",
188            IconName::MagicWand => "icons/magic_wand.svg",
189            IconName::MagnifyingGlass => "icons/magnifying_glass.svg",
190            IconName::MailOpen => "icons/mail_open.svg",
191            IconName::Maximize => "icons/maximize.svg",
192            IconName::Menu => "icons/menu.svg",
193            IconName::MessageBubbles => "icons/conversations.svg",
194            IconName::Mic => "icons/mic.svg",
195            IconName::MicMute => "icons/mic_mute.svg",
196            IconName::Minimize => "icons/minimize.svg",
197            IconName::Option => "icons/option.svg",
198            IconName::PageDown => "icons/page_down.svg",
199            IconName::PageUp => "icons/page_up.svg",
200            IconName::Person => "icons/person.svg",
201            IconName::Pencil => "icons/pencil.svg",
202            IconName::Play => "icons/play.svg",
203            IconName::Plus => "icons/plus.svg",
204            IconName::Public => "icons/public.svg",
205            IconName::Quote => "icons/quote.svg",
206            IconName::Regex => "icons/regex.svg",
207            IconName::Replace => "icons/replace.svg",
208            IconName::ReplaceAll => "icons/replace_all.svg",
209            IconName::ReplaceNext => "icons/replace_next.svg",
210            IconName::Return => "icons/return.svg",
211            IconName::ReplyArrowRight => "icons/reply_arrow_right.svg",
212            IconName::Settings => "icons/file_icons/settings.svg",
213            IconName::Sliders => "icons/sliders.svg",
214            IconName::Screen => "icons/desktop.svg",
215            IconName::SelectAll => "icons/select_all.svg",
216            IconName::Server => "icons/server.svg",
217            IconName::Shift => "icons/shift.svg",
218            IconName::Snip => "icons/snip.svg",
219            IconName::Space => "icons/space.svg",
220            IconName::Split => "icons/split.svg",
221            IconName::Tab => "icons/tab.svg",
222            IconName::Terminal => "icons/terminal.svg",
223            IconName::Trash => "icons/trash.svg",
224            IconName::Update => "icons/update.svg",
225            IconName::WholeWord => "icons/word_search.svg",
226            IconName::XCircle => "icons/error.svg",
227            IconName::ZedXCopilot => "icons/zed_x_copilot.svg",
228            IconName::PullRequest => "icons/pull_request.svg",
229        }
230    }
231}
232
233#[derive(IntoElement)]
234pub struct Icon {
235    path: SharedString,
236    color: Color,
237    size: IconSize,
238    transformation: Transformation,
239}
240
241impl Icon {
242    pub fn new(icon: IconName) -> Self {
243        Self {
244            path: icon.path().into(),
245            color: Color::default(),
246            size: IconSize::default(),
247            transformation: Transformation::default(),
248        }
249    }
250
251    pub fn from_path(path: impl Into<SharedString>) -> Self {
252        Self {
253            path: path.into(),
254            color: Color::default(),
255            size: IconSize::default(),
256            transformation: Transformation::default(),
257        }
258    }
259
260    pub fn color(mut self, color: Color) -> Self {
261        self.color = color;
262        self
263    }
264
265    pub fn size(mut self, size: IconSize) -> Self {
266        self.size = size;
267        self
268    }
269
270    pub fn transform(mut self, transformation: Transformation) -> Self {
271        self.transformation = transformation;
272        self
273    }
274}
275
276impl RenderOnce for Icon {
277    fn render(self, cx: &mut WindowContext) -> impl IntoElement {
278        svg()
279            .with_transformation(self.transformation)
280            .size(self.size.rems())
281            .flex_none()
282            .path(self.path)
283            .text_color(self.color.color(cx))
284    }
285}
286
287#[derive(IntoElement)]
288pub struct IconWithIndicator {
289    icon: Icon,
290    indicator: Option<Indicator>,
291    indicator_border_color: Option<Hsla>,
292}
293
294impl IconWithIndicator {
295    pub fn new(icon: Icon, indicator: Option<Indicator>) -> Self {
296        Self {
297            icon,
298            indicator,
299            indicator_border_color: None,
300        }
301    }
302
303    pub fn indicator(mut self, indicator: Option<Indicator>) -> Self {
304        self.indicator = indicator;
305        self
306    }
307
308    pub fn indicator_color(mut self, color: Color) -> Self {
309        if let Some(indicator) = self.indicator.as_mut() {
310            indicator.color = color;
311        }
312        self
313    }
314
315    pub fn indicator_border_color(mut self, color: Option<Hsla>) -> Self {
316        self.indicator_border_color = color;
317        self
318    }
319}
320
321impl RenderOnce for IconWithIndicator {
322    fn render(self, cx: &mut WindowContext) -> impl IntoElement {
323        let indicator_border_color = self
324            .indicator_border_color
325            .unwrap_or_else(|| cx.theme().colors().elevated_surface_background);
326
327        div()
328            .relative()
329            .child(self.icon)
330            .when_some(self.indicator, |this, indicator| {
331                this.child(
332                    div()
333                        .absolute()
334                        .w_2()
335                        .h_2()
336                        .border()
337                        .border_color(indicator_border_color)
338                        .rounded_full()
339                        .neg_bottom_0p5()
340                        .neg_right_1()
341                        .child(indicator),
342                )
343            })
344    }
345}