icon_button.rs

  1use gpui::{AnyView, DefiniteLength};
  2
  3use crate::{prelude::*, 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        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    }
 36
 37    pub fn shape(mut self, shape: IconButtonShape) -> Self {
 38        self.shape = shape;
 39        self
 40    }
 41
 42    pub fn icon_size(mut self, icon_size: IconSize) -> Self {
 43        self.icon_size = icon_size;
 44        self
 45    }
 46
 47    pub fn icon_color(mut self, icon_color: Color) -> Self {
 48        self.icon_color = icon_color;
 49        self
 50    }
 51
 52    pub fn selected_icon(mut self, icon: impl Into<Option<IconName>>) -> Self {
 53        self.selected_icon = icon.into();
 54        self
 55    }
 56}
 57
 58impl Disableable for IconButton {
 59    fn disabled(mut self, disabled: bool) -> Self {
 60        self.base = self.base.disabled(disabled);
 61        self
 62    }
 63}
 64
 65impl Selectable for IconButton {
 66    fn selected(mut self, selected: bool) -> Self {
 67        self.base = self.base.selected(selected);
 68        self
 69    }
 70}
 71
 72impl SelectableButton for IconButton {
 73    fn selected_style(mut self, style: ButtonStyle) -> Self {
 74        self.base = self.base.selected_style(style);
 75        self
 76    }
 77}
 78
 79impl Clickable for IconButton {
 80    fn on_click(
 81        mut self,
 82        handler: impl Fn(&gpui::ClickEvent, &mut WindowContext) + 'static,
 83    ) -> Self {
 84        self.base = self.base.on_click(handler);
 85        self
 86    }
 87}
 88
 89impl FixedWidth for IconButton {
 90    fn width(mut self, width: DefiniteLength) -> Self {
 91        self.base = self.base.width(width);
 92        self
 93    }
 94
 95    fn full_width(mut self) -> Self {
 96        self.base = self.base.full_width();
 97        self
 98    }
 99}
100
101impl ButtonCommon for IconButton {
102    fn id(&self) -> &ElementId {
103        self.base.id()
104    }
105
106    fn style(mut self, style: ButtonStyle) -> Self {
107        self.base = self.base.style(style);
108        self
109    }
110
111    fn size(mut self, size: ButtonSize) -> Self {
112        self.base = self.base.size(size);
113        self
114    }
115
116    fn tooltip(mut self, tooltip: impl Fn(&mut WindowContext) -> AnyView + 'static) -> Self {
117        self.base = self.base.tooltip(tooltip);
118        self
119    }
120}
121
122impl VisibleOnHover for IconButton {
123    fn visible_on_hover(mut self, group_name: impl Into<SharedString>) -> Self {
124        self.base = self.base.visible_on_hover(group_name);
125        self
126    }
127}
128
129impl RenderOnce for IconButton {
130    fn render(self, cx: &mut WindowContext) -> impl IntoElement {
131        let is_disabled = self.base.disabled;
132        let is_selected = self.base.selected;
133        let selected_style = self.base.selected_style;
134
135        self.base
136            .map(|this| match self.shape {
137                IconButtonShape::Square => {
138                    let icon_size = self.icon_size.rems() * cx.rem_size();
139                    let padding = match self.icon_size {
140                        IconSize::Indicator => px(0.),
141                        IconSize::XSmall => px(0.),
142                        IconSize::Small => px(2.),
143                        IconSize::Medium => px(2.),
144                    };
145
146                    this.width((icon_size + padding * 2.).into())
147                        .height((icon_size + padding * 2.).into())
148                }
149                IconButtonShape::Wide => this,
150            })
151            .child(
152                ButtonIcon::new(self.icon)
153                    .disabled(is_disabled)
154                    .selected(is_selected)
155                    .selected_icon(self.selected_icon)
156                    .when_some(selected_style, |this, style| this.selected_style(style))
157                    .size(self.icon_size)
158                    .color(self.icon_color),
159            )
160    }
161}