icon.rs

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