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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
use std::rc::Rc;
use gpui::{AnyView, ClickEvent, relative};
use crate::{ButtonLike, ButtonLikeRounding, TintColor, Tooltip, prelude::*};
/// The position of a [`ToggleButton`] within a group of buttons.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct ToggleButtonPosition {
/// The toggle button is one of the leftmost of the group.
leftmost: bool,
/// The toggle button is one of the rightmost of the group.
rightmost: bool,
/// The toggle button is one of the topmost of the group.
topmost: bool,
/// The toggle button is one of the bottommost of the group.
bottommost: bool,
}
impl ToggleButtonPosition {
pub const HORIZONTAL_FIRST: Self = Self {
leftmost: true,
..Self::HORIZONTAL_MIDDLE
};
pub const HORIZONTAL_MIDDLE: Self = Self {
leftmost: false,
rightmost: false,
topmost: true,
bottommost: true,
};
pub const HORIZONTAL_LAST: Self = Self {
rightmost: true,
..Self::HORIZONTAL_MIDDLE
};
pub(crate) fn to_rounding(self) -> ButtonLikeRounding {
ButtonLikeRounding {
top_left: self.topmost && self.leftmost,
top_right: self.topmost && self.rightmost,
bottom_right: self.bottommost && self.rightmost,
bottom_left: self.bottommost && self.leftmost,
}
}
}
pub struct ButtonConfiguration {
label: SharedString,
icon: Option<IconName>,
on_click: Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>,
selected: bool,
tooltip: Option<Rc<dyn Fn(&mut Window, &mut App) -> AnyView>>,
}
mod private {
pub trait ToggleButtonStyle {}
}
pub trait ButtonBuilder: 'static + private::ToggleButtonStyle {
fn into_configuration(self) -> ButtonConfiguration;
}
pub struct ToggleButtonSimple {
label: SharedString,
on_click: Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>,
selected: bool,
tooltip: Option<Rc<dyn Fn(&mut Window, &mut App) -> AnyView>>,
}
impl ToggleButtonSimple {
pub fn new(
label: impl Into<SharedString>,
on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> Self {
Self {
label: label.into(),
on_click: Box::new(on_click),
selected: false,
tooltip: None,
}
}
pub fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
self
}
pub fn tooltip(mut self, tooltip: impl Fn(&mut Window, &mut App) -> AnyView + 'static) -> Self {
self.tooltip = Some(Rc::new(tooltip));
self
}
}
impl private::ToggleButtonStyle for ToggleButtonSimple {}
impl ButtonBuilder for ToggleButtonSimple {
fn into_configuration(self) -> ButtonConfiguration {
ButtonConfiguration {
label: self.label,
icon: None,
on_click: self.on_click,
selected: self.selected,
tooltip: self.tooltip,
}
}
}
pub struct ToggleButtonWithIcon {
label: SharedString,
icon: IconName,
on_click: Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>,
selected: bool,
tooltip: Option<Rc<dyn Fn(&mut Window, &mut App) -> AnyView>>,
}
impl ToggleButtonWithIcon {
pub fn new(
label: impl Into<SharedString>,
icon: IconName,
on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> Self {
Self {
label: label.into(),
icon,
on_click: Box::new(on_click),
selected: false,
tooltip: None,
}
}
pub fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
self
}
pub fn tooltip(mut self, tooltip: impl Fn(&mut Window, &mut App) -> AnyView + 'static) -> Self {
self.tooltip = Some(Rc::new(tooltip));
self
}
}
impl private::ToggleButtonStyle for ToggleButtonWithIcon {}
impl ButtonBuilder for ToggleButtonWithIcon {
fn into_configuration(self) -> ButtonConfiguration {
ButtonConfiguration {
label: self.label,
icon: Some(self.icon),
on_click: self.on_click,
selected: self.selected,
tooltip: self.tooltip,
}
}
}
#[derive(Clone, Copy, PartialEq)]
pub enum ToggleButtonGroupStyle {
Transparent,
Filled,
Outlined,
}
#[derive(Clone, Copy, PartialEq)]
pub enum ToggleButtonGroupSize {
Default,
Medium,
Large,
Custom(Rems),
}
#[derive(IntoElement)]
pub struct ToggleButtonGroup<T, const COLS: usize = 3, const ROWS: usize = 1>
where
T: ButtonBuilder,
{
group_name: SharedString,
rows: [[T; COLS]; ROWS],
style: ToggleButtonGroupStyle,
size: ToggleButtonGroupSize,
label_size: LabelSize,
group_width: Option<DefiniteLength>,
auto_width: bool,
selected_index: usize,
tab_index: Option<isize>,
}
impl<T: ButtonBuilder, const COLS: usize> ToggleButtonGroup<T, COLS> {
pub fn single_row(group_name: impl Into<SharedString>, buttons: [T; COLS]) -> Self {
Self {
group_name: group_name.into(),
rows: [buttons],
style: ToggleButtonGroupStyle::Transparent,
size: ToggleButtonGroupSize::Default,
label_size: LabelSize::Small,
group_width: None,
auto_width: false,
selected_index: 0,
tab_index: None,
}
}
}
impl<T: ButtonBuilder, const COLS: usize> ToggleButtonGroup<T, COLS, 2> {
pub fn two_rows(
group_name: impl Into<SharedString>,
first_row: [T; COLS],
second_row: [T; COLS],
) -> Self {
Self {
group_name: group_name.into(),
rows: [first_row, second_row],
style: ToggleButtonGroupStyle::Transparent,
size: ToggleButtonGroupSize::Default,
label_size: LabelSize::Small,
group_width: None,
auto_width: false,
selected_index: 0,
tab_index: None,
}
}
}
impl<T: ButtonBuilder, const COLS: usize, const ROWS: usize> ToggleButtonGroup<T, COLS, ROWS> {
pub fn style(mut self, style: ToggleButtonGroupStyle) -> Self {
self.style = style;
self
}
pub fn size(mut self, size: ToggleButtonGroupSize) -> Self {
self.size = size;
self
}
pub fn selected_index(mut self, index: usize) -> Self {
self.selected_index = index;
self
}
/// Makes the button group size itself to fit the content of the buttons,
/// rather than filling the full width of its parent.
pub fn auto_width(mut self) -> Self {
self.auto_width = true;
self
}
pub fn label_size(mut self, label_size: LabelSize) -> Self {
self.label_size = label_size;
self
}
/// Sets the tab index for the toggle button group.
/// The tab index is set to the initial value provided, then the
/// value is incremented by the number of buttons in the group.
pub fn tab_index(mut self, tab_index: &mut isize) -> Self {
self.tab_index = Some(*tab_index);
*tab_index += (COLS * ROWS) as isize;
self
}
const fn button_width() -> DefiniteLength {
relative(1. / COLS as f32)
}
}
impl<T: ButtonBuilder, const COLS: usize, const ROWS: usize> FixedWidth
for ToggleButtonGroup<T, COLS, ROWS>
{
fn width(mut self, width: impl Into<DefiniteLength>) -> Self {
self.group_width = Some(width.into());
self
}
fn full_width(mut self) -> Self {
self.group_width = Some(relative(1.));
self
}
}
impl<T: ButtonBuilder, const COLS: usize, const ROWS: usize> RenderOnce
for ToggleButtonGroup<T, COLS, ROWS>
{
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let custom_height = match self.size {
ToggleButtonGroupSize::Custom(height) => Some(height),
_ => None,
};
let entries =
self.rows.into_iter().enumerate().map(|(row_index, row)| {
let group_name = self.group_name.clone();
row.into_iter().enumerate().map(move |(col_index, button)| {
let ButtonConfiguration {
label,
icon,
on_click,
selected,
tooltip,
} = button.into_configuration();
let entry_index = row_index * COLS + col_index;
ButtonLike::new((group_name.clone(), entry_index))
.when(!self.auto_width, |this| this.full_width())
.rounding(Some(
ToggleButtonPosition {
leftmost: col_index == 0,
rightmost: col_index == COLS - 1,
topmost: row_index == 0,
bottommost: row_index == ROWS - 1,
}
.to_rounding(),
))
.when_some(self.tab_index, |this, tab_index| {
this.tab_index(tab_index + entry_index as isize)
})
.when(entry_index == self.selected_index || selected, |this| {
this.toggle_state(true)
.selected_style(ButtonStyle::Tinted(TintColor::Accent))
})
.when(self.style == ToggleButtonGroupStyle::Filled, |button| {
button.style(ButtonStyle::Filled)
})
.when(self.size == ToggleButtonGroupSize::Medium, |button| {
button.size(ButtonSize::Medium)
})
.when(self.size == ToggleButtonGroupSize::Large, |button| {
button.size(ButtonSize::Large)
})
.when_some(custom_height, |button, height| button.height(height.into()))
.child(
h_flex()
.w_full()
.px_2()
.gap_1p5()
.justify_center()
.flex_none()
.when_some(icon, |this, icon| {
this.py_2()
.child(Icon::new(icon).size(IconSize::XSmall).map(|this| {
if entry_index == self.selected_index || selected {
this.color(Color::Accent)
} else {
this.color(Color::Muted)
}
}))
})
.child(Label::new(label).size(self.label_size).when(
entry_index == self.selected_index || selected,
|this| this.color(Color::Accent),
)),
)
.when_some(tooltip, |this, tooltip| {
this.tooltip(move |window, cx| tooltip(window, cx))
})
.on_click(on_click)
.into_any_element()
})
});
let border_color = cx.theme().colors().border.opacity(0.6);
let is_outlined_or_filled = self.style == ToggleButtonGroupStyle::Outlined
|| self.style == ToggleButtonGroupStyle::Filled;
let is_transparent = self.style == ToggleButtonGroupStyle::Transparent;
v_flex()
.map(|this| {
if let Some(width) = self.group_width {
this.w(width)
} else if self.auto_width {
this
} else {
this.w_full()
}
})
.rounded_md()
.overflow_hidden()
.map(|this| {
if is_transparent {
this.gap_px()
} else {
this.border_1().border_color(border_color)
}
})
.children(entries.enumerate().map(|(row_index, row)| {
let last_row = row_index == ROWS - 1;
h_flex()
.when(!is_outlined_or_filled, |this| this.gap_px())
.when(is_outlined_or_filled && !last_row, |this| {
this.border_b_1().border_color(border_color)
})
.children(row.enumerate().map(|(item_index, item)| {
let last_item = item_index == COLS - 1;
div()
.when(is_outlined_or_filled && !last_item, |this| {
this.border_r_1().border_color(border_color)
})
.when(!self.auto_width, |this| this.w(Self::button_width()))
.overflow_hidden()
.child(item)
}))
}))
}
}
fn register_toggle_button_group() {
component::register_component::<ToggleButtonGroup<ToggleButtonSimple>>();
}
component::__private::inventory::submit! {
component::ComponentFn::new(register_toggle_button_group)
}
impl<T: ButtonBuilder, const COLS: usize, const ROWS: usize> Component
for ToggleButtonGroup<T, COLS, ROWS>
{
fn name() -> &'static str {
"ToggleButtonGroup"
}
fn scope() -> ComponentScope {
ComponentScope::Input
}
fn sort_name() -> &'static str {
"ButtonG"
}
fn preview(_window: &mut Window, _cx: &mut App) -> Option<AnyElement> {
Some(
v_flex()
.gap_6()
.children(vec![example_group_with_title(
"Transparent Variant",
vec![
single_example(
"Single Row Group",
ToggleButtonGroup::single_row(
"single_row_test",
[
ToggleButtonSimple::new("First", |_, _, _| {}),
ToggleButtonSimple::new("Second", |_, _, _| {}),
ToggleButtonSimple::new("Third", |_, _, _| {}),
],
)
.selected_index(1)
.into_any_element(),
),
single_example(
"Single Row Group with icons",
ToggleButtonGroup::single_row(
"single_row_test_icon",
[
ToggleButtonWithIcon::new(
"First",
IconName::AiOpenCode,
|_, _, _| {},
),
ToggleButtonWithIcon::new(
"Second",
IconName::AiOpenCode,
|_, _, _| {},
),
ToggleButtonWithIcon::new(
"Third",
IconName::AiOpenCode,
|_, _, _| {},
),
],
)
.selected_index(1)
.into_any_element(),
),
single_example(
"Multiple Row Group",
ToggleButtonGroup::two_rows(
"multiple_row_test",
[
ToggleButtonSimple::new("First", |_, _, _| {}),
ToggleButtonSimple::new("Second", |_, _, _| {}),
ToggleButtonSimple::new("Third", |_, _, _| {}),
],
[
ToggleButtonSimple::new("Fourth", |_, _, _| {}),
ToggleButtonSimple::new("Fifth", |_, _, _| {}),
ToggleButtonSimple::new("Sixth", |_, _, _| {}),
],
)
.selected_index(3)
.into_any_element(),
),
single_example(
"Multiple Row Group with Icons",
ToggleButtonGroup::two_rows(
"multiple_row_test_icons",
[
ToggleButtonWithIcon::new(
"First",
IconName::AiOpenCode,
|_, _, _| {},
),
ToggleButtonWithIcon::new(
"Second",
IconName::AiOpenCode,
|_, _, _| {},
),
ToggleButtonWithIcon::new(
"Third",
IconName::AiOpenCode,
|_, _, _| {},
),
],
[
ToggleButtonWithIcon::new(
"Fourth",
IconName::AiOpenCode,
|_, _, _| {},
),
ToggleButtonWithIcon::new(
"Fifth",
IconName::AiOpenCode,
|_, _, _| {},
),
ToggleButtonWithIcon::new(
"Sixth",
IconName::AiOpenCode,
|_, _, _| {},
),
],
)
.selected_index(3)
.into_any_element(),
),
],
)])
.children(vec![example_group_with_title(
"Outlined Variant",
vec![
single_example(
"Single Row Group",
ToggleButtonGroup::single_row(
"single_row_test_outline",
[
ToggleButtonSimple::new("First", |_, _, _| {}),
ToggleButtonSimple::new("Second", |_, _, _| {}),
ToggleButtonSimple::new("Third", |_, _, _| {}),
],
)
.selected_index(1)
.style(ToggleButtonGroupStyle::Outlined)
.into_any_element(),
),
single_example(
"Single Row Group with icons",
ToggleButtonGroup::single_row(
"single_row_test_icon_outlined",
[
ToggleButtonWithIcon::new(
"First",
IconName::AiOpenCode,
|_, _, _| {},
),
ToggleButtonWithIcon::new(
"Second",
IconName::AiOpenCode,
|_, _, _| {},
),
ToggleButtonWithIcon::new(
"Third",
IconName::AiOpenCode,
|_, _, _| {},
),
],
)
.selected_index(1)
.style(ToggleButtonGroupStyle::Outlined)
.into_any_element(),
),
single_example(
"Multiple Row Group",
ToggleButtonGroup::two_rows(
"multiple_row_test",
[
ToggleButtonSimple::new("First", |_, _, _| {}),
ToggleButtonSimple::new("Second", |_, _, _| {}),
ToggleButtonSimple::new("Third", |_, _, _| {}),
],
[
ToggleButtonSimple::new("Fourth", |_, _, _| {}),
ToggleButtonSimple::new("Fifth", |_, _, _| {}),
ToggleButtonSimple::new("Sixth", |_, _, _| {}),
],
)
.selected_index(3)
.style(ToggleButtonGroupStyle::Outlined)
.into_any_element(),
),
single_example(
"Multiple Row Group with Icons",
ToggleButtonGroup::two_rows(
"multiple_row_test",
[
ToggleButtonWithIcon::new(
"First",
IconName::AiOpenCode,
|_, _, _| {},
),
ToggleButtonWithIcon::new(
"Second",
IconName::AiOpenCode,
|_, _, _| {},
),
ToggleButtonWithIcon::new(
"Third",
IconName::AiOpenCode,
|_, _, _| {},
),
],
[
ToggleButtonWithIcon::new(
"Fourth",
IconName::AiOpenCode,
|_, _, _| {},
),
ToggleButtonWithIcon::new(
"Fifth",
IconName::AiOpenCode,
|_, _, _| {},
),
ToggleButtonWithIcon::new(
"Sixth",
IconName::AiOpenCode,
|_, _, _| {},
),
],
)
.selected_index(3)
.style(ToggleButtonGroupStyle::Outlined)
.into_any_element(),
),
],
)])
.children(vec![example_group_with_title(
"Filled Variant",
vec![
single_example(
"Single Row Group",
ToggleButtonGroup::single_row(
"single_row_test_outline",
[
ToggleButtonSimple::new("First", |_, _, _| {}),
ToggleButtonSimple::new("Second", |_, _, _| {}),
ToggleButtonSimple::new("Third", |_, _, _| {}),
],
)
.selected_index(2)
.style(ToggleButtonGroupStyle::Filled)
.into_any_element(),
),
single_example(
"Single Row Group with icons",
ToggleButtonGroup::single_row(
"single_row_test_icon_outlined",
[
ToggleButtonWithIcon::new(
"First",
IconName::AiOpenCode,
|_, _, _| {},
),
ToggleButtonWithIcon::new(
"Second",
IconName::AiOpenCode,
|_, _, _| {},
),
ToggleButtonWithIcon::new(
"Third",
IconName::AiOpenCode,
|_, _, _| {},
),
],
)
.selected_index(1)
.style(ToggleButtonGroupStyle::Filled)
.into_any_element(),
),
single_example(
"Multiple Row Group",
ToggleButtonGroup::two_rows(
"multiple_row_test",
[
ToggleButtonSimple::new("First", |_, _, _| {}),
ToggleButtonSimple::new("Second", |_, _, _| {}),
ToggleButtonSimple::new("Third", |_, _, _| {}),
],
[
ToggleButtonSimple::new("Fourth", |_, _, _| {}),
ToggleButtonSimple::new("Fifth", |_, _, _| {}),
ToggleButtonSimple::new("Sixth", |_, _, _| {}),
],
)
.selected_index(3)
.width(rems_from_px(100.))
.style(ToggleButtonGroupStyle::Filled)
.into_any_element(),
),
single_example(
"Multiple Row Group with Icons",
ToggleButtonGroup::two_rows(
"multiple_row_test",
[
ToggleButtonWithIcon::new(
"First",
IconName::AiOpenCode,
|_, _, _| {},
),
ToggleButtonWithIcon::new(
"Second",
IconName::AiOpenCode,
|_, _, _| {},
),
ToggleButtonWithIcon::new(
"Third",
IconName::AiOpenCode,
|_, _, _| {},
),
],
[
ToggleButtonWithIcon::new(
"Fourth",
IconName::AiOpenCode,
|_, _, _| {},
),
ToggleButtonWithIcon::new(
"Fifth",
IconName::AiOpenCode,
|_, _, _| {},
),
ToggleButtonWithIcon::new(
"Sixth",
IconName::AiOpenCode,
|_, _, _| {},
),
],
)
.selected_index(3)
.width(rems_from_px(100.))
.style(ToggleButtonGroupStyle::Filled)
.into_any_element(),
),
],
)])
.children(vec![single_example(
"With Tooltips",
ToggleButtonGroup::single_row(
"with_tooltips",
[
ToggleButtonSimple::new("First", |_, _, _| {})
.tooltip(Tooltip::text("This is a tooltip. Hello!")),
ToggleButtonSimple::new("Second", |_, _, _| {})
.tooltip(Tooltip::text("This is a tooltip. Hey?")),
ToggleButtonSimple::new("Third", |_, _, _| {})
.tooltip(Tooltip::text("This is a tooltip. Get out of here now!")),
],
)
.selected_index(1)
.into_any_element(),
)])
.into_any_element(),
)
}
}