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