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