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    Medium,
 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    Exit,
 62    ExclamationTriangle,
 63    File,
 64    FileGeneric,
 65    FileDoc,
 66    FileGit,
 67    FileLock,
 68    FileRust,
 69    FileToml,
 70    FileTree,
 71    Folder,
 72    FolderOpen,
 73    FolderX,
 74    #[default]
 75    Hash,
 76    InlayHint,
 77    MagicWand,
 78    MagnifyingGlass,
 79    Maximize,
 80    Menu,
 81    MessageBubbles,
 82    Mic,
 83    MicMute,
 84    Plus,
 85    Quote,
 86    Replace,
 87    ReplaceAll,
 88    Screen,
 89    SelectAll,
 90    Split,
 91    SplitMessage,
 92    Terminal,
 93    XCircle,
 94    Copilot,
 95    Envelope,
 96}
 97
 98impl Icon {
 99    pub fn path(self) -> &'static str {
100        match self {
101            Icon::Ai => "icons/ai.svg",
102            Icon::ArrowLeft => "icons/arrow_left.svg",
103            Icon::ArrowRight => "icons/arrow_right.svg",
104            Icon::ArrowUpRight => "icons/arrow_up_right.svg",
105            Icon::AudioOff => "icons/speaker-off.svg",
106            Icon::AudioOn => "icons/speaker-loud.svg",
107            Icon::Bolt => "icons/bolt.svg",
108            Icon::ChevronDown => "icons/chevron_down.svg",
109            Icon::ChevronLeft => "icons/chevron_left.svg",
110            Icon::ChevronRight => "icons/chevron_right.svg",
111            Icon::ChevronUp => "icons/chevron_up.svg",
112            Icon::Close => "icons/x.svg",
113            Icon::Exit => "icons/exit.svg",
114            Icon::ExclamationTriangle => "icons/warning.svg",
115            Icon::File => "icons/file.svg",
116            Icon::FileGeneric => "icons/file_icons/file.svg",
117            Icon::FileDoc => "icons/file_icons/book.svg",
118            Icon::FileGit => "icons/file_icons/git.svg",
119            Icon::FileLock => "icons/file_icons/lock.svg",
120            Icon::FileRust => "icons/file_icons/rust.svg",
121            Icon::FileToml => "icons/file_icons/toml.svg",
122            Icon::FileTree => "icons/project.svg",
123            Icon::Folder => "icons/file_icons/folder.svg",
124            Icon::FolderOpen => "icons/file_icons/folder_open.svg",
125            Icon::FolderX => "icons/stop_sharing.svg",
126            Icon::Hash => "icons/hash.svg",
127            Icon::InlayHint => "icons/inlay_hint.svg",
128            Icon::MagicWand => "icons/magic-wand.svg",
129            Icon::MagnifyingGlass => "icons/magnifying_glass.svg",
130            Icon::Maximize => "icons/maximize.svg",
131            Icon::Menu => "icons/menu.svg",
132            Icon::MessageBubbles => "icons/conversations.svg",
133            Icon::Mic => "icons/mic.svg",
134            Icon::MicMute => "icons/mic-mute.svg",
135            Icon::Plus => "icons/plus.svg",
136            Icon::Quote => "icons/quote.svg",
137            Icon::Replace => "icons/replace.svg",
138            Icon::ReplaceAll => "icons/replace_all.svg",
139            Icon::Screen => "icons/desktop.svg",
140            Icon::SelectAll => "icons/select-all.svg",
141            Icon::Split => "icons/split.svg",
142            Icon::SplitMessage => "icons/split_message.svg",
143            Icon::Terminal => "icons/terminal.svg",
144            Icon::XCircle => "icons/error.svg",
145            Icon::Copilot => "icons/copilot.svg",
146            Icon::Envelope => "icons/feedback.svg",
147        }
148    }
149}
150
151#[derive(Element, Clone)]
152pub struct IconElement<S: 'static + Send + Sync> {
153    state_type: PhantomData<S>,
154    icon: Icon,
155    color: IconColor,
156    size: IconSize,
157}
158
159impl<S: 'static + Send + Sync> IconElement<S> {
160    pub fn new(icon: Icon) -> Self {
161        Self {
162            state_type: PhantomData,
163            icon,
164            color: IconColor::default(),
165            size: IconSize::default(),
166        }
167    }
168
169    pub fn color(mut self, color: IconColor) -> Self {
170        self.color = color;
171        self
172    }
173
174    pub fn size(mut self, size: IconSize) -> Self {
175        self.size = size;
176        self
177    }
178
179    fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
180        let theme = theme(cx);
181        let fill = self.color.color(theme);
182        let svg_size = match self.size {
183            IconSize::Small => ui_size(cx, 12. / 14.),
184            IconSize::Medium => ui_size(cx, 15. / 14.),
185        };
186
187        svg()
188            .size(svg_size)
189            .flex_none()
190            .path(self.icon.path())
191            .text_color(fill)
192    }
193}
194
195#[cfg(feature = "stories")]
196pub use stories::*;
197
198#[cfg(feature = "stories")]
199mod stories {
200    use strum::IntoEnumIterator;
201
202    use crate::Story;
203
204    use super::*;
205
206    #[derive(Element, Default)]
207    pub struct IconStory<S: 'static + Send + Sync> {
208        state_type: PhantomData<S>,
209    }
210
211    impl<S: 'static + Send + Sync> IconStory<S> {
212        pub fn new() -> Self {
213            Self {
214                state_type: PhantomData,
215            }
216        }
217
218        fn render(
219            &mut self,
220            _view: &mut S,
221            cx: &mut ViewContext<S>,
222        ) -> impl Element<ViewState = S> {
223            let icons = Icon::iter();
224
225            Story::container(cx)
226                .child(Story::title_for::<_, IconElement<S>>(cx))
227                .child(Story::label(cx, "All Icons"))
228                .child(div().flex().gap_3().children(icons.map(IconElement::new)))
229        }
230    }
231}