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
use gpui::{
div, prelude::FluentBuilder as _, App, Axis, Corners, Edges, ElementId, InteractiveElement,
IntoElement, ParentElement, RenderOnce, StatefulInteractiveElement as _, StyleRefinement,
Styled, Window,
};
use std::{cell::Cell, rc::Rc};
use crate::{
button::{Button, ButtonVariant, ButtonVariants},
Disableable, Sizable, Size, StyledExt,
};
/// A ButtonGroup element, to wrap multiple buttons in a group.
#[derive(IntoElement)]
pub struct ButtonGroup {
id: ElementId,
style: StyleRefinement,
children: Vec<Button>,
pub(super) multiple: bool,
pub(super) disabled: bool,
pub(super) layout: Axis,
// The button props
pub(super) compact: bool,
pub(super) outline: bool,
pub(super) variant: Option<ButtonVariant>,
pub(super) size: Option<Size>,
on_click: Option<Box<dyn Fn(&Vec<usize>, &mut Window, &mut App) + 'static>>,
}
impl Disableable for ButtonGroup {
fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
impl ButtonGroup {
/// Creates a new ButtonGroup.
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
style: StyleRefinement::default(),
children: Vec::new(),
variant: None,
size: None,
compact: false,
outline: false,
multiple: false,
disabled: false,
layout: Axis::Horizontal,
on_click: None,
}
}
/// Adds a button as a child to the ButtonGroup.
pub fn child(mut self, child: Button) -> Self {
self.children.push(child.disabled(self.disabled));
self
}
/// Adds multiple buttons as children to the ButtonGroup.
pub fn children(mut self, children: impl IntoIterator<Item = Button>) -> Self {
self.children.extend(children);
self
}
/// With the multiple selection mode, default is false (single selection).
pub fn multiple(mut self, multiple: bool) -> Self {
self.multiple = multiple;
self
}
/// Set the layout of the button group. Default is `Axis::Horizontal`.
pub fn layout(mut self, layout: Axis) -> Self {
self.layout = layout;
self
}
/// With the compact mode for the ButtonGroup.
///
/// See also: [`Button::compact()`]
pub fn compact(mut self) -> Self {
self.compact = true;
self
}
/// With the outline mode for the ButtonGroup.
///
/// See also: [`Button::outline()`]
pub fn outline(mut self) -> Self {
self.outline = true;
self
}
/// Sets the on_click handler for the ButtonGroup.
///
/// The handler first argument is a vector of the selected button indices.
///
/// The `&Vec<usize>` is the indices of the clicked (selected in `multiple` mode) buttons.
/// For example: `[0, 2, 3]` is means the first, third and fourth buttons are clicked.
///
/// ```ignore
/// ButtonGroup::new("size-button")
/// .child(Button::new("large").label("Large").selected(self.size == Size::Large))
/// .child(Button::new("medium").label("Medium").selected(self.size == Size::Medium))
/// .child(Button::new("small").label("Small").selected(self.size == Size::Small))
/// .on_click(cx.listener(|view, clicks: &Vec<usize>, _, cx| {
/// if clicks.contains(&0) {
/// view.size = Size::Large;
/// } else if clicks.contains(&1) {
/// view.size = Size::Medium;
/// } else if clicks.contains(&2) {
/// view.size = Size::Small;
/// }
/// cx.notify();
/// }))
/// ```
pub fn on_click(
mut self,
handler: impl Fn(&Vec<usize>, &mut Window, &mut App) + 'static,
) -> Self {
self.on_click = Some(Box::new(handler));
self
}
}
impl Sizable for ButtonGroup {
fn with_size(mut self, size: impl Into<Size>) -> Self {
self.size = Some(size.into());
self
}
}
impl Styled for ButtonGroup {
fn style(&mut self) -> &mut gpui::StyleRefinement {
&mut self.style
}
}
impl ButtonVariants for ButtonGroup {
fn with_variant(mut self, variant: ButtonVariant) -> Self {
self.variant = Some(variant);
self
}
}
impl RenderOnce for ButtonGroup {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let children_len = self.children.len();
let mut selected_ixs: Vec<usize> = Vec::new();
let state = Rc::new(Cell::new(None));
for (ix, child) in self.children.iter().enumerate() {
if child.selected {
selected_ixs.push(ix);
}
}
let vertical = self.layout == Axis::Vertical;
div()
.id(self.id)
.flex()
.when(vertical, |this| this.flex_col().justify_center())
.when(!vertical, |this| this.items_center())
.refine_style(&self.style)
.children(
self.children
.into_iter()
.enumerate()
.map(|(child_index, child)| {
let state = Rc::clone(&state);
let child = if children_len == 1 {
child
} else if child_index == 0 {
// First
child
.border_corners(Corners {
top_left: true,
top_right: vertical,
bottom_left: !vertical,
bottom_right: false,
})
.border_edges(Edges {
left: true,
top: true,
right: true,
bottom: true,
})
} else if child_index == children_len - 1 {
// Last
child
.border_edges(Edges {
left: vertical,
top: !vertical,
right: true,
bottom: true,
})
.border_corners(Corners {
top_left: false,
top_right: !vertical,
bottom_left: vertical,
bottom_right: true,
})
} else {
// Middle
child
.border_corners(Corners::all(false))
.border_edges(Edges {
left: vertical,
top: !vertical,
right: true,
bottom: true,
})
}
.when_some(self.size, |this, size| this.with_size(size))
.when_some(self.variant, |this, variant| this.with_variant(variant))
.when(self.compact, |this| this.compact())
.when(self.outline, |this| this.outline())
.when(self.on_click.is_some(), |this| {
this.on_click(move |_, _, _| {
state.set(Some(child_index));
})
});
child
}),
)
.when_some(
self.on_click.filter(|_| !self.disabled),
move |this, on_click| {
this.on_click(move |_, window, cx| {
let mut selected_ixs = selected_ixs.clone();
if let Some(ix) = state.get() {
if self.multiple {
if let Some(pos) = selected_ixs.iter().position(|&i| i == ix) {
selected_ixs.remove(pos);
} else {
selected_ixs.push(ix);
}
} else {
selected_ixs.clear();
selected_ixs.push(ix);
}
}
on_click(&selected_ixs, window, cx);
})
},
)
}
}