icon.rs

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