icon.rs

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