1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//! Toggle group component for gpuikit
//!
//! A toggle group allows selecting one or multiple options from a group of toggle buttons.
use crate::theme::{ActiveTheme, Themeable};
use crate::traits::disableable::Disableable;
use crate::traits::orientable::{Orientable, Orientation};
use gpui::{
div, prelude::*, rems, Context, Div, ElementId, EventEmitter, FontWeight, Hsla,
InteractiveElement, IntoElement, MouseButton, ParentElement, Render, SharedString, Stateful,
StatefulInteractiveElement, Styled, Window,
};
/// Selection mode for the toggle group
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum ToggleGroupMode {
/// Only one item can be selected at a time (like radio buttons)
#[default]
Single,
/// Multiple items can be selected simultaneously
Multiple,
}
/// Event emitted when the toggle group selection changes
pub struct ToggleGroupChanged<T: Clone> {
/// Currently selected values
pub selected: Vec<T>,
}
/// A single toggle option in the group
#[derive(Clone)]
pub struct ToggleOption<T: Clone> {
/// The value associated with this option
pub value: T,
/// Display label for the option
pub label: SharedString,
/// Whether this option is disabled
pub disabled: bool,
}
impl<T: Clone> ToggleOption<T> {
/// Create a new toggle option with a value and label
pub fn new(value: T, label: impl Into<SharedString>) -> Self {
Self {
value,
label: label.into(),
disabled: false,
}
}
/// Set whether this option is disabled
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
impl<T: Clone> Disableable for ToggleOption<T> {
fn is_disabled(&self) -> bool {
self.disabled
}
fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
/// A toggle group component for selecting one or multiple options
///
/// # Example
///
/// ```ignore
/// // Single-select mode (default)
/// let single = toggle_group(
/// "alignment",
/// vec![
/// toggle_option("left", "Left"),
/// toggle_option("center", "Center"),
/// toggle_option("right", "Right"),
/// ],
/// ).selected(vec!["center"]);
///
/// // Multi-select mode
/// let multi = toggle_group(
/// "features",
/// vec![
/// toggle_option("bold", "B"),
/// toggle_option("italic", "I"),
/// toggle_option("underline", "U"),
/// ],
/// ).mode(ToggleGroupMode::Multiple)
/// .selected(vec!["bold", "italic"]);
/// ```
pub struct ToggleGroup<T: Clone + PartialEq + 'static> {
id: ElementId,
options: Vec<ToggleOption<T>>,
selected: Vec<T>,
mode: ToggleGroupMode,
disabled: bool,
orientation: Orientation,
}
impl<T: Clone + PartialEq + 'static> EventEmitter<ToggleGroupChanged<T>> for ToggleGroup<T> {}
impl<T: Clone + PartialEq + 'static> ToggleGroup<T> {
/// Create a new toggle group with an ID and options
pub fn new(id: impl Into<ElementId>, options: Vec<ToggleOption<T>>) -> Self {
Self {
id: id.into(),
options,
selected: Vec::new(),
mode: ToggleGroupMode::default(),
disabled: false,
orientation: Orientation::Horizontal,
}
}
/// Set the selection mode (single or multiple)
pub fn mode(mut self, mode: ToggleGroupMode) -> Self {
self.mode = mode;
self
}
/// Set the selected values
pub fn selected(mut self, values: Vec<T>) -> Self {
self.selected = values;
self
}
/// Set a single selected value (convenience for single-select mode)
pub fn selected_value(mut self, value: T) -> Self {
self.selected = vec![value];
self
}
/// Get the currently selected values
pub fn get_selected(&self) -> &[T] {
&self.selected
}
/// Check if a value is selected
pub fn is_value_selected(&self, value: &T) -> bool {
self.selected.contains(value)
}
/// Set the selected values programmatically
pub fn set_selected(&mut self, values: Vec<T>, cx: &mut Context<Self>) {
if self.selected != values {
self.selected = values.clone();
cx.emit(ToggleGroupChanged { selected: values });
cx.notify();
}
}
fn toggle_option(&mut self, index: usize, cx: &mut Context<Self>) {
if let Some(option) = self.options.get(index) {
if option.disabled || self.disabled {
return;
}
let value = option.value.clone();
let is_selected = self.selected.contains(&value);
match self.mode {
ToggleGroupMode::Single => {
// In single mode, always select the clicked option (unless it's already selected)
if !is_selected {
self.selected = vec![value.clone()];
cx.emit(ToggleGroupChanged {
selected: vec![value],
});
cx.notify();
}
}
ToggleGroupMode::Multiple => {
// In multiple mode, toggle the selection
if is_selected {
self.selected.retain(|v| v != &value);
} else {
self.selected.push(value);
}
cx.emit(ToggleGroupChanged {
selected: self.selected.clone(),
});
cx.notify();
}
}
}
}
}
impl<T: Clone + PartialEq + 'static> Render for ToggleGroup<T> {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = cx.theme();
let group_disabled = self.disabled;
let selected = self.selected.clone();
let orientation = self.orientation;
let num_options = self.options.len();
let container = if orientation == Orientation::Vertical {
div().flex().flex_col()
} else {
div().flex().flex_row()
};
container
.id(self.id.clone())
.bg(theme.surface_secondary())
.border_1()
.border_color(theme.border())
.rounded_md()
.p(rems(0.125))
.gap(rems(0.125))
.children(
self.options
.iter()
.enumerate()
.map(|(index, option)| {
let is_selected = selected.contains(&option.value);
let is_disabled = group_disabled || option.disabled;
let label = option.label.clone();
let is_first = index == 0;
let is_last = index == num_options - 1;
let bg: Option<Hsla> = if is_disabled {
Some(theme.surface_tertiary())
} else if is_selected {
Some(theme.surface())
} else {
None
};
let text_color = if is_disabled {
theme.fg_disabled()
} else if is_selected {
theme.fg()
} else {
theme.fg_muted()
};
div()
.id(ElementId::NamedInteger("toggle-option".into(), index as u64))
.flex()
.items_center()
.justify_center()
.px(rems(0.75))
.py(rems(0.375))
.text_sm()
.font_weight(FontWeight::MEDIUM)
.when_some(bg, |this, bg| this.bg(bg))
.text_color(text_color)
.when(is_selected && !is_disabled, |this: Stateful<Div>| {
this.shadow_sm()
})
// Apply rounded corners based on orientation and position
.when(orientation == Orientation::Horizontal, |this| {
this.when(is_first, |t| t.rounded_l_sm())
.when(is_last, |t| t.rounded_r_sm())
.when(!is_first && !is_last, |t| t.rounded_none())
})
.when(orientation == Orientation::Vertical, |this| {
this.when(is_first, |t| t.rounded_t_sm())
.when(is_last, |t| t.rounded_b_sm())
.when(!is_first && !is_last, |t| t.rounded_none())
})
.when(!is_disabled, |this| {
this.cursor_pointer()
.hover(|style| {
if is_selected {
style
} else {
style.bg(theme.surface_tertiary())
}
})
.on_mouse_down(MouseButton::Left, |_, window, _| {
window.prevent_default()
})
.on_click(cx.listener(move |this, _, _, cx| {
this.toggle_option(index, cx);
}))
})
.when(is_disabled, |this| this.cursor_not_allowed().opacity(0.65))
.child(label)
})
.collect::<Vec<_>>(),
)
}
}
/// Convenience function to create a toggle group
pub fn toggle_group<T: Clone + PartialEq + 'static>(
id: impl Into<ElementId>,
options: Vec<ToggleOption<T>>,
) -> ToggleGroup<T> {
ToggleGroup::new(id, options)
}
/// Convenience function to create a toggle option
pub fn toggle_option<T: Clone>(value: T, label: impl Into<SharedString>) -> ToggleOption<T> {
ToggleOption::new(value, label)
}
impl<T: Clone + PartialEq + 'static> Disableable for ToggleGroup<T> {
fn is_disabled(&self) -> bool {
self.disabled
}
fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
impl<T: Clone + PartialEq + 'static> Orientable for ToggleGroup<T> {
fn orientation(mut self, orientation: Orientation) -> Self {
self.orientation = orientation;
self
}
}