icon.rs

  1use gpui2::{svg, Hsla};
  2use strum::EnumIter;
  3
  4use crate::prelude::*;
  5use crate::theme::old_theme;
  6
  7#[derive(Default, PartialEq, Copy, Clone)]
  8pub enum IconSize {
  9    Small,
 10    #[default]
 11    Medium,
 12}
 13
 14#[derive(Default, PartialEq, Copy, Clone)]
 15pub enum IconColor {
 16    #[default]
 17    Default,
 18    Muted,
 19    Disabled,
 20    Placeholder,
 21    Accent,
 22    Error,
 23    Warning,
 24    Success,
 25    Info,
 26}
 27
 28impl IconColor {
 29    pub fn color(self, cx: &WindowContext) -> Hsla {
 30        let theme = old_theme(cx);
 31        match self {
 32            IconColor::Default => theme.lowest.base.default.foreground,
 33            IconColor::Muted => theme.lowest.variant.default.foreground,
 34            IconColor::Disabled => theme.lowest.base.disabled.foreground,
 35            IconColor::Placeholder => theme.lowest.base.disabled.foreground,
 36            IconColor::Accent => theme.lowest.accent.default.foreground,
 37            IconColor::Error => theme.lowest.negative.default.foreground,
 38            IconColor::Warning => theme.lowest.warning.default.foreground,
 39            IconColor::Success => theme.lowest.positive.default.foreground,
 40            IconColor::Info => theme.lowest.accent.default.foreground,
 41        }
 42    }
 43}
 44
 45#[derive(Debug, Default, PartialEq, Copy, Clone, EnumIter)]
 46pub enum Icon {
 47    Ai,
 48    ArrowLeft,
 49    ArrowRight,
 50    ArrowUpRight,
 51    AudioOff,
 52    AudioOn,
 53    Bolt,
 54    ChevronDown,
 55    ChevronLeft,
 56    ChevronRight,
 57    ChevronUp,
 58    Close,
 59    Exit,
 60    ExclamationTriangle,
 61    File,
 62    FileGeneric,
 63    FileDoc,
 64    FileGit,
 65    FileLock,
 66    FileRust,
 67    FileToml,
 68    FileTree,
 69    Folder,
 70    FolderOpen,
 71    FolderX,
 72    #[default]
 73    Hash,
 74    InlayHint,
 75    MagicWand,
 76    MagnifyingGlass,
 77    Maximize,
 78    Menu,
 79    MessageBubbles,
 80    Mic,
 81    MicMute,
 82    Plus,
 83    Quote,
 84    Replace,
 85    ReplaceAll,
 86    Screen,
 87    SelectAll,
 88    Split,
 89    SplitMessage,
 90    Terminal,
 91    XCircle,
 92    Copilot,
 93    Envelope,
 94}
 95
 96impl Icon {
 97    pub fn path(self) -> &'static str {
 98        match self {
 99            Icon::Ai => "icons/ai.svg",
100            Icon::ArrowLeft => "icons/arrow_left.svg",
101            Icon::ArrowRight => "icons/arrow_right.svg",
102            Icon::ArrowUpRight => "icons/arrow_up_right.svg",
103            Icon::AudioOff => "icons/speaker-off.svg",
104            Icon::AudioOn => "icons/speaker-loud.svg",
105            Icon::Bolt => "icons/bolt.svg",
106            Icon::ChevronDown => "icons/chevron_down.svg",
107            Icon::ChevronLeft => "icons/chevron_left.svg",
108            Icon::ChevronRight => "icons/chevron_right.svg",
109            Icon::ChevronUp => "icons/chevron_up.svg",
110            Icon::Close => "icons/x.svg",
111            Icon::Exit => "icons/exit.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(Component)]
150pub struct IconElement {
151    icon: Icon,
152    color: IconColor,
153    size: IconSize,
154}
155
156impl IconElement {
157    pub fn new(icon: Icon) -> Self {
158        Self {
159            icon,
160            color: IconColor::default(),
161            size: IconSize::default(),
162        }
163    }
164
165    pub fn color(mut self, color: IconColor) -> Self {
166        self.color = color;
167        self
168    }
169
170    pub fn size(mut self, size: IconSize) -> Self {
171        self.size = size;
172        self
173    }
174
175    fn render<V: 'static>(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
176        let fill = self.color.color(cx);
177        let svg_size = match self.size {
178            IconSize::Small => ui_size(cx, 12. / 14.),
179            IconSize::Medium => ui_size(cx, 15. / 14.),
180        };
181
182        svg()
183            .size(svg_size)
184            .flex_none()
185            .path(self.icon.path())
186            .text_color(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(Component)]
202    pub struct IconStory;
203
204    impl IconStory {
205        fn render<V: 'static>(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
206            let icons = Icon::iter();
207
208            Story::container(cx)
209                .child(Story::title_for::<_, IconElement>(cx))
210                .child(Story::label(cx, "All Icons"))
211                .child(div().flex().gap_3().children(icons.map(IconElement::new)))
212        }
213    }
214}