1use gpui::{svg, AnimationElement, Hsla, IntoElement, Rems, Transformation};
2use serde::{Deserialize, Serialize};
3use strum::{EnumIter, EnumString, IntoStaticStr};
4
5use crate::{prelude::*, Indicator};
6
7#[derive(IntoElement)]
8pub enum AnyIcon {
9 Icon(Icon),
10 AnimatedIcon(AnimationElement<Icon>),
11}
12
13impl AnyIcon {
14 /// Returns a new [`AnyIcon`] after applying the given mapping function
15 /// to the contained [`Icon`].
16 pub fn map(self, f: impl FnOnce(Icon) -> Icon) -> Self {
17 match self {
18 Self::Icon(icon) => Self::Icon(f(icon)),
19 Self::AnimatedIcon(animated_icon) => Self::AnimatedIcon(animated_icon.map_element(f)),
20 }
21 }
22}
23
24impl From<Icon> for AnyIcon {
25 fn from(value: Icon) -> Self {
26 Self::Icon(value)
27 }
28}
29
30impl From<AnimationElement<Icon>> for AnyIcon {
31 fn from(value: AnimationElement<Icon>) -> Self {
32 Self::AnimatedIcon(value)
33 }
34}
35
36impl RenderOnce for AnyIcon {
37 fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
38 match self {
39 Self::Icon(icon) => icon.into_any_element(),
40 Self::AnimatedIcon(animated_icon) => animated_icon.into_any_element(),
41 }
42 }
43}
44
45/// The decoration for an icon.
46///
47/// For example, this can show an indicator, an "x",
48/// or a diagonal strkethrough to indicate something is disabled.
49#[derive(Debug, PartialEq, Copy, Clone, EnumIter)]
50pub enum IconDecoration {
51 Strikethrough,
52 IndicatorDot,
53 X,
54}
55
56#[derive(Default, PartialEq, Copy, Clone)]
57pub enum IconSize {
58 /// 10px
59 Indicator,
60 /// 12px
61 XSmall,
62 /// 14px
63 Small,
64 #[default]
65 /// 16px
66 Medium,
67}
68
69impl IconSize {
70 pub fn rems(self) -> Rems {
71 match self {
72 IconSize::Indicator => rems_from_px(10.),
73 IconSize::XSmall => rems_from_px(12.),
74 IconSize::Small => rems_from_px(14.),
75 IconSize::Medium => rems_from_px(16.),
76 }
77 }
78
79 /// Returns the individual components of the square that contains this [`IconSize`].
80 ///
81 /// The returned tuple contains:
82 /// 1. The length of one side of the square
83 /// 2. The padding of one side of the square
84 pub fn square_components(&self, cx: &mut WindowContext) -> (Pixels, Pixels) {
85 let icon_size = self.rems() * cx.rem_size();
86 let padding = match self {
87 IconSize::Indicator => Spacing::None.px(cx),
88 IconSize::XSmall => Spacing::XSmall.px(cx),
89 IconSize::Small => Spacing::XSmall.px(cx),
90 IconSize::Medium => Spacing::XSmall.px(cx),
91 };
92
93 (icon_size, padding)
94 }
95
96 /// Returns the length of a side of the square that contains this [`IconSize`], with padding.
97 pub fn square(&self, cx: &mut WindowContext) -> Pixels {
98 let (icon_size, padding) = self.square_components(cx);
99
100 icon_size + padding * 2.
101 }
102}
103
104#[derive(
105 Debug, PartialEq, Eq, Copy, Clone, EnumIter, EnumString, IntoStaticStr, Serialize, Deserialize,
106)]
107pub enum IconName {
108 Ai,
109 AiAnthropic,
110 AiOpenAi,
111 AiGoogle,
112 AiOllama,
113 AiZed,
114 ArrowCircle,
115 ArrowDown,
116 ArrowDownFromLine,
117 ArrowLeft,
118 ArrowRight,
119 ArrowUp,
120 ArrowUpFromLine,
121 ArrowUpRight,
122 AtSign,
123 AudioOff,
124 AudioOn,
125 Backspace,
126 Bell,
127 BellDot,
128 BellOff,
129 BellRing,
130 Bolt,
131 Book,
132 BookCopy,
133 BookPlus,
134 CaseSensitive,
135 Check,
136 ChevronDown,
137 /// This chevron indicates a popover menu.
138 ChevronDownSmall,
139 ChevronLeft,
140 ChevronRight,
141 ChevronUp,
142 ChevronUpDown,
143 Close,
144 Code,
145 Collab,
146 Command,
147 Context,
148 Control,
149 Copilot,
150 CopilotDisabled,
151 CopilotError,
152 CopilotInit,
153 Copy,
154 CountdownTimer,
155 Dash,
156 Delete,
157 Disconnected,
158 Download,
159 Ellipsis,
160 Envelope,
161 Escape,
162 ExclamationTriangle,
163 Exit,
164 ExpandVertical,
165 ExternalLink,
166 Eye,
167 File,
168 FileDoc,
169 FileGeneric,
170 FileGit,
171 FileLock,
172 FileRust,
173 FileToml,
174 FileTree,
175 FileText,
176 FileCode,
177 Filter,
178 Folder,
179 FolderOpen,
180 FolderX,
181 Font,
182 FontSize,
183 FontWeight,
184 Github,
185 GenericMinimize,
186 GenericMaximize,
187 GenericClose,
188 GenericRestore,
189 Hash,
190 HistoryRerun,
191 Indicator,
192 IndicatorX,
193 InlayHint,
194 Library,
195 LineHeight,
196 Link,
197 ListTree,
198 MagicWand,
199 MagnifyingGlass,
200 MailOpen,
201 Maximize,
202 Menu,
203 MessageBubbles,
204 Mic,
205 MicMute,
206 Minimize,
207 Option,
208 PageDown,
209 PageUp,
210 Pencil,
211 Person,
212 Play,
213 Plus,
214 Public,
215 PullRequest,
216 Quote,
217 Regex,
218 ReplNeutral,
219 Replace,
220 ReplaceAll,
221 ReplaceNext,
222 ReplyArrowRight,
223 Rerun,
224 Return,
225 Reveal,
226 Route,
227 RotateCcw,
228 RotateCw,
229 Save,
230 Screen,
231 SearchSelection,
232 SelectAll,
233 Server,
234 Settings,
235 Shift,
236 Sliders,
237 SlidersAlt,
238 Snip,
239 Space,
240 Sparkle,
241 SparkleAlt,
242 SparkleFilled,
243 Spinner,
244 Split,
245 Star,
246 StarFilled,
247 Stop,
248 Strikethrough,
249 Supermaven,
250 SupermavenDisabled,
251 SupermavenError,
252 SupermavenInit,
253 Tab,
254 Terminal,
255 TextCursor,
256 TextSearch,
257 Trash,
258 TriangleRight,
259 Update,
260 WholeWord,
261 XCircle,
262 ZedAssistant,
263 ZedAssistantFilled,
264 ZedXCopilot,
265 Visible,
266}
267
268impl IconName {
269 pub fn path(self) -> &'static str {
270 match self {
271 IconName::Ai => "icons/ai.svg",
272 IconName::AiAnthropic => "icons/ai_anthropic.svg",
273 IconName::AiOpenAi => "icons/ai_open_ai.svg",
274 IconName::AiGoogle => "icons/ai_google.svg",
275 IconName::AiOllama => "icons/ai_ollama.svg",
276 IconName::AiZed => "icons/ai_zed.svg",
277 IconName::ArrowCircle => "icons/arrow_circle.svg",
278 IconName::ArrowDown => "icons/arrow_down.svg",
279 IconName::ArrowDownFromLine => "icons/arrow_down_from_line.svg",
280 IconName::ArrowLeft => "icons/arrow_left.svg",
281 IconName::ArrowRight => "icons/arrow_right.svg",
282 IconName::ArrowUp => "icons/arrow_up.svg",
283 IconName::ArrowUpFromLine => "icons/arrow_up_from_line.svg",
284 IconName::ArrowUpRight => "icons/arrow_up_right.svg",
285 IconName::AtSign => "icons/at_sign.svg",
286 IconName::AudioOff => "icons/speaker_off.svg",
287 IconName::AudioOn => "icons/speaker_loud.svg",
288 IconName::Backspace => "icons/backspace.svg",
289 IconName::Bell => "icons/bell.svg",
290 IconName::BellDot => "icons/bell_dot.svg",
291 IconName::BellOff => "icons/bell_off.svg",
292 IconName::BellRing => "icons/bell_ring.svg",
293 IconName::Bolt => "icons/bolt.svg",
294 IconName::Book => "icons/book.svg",
295 IconName::BookCopy => "icons/book_copy.svg",
296 IconName::BookPlus => "icons/book_plus.svg",
297 IconName::CaseSensitive => "icons/case_insensitive.svg",
298 IconName::Check => "icons/check.svg",
299 IconName::ChevronDown => "icons/chevron_down.svg",
300 IconName::ChevronDownSmall => "icons/chevron_down_small.svg",
301 IconName::ChevronLeft => "icons/chevron_left.svg",
302 IconName::ChevronRight => "icons/chevron_right.svg",
303 IconName::ChevronUp => "icons/chevron_up.svg",
304 IconName::ChevronUpDown => "icons/chevron_up_down.svg",
305 IconName::Close => "icons/x.svg",
306 IconName::Code => "icons/code.svg",
307 IconName::Collab => "icons/user_group_16.svg",
308 IconName::Command => "icons/command.svg",
309 IconName::Context => "icons/context.svg",
310 IconName::Control => "icons/control.svg",
311 IconName::Copilot => "icons/copilot.svg",
312 IconName::CopilotDisabled => "icons/copilot_disabled.svg",
313 IconName::CopilotError => "icons/copilot_error.svg",
314 IconName::CopilotInit => "icons/copilot_init.svg",
315 IconName::Copy => "icons/copy.svg",
316 IconName::CountdownTimer => "icons/countdown_timer.svg",
317 IconName::Dash => "icons/dash.svg",
318 IconName::Delete => "icons/delete.svg",
319 IconName::Disconnected => "icons/disconnected.svg",
320 IconName::Download => "icons/download.svg",
321 IconName::Ellipsis => "icons/ellipsis.svg",
322 IconName::Envelope => "icons/feedback.svg",
323 IconName::Escape => "icons/escape.svg",
324 IconName::ExclamationTriangle => "icons/warning.svg",
325 IconName::Exit => "icons/exit.svg",
326 IconName::ExpandVertical => "icons/expand_vertical.svg",
327 IconName::ExternalLink => "icons/external_link.svg",
328 IconName::Eye => "icons/eye.svg",
329 IconName::File => "icons/file.svg",
330 IconName::FileDoc => "icons/file_icons/book.svg",
331 IconName::FileGeneric => "icons/file_icons/file.svg",
332 IconName::FileGit => "icons/file_icons/git.svg",
333 IconName::FileLock => "icons/file_icons/lock.svg",
334 IconName::FileRust => "icons/file_icons/rust.svg",
335 IconName::FileToml => "icons/file_icons/toml.svg",
336 IconName::FileTree => "icons/project.svg",
337 IconName::FileCode => "icons/file_code.svg",
338 IconName::FileText => "icons/file_text.svg",
339 IconName::Filter => "icons/filter.svg",
340 IconName::Folder => "icons/file_icons/folder.svg",
341 IconName::FolderOpen => "icons/file_icons/folder_open.svg",
342 IconName::FolderX => "icons/stop_sharing.svg",
343 IconName::Font => "icons/font.svg",
344 IconName::FontSize => "icons/font_size.svg",
345 IconName::FontWeight => "icons/font_weight.svg",
346 IconName::Github => "icons/github.svg",
347 IconName::GenericMinimize => "icons/generic_minimize.svg",
348 IconName::GenericMaximize => "icons/generic_maximize.svg",
349 IconName::GenericClose => "icons/generic_close.svg",
350 IconName::GenericRestore => "icons/generic_restore.svg",
351 IconName::Hash => "icons/hash.svg",
352 IconName::HistoryRerun => "icons/history_rerun.svg",
353 IconName::Indicator => "icons/indicator.svg",
354 IconName::IndicatorX => "icons/indicator_x.svg",
355 IconName::InlayHint => "icons/inlay_hint.svg",
356 IconName::Library => "icons/library.svg",
357 IconName::LineHeight => "icons/line_height.svg",
358 IconName::Link => "icons/link.svg",
359 IconName::ListTree => "icons/list_tree.svg",
360 IconName::MagicWand => "icons/magic_wand.svg",
361 IconName::MagnifyingGlass => "icons/magnifying_glass.svg",
362 IconName::MailOpen => "icons/mail_open.svg",
363 IconName::Maximize => "icons/maximize.svg",
364 IconName::Menu => "icons/menu.svg",
365 IconName::MessageBubbles => "icons/conversations.svg",
366 IconName::Mic => "icons/mic.svg",
367 IconName::MicMute => "icons/mic_mute.svg",
368 IconName::Minimize => "icons/minimize.svg",
369 IconName::Option => "icons/option.svg",
370 IconName::PageDown => "icons/page_down.svg",
371 IconName::PageUp => "icons/page_up.svg",
372 IconName::Pencil => "icons/pencil.svg",
373 IconName::Person => "icons/person.svg",
374 IconName::Play => "icons/play.svg",
375 IconName::Plus => "icons/plus.svg",
376 IconName::Public => "icons/public.svg",
377 IconName::PullRequest => "icons/pull_request.svg",
378 IconName::Quote => "icons/quote.svg",
379 IconName::Regex => "icons/regex.svg",
380 IconName::ReplNeutral => "icons/repl_neutral.svg",
381 IconName::Replace => "icons/replace.svg",
382 IconName::ReplaceAll => "icons/replace_all.svg",
383 IconName::ReplaceNext => "icons/replace_next.svg",
384 IconName::ReplyArrowRight => "icons/reply_arrow_right.svg",
385 IconName::Rerun => "icons/rerun.svg",
386 IconName::Return => "icons/return.svg",
387 IconName::Reveal => "icons/reveal.svg",
388 IconName::RotateCcw => "icons/rotate_ccw.svg",
389 IconName::RotateCw => "icons/rotate_cw.svg",
390 IconName::Route => "icons/route.svg",
391 IconName::Save => "icons/save.svg",
392 IconName::Screen => "icons/desktop.svg",
393 IconName::SearchSelection => "icons/search_selection.svg",
394 IconName::SelectAll => "icons/select_all.svg",
395 IconName::Server => "icons/server.svg",
396 IconName::Settings => "icons/file_icons/settings.svg",
397 IconName::Shift => "icons/shift.svg",
398 IconName::Sliders => "icons/sliders.svg",
399 IconName::SlidersAlt => "icons/sliders-alt.svg",
400 IconName::Snip => "icons/snip.svg",
401 IconName::Space => "icons/space.svg",
402 IconName::Sparkle => "icons/sparkle.svg",
403 IconName::SparkleAlt => "icons/sparkle_alt.svg",
404 IconName::SparkleFilled => "icons/sparkle_filled.svg",
405 IconName::Spinner => "icons/spinner.svg",
406 IconName::Split => "icons/split.svg",
407 IconName::Star => "icons/star.svg",
408 IconName::StarFilled => "icons/star_filled.svg",
409 IconName::Stop => "icons/stop.svg",
410 IconName::Strikethrough => "icons/strikethrough.svg",
411 IconName::Supermaven => "icons/supermaven.svg",
412 IconName::SupermavenDisabled => "icons/supermaven_disabled.svg",
413 IconName::SupermavenError => "icons/supermaven_error.svg",
414 IconName::SupermavenInit => "icons/supermaven_init.svg",
415 IconName::Tab => "icons/tab.svg",
416 IconName::Terminal => "icons/terminal.svg",
417 IconName::TextCursor => "icons/text-cursor.svg",
418 IconName::TextSearch => "icons/text-search.svg",
419 IconName::Trash => "icons/trash.svg",
420 IconName::TriangleRight => "icons/triangle_right.svg",
421 IconName::Update => "icons/update.svg",
422 IconName::WholeWord => "icons/word_search.svg",
423 IconName::XCircle => "icons/error.svg",
424 IconName::ZedAssistant => "icons/zed_assistant.svg",
425 IconName::ZedAssistantFilled => "icons/zed_assistant_filled.svg",
426 IconName::ZedXCopilot => "icons/zed_x_copilot.svg",
427 IconName::Visible => "icons/visible.svg",
428 }
429 }
430}
431
432#[derive(IntoElement)]
433pub struct Icon {
434 path: SharedString,
435 color: Color,
436 size: Rems,
437 transformation: Transformation,
438}
439
440impl Icon {
441 pub fn new(icon: IconName) -> Self {
442 Self {
443 path: icon.path().into(),
444 color: Color::default(),
445 size: IconSize::default().rems(),
446 transformation: Transformation::default(),
447 }
448 }
449
450 pub fn from_path(path: impl Into<SharedString>) -> Self {
451 Self {
452 path: path.into(),
453 color: Color::default(),
454 size: IconSize::default().rems(),
455 transformation: Transformation::default(),
456 }
457 }
458
459 pub fn color(mut self, color: Color) -> Self {
460 self.color = color;
461 self
462 }
463
464 pub fn size(mut self, size: IconSize) -> Self {
465 self.size = size.rems();
466 self
467 }
468
469 /// Sets a custom size for the icon, in [`Rems`].
470 ///
471 /// Not to be exposed outside of the `ui` crate.
472 pub(crate) fn custom_size(mut self, size: Rems) -> Self {
473 self.size = size;
474 self
475 }
476
477 pub fn transform(mut self, transformation: Transformation) -> Self {
478 self.transformation = transformation;
479 self
480 }
481}
482
483impl RenderOnce for Icon {
484 fn render(self, cx: &mut WindowContext) -> impl IntoElement {
485 svg()
486 .with_transformation(self.transformation)
487 .size(self.size)
488 .flex_none()
489 .path(self.path)
490 .text_color(self.color.color(cx))
491 }
492}
493
494#[derive(IntoElement)]
495pub struct DecoratedIcon {
496 icon: Icon,
497 decoration: IconDecoration,
498 decoration_color: Color,
499 parent_background: Option<Hsla>,
500}
501
502impl DecoratedIcon {
503 pub fn new(icon: Icon, decoration: IconDecoration) -> Self {
504 Self {
505 icon,
506 decoration,
507 decoration_color: Color::Default,
508 parent_background: None,
509 }
510 }
511
512 pub fn decoration_color(mut self, color: Color) -> Self {
513 self.decoration_color = color;
514 self
515 }
516
517 pub fn parent_background(mut self, background: Option<Hsla>) -> Self {
518 self.parent_background = background;
519 self
520 }
521}
522
523impl RenderOnce for DecoratedIcon {
524 fn render(self, cx: &mut WindowContext) -> impl IntoElement {
525 let background = self
526 .parent_background
527 .unwrap_or(cx.theme().colors().background);
528
529 let size = self.icon.size;
530
531 let decoration_icon = match self.decoration {
532 IconDecoration::Strikethrough => IconName::Strikethrough,
533 IconDecoration::IndicatorDot => IconName::Indicator,
534 IconDecoration::X => IconName::IndicatorX,
535 };
536
537 let decoration_svg = |icon: IconName| {
538 svg()
539 .absolute()
540 .top_0()
541 .left_0()
542 .path(icon.path())
543 .size(size)
544 .flex_none()
545 .text_color(self.decoration_color.color(cx))
546 };
547
548 let decoration_knockout = |icon: IconName| {
549 svg()
550 .absolute()
551 .top(-rems_from_px(2.))
552 .left(-rems_from_px(3.))
553 .path(icon.path())
554 .size(size + rems_from_px(2.))
555 .flex_none()
556 .text_color(background)
557 };
558
559 div()
560 .relative()
561 .size(self.icon.size)
562 .child(self.icon)
563 .child(decoration_knockout(decoration_icon))
564 .child(decoration_svg(decoration_icon))
565 }
566}
567
568#[derive(IntoElement)]
569pub struct IconWithIndicator {
570 icon: Icon,
571 indicator: Option<Indicator>,
572 indicator_border_color: Option<Hsla>,
573}
574
575impl IconWithIndicator {
576 pub fn new(icon: Icon, indicator: Option<Indicator>) -> Self {
577 Self {
578 icon,
579 indicator,
580 indicator_border_color: None,
581 }
582 }
583
584 pub fn indicator(mut self, indicator: Option<Indicator>) -> Self {
585 self.indicator = indicator;
586 self
587 }
588
589 pub fn indicator_color(mut self, color: Color) -> Self {
590 if let Some(indicator) = self.indicator.as_mut() {
591 indicator.color = color;
592 }
593 self
594 }
595
596 pub fn indicator_border_color(mut self, color: Option<Hsla>) -> Self {
597 self.indicator_border_color = color;
598 self
599 }
600}
601
602impl RenderOnce for IconWithIndicator {
603 fn render(self, cx: &mut WindowContext) -> impl IntoElement {
604 let indicator_border_color = self
605 .indicator_border_color
606 .unwrap_or_else(|| cx.theme().colors().elevated_surface_background);
607
608 div()
609 .relative()
610 .child(self.icon)
611 .when_some(self.indicator, |this, indicator| {
612 this.child(
613 div()
614 .absolute()
615 .w_2()
616 .h_2()
617 .border_1()
618 .border_color(indicator_border_color)
619 .rounded_full()
620 .bottom_neg_0p5()
621 .right_neg_1()
622 .child(indicator),
623 )
624 })
625 }
626}