1use gpui::{AnyView, DefiniteLength};
2
3use crate::{prelude::*, ElevationIndex, SelectableButton, Spacing};
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
91impl FixedWidth for IconButton {
92 fn width(mut self, width: DefiniteLength) -> Self {
93 self.base = self.base.width(width);
94 self
95 }
96
97 fn full_width(mut self) -> Self {
98 self.base = self.base.full_width();
99 self
100 }
101}
102
103impl ButtonCommon for IconButton {
104 fn id(&self) -> &ElementId {
105 self.base.id()
106 }
107
108 fn style(mut self, style: ButtonStyle) -> Self {
109 self.base = self.base.style(style);
110 self
111 }
112
113 fn size(mut self, size: ButtonSize) -> Self {
114 self.base = self.base.size(size);
115 self
116 }
117
118 fn tooltip(mut self, tooltip: impl Fn(&mut WindowContext) -> AnyView + 'static) -> Self {
119 self.base = self.base.tooltip(tooltip);
120 self
121 }
122
123 fn layer(mut self, elevation: ElevationIndex) -> Self {
124 self.base = self.base.layer(elevation);
125 self
126 }
127}
128
129impl VisibleOnHover for IconButton {
130 fn visible_on_hover(mut self, group_name: impl Into<SharedString>) -> Self {
131 self.base = self.base.visible_on_hover(group_name);
132 self
133 }
134}
135
136impl RenderOnce for IconButton {
137 fn render(self, cx: &mut WindowContext) -> impl IntoElement {
138 let is_disabled = self.base.disabled;
139 let is_selected = self.base.selected;
140 let selected_style = self.base.selected_style;
141
142 self.base
143 .map(|this| match self.shape {
144 IconButtonShape::Square => {
145 let icon_size = self.icon_size.rems() * cx.rem_size();
146 let padding = match self.icon_size {
147 IconSize::Indicator => Spacing::None.px(cx),
148 IconSize::XSmall => Spacing::None.px(cx),
149 IconSize::Small => Spacing::XSmall.px(cx),
150 IconSize::Medium => Spacing::XSmall.px(cx),
151 };
152
153 this.width((icon_size + padding * 2.).into())
154 .height((icon_size + padding * 2.).into())
155 }
156 IconButtonShape::Wide => this,
157 })
158 .child(
159 ButtonIcon::new(self.icon)
160 .disabled(is_disabled)
161 .selected(is_selected)
162 .selected_icon(self.selected_icon)
163 .when_some(selected_style, |this, style| this.selected_style(style))
164 .size(self.icon_size)
165 .color(self.icon_color),
166 )
167 }
168}