icon.rs

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