1use crate::{ActiveTheme, Sizable, Size};
2use gpui::{
3 prelude::FluentBuilder as _, svg, AnyElement, App, AppContext, Context, Entity, Hsla,
4 IntoElement, Radians, Render, RenderOnce, SharedString, StyleRefinement, Styled, Svg,
5 Transformation, Window,
6};
7
8#[derive(IntoElement, Clone)]
9pub enum IconName {
10 ALargeSmall,
11 ArrowDown,
12 ArrowLeft,
13 ArrowRight,
14 ArrowUp,
15 Asterisk,
16 Bell,
17 BookOpen,
18 Bot,
19 Building2,
20 Calendar,
21 CaseSensitive,
22 ChartPie,
23 Check,
24 ChevronDown,
25 ChevronLeft,
26 ChevronRight,
27 ChevronsUpDown,
28 ChevronUp,
29 CircleCheck,
30 CircleUser,
31 CircleX,
32 Close,
33 Copy,
34 Dash,
35 Delete,
36 Ellipsis,
37 EllipsisVertical,
38 ExternalLink,
39 Eye,
40 EyeOff,
41 File,
42 Folder,
43 FolderClosed,
44 FolderOpen,
45 Frame,
46 GalleryVerticalEnd,
47 GitHub,
48 Globe,
49 Heart,
50 HeartOff,
51 Inbox,
52 Info,
53 Inspector,
54 LayoutDashboard,
55 Loader,
56 LoaderCircle,
57 Map,
58 Maximize,
59 Menu,
60 Minimize,
61 Minus,
62 Moon,
63 Palette,
64 PanelBottom,
65 PanelBottomOpen,
66 PanelLeft,
67 PanelLeftClose,
68 PanelLeftOpen,
69 PanelRight,
70 PanelRightClose,
71 PanelRightOpen,
72 Plus,
73 Replace,
74 ResizeCorner,
75 Search,
76 Settings,
77 Settings2,
78 SortAscending,
79 SortDescending,
80 SquareTerminal,
81 Star,
82 StarOff,
83 Sun,
84 ThumbsDown,
85 ThumbsUp,
86 TriangleAlert,
87 User,
88 WindowClose,
89 WindowMaximize,
90 WindowMinimize,
91 WindowRestore,
92}
93
94impl IconName {
95 pub fn path(self) -> SharedString {
96 match self {
97 Self::ALargeSmall => "icons/a-large-small.svg",
98 Self::ArrowDown => "icons/arrow-down.svg",
99 Self::ArrowLeft => "icons/arrow-left.svg",
100 Self::ArrowRight => "icons/arrow-right.svg",
101 Self::ArrowUp => "icons/arrow-up.svg",
102 Self::Asterisk => "icons/asterisk.svg",
103 Self::Bell => "icons/bell.svg",
104 Self::BookOpen => "icons/book-open.svg",
105 Self::Bot => "icons/bot.svg",
106 Self::Building2 => "icons/building-2.svg",
107 Self::Calendar => "icons/calendar.svg",
108 Self::CaseSensitive => "icons/case-sensitive.svg",
109 Self::ChartPie => "icons/chart-pie.svg",
110 Self::Check => "icons/check.svg",
111 Self::ChevronDown => "icons/chevron-down.svg",
112 Self::ChevronLeft => "icons/chevron-left.svg",
113 Self::ChevronRight => "icons/chevron-right.svg",
114 Self::ChevronsUpDown => "icons/chevrons-up-down.svg",
115 Self::ChevronUp => "icons/chevron-up.svg",
116 Self::CircleCheck => "icons/circle-check.svg",
117 Self::CircleUser => "icons/circle-user.svg",
118 Self::CircleX => "icons/circle-x.svg",
119 Self::Close => "icons/close.svg",
120 Self::Copy => "icons/copy.svg",
121 Self::Dash => "icons/dash.svg",
122 Self::Delete => "icons/delete.svg",
123 Self::Ellipsis => "icons/ellipsis.svg",
124 Self::EllipsisVertical => "icons/ellipsis-vertical.svg",
125 Self::ExternalLink => "icons/external-link.svg",
126 Self::Eye => "icons/eye.svg",
127 Self::EyeOff => "icons/eye-off.svg",
128 Self::File => "icons/file.svg",
129 Self::Folder => "icons/folder.svg",
130 Self::FolderClosed => "icons/folder-closed.svg",
131 Self::FolderOpen => "icons/folder-open.svg",
132 Self::Frame => "icons/frame.svg",
133 Self::GalleryVerticalEnd => "icons/gallery-vertical-end.svg",
134 Self::GitHub => "icons/github.svg",
135 Self::Globe => "icons/globe.svg",
136 Self::Heart => "icons/heart.svg",
137 Self::HeartOff => "icons/heart-off.svg",
138 Self::Inbox => "icons/inbox.svg",
139 Self::Info => "icons/info.svg",
140 Self::Inspector => "icons/inspector.svg",
141 Self::LayoutDashboard => "icons/layout-dashboard.svg",
142 Self::Loader => "icons/loader.svg",
143 Self::LoaderCircle => "icons/loader-circle.svg",
144 Self::Map => "icons/map.svg",
145 Self::Maximize => "icons/maximize.svg",
146 Self::Menu => "icons/menu.svg",
147 Self::Minimize => "icons/minimize.svg",
148 Self::Minus => "icons/minus.svg",
149 Self::Moon => "icons/moon.svg",
150 Self::Palette => "icons/palette.svg",
151 Self::PanelBottom => "icons/panel-bottom.svg",
152 Self::PanelBottomOpen => "icons/panel-bottom-open.svg",
153 Self::PanelLeft => "icons/panel-left.svg",
154 Self::PanelLeftClose => "icons/panel-left-close.svg",
155 Self::PanelLeftOpen => "icons/panel-left-open.svg",
156 Self::PanelRight => "icons/panel-right.svg",
157 Self::PanelRightClose => "icons/panel-right-close.svg",
158 Self::PanelRightOpen => "icons/panel-right-open.svg",
159 Self::Plus => "icons/plus.svg",
160 Self::Replace => "icons/replace.svg",
161 Self::ResizeCorner => "icons/resize-corner.svg",
162 Self::Search => "icons/search.svg",
163 Self::Settings => "icons/settings.svg",
164 Self::Settings2 => "icons/settings-2.svg",
165 Self::SortAscending => "icons/sort-ascending.svg",
166 Self::SortDescending => "icons/sort-descending.svg",
167 Self::SquareTerminal => "icons/square-terminal.svg",
168 Self::Star => "icons/star.svg",
169 Self::StarOff => "icons/star-off.svg",
170 Self::Sun => "icons/sun.svg",
171 Self::ThumbsDown => "icons/thumbs-down.svg",
172 Self::ThumbsUp => "icons/thumbs-up.svg",
173 Self::TriangleAlert => "icons/triangle-alert.svg",
174 Self::User => "icons/user.svg",
175 Self::WindowClose => "icons/window-close.svg",
176 Self::WindowMaximize => "icons/window-maximize.svg",
177 Self::WindowMinimize => "icons/window-minimize.svg",
178 Self::WindowRestore => "icons/window-restore.svg",
179 }
180 .into()
181 }
182
183 pub fn view(self, cx: &mut App) -> Entity<Icon> {
185 Icon::build(self).view(cx)
186 }
187}
188
189impl From<IconName> for Icon {
190 fn from(val: IconName) -> Self {
191 Icon::build(val)
192 }
193}
194
195impl From<IconName> for AnyElement {
196 fn from(val: IconName) -> Self {
197 Icon::build(val).into_any_element()
198 }
199}
200
201impl RenderOnce for IconName {
202 fn render(self, _: &mut Window, _cx: &mut App) -> impl IntoElement {
203 Icon::build(self)
204 }
205}
206
207#[derive(IntoElement)]
208pub struct Icon {
209 base: Svg,
210 style: StyleRefinement,
211 path: SharedString,
212 text_color: Option<Hsla>,
213 size: Option<Size>,
214 rotation: Option<Radians>,
215}
216
217impl Default for Icon {
218 fn default() -> Self {
219 Self {
220 base: svg().flex_none().size_4(),
221 style: StyleRefinement::default(),
222 path: "".into(),
223 text_color: None,
224 size: None,
225 rotation: None,
226 }
227 }
228}
229
230impl Clone for Icon {
231 fn clone(&self) -> Self {
232 let mut this = Self::default().path(self.path.clone());
233 this.style = self.style.clone();
234 this.rotation = self.rotation;
235 this.size = self.size;
236 this.text_color = self.text_color;
237 this
238 }
239}
240
241pub trait IconNamed {
242 fn path(&self) -> SharedString;
243}
244
245impl Icon {
246 pub fn new(icon: impl Into<Icon>) -> Self {
247 icon.into()
248 }
249
250 fn build(name: IconName) -> Self {
251 Self::default().path(name.path())
252 }
253
254 pub fn path(mut self, path: impl Into<SharedString>) -> Self {
258 self.path = path.into();
259 self
260 }
261
262 pub fn view(self, cx: &mut App) -> Entity<Icon> {
264 cx.new(|_| self)
265 }
266
267 pub fn transform(mut self, transformation: gpui::Transformation) -> Self {
268 self.base = self.base.with_transformation(transformation);
269 self
270 }
271
272 pub fn empty() -> Self {
273 Self::default()
274 }
275
276 pub fn rotate(mut self, radians: impl Into<Radians>) -> Self {
278 self.base = self
279 .base
280 .with_transformation(Transformation::rotate(radians));
281 self
282 }
283}
284
285impl Styled for Icon {
286 fn style(&mut self) -> &mut StyleRefinement {
287 &mut self.style
288 }
289
290 fn text_color(mut self, color: impl Into<Hsla>) -> Self {
291 self.text_color = Some(color.into());
292 self
293 }
294}
295
296impl Sizable for Icon {
297 fn with_size(mut self, size: impl Into<Size>) -> Self {
298 self.size = Some(size.into());
299 self
300 }
301}
302
303impl RenderOnce for Icon {
304 fn render(self, window: &mut Window, _cx: &mut App) -> impl IntoElement {
305 let text_color = self.text_color.unwrap_or_else(|| window.text_style().color);
306 let text_size = window.text_style().font_size.to_pixels(window.rem_size());
307 let has_base_size = self.style.size.width.is_some() || self.style.size.height.is_some();
308
309 let mut base = self.base;
310 *base.style() = self.style;
311
312 base.flex_shrink_0()
313 .text_color(text_color)
314 .when(!has_base_size, |this| this.size(text_size))
315 .when_some(self.size, |this, size| match size {
316 Size::Size(px) => this.size(px),
317 Size::XSmall => this.size_3(),
318 Size::Small => this.size_3p5(),
319 Size::Medium => this.size_4(),
320 Size::Large => this.size_6(),
321 })
322 .path(self.path)
323 }
324}
325
326impl From<Icon> for AnyElement {
327 fn from(val: Icon) -> Self {
328 val.into_any_element()
329 }
330}
331
332impl Render for Icon {
333 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
334 let text_color = self.text_color.unwrap_or_else(|| cx.theme().foreground);
335 let text_size = window.text_style().font_size.to_pixels(window.rem_size());
336 let has_base_size = self.style.size.width.is_some() || self.style.size.height.is_some();
337
338 let mut base = svg().flex_none();
339 *base.style() = self.style.clone();
340
341 base.flex_shrink_0()
342 .text_color(text_color)
343 .when(!has_base_size, |this| this.size(text_size))
344 .when_some(self.size, |this, size| match size {
345 Size::Size(px) => this.size(px),
346 Size::XSmall => this.size_3(),
347 Size::Small => this.size_3p5(),
348 Size::Medium => this.size_4(),
349 Size::Large => this.size_6(),
350 })
351 .path(self.path.clone())
352 .when_some(self.rotation, |this, rotation| {
353 this.with_transformation(Transformation::rotate(rotation))
354 })
355 }
356}