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