icon.rs

  1use gpui::{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    Selected,
 26}
 27
 28impl IconColor {
 29    pub fn color(self, cx: &WindowContext) -> Hsla {
 30        match self {
 31            IconColor::Default => cx.theme().colors().icon,
 32            IconColor::Muted => cx.theme().colors().icon_muted,
 33            IconColor::Disabled => cx.theme().colors().icon_disabled,
 34            IconColor::Placeholder => cx.theme().colors().icon_placeholder,
 35            IconColor::Accent => cx.theme().colors().icon_accent,
 36            IconColor::Error => cx.theme().status().error,
 37            IconColor::Warning => cx.theme().status().warning,
 38            IconColor::Success => cx.theme().status().success,
 39            IconColor::Info => cx.theme().status().info,
 40            IconColor::Selected => cx.theme().colors().icon_accent,
 41        }
 42    }
 43}
 44
 45#[derive(Debug, PartialEq, Copy, Clone, EnumIter)]
 46pub enum Icon {
 47    Ai,
 48    ArrowLeft,
 49    ArrowRight,
 50    ArrowUpRight,
 51    AudioOff,
 52    AudioOn,
 53    Bolt,
 54    Check,
 55    ChevronDown,
 56    ChevronLeft,
 57    ChevronRight,
 58    ChevronUp,
 59    Close,
 60    Dash,
 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    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    Bell,
 96    BellOff,
 97    BellRing,
 98    MailOpen,
 99    AtSign,
100}
101
102impl Icon {
103    pub fn path(self) -> &'static str {
104        match self {
105            Icon::Ai => "icons/ai.svg",
106            Icon::ArrowLeft => "icons/arrow_left.svg",
107            Icon::ArrowRight => "icons/arrow_right.svg",
108            Icon::ArrowUpRight => "icons/arrow_up_right.svg",
109            Icon::AudioOff => "icons/speaker-off.svg",
110            Icon::AudioOn => "icons/speaker-loud.svg",
111            Icon::Bolt => "icons/bolt.svg",
112            Icon::Check => "icons/check.svg",
113            Icon::ChevronDown => "icons/chevron_down.svg",
114            Icon::ChevronLeft => "icons/chevron_left.svg",
115            Icon::ChevronRight => "icons/chevron_right.svg",
116            Icon::ChevronUp => "icons/chevron_up.svg",
117            Icon::Close => "icons/x.svg",
118            Icon::Dash => "icons/dash.svg",
119            Icon::Exit => "icons/exit.svg",
120            Icon::ExclamationTriangle => "icons/warning.svg",
121            Icon::File => "icons/file.svg",
122            Icon::FileGeneric => "icons/file_icons/file.svg",
123            Icon::FileDoc => "icons/file_icons/book.svg",
124            Icon::FileGit => "icons/file_icons/git.svg",
125            Icon::FileLock => "icons/file_icons/lock.svg",
126            Icon::FileRust => "icons/file_icons/rust.svg",
127            Icon::FileToml => "icons/file_icons/toml.svg",
128            Icon::FileTree => "icons/project.svg",
129            Icon::Folder => "icons/file_icons/folder.svg",
130            Icon::FolderOpen => "icons/file_icons/folder_open.svg",
131            Icon::FolderX => "icons/stop_sharing.svg",
132            Icon::Hash => "icons/hash.svg",
133            Icon::InlayHint => "icons/inlay_hint.svg",
134            Icon::MagicWand => "icons/magic-wand.svg",
135            Icon::MagnifyingGlass => "icons/magnifying_glass.svg",
136            Icon::Maximize => "icons/maximize.svg",
137            Icon::Menu => "icons/menu.svg",
138            Icon::MessageBubbles => "icons/conversations.svg",
139            Icon::Mic => "icons/mic.svg",
140            Icon::MicMute => "icons/mic-mute.svg",
141            Icon::Plus => "icons/plus.svg",
142            Icon::Quote => "icons/quote.svg",
143            Icon::Replace => "icons/replace.svg",
144            Icon::ReplaceAll => "icons/replace_all.svg",
145            Icon::Screen => "icons/desktop.svg",
146            Icon::SelectAll => "icons/select-all.svg",
147            Icon::Split => "icons/split.svg",
148            Icon::SplitMessage => "icons/split_message.svg",
149            Icon::Terminal => "icons/terminal.svg",
150            Icon::XCircle => "icons/error.svg",
151            Icon::Copilot => "icons/copilot.svg",
152            Icon::Envelope => "icons/feedback.svg",
153            Icon::Bell => "icons/bell.svg",
154            Icon::BellOff => "icons/bell-off.svg",
155            Icon::BellRing => "icons/bell-ring.svg",
156            Icon::MailOpen => "icons/mail-open.svg",
157            Icon::AtSign => "icons/at-sign.svg",
158        }
159    }
160}
161
162#[derive(Component)]
163pub struct IconElement {
164    icon: Icon,
165    color: IconColor,
166    size: IconSize,
167}
168
169impl IconElement {
170    pub fn new(icon: Icon) -> Self {
171        Self {
172            icon,
173            color: IconColor::default(),
174            size: IconSize::default(),
175        }
176    }
177
178    pub fn color(mut self, color: IconColor) -> Self {
179        self.color = color;
180        self
181    }
182
183    pub fn size(mut self, size: IconSize) -> Self {
184        self.size = size;
185        self
186    }
187
188    fn render<V: 'static>(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
189        let svg_size = match self.size {
190            IconSize::Small => rems(0.75),
191            IconSize::Medium => rems(0.9375),
192        };
193
194        svg()
195            .size(svg_size)
196            .flex_none()
197            .path(self.icon.path())
198            .text_color(self.color.color(cx))
199    }
200}
201
202#[cfg(feature = "stories")]
203pub use stories::*;
204
205#[cfg(feature = "stories")]
206mod stories {
207    use gpui::{Div, Render};
208    use strum::IntoEnumIterator;
209
210    use crate::Story;
211
212    use super::*;
213
214    pub struct IconStory;
215
216    impl Render for IconStory {
217        type Element = Div<Self>;
218
219        fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
220            let icons = Icon::iter();
221
222            Story::container(cx)
223                .child(Story::title_for::<_, IconElement>(cx))
224                .child(Story::label(cx, "All Icons"))
225                .child(div().flex().gap_3().children(icons.map(IconElement::new)))
226        }
227    }
228}