1use gpui::{AnyView, DefiniteLength};
2
3use crate::{prelude::*, ElevationIndex, SelectableButton};
4use crate::{ButtonCommon, ButtonLike, ButtonSize, ButtonStyle, IconName, IconSize};
5
6use super::button_icon::ButtonIcon;
7
8/// The shape of an [`IconButton`].
9#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
10pub enum IconButtonShape {
11 Square,
12 Wide,
13}
14
15#[derive(IntoElement)]
16pub struct IconButton {
17 base: ButtonLike,
18 shape: IconButtonShape,
19 icon: IconName,
20 icon_size: IconSize,
21 icon_color: Color,
22 selected_icon: Option<IconName>,
23}
24
25impl IconButton {
26 pub fn new(id: impl Into<ElementId>, icon: IconName) -> Self {
27 let mut this = Self {
28 base: ButtonLike::new(id),
29 shape: IconButtonShape::Wide,
30 icon,
31 icon_size: IconSize::default(),
32 icon_color: Color::Default,
33 selected_icon: None,
34 };
35 this.base.base = this.base.base.debug_selector(|| format!("ICON-{:?}", icon));
36 this
37 }
38
39 pub fn shape(mut self, shape: IconButtonShape) -> Self {
40 self.shape = shape;
41 self
42 }
43
44 pub fn icon_size(mut self, icon_size: IconSize) -> Self {
45 self.icon_size = icon_size;
46 self
47 }
48
49 pub fn icon_color(mut self, icon_color: Color) -> Self {
50 self.icon_color = icon_color;
51 self
52 }
53
54 pub fn selected_icon(mut self, icon: impl Into<Option<IconName>>) -> Self {
55 self.selected_icon = icon.into();
56 self
57 }
58}
59
60impl Disableable for IconButton {
61 fn disabled(mut self, disabled: bool) -> Self {
62 self.base = self.base.disabled(disabled);
63 self
64 }
65}
66
67impl Selectable for IconButton {
68 fn selected(mut self, selected: bool) -> Self {
69 self.base = self.base.selected(selected);
70 self
71 }
72}
73
74impl SelectableButton for IconButton {
75 fn selected_style(mut self, style: ButtonStyle) -> Self {
76 self.base = self.base.selected_style(style);
77 self
78 }
79}
80
81impl Clickable for IconButton {
82 fn on_click(
83 mut self,
84 handler: impl Fn(&gpui::ClickEvent, &mut WindowContext) + 'static,
85 ) -> Self {
86 self.base = self.base.on_click(handler);
87 self
88 }
89
90 fn cursor_style(mut self, cursor_style: gpui::CursorStyle) -> Self {
91 self.base = self.base.cursor_style(cursor_style);
92 self
93 }
94}
95
96impl FixedWidth for IconButton {
97 fn width(mut self, width: DefiniteLength) -> Self {
98 self.base = self.base.width(width);
99 self
100 }
101
102 fn full_width(mut self) -> Self {
103 self.base = self.base.full_width();
104 self
105 }
106}
107
108impl ButtonCommon for IconButton {
109 fn id(&self) -> &ElementId {
110 self.base.id()
111 }
112
113 fn style(mut self, style: ButtonStyle) -> Self {
114 self.base = self.base.style(style);
115 self
116 }
117
118 fn size(mut self, size: ButtonSize) -> Self {
119 self.base = self.base.size(size);
120 self
121 }
122
123 fn tooltip(mut self, tooltip: impl Fn(&mut WindowContext) -> AnyView + 'static) -> Self {
124 self.base = self.base.tooltip(tooltip);
125 self
126 }
127
128 fn layer(mut self, elevation: ElevationIndex) -> Self {
129 self.base = self.base.layer(elevation);
130 self
131 }
132}
133
134impl VisibleOnHover for IconButton {
135 fn visible_on_hover(mut self, group_name: impl Into<SharedString>) -> Self {
136 self.base = self.base.visible_on_hover(group_name);
137 self
138 }
139}
140
141impl RenderOnce for IconButton {
142 fn render(self, cx: &mut WindowContext) -> impl IntoElement {
143 let is_disabled = self.base.disabled;
144 let is_selected = self.base.selected;
145 let selected_style = self.base.selected_style;
146
147 self.base
148 .map(|this| match self.shape {
149 IconButtonShape::Square => {
150 let size = self.icon_size.square(cx);
151 this.width(size.into()).height(size.into())
152 }
153 IconButtonShape::Wide => this,
154 })
155 .child(
156 ButtonIcon::new(self.icon)
157 .disabled(is_disabled)
158 .selected(is_selected)
159 .selected_icon(self.selected_icon)
160 .when_some(selected_style, |this, style| this.selected_style(style))
161 .size(self.icon_size)
162 .color(self.icon_color),
163 )
164 }
165}