Skip to main content

gpui_material_components/settings_tiles/
switch.rs

1use std::rc::Rc;
2
3use gpui::{
4    App, ElementId, Entity, FocusHandle, IntoElement, RenderOnce, Role, SharedString, Window,
5};
6use gpui_material_foundation::InteractionSource;
7
8use crate::{ListItem, ListItemColors, ListVariant, Switch, SwitchColors, action::Change};
9
10use super::common::{TileFields, apply_standalone_variant};
11
12/// Settings convenience row wrapping [`ListItem`] with a synchronized decorative [`Switch`].
13///
14/// Clicking anywhere on the row or pressing Enter or Space toggles once with a single focus
15/// stop and `Role::Switch` semantics.
16#[derive(IntoElement)]
17pub struct SwitchTile {
18    fields: TileFields,
19    checked: bool,
20    switch_colors: SwitchColors,
21    on_change: Option<Change>,
22}
23
24impl SwitchTile {
25    /// Creates a switch settings row with an element ID, headline label, and controlled checked state.
26    pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>, checked: bool) -> Self {
27        Self {
28            fields: TileFields::new(id, label),
29            checked,
30            switch_colors: SwitchColors::default(),
31            on_change: None,
32        }
33    }
34
35    /// Sets secondary supporting text displayed below the headline.
36    pub fn supporting(mut self, text: impl Into<SharedString>) -> Self {
37        self.fields.supporting = Some(text.into());
38        self
39    }
40
41    /// Sets overline text displayed above the headline.
42    pub fn overline(mut self, text: impl Into<SharedString>) -> Self {
43        self.fields.overline = Some(text.into());
44        self
45    }
46
47    /// Sets an optional leading icon or graphic.
48    pub fn leading(mut self, leading: impl IntoElement) -> Self {
49        self.fields.leading = Some(leading.into_any_element());
50        self
51    }
52
53    /// Disables row interaction and dims the switch when `true`.
54    pub fn disabled(mut self, disabled: bool) -> Self {
55        self.fields.disabled = disabled;
56        self
57    }
58
59    /// Sets standalone row styling (`Standard` or `Segmented`).
60    pub fn variant(mut self, variant: ListVariant) -> Self {
61        self.fields.variant = variant;
62        self
63    }
64
65    /// Overrides container and slot colors on the underlying [`ListItem`].
66    pub fn colors(mut self, colors: ListItemColors) -> Self {
67        self.fields.colors = colors;
68        self
69    }
70
71    /// Overrides track, thumb, border, and content colors on the decorative [`Switch`].
72    pub fn switch_colors(mut self, colors: SwitchColors) -> Self {
73        self.switch_colors = colors;
74        self
75    }
76
77    /// Uses a caller-owned focus handle for this tile.
78    pub fn track_focus(mut self, focus: &FocusHandle) -> Self {
79        self.fields.focus = Some(focus.clone());
80        self
81    }
82
83    /// Reports hover, focus, press, and disabled states through a caller-owned source.
84    pub fn interaction_source(mut self, source: &Entity<InteractionSource>) -> Self {
85        self.fields.source = Some(source.clone());
86        self
87    }
88
89    /// Invoked with the requested next checked state when the tile is toggled.
90    pub fn on_change(mut self, handler: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
91        self.on_change = Some(Rc::new(handler));
92        self
93    }
94
95    pub(super) fn into_list_item(self) -> ListItem {
96        let switch = Switch::new(
97            (self.fields.id.clone(), "switch"),
98            self.fields.label.clone(),
99            self.checked,
100        )
101        .disabled(self.fields.disabled)
102        .colors(self.switch_colors)
103        .decorative(true);
104
105        let mut item = self
106            .fields
107            .into_list_item()
108            .checked(self.checked)
109            .checked_role(Role::Switch)
110            .checked_selects_row(false)
111            .trailing(switch);
112
113        if let Some(on_change) = self.on_change {
114            item = item.on_checked_change(move |next, window, cx| on_change(next, window, cx));
115        }
116        item
117    }
118}
119
120impl RenderOnce for SwitchTile {
121    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
122        let variant = self.fields.variant;
123        apply_standalone_variant(self.into_list_item(), variant)
124    }
125}