fc_ui/capabilities/primitives/
icon_button.rs1use crate::capabilities::foundation::icon_config::resolve_icon_path;
4use crate::capabilities::foundation::theme::use_theme;
5use crate::capabilities::primitives::button::ButtonVariant;
6use crate::capabilities::primitives::icon_source::IconSource;
7use crate::capabilities::primitives::ripple::Ripple;
8use gpui::{prelude::FluentBuilder as _, *};
9use std::rc::Rc;
10
11fn icon_path_from_name(name: &str) -> String {
12 resolve_icon_path(name)
13}
14
15#[derive(IntoElement)]
16pub struct IconButton {
17 id: ElementId,
18 base: Stateful<Div>,
19 icon_source: IconSource,
20 variant: ButtonVariant,
21 size: Pixels,
22 icon_size: Option<Pixels>,
23 disabled: bool,
24 no_background: bool,
25 on_click: Option<Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>>,
26 ripple_enabled: bool,
27 style: StyleRefinement,
28}
29
30impl IconButton {
31 pub fn new(icon: impl Into<IconSource>) -> Self {
32 let icon_source = icon.into();
33
34 let id_string = match &icon_source {
35 IconSource::Named(name) => format!("icon-button-{}", name),
36 IconSource::FilePath(path) => format!("icon-button-{}", path),
37 };
38 let id = ElementId::Name(SharedString::from(id_string));
39
40 Self {
41 id: id.clone(),
42 base: div().flex_shrink_0().id(id),
43 icon_source,
44 variant: ButtonVariant::Default,
45 size: px(40.0),
46 icon_size: None,
47 disabled: false,
48 no_background: false,
49 on_click: None,
50 ripple_enabled: false,
51 style: StyleRefinement::default(),
52 }
53 }
54
55 pub fn ripple(mut self, enabled: bool) -> Self {
56 self.ripple_enabled = enabled;
57 self
58 }
59
60 pub fn variant(mut self, variant: ButtonVariant) -> Self {
61 self.variant = variant;
62 self
63 }
64
65 pub fn size(mut self, size: Pixels) -> Self {
66 self.size = size;
67 self
68 }
69
70 pub fn icon_size(mut self, size: Pixels) -> Self {
71 self.icon_size = Some(size);
72 self
73 }
74
75 pub fn disabled(mut self, disabled: bool) -> Self {
76 self.disabled = disabled;
77 self
78 }
79
80 pub fn no_background(mut self, no_background: bool) -> Self {
81 self.no_background = no_background;
82 self
83 }
84
85 pub fn on_click(
86 mut self,
87 handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
88 ) -> Self {
89 self.on_click = Some(Rc::new(handler));
90 self
91 }
92
93 fn clickable(&self) -> bool {
94 !self.disabled && self.on_click.is_some()
95 }
96
97 fn get_svg_path(&self) -> Option<SharedString> {
98 match &self.icon_source {
99 IconSource::FilePath(path) => Some(path.clone()),
100 IconSource::Named(name) => Some(SharedString::from(icon_path_from_name(name))),
101 }
102 }
103}
104
105impl Styled for IconButton {
106 fn style(&mut self) -> &mut StyleRefinement {
107 &mut self.style
108 }
109}
110
111impl InteractiveElement for IconButton {
112 fn interactivity(&mut self) -> &mut Interactivity {
113 self.base.interactivity()
114 }
115}
116
117impl StatefulInteractiveElement for IconButton {}
118
119impl RenderOnce for IconButton {
120 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
121 let theme = use_theme(cx);
122
123 let icon_size = self.icon_size.unwrap_or(self.size * 0.5);
124
125 let (bg, fg, border, hover_bg, _hover_fg) = match self.variant {
126 ButtonVariant::Default => (
127 theme.tokens.primary,
128 theme.tokens.primary_foreground,
129 theme.tokens.primary,
130 theme.tokens.primary.opacity(0.9),
131 theme.tokens.primary_foreground,
132 ),
133 ButtonVariant::Secondary => (
134 theme.tokens.secondary,
135 theme.tokens.secondary_foreground,
136 theme.tokens.secondary,
137 theme.tokens.secondary.opacity(0.8),
138 theme.tokens.secondary_foreground,
139 ),
140 ButtonVariant::Destructive => (
141 theme.tokens.destructive,
142 theme.tokens.destructive_foreground,
143 theme.tokens.destructive,
144 theme.tokens.destructive.opacity(0.9),
145 theme.tokens.destructive_foreground,
146 ),
147 ButtonVariant::Outline => (
148 gpui::transparent_black(),
149 theme.tokens.foreground,
150 theme.tokens.border,
151 theme.tokens.accent,
152 theme.tokens.accent_foreground,
153 ),
154 ButtonVariant::Ghost => (
155 gpui::transparent_black(),
156 theme.tokens.foreground,
157 gpui::transparent_black(),
158 theme.tokens.accent,
159 theme.tokens.accent_foreground,
160 ),
161 ButtonVariant::Link => (
162 gpui::transparent_black(),
163 theme.tokens.primary,
164 gpui::transparent_black(),
165 gpui::transparent_black(),
166 theme.tokens.primary.opacity(0.8),
167 ),
168 };
169
170 let clickable = self.clickable();
171 let handler = self.on_click.clone();
172 let svg_path = self.get_svg_path();
173 let user_style = self.style;
174 let ripple_enabled = self.ripple_enabled && clickable;
175 let ripple_id = ElementId::Name(format!("{}-ripple", self.id).into());
176 let ripple_color = fg;
177
178 let focus_handle = window
179 .use_keyed_state(self.id.clone(), cx, |_, cx| cx.focus_handle())
180 .read(cx)
181 .clone();
182
183 self.base
184 .when(!self.disabled, |this| {
185 this.track_focus(&focus_handle.tab_index(0).tab_stop(true))
186 })
187 .relative()
188 .overflow_hidden()
189 .flex()
190 .items_center()
191 .justify_center()
192 .size(self.size)
193 .rounded(theme.tokens.radius_md)
194 .when(!self.no_background, |this| {
195 this.bg(bg)
196 .when(self.variant == ButtonVariant::Outline, |this| {
197 this.border_1().border_color(border)
198 })
199 })
200 .when(self.disabled, |this| {
201 this.opacity(0.5).cursor(CursorStyle::Arrow)
202 })
203 .when(!self.disabled, |this| {
204 this.cursor(CursorStyle::PointingHand)
205 .when(!self.no_background, |this| {
206 this.hover(|style| style.bg(hover_bg))
207 })
208 .when(self.no_background, |this| {
209 this.hover(|style| style.opacity(0.7))
210 })
211 .active(|style| style.opacity(0.9))
212 })
213 .map(|this| {
214 let mut div = this;
215 div.style().refine(&user_style);
216 div
217 })
218 .on_mouse_down(MouseButton::Left, move |_, window, _| {
219 window.prevent_default();
220 if ripple_enabled {
221 window.refresh();
222 }
223 })
224 .when(self.ripple_enabled && clickable, |this| {
225 let center = self.size / 2.0;
226 this.child(
227 Ripple::new(ripple_id, point(center, center), ripple_color)
228 .max_size(self.size * 2.0),
229 )
230 })
231 .when_some(handler.filter(|_| clickable), |this, on_click| {
232 this.on_click(move |event, window, cx| {
233 cx.stop_propagation();
234 (on_click)(event, window, cx);
235 })
236 })
237 .when_some(svg_path, |this, path| {
238 this.child(
239 svg()
240 .path(path)
241 .size(icon_size)
242 .text_color(if self.disabled {
243 theme.tokens.muted_foreground
244 } else if self.no_background {
245 theme.tokens.primary
246 } else {
247 fg
248 }),
249 )
250 })
251 }
252}