1use std::rc::Rc;
2
3use gpui::{AnyElement, App, ElementId, IntoElement, RenderOnce, SharedString, Window};
4use gpui_material_theme::{MotionKind, MotionSpeed};
5
6use crate::action::{Action, Change, InvokeAction, icon_slot};
7
8use super::{
9 appearance::icon_appearance,
10 appearance_builders, common_builders,
11 slots::loading_icon_spinner,
12 types::{Common, IconButtonVariant, IconButtonWidth},
13};
14
15#[derive(IntoElement)]
17pub struct IconButton {
18 common: Common,
19 icon: AnyElement,
20 pub(super) variant: IconButtonVariant,
21 width: IconButtonWidth,
22 on_activate: Option<InvokeAction>,
23}
24
25impl IconButton {
26 pub fn new(
28 id: impl Into<ElementId>,
29 name: impl Into<SharedString>,
30 icon: impl IntoElement,
31 ) -> Self {
32 Self {
33 common: Common::new(id, name),
34 icon: icon.into_any_element(),
35 variant: IconButtonVariant::default(),
36 width: IconButtonWidth::default(),
37 on_activate: None,
38 }
39 }
40
41 pub fn standard(
43 id: impl Into<ElementId>,
44 name: impl Into<SharedString>,
45 icon: impl IntoElement,
46 ) -> Self {
47 Self::new(id, name, icon).variant(IconButtonVariant::Standard)
48 }
49
50 pub fn filled(
52 id: impl Into<ElementId>,
53 name: impl Into<SharedString>,
54 icon: impl IntoElement,
55 ) -> Self {
56 Self::new(id, name, icon).variant(IconButtonVariant::Filled)
57 }
58
59 pub fn tonal(
61 id: impl Into<ElementId>,
62 name: impl Into<SharedString>,
63 icon: impl IntoElement,
64 ) -> Self {
65 Self::new(id, name, icon).variant(IconButtonVariant::FilledTonal)
66 }
67
68 pub fn outlined(
70 id: impl Into<ElementId>,
71 name: impl Into<SharedString>,
72 icon: impl IntoElement,
73 ) -> Self {
74 Self::new(id, name, icon).variant(IconButtonVariant::Outlined)
75 }
76
77 common_builders!();
78 appearance_builders!();
79
80 pub fn variant(mut self, variant: IconButtonVariant) -> Self {
82 self.variant = variant;
83 self
84 }
85
86 pub fn width(mut self, width: IconButtonWidth) -> Self {
88 self.width = width;
89 self
90 }
91
92 pub fn loading(mut self, loading: bool) -> Self {
95 self.common.loading = loading;
96 self
97 }
98
99 pub fn on_activate(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
103 self.on_activate = Some(InvokeAction::Full(Rc::new(handler)));
104 self
105 }
106
107 pub fn on_accessible_activate(
115 mut self,
116 handler: impl Fn(&mut Window, &mut App) + 'static,
117 ) -> Self {
118 self.on_activate = Some(InvokeAction::AccessibleOnly(Rc::new(handler)));
119 self
120 }
121}
122
123impl RenderOnce for IconButton {
124 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
125 let (metrics, width) = self.common.size.icon_metrics(self.width);
126 let mut style = icon_appearance(self.variant, None, self.common.tone).style(
127 metrics,
128 self.common.shape.corner(metrics.square, false),
129 width,
130 );
131 style.corner_motion = (MotionKind::Effects, MotionSpeed::Default);
132 let content = if self.common.loading {
133 let tint = self
134 .common
135 .appearance
136 .colors
137 .content(self.common.disabled, false)
138 .unwrap_or_else(|| style.foreground(self.common.disabled, window, cx));
139 loading_icon_spinner(&self.common.id, metrics.icon, tint, window, cx)
140 } else {
141 icon_slot(self.icon, metrics.icon).into_any_element()
142 };
143 self.common
144 .render(content, style, Action::Invoke(self.on_activate), window, cx)
145 }
146}
147
148#[derive(IntoElement)]
151pub struct IconToggleButton {
152 common: Common,
153 icon: AnyElement,
154 selected_icon: Option<AnyElement>,
155 selected: bool,
156 pub(super) variant: IconButtonVariant,
157 width: IconButtonWidth,
158 on_change: Option<Change>,
159}
160
161impl IconToggleButton {
162 pub fn new(
164 id: impl Into<ElementId>,
165 name: impl Into<SharedString>,
166 icon: impl IntoElement,
167 selected: bool,
168 ) -> Self {
169 Self {
170 common: Common::new(id, name),
171 icon: icon.into_any_element(),
172 selected_icon: None,
173 selected,
174 variant: IconButtonVariant::default(),
175 width: IconButtonWidth::default(),
176 on_change: None,
177 }
178 }
179
180 pub fn standard(
182 id: impl Into<ElementId>,
183 name: impl Into<SharedString>,
184 icon: impl IntoElement,
185 selected: bool,
186 ) -> Self {
187 Self::new(id, name, icon, selected).variant(IconButtonVariant::Standard)
188 }
189
190 pub fn filled(
192 id: impl Into<ElementId>,
193 name: impl Into<SharedString>,
194 icon: impl IntoElement,
195 selected: bool,
196 ) -> Self {
197 Self::new(id, name, icon, selected).variant(IconButtonVariant::Filled)
198 }
199
200 pub fn tonal(
202 id: impl Into<ElementId>,
203 name: impl Into<SharedString>,
204 icon: impl IntoElement,
205 selected: bool,
206 ) -> Self {
207 Self::new(id, name, icon, selected).variant(IconButtonVariant::FilledTonal)
208 }
209
210 pub fn outlined(
212 id: impl Into<ElementId>,
213 name: impl Into<SharedString>,
214 icon: impl IntoElement,
215 selected: bool,
216 ) -> Self {
217 Self::new(id, name, icon, selected).variant(IconButtonVariant::Outlined)
218 }
219
220 common_builders!();
221 appearance_builders!();
222
223 pub fn selected_icon(mut self, icon: impl IntoElement) -> Self {
225 self.selected_icon = Some(icon.into_any_element());
226 self
227 }
228
229 pub fn variant(mut self, variant: IconButtonVariant) -> Self {
231 self.variant = variant;
232 self
233 }
234
235 pub fn width(mut self, width: IconButtonWidth) -> Self {
237 self.width = width;
238 self
239 }
240
241 pub fn loading(mut self, loading: bool) -> Self {
244 self.common.loading = loading;
245 self
246 }
247
248 pub fn on_change(mut self, handler: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
250 self.on_change = Some(Rc::new(handler));
251 self
252 }
253}
254
255impl RenderOnce for IconToggleButton {
256 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
257 let (metrics, width) = self.common.size.icon_metrics(self.width);
258 let mut style = icon_appearance(self.variant, Some(self.selected), self.common.tone).style(
259 metrics,
260 self.common.shape.corner(metrics.square, false),
261 width,
262 );
263 style.selected_corner = Some(self.common.shape.corner(metrics.square, true));
264 style.corner_motion = (MotionKind::Effects, MotionSpeed::Default);
265 let content = if self.common.loading {
266 let tint = self
267 .common
268 .appearance
269 .colors
270 .content(self.common.disabled, self.selected)
271 .unwrap_or_else(|| style.foreground(self.common.disabled, window, cx));
272 loading_icon_spinner(&self.common.id, metrics.icon, tint, window, cx)
273 } else {
274 let icon = if self.selected {
275 self.selected_icon.unwrap_or(self.icon)
276 } else {
277 self.icon
278 };
279 icon_slot(icon, metrics.icon).into_any_element()
280 };
281 self.common.render(
282 content,
283 style,
284 Action::Toggle {
285 selected: self.selected,
286 on_change: self.on_change,
287 },
288 window,
289 cx,
290 )
291 }
292}
293
294#[cfg(test)]
295mod tests {
296 use gpui::div;
297
298 use super::*;
299
300 #[test]
301 fn icon_button_activation_modes_replace_each_other() {
302 let icon = IconButton::new("i", "More", div()).on_accessible_activate(|_, _| {});
303 assert!(matches!(
304 icon.on_activate,
305 Some(InvokeAction::AccessibleOnly(_))
306 ));
307 let icon = icon.on_activate(|_, _| {});
308 assert!(matches!(icon.on_activate, Some(InvokeAction::Full(_))));
309 let icon = icon.on_accessible_activate(|_, _| {});
310 assert!(matches!(
311 icon.on_activate,
312 Some(InvokeAction::AccessibleOnly(_))
313 ));
314 }
315}