toggle_button.rs

  1#![allow(missing_docs)]
  2use gpui::{AnyView, ClickEvent};
  3
  4use crate::{prelude::*, ButtonLike, ButtonLikeRounding, ElevationIndex};
  5
  6/// The position of a [`ToggleButton`] within a group of buttons.
  7#[derive(Debug, PartialEq, Eq, Clone, Copy)]
  8pub enum ToggleButtonPosition {
  9    /// The toggle button is first in the group.
 10    First,
 11
 12    /// The toggle button is in the middle of the group (i.e., it is not the first or last toggle button).
 13    Middle,
 14
 15    /// The toggle button is last in the group.
 16    Last,
 17}
 18
 19#[derive(IntoElement)]
 20pub struct ToggleButton {
 21    base: ButtonLike,
 22    position_in_group: Option<ToggleButtonPosition>,
 23    label: SharedString,
 24    label_color: Option<Color>,
 25}
 26
 27impl ToggleButton {
 28    pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
 29        Self {
 30            base: ButtonLike::new(id),
 31            position_in_group: None,
 32            label: label.into(),
 33            label_color: None,
 34        }
 35    }
 36
 37    pub fn color(mut self, label_color: impl Into<Option<Color>>) -> Self {
 38        self.label_color = label_color.into();
 39        self
 40    }
 41
 42    pub fn position_in_group(mut self, position: ToggleButtonPosition) -> Self {
 43        self.position_in_group = Some(position);
 44        self
 45    }
 46
 47    pub fn first(self) -> Self {
 48        self.position_in_group(ToggleButtonPosition::First)
 49    }
 50
 51    pub fn middle(self) -> Self {
 52        self.position_in_group(ToggleButtonPosition::Middle)
 53    }
 54
 55    pub fn last(self) -> Self {
 56        self.position_in_group(ToggleButtonPosition::Last)
 57    }
 58}
 59
 60impl Toggleable for ToggleButton {
 61    fn toggle_state(mut self, selected: bool) -> Self {
 62        self.base = self.base.toggle_state(selected);
 63        self
 64    }
 65}
 66
 67impl SelectableButton for ToggleButton {
 68    fn selected_style(mut self, style: ButtonStyle) -> Self {
 69        self.base.selected_style = Some(style);
 70        self
 71    }
 72}
 73
 74impl Disableable for ToggleButton {
 75    fn disabled(mut self, disabled: bool) -> Self {
 76        self.base = self.base.disabled(disabled);
 77        self
 78    }
 79}
 80
 81impl Clickable for ToggleButton {
 82    fn on_click(mut self, handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static) -> Self {
 83        self.base = self.base.on_click(handler);
 84        self
 85    }
 86
 87    fn cursor_style(mut self, cursor_style: gpui::CursorStyle) -> Self {
 88        self.base = self.base.cursor_style(cursor_style);
 89        self
 90    }
 91}
 92
 93impl ButtonCommon for ToggleButton {
 94    fn id(&self) -> &ElementId {
 95        self.base.id()
 96    }
 97
 98    fn style(mut self, style: ButtonStyle) -> Self {
 99        self.base = self.base.style(style);
100        self
101    }
102
103    fn size(mut self, size: ButtonSize) -> Self {
104        self.base = self.base.size(size);
105        self
106    }
107
108    fn tooltip(mut self, tooltip: impl Fn(&mut Window, &mut App) -> AnyView + 'static) -> Self {
109        self.base = self.base.tooltip(tooltip);
110        self
111    }
112
113    fn layer(mut self, elevation: ElevationIndex) -> Self {
114        self.base = self.base.layer(elevation);
115        self
116    }
117}
118
119impl RenderOnce for ToggleButton {
120    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
121        let is_disabled = self.base.disabled;
122        let is_selected = self.base.selected;
123
124        let label_color = if is_disabled {
125            Color::Disabled
126        } else if is_selected {
127            Color::Selected
128        } else {
129            self.label_color.unwrap_or_default()
130        };
131
132        self.base
133            .when_some(self.position_in_group, |this, position| match position {
134                ToggleButtonPosition::First => this.rounding(ButtonLikeRounding::Left),
135                ToggleButtonPosition::Middle => this.rounding(None),
136                ToggleButtonPosition::Last => this.rounding(ButtonLikeRounding::Right),
137            })
138            .child(
139                Label::new(self.label)
140                    .color(label_color)
141                    .line_height_style(LineHeightStyle::UiLabel),
142            )
143    }
144}