icon.rs

  1use gpui::{svg, AnimationElement, Hsla, IntoElement, Rems, Transformation};
  2use serde::{Deserialize, Serialize};
  3use strum::{EnumIter, EnumString, IntoStaticStr};
  4
  5use crate::{prelude::*, Indicator};
  6
  7#[derive(IntoElement)]
  8pub enum AnyIcon {
  9    Icon(Icon),
 10    AnimatedIcon(AnimationElement<Icon>),
 11}
 12
 13impl AnyIcon {
 14    /// Returns a new [`AnyIcon`] after applying the given mapping function
 15    /// to the contained [`Icon`].
 16    pub fn map(self, f: impl FnOnce(Icon) -> Icon) -> Self {
 17        match self {
 18            Self::Icon(icon) => Self::Icon(f(icon)),
 19            Self::AnimatedIcon(animated_icon) => Self::AnimatedIcon(animated_icon.map_element(f)),
 20        }
 21    }
 22}
 23
 24impl From<Icon> for AnyIcon {
 25    fn from(value: Icon) -> Self {
 26        Self::Icon(value)
 27    }
 28}
 29
 30impl From<AnimationElement<Icon>> for AnyIcon {
 31    fn from(value: AnimationElement<Icon>) -> Self {
 32        Self::AnimatedIcon(value)
 33    }
 34}
 35
 36impl RenderOnce for AnyIcon {
 37    fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
 38        match self {
 39            Self::Icon(icon) => icon.into_any_element(),
 40            Self::AnimatedIcon(animated_icon) => animated_icon.into_any_element(),
 41        }
 42    }
 43}
 44
 45/// The decoration for an icon.
 46///
 47/// For example, this can show an indicator, an "x",
 48/// or a diagonal strkethrough to indicate something is disabled.
 49#[derive(Debug, PartialEq, Copy, Clone, EnumIter)]
 50pub enum IconDecoration {
 51    Strikethrough,
 52    IndicatorDot,
 53    X,
 54}
 55
 56#[derive(Default, PartialEq, Copy, Clone)]
 57pub enum IconSize {
 58    /// 10px
 59    Indicator,
 60    /// 12px
 61    XSmall,
 62    /// 14px
 63    Small,
 64    #[default]
 65    /// 16px
 66    Medium,
 67}
 68
 69impl IconSize {
 70    pub fn rems(self) -> Rems {
 71        match self {
 72            IconSize::Indicator => rems_from_px(10.),
 73            IconSize::XSmall => rems_from_px(12.),
 74            IconSize::Small => rems_from_px(14.),
 75            IconSize::Medium => rems_from_px(16.),
 76        }
 77    }
 78
 79    /// Returns the individual components of the square that contains this [`IconSize`].
 80    ///
 81    /// The returned tuple contains:
 82    ///   1. The length of one side of the square
 83    ///   2. The padding of one side of the square
 84    pub fn square_components(&self, cx: &mut WindowContext) -> (Pixels, Pixels) {
 85        let icon_size = self.rems() * cx.rem_size();
 86        let padding = match self {
 87            IconSize::Indicator => Spacing::None.px(cx),
 88            IconSize::XSmall => Spacing::XSmall.px(cx),
 89            IconSize::Small => Spacing::XSmall.px(cx),
 90            IconSize::Medium => Spacing::XSmall.px(cx),
 91        };
 92
 93        (icon_size, padding)
 94    }
 95
 96    /// Returns the length of a side of the square that contains this [`IconSize`], with padding.
 97    pub fn square(&self, cx: &mut WindowContext) -> Pixels {
 98        let (icon_size, padding) = self.square_components(cx);
 99
100        icon_size + padding * 2.
101    }
102}
103
104#[derive(
105    Debug, PartialEq, Eq, Copy, Clone, EnumIter, EnumString, IntoStaticStr, Serialize, Deserialize,
106)]
107pub enum IconName {
108    Ai,
109    AiAnthropic,
110    AiOpenAi,
111    AiGoogle,
112    AiOllama,
113    AiZed,
114    ArrowCircle,
115    ArrowDown,
116    ArrowDownFromLine,
117    ArrowLeft,
118    ArrowRight,
119    ArrowUp,
120    ArrowUpFromLine,
121    ArrowUpRight,
122    AtSign,
123    AudioOff,
124    AudioOn,
125    Backspace,
126    Bell,
127    BellDot,
128    BellOff,
129    BellRing,
130    Bolt,
131    Book,
132    BookCopy,
133    BookPlus,
134    CaseSensitive,
135    Check,
136    ChevronDown,
137    /// This chevron indicates a popover menu.
138    ChevronDownSmall,
139    ChevronLeft,
140    ChevronRight,
141    ChevronUp,
142    ChevronUpDown,
143    Close,
144    Code,
145    Collab,
146    Command,
147    Context,
148    Control,
149    Copilot,
150    CopilotDisabled,
151    CopilotError,
152    CopilotInit,
153    Copy,
154    CountdownTimer,
155    Dash,
156    Delete,
157    Disconnected,
158    Download,
159    Ellipsis,
160    Envelope,
161    Escape,
162    ExclamationTriangle,
163    Exit,
164    ExpandVertical,
165    ExternalLink,
166    Eye,
167    File,
168    FileDoc,
169    FileGeneric,
170    FileGit,
171    FileLock,
172    FileRust,
173    FileToml,
174    FileTree,
175    FileText,
176    FileCode,
177    Filter,
178    Folder,
179    FolderOpen,
180    FolderX,
181    Font,
182    FontSize,
183    FontWeight,
184    Github,
185    GenericMinimize,
186    GenericMaximize,
187    GenericClose,
188    GenericRestore,
189    Hash,
190    HistoryRerun,
191    Indicator,
192    IndicatorX,
193    InlayHint,
194    Library,
195    LineHeight,
196    Link,
197    ListTree,
198    MagicWand,
199    MagnifyingGlass,
200    MailOpen,
201    Maximize,
202    Menu,
203    MessageBubbles,
204    Mic,
205    MicMute,
206    Microscope,
207    Minimize,
208    Option,
209    PageDown,
210    PageUp,
211    Pencil,
212    Person,
213    Play,
214    Plus,
215    Public,
216    PullRequest,
217    Quote,
218    Regex,
219    ReplNeutral,
220    Replace,
221    ReplaceAll,
222    ReplaceNext,
223    ReplyArrowRight,
224    Rerun,
225    Return,
226    Reveal,
227    Route,
228    RotateCcw,
229    RotateCw,
230    Save,
231    Screen,
232    SearchSelection,
233    SelectAll,
234    Server,
235    Settings,
236    Shift,
237    Sliders,
238    SlidersAlt,
239    Snip,
240    Space,
241    Sparkle,
242    SparkleAlt,
243    SparkleFilled,
244    Spinner,
245    Split,
246    Star,
247    StarFilled,
248    Stop,
249    Strikethrough,
250    Supermaven,
251    SupermavenDisabled,
252    SupermavenError,
253    SupermavenInit,
254    Tab,
255    Terminal,
256    TextCursor,
257    TextSelect,
258    Trash,
259    TriangleRight,
260    Undo,
261    Update,
262    WholeWord,
263    XCircle,
264    ZedAssistant,
265    ZedAssistantFilled,
266    ZedXCopilot,
267    Visible,
268}
269
270impl IconName {
271    pub fn path(self) -> &'static str {
272        match self {
273            IconName::Ai => "icons/ai.svg",
274            IconName::AiAnthropic => "icons/ai_anthropic.svg",
275            IconName::AiOpenAi => "icons/ai_open_ai.svg",
276            IconName::AiGoogle => "icons/ai_google.svg",
277            IconName::AiOllama => "icons/ai_ollama.svg",
278            IconName::AiZed => "icons/ai_zed.svg",
279            IconName::ArrowCircle => "icons/arrow_circle.svg",
280            IconName::ArrowDown => "icons/arrow_down.svg",
281            IconName::ArrowDownFromLine => "icons/arrow_down_from_line.svg",
282            IconName::ArrowLeft => "icons/arrow_left.svg",
283            IconName::ArrowRight => "icons/arrow_right.svg",
284            IconName::ArrowUp => "icons/arrow_up.svg",
285            IconName::ArrowUpFromLine => "icons/arrow_up_from_line.svg",
286            IconName::ArrowUpRight => "icons/arrow_up_right.svg",
287            IconName::AtSign => "icons/at_sign.svg",
288            IconName::AudioOff => "icons/speaker_off.svg",
289            IconName::AudioOn => "icons/speaker_loud.svg",
290            IconName::Backspace => "icons/backspace.svg",
291            IconName::Bell => "icons/bell.svg",
292            IconName::BellDot => "icons/bell_dot.svg",
293            IconName::BellOff => "icons/bell_off.svg",
294            IconName::BellRing => "icons/bell_ring.svg",
295            IconName::Bolt => "icons/bolt.svg",
296            IconName::Book => "icons/book.svg",
297            IconName::BookCopy => "icons/book_copy.svg",
298            IconName::BookPlus => "icons/book_plus.svg",
299            IconName::CaseSensitive => "icons/case_insensitive.svg",
300            IconName::Check => "icons/check.svg",
301            IconName::ChevronDown => "icons/chevron_down.svg",
302            IconName::ChevronDownSmall => "icons/chevron_down_small.svg",
303            IconName::ChevronLeft => "icons/chevron_left.svg",
304            IconName::ChevronRight => "icons/chevron_right.svg",
305            IconName::ChevronUp => "icons/chevron_up.svg",
306            IconName::ChevronUpDown => "icons/chevron_up_down.svg",
307            IconName::Close => "icons/x.svg",
308            IconName::Code => "icons/code.svg",
309            IconName::Collab => "icons/user_group_16.svg",
310            IconName::Command => "icons/command.svg",
311            IconName::Context => "icons/context.svg",
312            IconName::Control => "icons/control.svg",
313            IconName::Copilot => "icons/copilot.svg",
314            IconName::CopilotDisabled => "icons/copilot_disabled.svg",
315            IconName::CopilotError => "icons/copilot_error.svg",
316            IconName::CopilotInit => "icons/copilot_init.svg",
317            IconName::Copy => "icons/copy.svg",
318            IconName::CountdownTimer => "icons/countdown_timer.svg",
319            IconName::Dash => "icons/dash.svg",
320            IconName::Delete => "icons/delete.svg",
321            IconName::Disconnected => "icons/disconnected.svg",
322            IconName::Download => "icons/download.svg",
323            IconName::Ellipsis => "icons/ellipsis.svg",
324            IconName::Envelope => "icons/feedback.svg",
325            IconName::Escape => "icons/escape.svg",
326            IconName::ExclamationTriangle => "icons/warning.svg",
327            IconName::Exit => "icons/exit.svg",
328            IconName::ExpandVertical => "icons/expand_vertical.svg",
329            IconName::ExternalLink => "icons/external_link.svg",
330            IconName::Eye => "icons/eye.svg",
331            IconName::File => "icons/file.svg",
332            IconName::FileDoc => "icons/file_icons/book.svg",
333            IconName::FileGeneric => "icons/file_icons/file.svg",
334            IconName::FileGit => "icons/file_icons/git.svg",
335            IconName::FileLock => "icons/file_icons/lock.svg",
336            IconName::FileRust => "icons/file_icons/rust.svg",
337            IconName::FileToml => "icons/file_icons/toml.svg",
338            IconName::FileTree => "icons/project.svg",
339            IconName::FileCode => "icons/file_code.svg",
340            IconName::FileText => "icons/file_text.svg",
341            IconName::Filter => "icons/filter.svg",
342            IconName::Folder => "icons/file_icons/folder.svg",
343            IconName::FolderOpen => "icons/file_icons/folder_open.svg",
344            IconName::FolderX => "icons/stop_sharing.svg",
345            IconName::Font => "icons/font.svg",
346            IconName::FontSize => "icons/font_size.svg",
347            IconName::FontWeight => "icons/font_weight.svg",
348            IconName::Github => "icons/github.svg",
349            IconName::GenericMinimize => "icons/generic_minimize.svg",
350            IconName::GenericMaximize => "icons/generic_maximize.svg",
351            IconName::GenericClose => "icons/generic_close.svg",
352            IconName::GenericRestore => "icons/generic_restore.svg",
353            IconName::Hash => "icons/hash.svg",
354            IconName::HistoryRerun => "icons/history_rerun.svg",
355            IconName::Indicator => "icons/indicator.svg",
356            IconName::IndicatorX => "icons/indicator_x.svg",
357            IconName::InlayHint => "icons/inlay_hint.svg",
358            IconName::Library => "icons/library.svg",
359            IconName::LineHeight => "icons/line_height.svg",
360            IconName::Link => "icons/link.svg",
361            IconName::ListTree => "icons/list_tree.svg",
362            IconName::MagicWand => "icons/magic_wand.svg",
363            IconName::MagnifyingGlass => "icons/magnifying_glass.svg",
364            IconName::MailOpen => "icons/mail_open.svg",
365            IconName::Maximize => "icons/maximize.svg",
366            IconName::Menu => "icons/menu.svg",
367            IconName::MessageBubbles => "icons/conversations.svg",
368            IconName::Mic => "icons/mic.svg",
369            IconName::MicMute => "icons/mic_mute.svg",
370            IconName::Microscope => "icons/microscope.svg",
371            IconName::Minimize => "icons/minimize.svg",
372            IconName::Option => "icons/option.svg",
373            IconName::PageDown => "icons/page_down.svg",
374            IconName::PageUp => "icons/page_up.svg",
375            IconName::Pencil => "icons/pencil.svg",
376            IconName::Person => "icons/person.svg",
377            IconName::Play => "icons/play.svg",
378            IconName::Plus => "icons/plus.svg",
379            IconName::Public => "icons/public.svg",
380            IconName::PullRequest => "icons/pull_request.svg",
381            IconName::Quote => "icons/quote.svg",
382            IconName::Regex => "icons/regex.svg",
383            IconName::ReplNeutral => "icons/repl_neutral.svg",
384            IconName::Replace => "icons/replace.svg",
385            IconName::ReplaceAll => "icons/replace_all.svg",
386            IconName::ReplaceNext => "icons/replace_next.svg",
387            IconName::ReplyArrowRight => "icons/reply_arrow_right.svg",
388            IconName::Rerun => "icons/rerun.svg",
389            IconName::Return => "icons/return.svg",
390            IconName::Reveal => "icons/reveal.svg",
391            IconName::RotateCcw => "icons/rotate_ccw.svg",
392            IconName::RotateCw => "icons/rotate_cw.svg",
393            IconName::Route => "icons/route.svg",
394            IconName::Save => "icons/save.svg",
395            IconName::Screen => "icons/desktop.svg",
396            IconName::SearchSelection => "icons/search_selection.svg",
397            IconName::SelectAll => "icons/select_all.svg",
398            IconName::Server => "icons/server.svg",
399            IconName::Settings => "icons/file_icons/settings.svg",
400            IconName::Shift => "icons/shift.svg",
401            IconName::Sliders => "icons/sliders.svg",
402            IconName::SlidersAlt => "icons/sliders-alt.svg",
403            IconName::Snip => "icons/snip.svg",
404            IconName::Space => "icons/space.svg",
405            IconName::Sparkle => "icons/sparkle.svg",
406            IconName::SparkleAlt => "icons/sparkle_alt.svg",
407            IconName::SparkleFilled => "icons/sparkle_filled.svg",
408            IconName::Spinner => "icons/spinner.svg",
409            IconName::Split => "icons/split.svg",
410            IconName::Star => "icons/star.svg",
411            IconName::StarFilled => "icons/star_filled.svg",
412            IconName::Stop => "icons/stop.svg",
413            IconName::Strikethrough => "icons/strikethrough.svg",
414            IconName::Supermaven => "icons/supermaven.svg",
415            IconName::SupermavenDisabled => "icons/supermaven_disabled.svg",
416            IconName::SupermavenError => "icons/supermaven_error.svg",
417            IconName::SupermavenInit => "icons/supermaven_init.svg",
418            IconName::Tab => "icons/tab.svg",
419            IconName::Terminal => "icons/terminal.svg",
420            IconName::TextCursor => "icons/text-cursor.svg",
421            IconName::TextSelect => "icons/text_select.svg",
422            IconName::Trash => "icons/trash.svg",
423            IconName::TriangleRight => "icons/triangle_right.svg",
424            IconName::Update => "icons/update.svg",
425            IconName::Undo => "icons/undo.svg",
426            IconName::WholeWord => "icons/word_search.svg",
427            IconName::XCircle => "icons/error.svg",
428            IconName::ZedAssistant => "icons/zed_assistant.svg",
429            IconName::ZedAssistantFilled => "icons/zed_assistant_filled.svg",
430            IconName::ZedXCopilot => "icons/zed_x_copilot.svg",
431            IconName::Visible => "icons/visible.svg",
432        }
433    }
434}
435
436#[derive(IntoElement)]
437pub struct Icon {
438    path: SharedString,
439    color: Color,
440    size: Rems,
441    transformation: Transformation,
442}
443
444impl Icon {
445    pub fn new(icon: IconName) -> Self {
446        Self {
447            path: icon.path().into(),
448            color: Color::default(),
449            size: IconSize::default().rems(),
450            transformation: Transformation::default(),
451        }
452    }
453
454    pub fn from_path(path: impl Into<SharedString>) -> Self {
455        Self {
456            path: path.into(),
457            color: Color::default(),
458            size: IconSize::default().rems(),
459            transformation: Transformation::default(),
460        }
461    }
462
463    pub fn color(mut self, color: Color) -> Self {
464        self.color = color;
465        self
466    }
467
468    pub fn size(mut self, size: IconSize) -> Self {
469        self.size = size.rems();
470        self
471    }
472
473    /// Sets a custom size for the icon, in [`Rems`].
474    ///
475    /// Not to be exposed outside of the `ui` crate.
476    pub(crate) fn custom_size(mut self, size: Rems) -> Self {
477        self.size = size;
478        self
479    }
480
481    pub fn transform(mut self, transformation: Transformation) -> Self {
482        self.transformation = transformation;
483        self
484    }
485}
486
487impl RenderOnce for Icon {
488    fn render(self, cx: &mut WindowContext) -> impl IntoElement {
489        svg()
490            .with_transformation(self.transformation)
491            .size(self.size)
492            .flex_none()
493            .path(self.path)
494            .text_color(self.color.color(cx))
495    }
496}
497
498#[derive(IntoElement)]
499pub struct DecoratedIcon {
500    icon: Icon,
501    decoration: IconDecoration,
502    decoration_color: Color,
503    parent_background: Option<Hsla>,
504}
505
506impl DecoratedIcon {
507    pub fn new(icon: Icon, decoration: IconDecoration) -> Self {
508        Self {
509            icon,
510            decoration,
511            decoration_color: Color::Default,
512            parent_background: None,
513        }
514    }
515
516    pub fn decoration_color(mut self, color: Color) -> Self {
517        self.decoration_color = color;
518        self
519    }
520
521    pub fn parent_background(mut self, background: Option<Hsla>) -> Self {
522        self.parent_background = background;
523        self
524    }
525}
526
527impl RenderOnce for DecoratedIcon {
528    fn render(self, cx: &mut WindowContext) -> impl IntoElement {
529        let background = self
530            .parent_background
531            .unwrap_or(cx.theme().colors().background);
532
533        let size = self.icon.size;
534
535        let decoration_icon = match self.decoration {
536            IconDecoration::Strikethrough => IconName::Strikethrough,
537            IconDecoration::IndicatorDot => IconName::Indicator,
538            IconDecoration::X => IconName::IndicatorX,
539        };
540
541        let decoration_svg = |icon: IconName| {
542            svg()
543                .absolute()
544                .top_0()
545                .left_0()
546                .path(icon.path())
547                .size(size)
548                .flex_none()
549                .text_color(self.decoration_color.color(cx))
550        };
551
552        let decoration_knockout = |icon: IconName| {
553            svg()
554                .absolute()
555                .top(-rems_from_px(2.))
556                .left(-rems_from_px(3.))
557                .path(icon.path())
558                .size(size + rems_from_px(2.))
559                .flex_none()
560                .text_color(background)
561        };
562
563        div()
564            .relative()
565            .size(self.icon.size)
566            .child(self.icon)
567            .child(decoration_knockout(decoration_icon))
568            .child(decoration_svg(decoration_icon))
569    }
570}
571
572#[derive(IntoElement)]
573pub struct IconWithIndicator {
574    icon: Icon,
575    indicator: Option<Indicator>,
576    indicator_border_color: Option<Hsla>,
577}
578
579impl IconWithIndicator {
580    pub fn new(icon: Icon, indicator: Option<Indicator>) -> Self {
581        Self {
582            icon,
583            indicator,
584            indicator_border_color: None,
585        }
586    }
587
588    pub fn indicator(mut self, indicator: Option<Indicator>) -> Self {
589        self.indicator = indicator;
590        self
591    }
592
593    pub fn indicator_color(mut self, color: Color) -> Self {
594        if let Some(indicator) = self.indicator.as_mut() {
595            indicator.color = color;
596        }
597        self
598    }
599
600    pub fn indicator_border_color(mut self, color: Option<Hsla>) -> Self {
601        self.indicator_border_color = color;
602        self
603    }
604}
605
606impl RenderOnce for IconWithIndicator {
607    fn render(self, cx: &mut WindowContext) -> impl IntoElement {
608        let indicator_border_color = self
609            .indicator_border_color
610            .unwrap_or_else(|| cx.theme().colors().elevated_surface_background);
611
612        div()
613            .relative()
614            .child(self.icon)
615            .when_some(self.indicator, |this, indicator| {
616                this.child(
617                    div()
618                        .absolute()
619                        .w_2()
620                        .h_2()
621                        .border_1()
622                        .border_color(indicator_border_color)
623                        .rounded_full()
624                        .bottom_neg_0p5()
625                        .right_neg_1()
626                        .child(indicator),
627                )
628            })
629    }
630}