1use std::marker::PhantomData;
2use std::sync::Arc;
3
4use gpui3::{svg, Hsla};
5use strum::EnumIter;
6
7use crate::prelude::*;
8use crate::theme::{theme, Theme};
9
10#[derive(Default, PartialEq, Copy, Clone)]
11pub enum IconSize {
12 Small,
13 #[default]
14 Large,
15}
16
17#[derive(Default, PartialEq, Copy, Clone)]
18pub enum IconColor {
19 #[default]
20 Default,
21 Muted,
22 Disabled,
23 Placeholder,
24 Accent,
25 Error,
26 Warning,
27 Success,
28 Info,
29}
30
31impl IconColor {
32 pub fn color(self, theme: Arc<Theme>) -> Hsla {
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(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 ExclamationTriangle,
62 File,
63 FileGeneric,
64 FileDoc,
65 FileGit,
66 FileLock,
67 FileRust,
68 FileToml,
69 FileTree,
70 Folder,
71 FolderOpen,
72 FolderX,
73 #[default]
74 Hash,
75 InlayHint,
76 MagicWand,
77 MagnifyingGlass,
78 Maximize,
79 Menu,
80 MessageBubbles,
81 Mic,
82 MicMute,
83 Plus,
84 Quote,
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::ExclamationTriangle => "icons/warning.svg",
111 Icon::File => "icons/file.svg",
112 Icon::FileGeneric => "icons/file_icons/file.svg",
113 Icon::FileDoc => "icons/file_icons/book.svg",
114 Icon::FileGit => "icons/file_icons/git.svg",
115 Icon::FileLock => "icons/file_icons/lock.svg",
116 Icon::FileRust => "icons/file_icons/rust.svg",
117 Icon::FileToml => "icons/file_icons/toml.svg",
118 Icon::FileTree => "icons/project.svg",
119 Icon::Folder => "icons/file_icons/folder.svg",
120 Icon::FolderOpen => "icons/file_icons/folder_open.svg",
121 Icon::FolderX => "icons/stop_sharing.svg",
122 Icon::Hash => "icons/hash.svg",
123 Icon::InlayHint => "icons/inlay_hint.svg",
124 Icon::MagicWand => "icons/magic-wand.svg",
125 Icon::MagnifyingGlass => "icons/magnifying_glass.svg",
126 Icon::Maximize => "icons/maximize.svg",
127 Icon::Menu => "icons/menu.svg",
128 Icon::MessageBubbles => "icons/conversations.svg",
129 Icon::Mic => "icons/mic.svg",
130 Icon::MicMute => "icons/mic-mute.svg",
131 Icon::Plus => "icons/plus.svg",
132 Icon::Quote => "icons/quote.svg",
133 Icon::Screen => "icons/desktop.svg",
134 Icon::SelectAll => "icons/select-all.svg",
135 Icon::Split => "icons/split.svg",
136 Icon::SplitMessage => "icons/split_message.svg",
137 Icon::Terminal => "icons/terminal.svg",
138 Icon::XCircle => "icons/error.svg",
139 Icon::Copilot => "icons/copilot.svg",
140 Icon::Envelope => "icons/feedback.svg",
141 }
142 }
143}
144
145#[derive(Element, Clone)]
146pub struct IconElement<S: 'static + Send + Sync> {
147 state_type: PhantomData<S>,
148 icon: Icon,
149 color: IconColor,
150 size: IconSize,
151}
152
153impl<S: 'static + Send + Sync> IconElement<S> {
154 pub fn new(icon: Icon) -> Self {
155 Self {
156 state_type: PhantomData,
157 icon,
158 color: IconColor::default(),
159 size: IconSize::default(),
160 }
161 }
162
163 pub fn color(mut self, color: IconColor) -> Self {
164 self.color = color;
165 self
166 }
167
168 pub fn size(mut self, size: IconSize) -> Self {
169 self.size = size;
170 self
171 }
172
173 fn render(&mut self, cx: &mut ViewContext<S>) -> impl Element<State = S> {
174 let theme = theme(cx);
175 let fill = self.color.color(theme);
176
177 let sized_svg = match self.size {
178 IconSize::Small => svg().size_3p5(),
179 IconSize::Large => svg().size_4(),
180 };
181
182 sized_svg.flex_none().path(self.icon.path()).fill(fill)
183 }
184}