icon.rs

  1use gpui2::{rems, svg, Hsla};
  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(Default, PartialEq, Copy, Clone)]
 14pub enum IconColor {
 15    #[default]
 16    Default,
 17    Muted,
 18    Disabled,
 19    Placeholder,
 20    Accent,
 21    Error,
 22    Warning,
 23    Success,
 24    Info,
 25}
 26
 27impl IconColor {
 28    pub fn color(self, cx: &WindowContext) -> Hsla {
 29        match self {
 30            IconColor::Default => cx.theme().colors().icon,
 31            IconColor::Muted => cx.theme().colors().icon_muted,
 32            IconColor::Disabled => cx.theme().colors().icon_disabled,
 33            IconColor::Placeholder => cx.theme().colors().icon_placeholder,
 34            IconColor::Accent => cx.theme().colors().icon_accent,
 35            IconColor::Error => cx.theme().status().error,
 36            IconColor::Warning => cx.theme().status().warning,
 37            IconColor::Success => cx.theme().status().success,
 38            IconColor::Info => cx.theme().status().info,
 39        }
 40    }
 41}
 42
 43#[derive(Debug, PartialEq, Copy, Clone, EnumIter)]
 44pub enum Icon {
 45    Ai,
 46    ArrowLeft,
 47    ArrowRight,
 48    ArrowUpRight,
 49    AudioOff,
 50    AudioOn,
 51    Bolt,
 52    Check,
 53    ChevronDown,
 54    ChevronLeft,
 55    ChevronRight,
 56    ChevronUp,
 57    Close,
 58    Exit,
 59    ExclamationTriangle,
 60    File,
 61    FileGeneric,
 62    FileDoc,
 63    FileGit,
 64    FileLock,
 65    FileRust,
 66    FileToml,
 67    FileTree,
 68    Folder,
 69    FolderOpen,
 70    FolderX,
 71    Hash,
 72    InlayHint,
 73    MagicWand,
 74    MagnifyingGlass,
 75    Maximize,
 76    Menu,
 77    MessageBubbles,
 78    Mic,
 79    MicMute,
 80    Plus,
 81    Quote,
 82    Replace,
 83    ReplaceAll,
 84    Screen,
 85    SelectAll,
 86    Split,
 87    SplitMessage,
 88    Terminal,
 89    XCircle,
 90    Copilot,
 91    Envelope,
 92    Bell,
 93    BellOff,
 94    BellRing,
 95    MailOpen,
 96    AtSign,
 97}
 98
 99impl Icon {
100    pub fn path(self) -> &'static str {
101        match self {
102            Icon::Ai => "icons/ai.svg",
103            Icon::ArrowLeft => "icons/arrow_left.svg",
104            Icon::ArrowRight => "icons/arrow_right.svg",
105            Icon::ArrowUpRight => "icons/arrow_up_right.svg",
106            Icon::AudioOff => "icons/speaker-off.svg",
107            Icon::AudioOn => "icons/speaker-loud.svg",
108            Icon::Bolt => "icons/bolt.svg",
109            Icon::Check => "icons/check.svg",
110            Icon::ChevronDown => "icons/chevron_down.svg",
111            Icon::ChevronLeft => "icons/chevron_left.svg",
112            Icon::ChevronRight => "icons/chevron_right.svg",
113            Icon::ChevronUp => "icons/chevron_up.svg",
114            Icon::Close => "icons/x.svg",
115            Icon::Exit => "icons/exit.svg",
116            Icon::ExclamationTriangle => "icons/warning.svg",
117            Icon::File => "icons/file.svg",
118            Icon::FileGeneric => "icons/file_icons/file.svg",
119            Icon::FileDoc => "icons/file_icons/book.svg",
120            Icon::FileGit => "icons/file_icons/git.svg",
121            Icon::FileLock => "icons/file_icons/lock.svg",
122            Icon::FileRust => "icons/file_icons/rust.svg",
123            Icon::FileToml => "icons/file_icons/toml.svg",
124            Icon::FileTree => "icons/project.svg",
125            Icon::Folder => "icons/file_icons/folder.svg",
126            Icon::FolderOpen => "icons/file_icons/folder_open.svg",
127            Icon::FolderX => "icons/stop_sharing.svg",
128            Icon::Hash => "icons/hash.svg",
129            Icon::InlayHint => "icons/inlay_hint.svg",
130            Icon::MagicWand => "icons/magic-wand.svg",
131            Icon::MagnifyingGlass => "icons/magnifying_glass.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::Quote => "icons/quote.svg",
139            Icon::Replace => "icons/replace.svg",
140            Icon::ReplaceAll => "icons/replace_all.svg",
141            Icon::Screen => "icons/desktop.svg",
142            Icon::SelectAll => "icons/select-all.svg",
143            Icon::Split => "icons/split.svg",
144            Icon::SplitMessage => "icons/split_message.svg",
145            Icon::Terminal => "icons/terminal.svg",
146            Icon::XCircle => "icons/error.svg",
147            Icon::Copilot => "icons/copilot.svg",
148            Icon::Envelope => "icons/feedback.svg",
149            Icon::Bell => "icons/bell.svg",
150            Icon::BellOff => "icons/bell-off.svg",
151            Icon::BellRing => "icons/bell-ring.svg",
152            Icon::MailOpen => "icons/mail-open.svg",
153            Icon::AtSign => "icons/at-sign.svg",
154        }
155    }
156}
157
158#[derive(Component)]
159pub struct IconElement {
160    icon: Icon,
161    color: IconColor,
162    size: IconSize,
163}
164
165impl IconElement {
166    pub fn new(icon: Icon) -> Self {
167        Self {
168            icon,
169            color: IconColor::default(),
170            size: IconSize::default(),
171        }
172    }
173
174    pub fn color(mut self, color: IconColor) -> Self {
175        self.color = color;
176        self
177    }
178
179    pub fn size(mut self, size: IconSize) -> Self {
180        self.size = size;
181        self
182    }
183
184    fn render<V: 'static>(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
185        let fill = self.color.color(cx);
186        let svg_size = match self.size {
187            IconSize::Small => rems(0.75),
188            IconSize::Medium => rems(0.9375),
189        };
190
191        svg()
192            .size(svg_size)
193            .flex_none()
194            .path(self.icon.path())
195            .text_color(fill)
196    }
197}
198
199#[cfg(feature = "stories")]
200pub use stories::*;
201
202#[cfg(feature = "stories")]
203mod stories {
204    use gpui2::{Div, Render};
205    use strum::IntoEnumIterator;
206
207    use crate::Story;
208
209    use super::*;
210
211    pub struct IconStory;
212
213    impl Render for IconStory {
214        type Element = Div<Self>;
215
216        fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
217            let icons = Icon::iter();
218
219            Story::container(cx)
220                .child(Story::title_for::<_, IconElement>(cx))
221                .child(Story::label(cx, "All Icons"))
222                .child(div().flex().gap_3().children(icons.map(IconElement::new)))
223        }
224    }
225}