fc_ui/capabilities/primitives/
icon.rs1use crate::capabilities::foundation::icon_config::resolve_icon_path;
4use crate::capabilities::foundation::theme::use_theme;
5use crate::capabilities::primitives::icon_source::IconSource;
6use gpui::{prelude::*, *};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
11pub enum IconVariant {
12 #[default]
13 Regular,
14 Solid,
15}
16
17#[derive(Debug, Clone, Copy, PartialEq, Default)]
19pub enum IconSize {
20 XSmall,
22 Small,
24 #[default]
26 Medium,
27 Large,
29 Custom(Pixels),
31}
32
33impl From<Pixels> for IconSize {
34 fn from(pixels: Pixels) -> Self {
35 Self::Custom(pixels)
36 }
37}
38
39impl From<f32> for IconSize {
40 fn from(value: f32) -> Self {
41 Self::Custom(px(value))
42 }
43}
44
45impl IconSize {
46 pub fn to_pixels(&self) -> Pixels {
48 match self {
49 IconSize::XSmall => px(12.0),
50 IconSize::Small => px(14.0),
51 IconSize::Medium => px(16.0),
52 IconSize::Large => px(24.0),
53 IconSize::Custom(pixels) => *pixels,
54 }
55 }
56}
57
58fn icon_path_from_name(name: &str) -> String {
59 resolve_icon_path(name)
60}
61
62#[derive(IntoElement)]
63pub struct Icon {
64 source: IconSource,
65 variant: IconVariant,
66 size: IconSize,
67 color: Option<Hsla>,
68 clickable: bool,
69 disabled: bool,
70 on_click: Option<Box<dyn Fn(&mut Window, &mut App) + Send + Sync + 'static>>,
71 focus_handle: Option<FocusHandle>,
72 style: StyleRefinement,
73 rotation: Option<Radians>,
74}
75
76impl Icon {
77 pub fn new(source: impl Into<IconSource>) -> Self {
78 Self {
79 source: source.into(),
80 variant: IconVariant::default(),
81 size: IconSize::default(),
82 color: None,
83 clickable: false,
84 disabled: false,
85 on_click: None,
86 focus_handle: None,
87 style: StyleRefinement::default(),
88 rotation: None,
89 }
90 }
91
92 pub fn variant(mut self, variant: IconVariant) -> Self {
93 self.variant = variant;
94 self
95 }
96
97 pub fn size(mut self, size: impl Into<IconSize>) -> Self {
98 self.size = size.into();
99 self
100 }
101
102 pub fn color(mut self, color: Hsla) -> Self {
103 self.color = Some(color);
104 self
105 }
106
107 pub fn clickable(mut self, clickable: bool) -> Self {
108 self.clickable = clickable;
109 self
110 }
111
112 pub fn disabled(mut self, disabled: bool) -> Self {
113 self.disabled = disabled;
114 self
115 }
116
117 pub fn focus_handle(mut self, focus_handle: FocusHandle) -> Self {
119 self.focus_handle = Some(focus_handle);
120 self
121 }
122
123 pub fn on_click<F>(mut self, f: F) -> Self
124 where
125 F: Fn(&mut Window, &mut App) + Send + Sync + 'static,
126 {
127 self.on_click = Some(Box::new(f));
128 self.clickable = true;
129 self
130 }
131
132 pub fn rotate(mut self, radians: impl Into<Radians>) -> Self {
134 self.rotation = Some(radians.into());
135 self
136 }
137
138 fn get_svg_path(&self) -> Option<SharedString> {
139 match &self.source {
140 IconSource::FilePath(path) => Some(path.clone()),
141 IconSource::Named(name) => Some(SharedString::from(icon_path_from_name(name))),
142 }
143 }
144}
145
146impl Styled for Icon {
147 fn style(&mut self) -> &mut StyleRefinement {
148 &mut self.style
149 }
150}
151
152impl RenderOnce for Icon {
153 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
154 let theme = use_theme(cx);
155 let color = self.color.unwrap_or(theme.tokens.primary);
156 let svg_content = self.get_svg_path();
157
158 if !self.clickable {
160 let mut base = svg();
161 *base.style() = self.style;
162
163 return base
164 .flex_shrink_0()
165 .when_some(svg_content, |this, svg_string| this.path(svg_string))
166 .size(self.size.to_pixels())
167 .text_color(if self.disabled {
168 theme.tokens.muted_foreground
169 } else {
170 color
171 })
172 .when_some(self.rotation, |this, rotation| {
173 this.with_transformation(Transformation::rotate(rotation))
174 })
175 .into_any_element();
176 }
177
178 let on_click = self.on_click;
180 let disabled = self.disabled;
181 let element_id = match &self.source {
182 IconSource::Named(name) => format!("icon-{name}"),
183 IconSource::FilePath(path) => format!("icon-{path}"),
184 };
185
186 div()
187 .id(ElementId::Name(SharedString::from(element_id)))
188 .flex()
189 .flex_shrink_0()
190 .items_center()
191 .justify_center()
192 .cursor(if disabled {
193 CursorStyle::Arrow
194 } else {
195 CursorStyle::PointingHand
196 })
197 .when_some(self.focus_handle, |div, handle| div.track_focus(&handle))
198 .when(!disabled && on_click.is_some(), |div| {
199 div.on_click(move |_, window, cx| {
200 if let Some(ref cb) = on_click {
201 cb(window, cx);
202 }
203 })
204 })
205 .when(!disabled, |div| {
206 div.hover(|mut style| {
207 style.opacity = Some(0.7);
208 style
209 })
210 })
211 .when_some(svg_content, |div, svg_string| {
212 let mut icon_svg = svg();
213 *icon_svg.style() = self.style.clone();
214
215 div.child(
216 icon_svg
217 .path(svg_string)
218 .size(self.size.to_pixels())
219 .text_color(if disabled {
220 theme.tokens.muted_foreground
221 } else {
222 color
223 })
224 .when_some(self.rotation, |this, rotation| {
225 this.with_transformation(Transformation::rotate(rotation))
226 }),
227 )
228 })
229 .into_any_element()
230 }
231}
232
233pub fn icon(source: impl Into<IconSource>) -> Icon {
234 Icon::new(source)
235}
236
237#[deprecated(
238 note = "conflicts with the IconButton component; use IconButton::new(source).on_click(...) instead"
239)]
240pub fn icon_button<F>(source: impl Into<IconSource>, on_click: F) -> Icon
241where
242 F: Fn(&mut Window, &mut App) + Send + Sync + 'static,
243{
244 Icon::new(source).clickable(true).on_click(on_click)
245}