1use std::{rc::Rc, time::Duration};
2
3use crate::animation::{Lerp, ease_in_out_cubic};
4use crate::{ActiveTheme, Icon, IconName, Selectable, Sizable, Size, StyledExt, h_flex};
5use gpui::ColorExt as _;
6use gpui::prelude::FluentBuilder as _;
7use gpui::{
8 Animation, AnimationExt as _, AnyElement, App, Background, ClickEvent, Edges, ElementId, Hsla,
9 InteractiveElement, IntoElement, ParentElement, Pixels, RenderOnce, SharedString,
10 StatefulInteractiveElement, Styled, Window, div, px, relative,
11};
12
13#[derive(Debug, Clone, Default, Copy, PartialEq, Eq, Hash)]
15pub enum TabVariant {
16 #[default]
17 Tab,
18 Outline,
19 Pill,
20 Segmented,
21 Underline,
22}
23
24impl TabVariant {
25 fn height(&self, size: Size) -> Pixels {
26 match size {
27 Size::XSmall => match self {
28 TabVariant::Underline => px(26.),
29 _ => px(20.),
30 },
31 Size::Small => match self {
32 TabVariant::Underline => px(30.),
33 _ => px(24.),
34 },
35 Size::Large => match self {
36 TabVariant::Underline => px(44.),
37 _ => px(36.),
38 },
39 _ => match self {
40 TabVariant::Underline => px(36.),
41 _ => px(32.),
42 },
43 }
44 }
45
46 pub(super) fn inner_height(&self, size: Size) -> Pixels {
47 match size {
48 Size::XSmall => match self {
49 TabVariant::Tab | TabVariant::Outline | TabVariant::Pill => px(18.),
50 TabVariant::Segmented => px(16.),
51 TabVariant::Underline => px(20.),
52 },
53 Size::Small => match self {
54 TabVariant::Tab | TabVariant::Outline | TabVariant::Pill => px(22.),
55 TabVariant::Segmented => px(18.),
56 TabVariant::Underline => px(22.),
57 },
58 Size::Large => match self {
59 TabVariant::Tab | TabVariant::Outline | TabVariant::Pill => px(36.),
60 TabVariant::Segmented => px(28.),
61 TabVariant::Underline => px(32.),
62 },
63 _ => match self {
64 TabVariant::Tab => px(30.),
65 TabVariant::Outline | TabVariant::Pill => px(26.),
66 TabVariant::Segmented => px(24.),
67 TabVariant::Underline => px(26.),
68 },
69 }
70 }
71
72 fn inner_paddings(&self, size: Size) -> Edges<Pixels> {
74 let mut padding_x = match size {
75 Size::XSmall => px(8.),
76 Size::Small => px(10.),
77 Size::Large => px(16.),
78 _ => px(12.),
79 };
80
81 if matches!(self, TabVariant::Underline) {
82 padding_x = px(0.);
83 }
84
85 Edges {
86 left: padding_x,
87 right: padding_x,
88 ..Default::default()
89 }
90 }
91
92 fn inner_margins(&self, size: Size) -> Edges<Pixels> {
93 match size {
94 Size::XSmall => match self {
95 TabVariant::Underline => Edges {
96 top: px(1.),
97 bottom: px(2.),
98 ..Default::default()
99 },
100 _ => Edges::all(px(0.)),
101 },
102 Size::Small => match self {
103 TabVariant::Underline => Edges {
104 top: px(2.),
105 bottom: px(3.),
106 ..Default::default()
107 },
108 _ => Edges::all(px(0.)),
109 },
110 Size::Large => match self {
111 TabVariant::Underline => Edges {
112 top: px(5.),
113 bottom: px(6.),
114 ..Default::default()
115 },
116 _ => Edges::all(px(0.)),
117 },
118 _ => match self {
119 TabVariant::Underline => Edges {
120 top: px(3.),
121 bottom: px(4.),
122 ..Default::default()
123 },
124 _ => Edges::all(px(0.)),
125 },
126 }
127 }
128
129 fn normal(&self, cx: &App) -> TabStyle {
130 match self {
131 TabVariant::Tab => TabStyle {
132 fg: cx.theme().tab_foreground,
133 bg: cx.theme().transparent.into(),
134 borders: Edges {
135 left: px(1.),
136 right: px(1.),
137 ..Default::default()
138 },
139 border_color: cx.theme().transparent,
140 ..Default::default()
141 },
142 TabVariant::Outline => TabStyle {
143 fg: cx.theme().tab_foreground,
144 bg: cx.theme().transparent.into(),
145 borders: Edges::all(px(1.)),
146 border_color: cx.theme().border,
147 ..Default::default()
148 },
149 TabVariant::Pill => TabStyle {
150 fg: cx.theme().foreground,
151 bg: cx.theme().transparent.into(),
152 ..Default::default()
153 },
154 TabVariant::Segmented => TabStyle {
155 fg: cx.theme().tab_foreground,
156 bg: cx.theme().transparent.into(),
157 ..Default::default()
158 },
159 TabVariant::Underline => TabStyle {
160 fg: cx.theme().tab_foreground,
161 bg: cx.theme().transparent.into(),
162 inner_bg: cx.theme().transparent.into(),
163 borders: Edges {
164 bottom: px(2.),
165 ..Default::default()
166 },
167 border_color: cx.theme().transparent,
168 ..Default::default()
169 },
170 }
171 }
172
173 fn hovered(&self, selected: bool, cx: &App) -> TabStyle {
174 match self {
175 TabVariant::Tab => TabStyle {
176 fg: cx.theme().tab_active_foreground,
177 bg: cx.theme().transparent.into(),
178 borders: Edges {
179 left: px(1.),
180 right: px(1.),
181 ..Default::default()
182 },
183 border_color: cx.theme().transparent,
184 ..Default::default()
185 },
186 TabVariant::Outline => TabStyle {
187 fg: cx.theme().secondary_foreground,
188 bg: cx.theme().tokens.secondary_hover.into(),
189 borders: Edges::all(px(1.)),
190 border_color: cx.theme().border,
191 ..Default::default()
192 },
193 TabVariant::Pill => TabStyle {
194 fg: cx.theme().secondary_foreground,
195 bg: cx.theme().tokens.secondary.into(),
196 ..Default::default()
197 },
198 TabVariant::Segmented => TabStyle {
199 fg: cx.theme().tab_active_foreground,
200 bg: cx.theme().transparent.into(),
201 inner_bg: if selected {
202 cx.theme().tokens.background.into()
203 } else {
204 cx.theme().transparent.into()
205 },
206 ..Default::default()
207 },
208 TabVariant::Underline => TabStyle {
209 fg: cx.theme().tab_active_foreground,
210 bg: cx.theme().transparent.into(),
211 inner_bg: cx.theme().transparent.into(),
212 borders: Edges {
213 bottom: px(2.),
214 ..Default::default()
215 },
216 border_color: cx.theme().transparent,
217 ..Default::default()
218 },
219 }
220 }
221
222 fn selected(&self, cx: &App) -> TabStyle {
223 match self {
224 TabVariant::Tab => TabStyle {
225 fg: cx.theme().tab_active_foreground,
226 bg: cx.theme().tokens.tab_active.into(),
227 borders: Edges {
228 left: px(1.),
229 right: px(1.),
230 ..Default::default()
231 },
232 border_color: cx.theme().border,
233 ..Default::default()
234 },
235 TabVariant::Outline => TabStyle {
236 fg: cx.theme().primary,
237 bg: cx.theme().transparent.into(),
238 borders: Edges::all(px(1.)),
239 border_color: cx.theme().primary,
240 ..Default::default()
241 },
242 TabVariant::Pill => TabStyle {
243 fg: cx.theme().primary_foreground,
244 bg: cx.theme().tokens.primary.into(),
245 ..Default::default()
246 },
247 TabVariant::Segmented => TabStyle {
248 fg: cx.theme().tab_active_foreground,
249 bg: cx.theme().transparent.into(),
250 inner_bg: cx.theme().tokens.background.into(),
251 shadow: true,
252 ..Default::default()
253 },
254 TabVariant::Underline => TabStyle {
255 fg: cx.theme().tab_active_foreground,
256 bg: cx.theme().transparent.into(),
257 borders: Edges {
258 bottom: px(2.),
259 ..Default::default()
260 },
261 border_color: cx.theme().primary,
262 ..Default::default()
263 },
264 }
265 }
266
267 fn disabled(&self, selected: bool, cx: &App) -> TabStyle {
268 match self {
269 TabVariant::Tab => TabStyle {
270 fg: cx.theme().muted_foreground,
271 bg: cx.theme().transparent.into(),
272 border_color: if selected {
273 cx.theme().border
274 } else {
275 cx.theme().transparent
276 },
277 borders: Edges {
278 left: px(1.),
279 right: px(1.),
280 ..Default::default()
281 },
282 ..Default::default()
283 },
284 TabVariant::Outline => TabStyle {
285 fg: cx.theme().muted_foreground,
286 bg: cx.theme().transparent.into(),
287 borders: Edges::all(px(1.)),
288 border_color: if selected {
289 cx.theme().primary
290 } else {
291 cx.theme().border
292 },
293 ..Default::default()
294 },
295 TabVariant::Pill => TabStyle {
296 fg: if selected {
297 cx.theme().primary_foreground.opacity(0.5)
298 } else {
299 cx.theme().muted_foreground
300 },
301 bg: if selected {
302 cx.theme().primary.opacity(0.5).into()
303 } else {
304 cx.theme().transparent.into()
305 },
306 ..Default::default()
307 },
308 TabVariant::Segmented => TabStyle {
309 fg: cx.theme().muted_foreground,
310 bg: cx.theme().tokens.tab_bar.into(),
311 inner_bg: if selected {
312 cx.theme().tokens.background.into()
313 } else {
314 cx.theme().transparent.into()
315 },
316 ..Default::default()
317 },
318 TabVariant::Underline => TabStyle {
319 fg: cx.theme().muted_foreground,
320 bg: cx.theme().transparent.into(),
321 border_color: if selected {
322 cx.theme().border
323 } else {
324 cx.theme().transparent
325 },
326 borders: Edges {
327 bottom: px(2.),
328 ..Default::default()
329 },
330 ..Default::default()
331 },
332 }
333 }
334
335 pub(super) fn tab_bar_radius(&self, size: Size, cx: &App) -> Pixels {
336 if *self != TabVariant::Segmented {
337 return px(0.);
338 }
339
340 match size {
341 Size::XSmall | Size::Small => cx.theme().radius,
342 Size::Large => cx.theme().radius_lg,
343 _ => cx.theme().radius_lg,
344 }
345 }
346
347 fn radius(&self, size: Size, cx: &App) -> Pixels {
348 match self {
349 TabVariant::Outline | TabVariant::Pill => cx.theme().radius_full(),
350 TabVariant::Segmented => match size {
351 Size::XSmall | Size::Small => cx.theme().radius,
352 Size::Large => cx.theme().radius_lg,
353 _ => cx.theme().radius_lg,
354 },
355 _ => px(0.),
356 }
357 }
358
359 pub(super) fn inner_radius(&self, size: Size, cx: &App) -> Pixels {
360 match self {
361 TabVariant::Segmented => match size {
365 Size::Large => (self.tab_bar_radius(size, cx) - px(3.)).max(px(0.)),
366 _ => (self.tab_bar_radius(size, cx) - px(2.)).max(px(0.)),
367 },
368 _ => px(0.),
369 }
370 }
371}
372
373#[allow(dead_code)]
374struct TabStyle {
375 borders: Edges<Pixels>,
376 border_color: Hsla,
377 bg: Background,
378 fg: Hsla,
379 shadow: bool,
380 inner_bg: Background,
381}
382
383impl Default for TabStyle {
384 fn default() -> Self {
385 TabStyle {
386 borders: Edges::all(px(0.)),
387 border_color: gpui::transparent_white(),
388 bg: gpui::transparent_white().into(),
389 fg: gpui::transparent_white(),
390 shadow: false,
391 inner_bg: gpui::transparent_white().into(),
392 }
393 }
394}
395
396#[derive(IntoElement)]
398pub struct Tab {
399 ix: usize,
400 base: gpui_base::Tab,
401 pub(super) label: Option<SharedString>,
402 aria_label: Option<SharedString>,
403 pub(super) icon: Option<Icon>,
404 prefix: Option<AnyElement>,
405 pub(super) tab_bar_prefix: Option<bool>,
406 suffix: Option<AnyElement>,
407 children: Vec<AnyElement>,
408 variant: TabVariant,
409 size: Size,
410 pub(super) disabled: bool,
411 pub(super) selected: bool,
412 pub(super) indicator_active: bool,
413 pub(super) indicator_ready: bool,
414 pub(super) indicator_epoch: u64,
418 pub(super) max_width: Option<Pixels>,
419 on_click: Option<Rc<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>>,
420}
421
422impl From<&'static str> for Tab {
423 fn from(label: &'static str) -> Self {
424 Self::new().label(label)
425 }
426}
427
428impl From<String> for Tab {
429 fn from(label: String) -> Self {
430 Self::new().label(label)
431 }
432}
433
434impl From<SharedString> for Tab {
435 fn from(label: SharedString) -> Self {
436 Self::new().label(label)
437 }
438}
439
440impl From<Icon> for Tab {
441 fn from(icon: Icon) -> Self {
442 Self::default().icon(icon)
443 }
444}
445
446impl From<IconName> for Tab {
447 fn from(icon_name: IconName) -> Self {
448 Self::default().icon(Icon::new(icon_name))
449 }
450}
451
452impl Default for Tab {
453 fn default() -> Self {
454 Self {
455 ix: 0,
456 base: gpui_base::Tab::new(0usize),
457 label: None,
458 aria_label: None,
459 icon: None,
460 tab_bar_prefix: None,
461 children: Vec::new(),
462 disabled: false,
463 selected: false,
464 indicator_active: false,
465 indicator_ready: true,
466 indicator_epoch: 0,
467 prefix: None,
468 suffix: None,
469 variant: TabVariant::default(),
470 size: Size::default(),
471 max_width: None,
472 on_click: None,
473 }
474 }
475}
476
477impl Tab {
478 pub fn new() -> Self {
480 Self::default()
481 }
482
483 pub fn label(mut self, label: impl Into<SharedString>) -> Self {
485 self.label = Some(label.into());
486 self
487 }
488
489 pub fn aria_label(mut self, label: impl Into<SharedString>) -> Self {
491 self.aria_label = Some(label.into());
492 self
493 }
494
495 fn a11y_label(&self) -> Option<SharedString> {
496 self.aria_label.clone().or_else(|| self.label.clone())
497 }
498
499 pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
501 self.icon = Some(icon.into());
502 self
503 }
504
505 pub fn with_variant(mut self, variant: TabVariant) -> Self {
507 self.variant = variant;
508 self
509 }
510
511 pub fn pill(mut self) -> Self {
513 self.variant = TabVariant::Pill;
514 self
515 }
516
517 pub fn outline(mut self) -> Self {
519 self.variant = TabVariant::Outline;
520 self
521 }
522
523 pub fn segmented(mut self) -> Self {
525 self.variant = TabVariant::Segmented;
526 self
527 }
528
529 pub fn underline(mut self) -> Self {
531 self.variant = TabVariant::Underline;
532 self
533 }
534
535 pub fn prefix(mut self, prefix: impl IntoElement) -> Self {
537 self.prefix = Some(prefix.into_any_element());
538 self
539 }
540
541 pub fn suffix(mut self, suffix: impl IntoElement) -> Self {
543 self.suffix = Some(suffix.into_any_element());
544 self
545 }
546
547 pub fn disabled(mut self, disabled: bool) -> Self {
549 self.disabled = disabled;
550 self
551 }
552
553 pub fn on_click(
555 mut self,
556 on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
557 ) -> Self {
558 self.on_click = Some(Rc::new(on_click));
559 self
560 }
561
562 pub(crate) fn ix(mut self, ix: usize) -> Self {
564 self.ix = ix;
565 self.base = self.base.id(ix);
566 self
567 }
568
569 pub(crate) fn tab_bar_prefix(mut self, tab_bar_prefix: bool) -> Self {
571 self.tab_bar_prefix = Some(tab_bar_prefix);
572 self
573 }
574
575 pub(super) fn max_width(mut self, max_width: Option<Pixels>) -> Self {
577 self.max_width = max_width;
578 self
579 }
580}
581
582impl ParentElement for Tab {
583 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
584 self.children.extend(elements);
585 }
586}
587
588impl Selectable for Tab {
589 fn selected(mut self, selected: bool) -> Self {
590 self.selected = selected;
591 self
592 }
593
594 fn is_selected(&self) -> bool {
595 self.selected
596 }
597}
598
599impl InteractiveElement for Tab {
600 fn interactivity(&mut self) -> &mut gpui::Interactivity {
601 self.base.interactivity()
602 }
603}
604
605impl StatefulInteractiveElement for Tab {}
606
607impl Styled for Tab {
608 fn style(&mut self) -> &mut gpui::StyleRefinement {
609 self.base.style()
610 }
611}
612
613impl Sizable for Tab {
614 fn with_size(mut self, size: impl Into<Size>) -> Self {
615 self.size = size.into();
616 self
617 }
618}
619
620impl RenderOnce for Tab {
621 fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
622 let mut normal_style = self.variant.normal(cx);
623 let mut selected_style = self.variant.selected(cx);
624 let mut disabled_style = self.variant.disabled(self.selected, cx);
625 let mut hover_style = self.variant.hovered(self.selected, cx);
626 if self.disabled {
627 hover_style = self.variant.disabled(self.selected, cx);
628 }
629 let tab_bar_prefix = self.tab_bar_prefix.unwrap_or_default();
630 if !tab_bar_prefix {
631 if self.ix == 0 && self.variant == TabVariant::Tab {
632 normal_style.borders.left = px(0.);
633 selected_style.borders.left = px(0.);
634 disabled_style.borders.left = px(0.);
635 hover_style.borders.left = px(0.);
636 }
637 }
638 let tab_style = if self.disabled {
639 &disabled_style
640 } else if self.selected {
641 &selected_style
642 } else {
643 &normal_style
644 };
645 let radius = self.variant.radius(self.size, cx);
646 let inner_radius = self.variant.inner_radius(self.size, cx);
647 let inner_paddings = self.variant.inner_paddings(self.size);
648 let inner_margins = self.variant.inner_margins(self.size);
649 let inner_height = self.variant.inner_height(self.size);
650 let height = self.variant.height(self.size);
651 let aria_label = self.a11y_label();
652
653 let segmented_indicator_active =
654 self.variant == TabVariant::Segmented && self.indicator_active;
655 let has_inline_inner_bg =
656 self.selected && segmented_indicator_active && !self.indicator_ready;
657 let inline_inner_bg = tab_style.inner_bg;
658 let (inner_bg, hover_inner_bg) = if segmented_indicator_active && self.indicator_ready {
659 (cx.theme().transparent.into(), cx.theme().transparent.into())
660 } else if has_inline_inner_bg {
661 (inline_inner_bg, inline_inner_bg)
662 } else {
663 (tab_style.inner_bg, hover_style.inner_bg)
664 };
665 let inner_shadow = tab_style.shadow && !segmented_indicator_active;
666
667 let suppress_active_visual =
674 self.selected && !self.disabled && self.indicator_active && self.indicator_ready;
675 let selected_outer_bg = if suppress_active_visual && self.variant == TabVariant::Pill {
677 cx.theme().transparent.into()
678 } else {
679 selected_style.bg
680 };
681 let selected_outer_border_color =
683 if suppress_active_visual && self.variant == TabVariant::Underline {
684 cx.theme().transparent
685 } else {
686 selected_style.border_color
687 };
688
689 let animate_fg = self.selected
695 && !self.disabled
696 && self.variant == TabVariant::Pill
697 && self.indicator_active
698 && self.indicator_ready
699 && self.indicator_epoch > 0;
700 let fg_from = self.variant.normal(cx).fg;
701 let fg_to = tab_style.fg;
702 let max_width = self.max_width.filter(|_| self.icon.is_none());
704
705 let inner_content = h_flex()
706 .flex_1()
707 .h(inner_height)
708 .line_height(relative(1.25))
709 .whitespace_nowrap()
710 .items_center()
711 .justify_center()
712 .overflow_hidden()
713 .margins(inner_margins)
714 .map(|this| match max_width {
717 Some(_) => this.flex_auto(),
718 None => this.flex_shrink_0(),
719 })
720 .map(|this| match self.icon {
721 Some(icon) => this
722 .w(inner_height * 1.25)
723 .child(icon.map(|this| match self.size {
724 Size::XSmall => this.size_2p5(),
725 Size::Small => this.size_3p5(),
726 Size::Large => this.size_4(),
727 _ => this.size_4(),
728 })),
729 None => this
730 .paddings(inner_paddings)
731 .map(|this| match (self.label, max_width) {
732 (Some(label), Some(_)) => this.child(
735 div()
736 .min_w_0()
737 .whitespace_nowrap()
738 .text_ellipsis()
739 .child(label),
740 ),
741 (Some(label), None) => this.child(label),
742 (None, _) => this,
743 })
744 .children(self.children),
745 })
746 .bg(inner_bg)
747 .rounded(inner_radius)
748 .when(inner_shadow, |this| this.shadow_xs())
749 .hover(|this| this.bg(hover_inner_bg).rounded(inner_radius));
750
751 let inner_element = if animate_fg {
752 inner_content
753 .with_animation(
754 ElementId::NamedInteger("tab-fg".into(), self.indicator_epoch),
755 Animation::new(Duration::from_millis(200)).with_easing(ease_in_out_cubic),
756 move |this, delta| this.text_color(Lerp::lerp(&fg_from, &fg_to, delta)),
757 )
758 .into_any_element()
759 } else {
760 inner_content.into_any_element()
761 };
762
763 self.base
764 .id(self.ix)
765 .selected(self.selected)
766 .disabled(self.disabled)
767 .when_some(aria_label, |this, label| this.accessibility_label(label))
768 .styles(|styles| {
769 styles
770 .selected(|style| {
771 style
772 .text_color(selected_style.fg)
773 .bg(selected_outer_bg)
774 .border_l(selected_style.borders.left)
775 .border_r(selected_style.borders.right)
776 .border_t(selected_style.borders.top)
777 .border_b(selected_style.borders.bottom)
778 .border_color(selected_outer_border_color)
779 })
780 .disabled(|style| {
781 style
782 .text_color(disabled_style.fg)
783 .bg(disabled_style.bg)
784 .border_l(disabled_style.borders.left)
785 .border_r(disabled_style.borders.right)
786 .border_t(disabled_style.borders.top)
787 .border_b(disabled_style.borders.bottom)
788 .border_color(disabled_style.border_color)
789 })
790 })
791 .relative()
792 .flex()
793 .map(|this| match max_width {
796 Some(max_width) => this.flex_nowrap().max_w(max_width),
797 None => this.flex_wrap(),
798 })
799 .gap_1()
800 .items_center()
801 .flex_shrink_0()
802 .h(height)
803 .overflow_hidden()
804 .map(|this| match self.size {
805 Size::XSmall => this.text_xs(),
806 Size::Large => this.text_base(),
807 _ => this.text_sm(),
808 })
809 .rounded(radius)
810 .when(!self.selected && !self.disabled, |this| {
811 this.text_color(normal_style.fg)
812 .bg(normal_style.bg)
813 .border_l(normal_style.borders.left)
814 .border_r(normal_style.borders.right)
815 .border_t(normal_style.borders.top)
816 .border_b(normal_style.borders.bottom)
817 .border_color(normal_style.border_color)
818 })
819 .hover(|this| {
820 if self.selected || self.disabled {
824 return this;
825 }
826 this.text_color(hover_style.fg)
827 .bg(hover_style.bg)
828 .border_l(hover_style.borders.left)
829 .border_r(hover_style.borders.right)
830 .border_t(hover_style.borders.top)
831 .border_b(hover_style.borders.bottom)
832 .border_color(hover_style.border_color)
833 .rounded(radius)
834 })
835 .when(has_inline_inner_bg, |this| {
836 this.child(
837 div()
838 .absolute()
839 .left_0()
840 .right_0()
841 .top_0()
842 .bottom_0()
843 .flex()
844 .items_center()
845 .child(
846 div()
847 .w_full()
848 .h(inner_height)
849 .bg(inline_inner_bg)
850 .rounded(inner_radius)
851 .when(tab_style.shadow, |this| this.shadow_sm()),
852 ),
853 )
854 })
855 .when_some(self.prefix, |this, prefix| {
858 this.child(
859 div()
860 .when_some(max_width, |this, _| this.flex_shrink_0())
861 .child(prefix),
862 )
863 })
864 .child(inner_element)
865 .when_some(self.suffix, |this, suffix| {
866 this.child(
867 div()
868 .when_some(max_width, |this, _| this.flex_shrink_0())
869 .child(suffix),
870 )
871 })
872 .when_some(self.on_click.clone(), |this, on_click| {
873 this.on_click(move |event, window, cx| on_click(event, window, cx))
874 })
875 }
876}
877
878#[cfg(test)]
879mod tests {
880 use super::*;
881 use crate::tab::TabBar;
882 use gpui::{Context, Render, TestAppContext, VisualTestContext};
883
884 const VARIANTS: [TabVariant; 5] = [
885 TabVariant::Tab,
886 TabVariant::Outline,
887 TabVariant::Pill,
888 TabVariant::Segmented,
889 TabVariant::Underline,
890 ];
891
892 const LONG_LABEL: &str = "Account Settings & Preferences";
893
894 struct TabBarTest {
896 variant: TabVariant,
897 max_width: Option<Pixels>,
898 build: fn(Tab) -> Tab,
899 }
900
901 impl Render for TabBarTest {
902 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
903 TabBar::new("tabs")
904 .with_variant(self.variant)
905 .selected_index(0)
906 .when_some(self.max_width, |this, width| this.max_width(width))
907 .child((self.build)(
908 Tab::new().debug_selector(|| "tab".to_string()),
909 ))
910 }
911 }
912
913 fn show(
914 cx: &mut TestAppContext,
915 variant: TabVariant,
916 max_width: Option<Pixels>,
917 build: fn(Tab) -> Tab,
918 ) -> &mut VisualTestContext {
919 cx.update(crate::init);
920 let (_, cx) = cx.add_window_view(|_, _| TabBarTest {
921 variant,
922 max_width,
923 build,
924 });
925 cx.run_until_parked();
926 cx
927 }
928
929 fn variant_widths(
931 cx: &mut TestAppContext,
932 build: fn(Tab) -> Tab,
933 max_width: Option<Pixels>,
934 ) -> Vec<Pixels> {
935 VARIANTS
936 .into_iter()
937 .map(|variant| {
938 show(cx, variant, max_width, build)
939 .debug_bounds("tab")
940 .expect("tab not rendered")
941 .size
942 .width
943 })
944 .collect()
945 }
946
947 #[gpui::test]
948 fn a11y_label_defaults_to_visible_label(_cx: &mut TestAppContext) {
949 let tab = Tab::new().label("Account");
950
951 assert_eq!(tab.a11y_label(), Some("Account".into()));
952 }
953
954 #[gpui::test]
955 fn explicit_a11y_label_overrides_visible_label(_cx: &mut TestAppContext) {
956 let tab = Tab::new().label("Acct").aria_label("Account settings");
957
958 assert_eq!(tab.a11y_label(), Some("Account settings".into()));
959 }
960
961 #[gpui::test]
962 fn max_width_leaves_short_tabs_untouched(cx: &mut TestAppContext) {
963 let build: fn(Tab) -> Tab = |tab| tab.label("Go");
966
967 let uncapped = variant_widths(cx, build, None);
968 let capped = variant_widths(cx, build, Some(px(200.)));
969
970 for (variant, (uncapped, capped)) in
975 VARIANTS.into_iter().zip(uncapped.into_iter().zip(capped))
976 {
977 let delta = (capped - uncapped).abs();
978 assert!(
979 delta <= px(8.),
980 "{variant:?} resized under the cap: {uncapped:?} -> {capped:?}"
981 );
982 }
983 }
984
985 #[gpui::test]
986 fn max_width_caps_long_tabs(cx: &mut TestAppContext) {
987 let build: fn(Tab) -> Tab = |tab| tab.label(LONG_LABEL);
988
989 let uncapped = variant_widths(cx, build, None);
990 let capped = variant_widths(cx, build, Some(px(120.)));
991
992 for (variant, (uncapped, capped)) in
993 VARIANTS.into_iter().zip(uncapped.into_iter().zip(capped))
994 {
995 assert!(
996 uncapped > px(120.),
997 "{variant:?} is not long enough to exercise the cap ({uncapped:?})"
998 );
999 assert!(
1000 capped <= px(120.),
1001 "{variant:?} width {capped:?} exceeds max_width"
1002 );
1003 }
1004 }
1005
1006 #[gpui::test]
1007 fn max_width_keeps_prefix_and_suffix_intact(cx: &mut TestAppContext) {
1008 let cx = show(cx, TabVariant::Segmented, Some(px(140.)), |tab| {
1009 tab.prefix(Icon::new(IconName::BookOpen))
1010 .label(LONG_LABEL)
1011 .suffix(div().size(px(16.)).debug_selector(|| "suffix".to_string()))
1012 });
1013
1014 let tab = cx.debug_bounds("tab").expect("tab not rendered");
1015 let suffix = cx.debug_bounds("suffix").expect("suffix not rendered");
1016
1017 assert!(tab.size.width <= px(140.));
1018 assert_eq!(
1019 suffix.size.width,
1020 px(16.),
1021 "the label should absorb the truncation, not the suffix"
1022 );
1023 assert!(
1024 suffix.right() <= tab.right(),
1025 "suffix must stay within the tab"
1026 );
1027 assert!(
1031 suffix.left() > tab.center().x,
1032 "suffix must follow the label, not wrap below it"
1033 );
1034 }
1035
1036 #[gpui::test]
1039 fn max_width_exempts_icon_only_tabs(cx: &mut TestAppContext) {
1040 let build: fn(Tab) -> Tab = |tab| tab.icon(Icon::new(IconName::BookOpen));
1041 let width = |cx: &mut TestAppContext, max_width| {
1042 show(cx, TabVariant::Tab, max_width, build)
1043 .debug_bounds("tab")
1044 .expect("tab not rendered")
1045 .size
1046 .width
1047 };
1048
1049 let uncapped = width(cx, None);
1050 assert!(
1051 uncapped > px(16.),
1052 "the cap has to be narrower than the icon tab to be meaningful"
1053 );
1054 assert_eq!(
1055 width(cx, Some(px(16.))),
1056 uncapped,
1057 "an icon-only tab must ignore max_width"
1058 );
1059 }
1060}