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    hover_color: Option<IconColor>,
163    size: IconSize,
164}
165
166impl IconElement {
167    pub fn new(icon: Icon) -> Self {
168        Self {
169            icon,
170            color: IconColor::default(),
171            hover_color: None,
172            size: IconSize::default(),
173        }
174    }
175
176    pub fn color(mut self, color: IconColor) -> Self {
177        self.color = color;
178        self
179    }
180
181    pub fn hover_color(mut self, hover_color: impl Into<Option<IconColor>>) -> Self {
182        self.hover_color = hover_color.into();
183        self
184    }
185
186    pub fn size(mut self, size: IconSize) -> Self {
187        self.size = size;
188        self
189    }
190
191    fn render<V: 'static>(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
192        let svg_size = match self.size {
193            IconSize::Small => rems(0.75),
194            IconSize::Medium => rems(0.9375),
195        };
196
197        svg()
198            .size(svg_size)
199            .flex_none()
200            .path(self.icon.path())
201            .text_color(self.color.color(cx))
202            .when_some(self.hover_color, |this, hover_color| {
203                this.hover(|style| style.text_color(hover_color.color(cx)))
204            })
205    }
206}
207
208#[cfg(feature = "stories")]
209pub use stories::*;
210
211#[cfg(feature = "stories")]
212mod stories {
213    use gpui2::{Div, Render};
214    use strum::IntoEnumIterator;
215
216    use crate::Story;
217
218    use super::*;
219
220    pub struct IconStory;
221
222    impl Render for IconStory {
223        type Element = Div<Self>;
224
225        fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
226            let icons = Icon::iter();
227
228            Story::container(cx)
229                .child(Story::title_for::<_, IconElement>(cx))
230                .child(Story::label(cx, "All Icons"))
231                .child(div().flex().gap_3().children(icons.map(IconElement::new)))
232        }
233    }
234}