1use std::marker::PhantomData;
2
3use gpui2::{svg, Hsla};
4use strum::EnumIter;
5
6use crate::prelude::*;
7use crate::theme::old_theme;
8
9#[derive(Default, PartialEq, Copy, Clone)]
10pub enum IconSize {
11 Small,
12 #[default]
13 Medium,
14}
15
16#[derive(Default, PartialEq, Copy, Clone)]
17pub enum IconColor {
18 #[default]
19 Default,
20 Muted,
21 Disabled,
22 Placeholder,
23 Accent,
24 Error,
25 Warning,
26 Success,
27 Info,
28}
29
30impl IconColor {
31 pub fn color(self, cx: &WindowContext) -> Hsla {
32 let theme = old_theme(cx);
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(Debug, 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)]
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 fill = self.color.color(cx);
181 let svg_size = match self.size {
182 IconSize::Small => ui_size(cx, 12. / 14.),
183 IconSize::Medium => ui_size(cx, 15. / 14.),
184 };
185
186 svg()
187 .size(svg_size)
188 .flex_none()
189 .path(self.icon.path())
190 .text_color(fill)
191 }
192}
193
194#[cfg(feature = "stories")]
195pub use stories::*;
196
197#[cfg(feature = "stories")]
198mod stories {
199 use strum::IntoEnumIterator;
200
201 use crate::Story;
202
203 use super::*;
204
205 #[derive(Element, Default)]
206 pub struct IconStory<S: 'static + Send + Sync> {
207 state_type: PhantomData<S>,
208 }
209
210 impl<S: 'static + Send + Sync> IconStory<S> {
211 pub fn new() -> Self {
212 Self {
213 state_type: PhantomData,
214 }
215 }
216
217 fn render(
218 &mut self,
219 _view: &mut S,
220 cx: &mut ViewContext<S>,
221 ) -> impl Element<ViewState = S> {
222 let icons = Icon::iter();
223
224 Story::container(cx)
225 .child(Story::title_for::<_, IconElement<S>>(cx))
226 .child(Story::label(cx, "All Icons"))
227 .child(div().flex().gap_3().children(icons.map(IconElement::new)))
228 }
229 }
230}