Skip to main content

gpui_material_components/button/
toggle_button.rs

1use std::rc::Rc;
2
3use gpui::{App, ElementId, IntoElement, RenderOnce, SharedString, Window};
4
5use crate::action::{Action, Change};
6
7use super::{
8    appearance::button_appearance,
9    appearance_builders, common_builders, slot_builders,
10    slots::{Slots, loading_overlay},
11    types::{ButtonVariant, Common, ToggleButtonVariant},
12};
13
14/// A controlled labeled toggle. The caller re-renders with the selected value
15/// after accepting a requested change.
16#[derive(IntoElement)]
17pub struct ToggleButton {
18    common: Common,
19    pub(super) variant: ToggleButtonVariant,
20    selected: bool,
21    slots: Slots,
22    on_change: Option<Change>,
23}
24
25impl ToggleButton {
26    /// Creates a controlled toggle button with the default [`ToggleButtonVariant::Filled`] emphasis.
27    pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>, selected: bool) -> Self {
28        Self {
29            common: Common::new(id, label),
30            variant: ToggleButtonVariant::default(),
31            selected,
32            slots: Slots::default(),
33            on_change: None,
34        }
35    }
36
37    /// Creates a Filled toggle button.
38    pub fn filled(
39        id: impl Into<ElementId>,
40        label: impl Into<SharedString>,
41        selected: bool,
42    ) -> Self {
43        Self::new(id, label, selected).variant(ToggleButtonVariant::Filled)
44    }
45
46    /// Creates a Filled Tonal toggle button.
47    pub fn tonal(id: impl Into<ElementId>, label: impl Into<SharedString>, selected: bool) -> Self {
48        Self::new(id, label, selected).variant(ToggleButtonVariant::FilledTonal)
49    }
50
51    /// Creates an Elevated toggle button.
52    pub fn elevated(
53        id: impl Into<ElementId>,
54        label: impl Into<SharedString>,
55        selected: bool,
56    ) -> Self {
57        Self::new(id, label, selected).variant(ToggleButtonVariant::Elevated)
58    }
59
60    /// Creates an Outlined toggle button.
61    pub fn outlined(
62        id: impl Into<ElementId>,
63        label: impl Into<SharedString>,
64        selected: bool,
65    ) -> Self {
66        Self::new(id, label, selected).variant(ToggleButtonVariant::Outlined)
67    }
68
69    common_builders!();
70    appearance_builders!();
71    slot_builders!();
72
73    /// Sets the visual emphasis variant. Defaults to [`ToggleButtonVariant::Filled`].
74    pub fn variant(mut self, variant: ToggleButtonVariant) -> Self {
75        self.variant = variant;
76        self
77    }
78
79    /// Shows a centered inline circular spinner while suppressing activation and preserving the
80    /// button's resting width. Keeps the active palette unless `.disabled(true)` is also set.
81    pub fn loading(mut self, loading: bool) -> Self {
82        self.common.loading = loading;
83        self
84    }
85
86    /// Invoked with the requested next selected state when the toggle button is activated.
87    pub fn on_change(mut self, handler: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
88        self.on_change = Some(Rc::new(handler));
89        self
90    }
91}
92
93impl RenderOnce for ToggleButton {
94    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
95        let metrics = self.common.size.metrics();
96        let variant = match self.variant {
97            ToggleButtonVariant::Elevated => ButtonVariant::Elevated,
98            ToggleButtonVariant::Filled => ButtonVariant::Filled,
99            ToggleButtonVariant::FilledTonal => ButtonVariant::FilledTonal,
100            ToggleButtonVariant::Outlined => ButtonVariant::Outlined,
101        };
102        let mut style = button_appearance(variant, Some(self.selected), self.common.tone).style(
103            metrics,
104            self.common.shape.corner(metrics.square, false),
105            0.,
106        );
107        style.selected_corner = Some(self.common.shape.corner(metrics.square, true));
108        let tint = self
109            .common
110            .appearance
111            .colors
112            .content(self.common.disabled, self.selected)
113            .unwrap_or_else(|| style.foreground(self.common.disabled, window, cx));
114        let resting_content =
115            self.slots
116                .render(self.common.label.clone(), metrics, 1., window, tint, cx);
117        let content = if self.common.loading {
118            loading_overlay(
119                &self.common.id,
120                resting_content,
121                metrics.icon,
122                tint,
123                window,
124                cx,
125            )
126        } else {
127            resting_content
128        };
129        self.common.render(
130            content,
131            style,
132            Action::Toggle {
133                selected: self.selected,
134                on_change: self.on_change,
135            },
136            window,
137            cx,
138        )
139    }
140}