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::prelude::FluentBuilder as _;
6use gpui::{
7 Animation, AnimationExt as _, AnyElement, App, Background, ClickEvent, Edges, ElementId, Hsla,
8 InteractiveElement, IntoElement, ParentElement, PathBuilder, Pixels, Point, RenderOnce,
9 SharedString, StatefulInteractiveElement, Styled, Window, canvas, div, point, px, relative,
10};
11use gpui_base::LogicalStyledExt as _;
12
13pub(super) const FOLDER_TAB_GAP: Pixels = px(4.);
15
16#[derive(Clone, Copy)]
18pub(super) struct FolderTabMetrics {
19 pub(super) top_padding: Pixels,
22 pub(super) radius: Pixels,
25 pub(super) padding_x: Pixels,
26 pub(super) separator_height: Pixels,
27}
28
29impl FolderTabMetrics {
30 pub(super) fn new(size: Size, cx: &App) -> Self {
31 let (top_padding, padding_x, separator_height, radius) = match size {
32 Size::XSmall => (px(2.), px(8.), px(12.), cx.theme().radius),
33 Size::Small => (px(4.), px(8.), px(12.), cx.theme().radius),
34 Size::Large => (px(8.), px(16.), px(16.), cx.theme().radius_lg),
35 _ => (px(4.), px(12.), px(16.), cx.theme().radius_lg),
36 };
37 Self {
38 top_padding,
39 radius: radius.min(TabVariant::Folder.height(size) / 3.),
42 padding_x,
43 separator_height,
44 }
45 }
46}
47
48#[derive(Debug, Clone, Default, Copy, PartialEq, Eq, Hash)]
50pub enum TabVariant {
51 #[default]
52 Tab,
53 Outline,
54 Pill,
55 Segmented,
56 Underline,
57 Folder,
61}
62
63impl TabVariant {
64 fn height(&self, size: Size) -> Pixels {
65 match size {
66 Size::XSmall => match self {
67 TabVariant::Underline => px(26.),
68 _ => px(20.),
69 },
70 Size::Small => match self {
71 TabVariant::Underline => px(30.),
72 _ => px(24.),
73 },
74 Size::Large => match self {
75 TabVariant::Underline => px(44.),
76 _ => px(36.),
77 },
78 _ => match self {
79 TabVariant::Underline => px(36.),
80 _ => px(32.),
81 },
82 }
83 }
84
85 pub(super) fn inner_height(&self, size: Size) -> Pixels {
86 match size {
87 Size::XSmall => match self {
88 TabVariant::Tab | TabVariant::Outline | TabVariant::Pill => px(18.),
89 TabVariant::Segmented | TabVariant::Folder => px(16.),
90 TabVariant::Underline => px(20.),
91 },
92 Size::Small => match self {
93 TabVariant::Tab | TabVariant::Outline | TabVariant::Pill => px(22.),
94 TabVariant::Segmented | TabVariant::Folder => px(18.),
95 TabVariant::Underline => px(22.),
96 },
97 Size::Large => match self {
98 TabVariant::Tab | TabVariant::Outline | TabVariant::Pill => px(36.),
99 TabVariant::Segmented | TabVariant::Folder => px(28.),
100 TabVariant::Underline => px(32.),
101 },
102 _ => match self {
103 TabVariant::Tab => px(30.),
104 TabVariant::Outline | TabVariant::Pill => px(26.),
105 TabVariant::Segmented | TabVariant::Folder => px(24.),
106 TabVariant::Underline => px(26.),
107 },
108 }
109 }
110
111 fn inner_paddings(&self, size: Size) -> Edges<Pixels> {
113 let mut padding_x = match size {
114 Size::XSmall => px(8.),
115 Size::Small => px(10.),
116 Size::Large => px(16.),
117 _ => px(12.),
118 };
119
120 if matches!(self, TabVariant::Underline | TabVariant::Folder) {
122 padding_x = px(0.);
123 }
124
125 Edges {
126 left: padding_x,
127 right: padding_x,
128 ..Default::default()
129 }
130 }
131
132 fn inner_margins(&self, size: Size) -> Edges<Pixels> {
133 match size {
134 Size::XSmall => match self {
135 TabVariant::Underline => Edges {
136 top: px(1.),
137 bottom: px(2.),
138 ..Default::default()
139 },
140 _ => Edges::all(px(0.)),
141 },
142 Size::Small => match self {
143 TabVariant::Underline => Edges {
144 top: px(2.),
145 bottom: px(3.),
146 ..Default::default()
147 },
148 _ => Edges::all(px(0.)),
149 },
150 Size::Large => match self {
151 TabVariant::Underline => Edges {
152 top: px(5.),
153 bottom: px(6.),
154 ..Default::default()
155 },
156 _ => Edges::all(px(0.)),
157 },
158 _ => match self {
159 TabVariant::Underline => Edges {
160 top: px(3.),
161 bottom: px(4.),
162 ..Default::default()
163 },
164 _ => Edges::all(px(0.)),
165 },
166 }
167 }
168
169 fn normal(&self, cx: &App) -> TabStyle {
170 match self {
171 TabVariant::Tab => TabStyle {
172 fg: cx.theme().tab_foreground,
173 bg: cx.theme().transparent.into(),
174 borders: Edges {
175 left: px(1.),
176 right: px(1.),
177 ..Default::default()
178 },
179 border_color: cx.theme().transparent,
180 ..Default::default()
181 },
182 TabVariant::Outline => TabStyle {
183 fg: cx.theme().tab_foreground,
184 bg: cx.theme().transparent.into(),
185 borders: Edges::all(px(1.)),
186 border_color: cx.theme().border,
187 ..Default::default()
188 },
189 TabVariant::Pill => TabStyle {
190 fg: cx.theme().foreground,
191 bg: cx.theme().transparent.into(),
192 ..Default::default()
193 },
194 TabVariant::Segmented => TabStyle {
195 fg: cx.theme().tab_foreground,
196 bg: cx.theme().transparent.into(),
197 ..Default::default()
198 },
199 TabVariant::Underline => TabStyle {
200 fg: cx.theme().tab_foreground,
201 bg: cx.theme().transparent.into(),
202 inner_bg: cx.theme().transparent.into(),
203 borders: Edges {
204 bottom: px(2.),
205 ..Default::default()
206 },
207 border_color: cx.theme().transparent,
208 ..Default::default()
209 },
210 TabVariant::Folder => TabStyle {
211 fg: cx.theme().tab_foreground,
212 bg: cx.theme().transparent.into(),
213 ..Default::default()
214 },
215 }
216 }
217
218 fn hovered(&self, selected: bool, cx: &App) -> TabStyle {
219 match self {
220 TabVariant::Tab => TabStyle {
221 fg: cx.theme().tab_active_foreground,
222 bg: cx.theme().transparent.into(),
223 borders: Edges {
224 left: px(1.),
225 right: px(1.),
226 ..Default::default()
227 },
228 border_color: cx.theme().transparent,
229 ..Default::default()
230 },
231 TabVariant::Outline => TabStyle {
232 fg: cx.theme().secondary_foreground,
233 bg: cx.theme().tokens.secondary_hover.into(),
234 borders: Edges::all(px(1.)),
235 border_color: cx.theme().border,
236 ..Default::default()
237 },
238 TabVariant::Pill => TabStyle {
239 fg: cx.theme().secondary_foreground,
240 bg: cx.theme().tokens.secondary.into(),
241 ..Default::default()
242 },
243 TabVariant::Segmented => TabStyle {
244 fg: cx.theme().tab_active_foreground,
245 bg: cx.theme().transparent.into(),
246 inner_bg: if selected {
247 cx.theme().tokens.background.into()
248 } else {
249 cx.theme().transparent.into()
250 },
251 ..Default::default()
252 },
253 TabVariant::Underline => TabStyle {
254 fg: cx.theme().tab_active_foreground,
255 bg: cx.theme().transparent.into(),
256 inner_bg: cx.theme().transparent.into(),
257 borders: Edges {
258 bottom: px(2.),
259 ..Default::default()
260 },
261 border_color: cx.theme().transparent,
262 ..Default::default()
263 },
264 TabVariant::Folder => TabStyle {
266 fg: cx.theme().tab_active_foreground,
267 bg: cx.theme().transparent.into(),
268 ..Default::default()
269 },
270 }
271 }
272
273 fn selected(&self, cx: &App) -> TabStyle {
274 match self {
275 TabVariant::Tab => TabStyle {
276 fg: cx.theme().tab_active_foreground,
277 bg: cx.theme().tokens.tab_active.into(),
278 borders: Edges {
279 left: px(1.),
280 right: px(1.),
281 ..Default::default()
282 },
283 border_color: cx.theme().border,
284 ..Default::default()
285 },
286 TabVariant::Outline => TabStyle {
287 fg: cx.theme().primary,
288 bg: cx.theme().transparent.into(),
289 borders: Edges::all(px(1.)),
290 border_color: cx.theme().primary,
291 ..Default::default()
292 },
293 TabVariant::Pill => TabStyle {
294 fg: cx.theme().primary_foreground,
295 bg: cx.theme().tokens.primary.into(),
296 ..Default::default()
297 },
298 TabVariant::Segmented => TabStyle {
299 fg: cx.theme().tab_active_foreground,
300 bg: cx.theme().transparent.into(),
301 inner_bg: cx.theme().tokens.background.into(),
302 shadow: true,
303 ..Default::default()
304 },
305 TabVariant::Underline => TabStyle {
306 fg: cx.theme().tab_active_foreground,
307 bg: cx.theme().transparent.into(),
308 borders: Edges {
309 bottom: px(2.),
310 ..Default::default()
311 },
312 border_color: cx.theme().primary,
313 ..Default::default()
314 },
315 TabVariant::Folder => TabStyle {
317 fg: cx.theme().tab_active_foreground,
318 bg: cx.theme().transparent.into(),
319 ..Default::default()
320 },
321 }
322 }
323
324 fn disabled(&self, selected: bool, cx: &App) -> TabStyle {
325 match self {
326 TabVariant::Tab => TabStyle {
327 fg: cx.theme().muted_foreground,
328 bg: cx.theme().transparent.into(),
329 border_color: if selected {
330 cx.theme().border
331 } else {
332 cx.theme().transparent
333 },
334 borders: Edges {
335 left: px(1.),
336 right: px(1.),
337 ..Default::default()
338 },
339 ..Default::default()
340 },
341 TabVariant::Outline => TabStyle {
342 fg: cx.theme().muted_foreground,
343 bg: cx.theme().transparent.into(),
344 borders: Edges::all(px(1.)),
345 border_color: if selected {
346 cx.theme().primary
347 } else {
348 cx.theme().border
349 },
350 ..Default::default()
351 },
352 TabVariant::Pill => TabStyle {
353 fg: if selected {
354 cx.theme().primary_foreground.opacity(0.5)
355 } else {
356 cx.theme().muted_foreground
357 },
358 bg: if selected {
359 cx.theme().primary.opacity(0.5).into()
360 } else {
361 cx.theme().transparent.into()
362 },
363 ..Default::default()
364 },
365 TabVariant::Segmented => TabStyle {
366 fg: cx.theme().muted_foreground,
367 bg: cx.theme().tokens.tab_bar.into(),
368 inner_bg: if selected {
369 cx.theme().tokens.background.into()
370 } else {
371 cx.theme().transparent.into()
372 },
373 ..Default::default()
374 },
375 TabVariant::Underline => TabStyle {
376 fg: cx.theme().muted_foreground,
377 bg: cx.theme().transparent.into(),
378 border_color: if selected {
379 cx.theme().border
380 } else {
381 cx.theme().transparent
382 },
383 borders: Edges {
384 bottom: px(2.),
385 ..Default::default()
386 },
387 ..Default::default()
388 },
389 TabVariant::Folder => TabStyle {
390 fg: cx.theme().muted_foreground,
391 bg: cx.theme().transparent.into(),
392 ..Default::default()
393 },
394 }
395 }
396
397 pub(super) fn tab_bar_radius(&self, size: Size, cx: &App) -> Pixels {
398 if *self != TabVariant::Segmented {
399 return px(0.);
400 }
401
402 match size {
403 Size::XSmall | Size::Small => cx.theme().radius,
404 Size::Large => cx.theme().radius_lg,
405 _ => cx.theme().radius_lg,
406 }
407 }
408
409 fn radius(&self, size: Size, cx: &App) -> Pixels {
410 match self {
411 TabVariant::Outline | TabVariant::Pill => cx.theme().radius_full(),
412 TabVariant::Segmented => match size {
413 Size::XSmall | Size::Small => cx.theme().radius,
414 Size::Large => cx.theme().radius_lg,
415 _ => cx.theme().radius_lg,
416 },
417 _ => px(0.),
418 }
419 }
420
421 pub(super) fn inner_radius(&self, size: Size, cx: &App) -> Pixels {
422 match self {
423 TabVariant::Segmented => match size {
427 Size::Large => (self.tab_bar_radius(size, cx) - px(3.)).max(px(0.)),
428 _ => (self.tab_bar_radius(size, cx) - px(2.)).max(px(0.)),
429 },
430 _ => px(0.),
431 }
432 }
433
434 pub(super) fn overhang(&self, size: Size, cx: &App) -> Pixels {
436 match self {
437 TabVariant::Folder => FolderTabMetrics::new(size, cx).radius,
438 _ => px(0.),
439 }
440 }
441}
442
443fn folder_tab_shape(metrics: FolderTabMetrics, bg: Background) -> impl IntoElement {
446 let curve = metrics.radius;
447 canvas(
448 |_, _, _| {},
449 move |bounds, _, window, _| {
450 let (top, bottom) = (bounds.top(), bounds.bottom());
451 let (left, right) = (bounds.left(), bounds.right());
452 let (tab_left, tab_right) = (left + curve, right - curve);
453 let radius = ((bounds.size.width - curve * 2.) / 3.).clamp(px(0.), curve);
455
456 let mut path = PathBuilder::fill();
457 path.move_to(point(left, bottom));
458 quarter_circle_to(
459 &mut path,
460 point(left, bottom),
461 point(tab_left, bottom),
462 point(tab_left, bottom - curve),
463 );
464 path.line_to(point(tab_left, top + radius));
465 quarter_circle_to(
466 &mut path,
467 point(tab_left, top + radius),
468 point(tab_left, top),
469 point(tab_left + radius, top),
470 );
471 path.line_to(point(tab_right - radius, top));
472 quarter_circle_to(
473 &mut path,
474 point(tab_right - radius, top),
475 point(tab_right, top),
476 point(tab_right, top + radius),
477 );
478 path.line_to(point(tab_right, bottom - curve));
479 quarter_circle_to(
480 &mut path,
481 point(tab_right, bottom - curve),
482 point(tab_right, bottom),
483 point(right, bottom),
484 );
485 path.close();
486 if let Ok(path) = path.build() {
487 window.paint_path(path, bg);
488 }
489 },
490 )
491 .absolute()
492 .top_0()
493 .bottom_0()
494 .left(-curve)
495 .right(-curve)
496}
497
498fn quarter_circle_to(
500 path: &mut PathBuilder,
501 from: Point<Pixels>,
502 corner: Point<Pixels>,
503 to: Point<Pixels>,
504) {
505 const K: f32 = 0.552_284_8;
507 path.cubic_bezier_to(to, from + (corner - from) * K, to + (corner - to) * K);
508}
509
510#[allow(dead_code)]
511struct TabStyle {
512 borders: Edges<Pixels>,
513 border_color: Hsla,
514 bg: Background,
515 fg: Hsla,
516 shadow: bool,
517 inner_bg: Background,
518}
519
520impl Default for TabStyle {
521 fn default() -> Self {
522 TabStyle {
523 borders: Edges::all(px(0.)),
524 border_color: gpui::transparent_white(),
525 bg: gpui::transparent_white().into(),
526 fg: gpui::transparent_white(),
527 shadow: false,
528 inner_bg: gpui::transparent_white().into(),
529 }
530 }
531}
532
533#[derive(IntoElement)]
535pub struct Tab {
536 ix: usize,
537 base: gpui_base::Tab,
538 pub(super) label: Option<SharedString>,
539 aria_label: Option<SharedString>,
540 pub(super) icon: Option<Icon>,
541 prefix: Option<AnyElement>,
542 pub(super) tab_bar_prefix: Option<bool>,
543 suffix: Option<AnyElement>,
544 children: Vec<AnyElement>,
545 variant: TabVariant,
546 size: Size,
547 pub(super) disabled: bool,
548 pub(super) selected: bool,
549 pub(super) indicator_active: bool,
550 pub(super) indicator_ready: bool,
551 pub(super) indicator_epoch: u64,
555 pub(super) separator: bool,
557 pub(super) hovered: bool,
560 pub(super) max_width: Option<Pixels>,
561 pub(super) on_click: Option<Rc<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>>,
562}
563
564impl From<&'static str> for Tab {
565 fn from(label: &'static str) -> Self {
566 Self::new().label(label)
567 }
568}
569
570impl From<String> for Tab {
571 fn from(label: String) -> Self {
572 Self::new().label(label)
573 }
574}
575
576impl From<SharedString> for Tab {
577 fn from(label: SharedString) -> Self {
578 Self::new().label(label)
579 }
580}
581
582impl From<Icon> for Tab {
583 fn from(icon: Icon) -> Self {
584 Self::default().icon(icon)
585 }
586}
587
588impl From<IconName> for Tab {
589 fn from(icon_name: IconName) -> Self {
590 Self::default().icon(Icon::new(icon_name))
591 }
592}
593
594impl Default for Tab {
595 fn default() -> Self {
596 Self {
597 ix: 0,
598 base: gpui_base::Tab::new(0usize),
599 label: None,
600 aria_label: None,
601 icon: None,
602 tab_bar_prefix: None,
603 children: Vec::new(),
604 disabled: false,
605 selected: false,
606 indicator_active: false,
607 indicator_ready: true,
608 indicator_epoch: 0,
609 separator: false,
610 hovered: false,
611 prefix: None,
612 suffix: None,
613 variant: TabVariant::default(),
614 size: Size::default(),
615 max_width: None,
616 on_click: None,
617 }
618 }
619}
620
621impl Tab {
622 pub fn new() -> Self {
624 Self::default()
625 }
626
627 pub fn label(mut self, label: impl Into<SharedString>) -> Self {
629 self.label = Some(label.into());
630 self
631 }
632
633 pub fn aria_label(mut self, label: impl Into<SharedString>) -> Self {
635 self.aria_label = Some(label.into());
636 self
637 }
638
639 fn a11y_label(&self) -> Option<SharedString> {
640 self.aria_label.clone().or_else(|| self.label.clone())
641 }
642
643 pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
645 self.icon = Some(icon.into());
646 self
647 }
648
649 pub fn with_variant(mut self, variant: TabVariant) -> Self {
651 self.variant = variant;
652 self
653 }
654
655 pub fn pill(mut self) -> Self {
657 self.variant = TabVariant::Pill;
658 self
659 }
660
661 pub fn outline(mut self) -> Self {
663 self.variant = TabVariant::Outline;
664 self
665 }
666
667 pub fn segmented(mut self) -> Self {
669 self.variant = TabVariant::Segmented;
670 self
671 }
672
673 pub fn underline(mut self) -> Self {
675 self.variant = TabVariant::Underline;
676 self
677 }
678
679 pub fn folder(mut self) -> Self {
681 self.variant = TabVariant::Folder;
682 self
683 }
684
685 pub fn prefix(mut self, prefix: impl IntoElement) -> Self {
687 self.prefix = Some(prefix.into_any_element());
688 self
689 }
690
691 pub fn suffix(mut self, suffix: impl IntoElement) -> Self {
693 self.suffix = Some(suffix.into_any_element());
694 self
695 }
696
697 pub fn disabled(mut self, disabled: bool) -> Self {
699 self.disabled = disabled;
700 self
701 }
702
703 pub fn on_click(
705 mut self,
706 on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
707 ) -> Self {
708 self.on_click = Some(Rc::new(on_click));
709 self
710 }
711
712 pub(crate) fn ix(mut self, ix: usize) -> Self {
714 self.ix = ix;
715 self.base = self.base.id(ix);
716 self
717 }
718
719 pub(crate) fn tab_bar_prefix(mut self, tab_bar_prefix: bool) -> Self {
721 self.tab_bar_prefix = Some(tab_bar_prefix);
722 self
723 }
724
725 pub(super) fn max_width(mut self, max_width: Option<Pixels>) -> Self {
727 self.max_width = max_width;
728 self
729 }
730}
731
732impl ParentElement for Tab {
733 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
734 self.children.extend(elements);
735 }
736}
737
738impl Selectable for Tab {
739 fn selected(mut self, selected: bool) -> Self {
740 self.selected = selected;
741 self
742 }
743
744 fn is_selected(&self) -> bool {
745 self.selected
746 }
747}
748
749impl InteractiveElement for Tab {
750 fn interactivity(&mut self) -> &mut gpui::Interactivity {
751 self.base.interactivity()
752 }
753}
754
755impl StatefulInteractiveElement for Tab {}
756
757impl Styled for Tab {
758 fn style(&mut self) -> &mut gpui::StyleRefinement {
759 self.base.style()
760 }
761}
762
763impl Sizable for Tab {
764 fn with_size(mut self, size: impl Into<Size>) -> Self {
765 self.size = size.into();
766 self
767 }
768}
769
770impl RenderOnce for Tab {
771 fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
772 let mut normal_style = self.variant.normal(cx);
773 let mut selected_style = self.variant.selected(cx);
774 let mut disabled_style = self.variant.disabled(self.selected, cx);
775 let mut hover_style = self.variant.hovered(self.selected, cx);
776 if self.disabled {
777 hover_style = self.variant.disabled(self.selected, cx);
778 }
779 let tab_bar_prefix = self.tab_bar_prefix.unwrap_or_default();
780 if !tab_bar_prefix {
781 if self.ix == 0 && self.variant == TabVariant::Tab {
783 let rtl = gpui_base::LayoutDirection::global(cx).is_rtl();
784 for style in [
785 &mut normal_style,
786 &mut selected_style,
787 &mut disabled_style,
788 &mut hover_style,
789 ] {
790 if rtl {
791 style.borders.right = px(0.);
792 } else {
793 style.borders.left = px(0.);
794 }
795 }
796 }
797 }
798 let tab_style = if self.disabled {
799 &disabled_style
800 } else if self.selected {
801 &selected_style
802 } else {
803 &normal_style
804 };
805 let radius = self.variant.radius(self.size, cx);
806 let inner_radius = self.variant.inner_radius(self.size, cx);
807 let inner_paddings = self.variant.inner_paddings(self.size);
808 let inner_margins = self.variant.inner_margins(self.size);
809 let inner_height = self.variant.inner_height(self.size);
810 let height = self.variant.height(self.size);
811 let aria_label = self.a11y_label();
812 let folder = self.variant == TabVariant::Folder;
813 let metrics = FolderTabMetrics::new(self.size, cx);
814 let has_prefix = self.prefix.is_some();
815
816 let segmented_indicator_active =
817 self.variant == TabVariant::Segmented && self.indicator_active;
818 let has_inline_inner_bg =
819 self.selected && segmented_indicator_active && !self.indicator_ready;
820 let inline_inner_bg = tab_style.inner_bg;
821 let (inner_bg, hover_inner_bg) = if segmented_indicator_active && self.indicator_ready {
822 (cx.theme().transparent.into(), cx.theme().transparent.into())
823 } else if has_inline_inner_bg {
824 (inline_inner_bg, inline_inner_bg)
825 } else {
826 (tab_style.inner_bg, hover_style.inner_bg)
827 };
828 let inner_shadow = tab_style.shadow && !segmented_indicator_active;
829
830 let suppress_active_visual =
837 self.selected && !self.disabled && self.indicator_active && self.indicator_ready;
838 let selected_outer_bg = if suppress_active_visual && self.variant == TabVariant::Pill {
840 cx.theme().transparent.into()
841 } else {
842 selected_style.bg
843 };
844 let selected_outer_border_color =
846 if suppress_active_visual && self.variant == TabVariant::Underline {
847 cx.theme().transparent
848 } else {
849 selected_style.border_color
850 };
851
852 let animate_fg = self.selected
858 && !self.disabled
859 && self.variant == TabVariant::Pill
860 && self.indicator_active
861 && self.indicator_ready
862 && self.indicator_epoch > 0;
863 let fg_from = self.variant.normal(cx).fg;
864 let fg_to = tab_style.fg;
865 let max_width = self.max_width.filter(|_| self.icon.is_none());
867
868 let inner_content = h_flex()
869 .flex_1()
870 .h(inner_height)
871 .line_height(relative(1.25))
872 .whitespace_nowrap()
873 .items_center()
874 .justify_center()
875 .overflow_hidden()
876 .margins(inner_margins)
877 .map(|this| match max_width {
880 Some(_) => this.flex_auto(),
881 None => this.flex_shrink_0(),
882 })
883 .map(|this| match self.icon {
884 Some(icon) => this
887 .w(inner_height * 1.25)
888 .min_w(inner_height * 1.25)
889 .child(icon.map(|this| match self.size {
890 Size::XSmall => this.size_2p5(),
891 Size::Small => this.size_3p5(),
892 Size::Large => this.size_4(),
893 _ => this.size_4(),
894 })),
895 None => this
896 .paddings(inner_paddings)
897 .map(|this| match (self.label, max_width) {
898 (Some(label), Some(_)) => this.child(
901 div()
902 .min_w_0()
903 .whitespace_nowrap()
904 .text_ellipsis()
905 .child(label),
906 ),
907 (Some(label), None) => this.child(label),
908 (None, _) => this,
909 })
910 .children(self.children)
911 .when(folder && has_prefix, |this| this.ps(px(4.), cx)),
913 })
914 .bg(inner_bg)
915 .rounded(inner_radius)
916 .when(inner_shadow, |this| this.shadow_xs())
917 .hover(|this| this.bg(hover_inner_bg).rounded(inner_radius));
918
919 let inner_element = if animate_fg {
920 inner_content
921 .with_animation(
922 ElementId::NamedInteger("tab-fg".into(), self.indicator_epoch),
923 Animation::new(Duration::from_millis(200)).with_easing(ease_in_out_cubic),
924 move |this, delta| this.text_color(Lerp::lerp(&fg_from, &fg_to, delta)),
925 )
926 .into_any_element()
927 } else {
928 inner_content.into_any_element()
929 };
930
931 let ring = cx.theme().ring;
932 self.base
933 .id(self.ix)
934 .selected(self.selected)
935 .disabled(self.disabled)
936 .when_some(aria_label, |this, label| this.accessibility_label(label))
937 .styles(|styles| {
938 styles
939 .selected(|style| {
940 style
941 .text_color(selected_style.fg)
942 .bg(selected_outer_bg)
943 .border_l(selected_style.borders.left)
944 .border_r(selected_style.borders.right)
945 .border_t(selected_style.borders.top)
946 .border_b(selected_style.borders.bottom)
947 .border_color(selected_outer_border_color)
948 })
949 .disabled(|style| {
950 style
951 .text_color(disabled_style.fg)
952 .bg(disabled_style.bg)
953 .border_l(disabled_style.borders.left)
954 .border_r(disabled_style.borders.right)
955 .border_t(disabled_style.borders.top)
956 .border_b(disabled_style.borders.bottom)
957 .border_color(disabled_style.border_color)
958 })
959 })
960 .focus_visible(|style| {
964 style.shadow(vec![gpui::BoxShadow {
965 color: ring,
966 offset: point(px(0.), px(0.)),
967 blur_radius: px(0.),
968 spread_radius: px(2.),
969 inset: true,
970 }])
971 })
972 .relative()
973 .flex_row_dir(cx)
975 .map(|this| match max_width {
978 Some(max_width) => this.flex_nowrap().max_w(max_width),
979 None => this.flex_wrap(),
980 })
981 .gap_1()
982 .items_center()
983 .flex_shrink_0()
984 .h(height)
985 .when(!folder, |this| this.overflow_hidden())
987 .map(|this| match self.size {
988 Size::XSmall => this.text_xs(),
989 Size::Large => this.text_base(),
990 _ => this.text_sm(),
991 })
992 .rounded(radius)
993 .when(folder, |this| {
994 this.px(metrics.padding_x).pb(metrics.top_padding)
995 })
996 .when(!self.selected && !self.disabled, |this| {
997 this.text_color(normal_style.fg)
998 .bg(normal_style.bg)
999 .border_l(normal_style.borders.left)
1000 .border_r(normal_style.borders.right)
1001 .border_t(normal_style.borders.top)
1002 .border_b(normal_style.borders.bottom)
1003 .border_color(normal_style.border_color)
1004 })
1005 .hover(|this| {
1006 if self.selected || self.disabled {
1010 return this;
1011 }
1012 this.text_color(hover_style.fg)
1013 .bg(hover_style.bg)
1014 .border_l(hover_style.borders.left)
1015 .border_r(hover_style.borders.right)
1016 .border_t(hover_style.borders.top)
1017 .border_b(hover_style.borders.bottom)
1018 .border_color(hover_style.border_color)
1019 .rounded(radius)
1020 })
1021 .when(folder && self.selected, |this| {
1022 this.child(folder_tab_shape(
1023 metrics,
1024 cx.theme().tokens.tab_active.into(),
1025 ))
1026 })
1027 .when(
1028 folder && self.hovered && !self.selected && !self.disabled,
1029 |this| {
1030 this.child(
1031 div()
1032 .absolute()
1033 .top_0()
1034 .left_0()
1035 .right_0()
1036 .bottom(metrics.top_padding)
1037 .rounded(metrics.radius)
1038 .bg(cx.theme().tokens.secondary),
1039 )
1040 },
1041 )
1042 .when(folder && self.separator, |this| {
1045 this.child(
1046 div()
1047 .absolute()
1048 .inset_e(-(FOLDER_TAB_GAP / 2.), cx)
1049 .top((height - metrics.top_padding - metrics.separator_height) / 2.)
1050 .w(px(1.))
1051 .h(metrics.separator_height)
1052 .bg(cx.theme().border),
1053 )
1054 })
1055 .when(has_inline_inner_bg, |this| {
1056 this.child(
1057 div()
1058 .absolute()
1059 .left_0()
1060 .right_0()
1061 .top_0()
1062 .bottom_0()
1063 .flex()
1064 .items_center()
1065 .child(
1066 div()
1067 .w_full()
1068 .h(inner_height)
1069 .bg(inline_inner_bg)
1070 .rounded(inner_radius)
1071 .when(tab_style.shadow, |this| this.shadow_sm()),
1072 ),
1073 )
1074 })
1075 .when_some(self.prefix, |this, prefix| {
1078 this.child(
1079 div()
1080 .when_some(max_width, |this, _| this.flex_shrink_0())
1081 .child(prefix),
1082 )
1083 })
1084 .child(inner_element)
1085 .when_some(self.suffix, |this, suffix| {
1086 this.child(
1087 div()
1088 .when_some(max_width, |this, _| this.flex_shrink_0())
1089 .child(suffix),
1090 )
1091 })
1092 .when_some(self.on_click.clone(), |this, on_click| {
1093 this.on_click(move |event, window, cx| on_click(event, window, cx))
1094 })
1095 }
1096}
1097
1098#[cfg(test)]
1099mod tests {
1100 use super::*;
1101 use crate::tab::TabBar;
1102 use gpui::{Context, Render, TestAppContext, VisualTestContext};
1103
1104 const VARIANTS: [TabVariant; 6] = [
1105 TabVariant::Tab,
1106 TabVariant::Outline,
1107 TabVariant::Pill,
1108 TabVariant::Segmented,
1109 TabVariant::Underline,
1110 TabVariant::Folder,
1111 ];
1112
1113 const LONG_LABEL: &str = "Account Settings & Preferences";
1114
1115 struct TabBarTest {
1117 variant: TabVariant,
1118 max_width: Option<Pixels>,
1119 build: fn(Tab) -> Tab,
1120 }
1121
1122 impl Render for TabBarTest {
1123 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
1124 TabBar::new("tabs")
1125 .with_variant(self.variant)
1126 .selected_index(0)
1127 .when_some(self.max_width, |this, width| this.max_width(width))
1128 .child((self.build)(
1129 Tab::new().debug_selector(|| "tab".to_string()),
1130 ))
1131 }
1132 }
1133
1134 fn show(
1135 cx: &mut TestAppContext,
1136 variant: TabVariant,
1137 max_width: Option<Pixels>,
1138 build: fn(Tab) -> Tab,
1139 ) -> &mut VisualTestContext {
1140 cx.update(crate::init);
1141 let (_, cx) = cx.add_window_view(|_, _| TabBarTest {
1142 variant,
1143 max_width,
1144 build,
1145 });
1146 cx.run_until_parked();
1147 cx
1148 }
1149
1150 fn variant_widths(
1152 cx: &mut TestAppContext,
1153 build: fn(Tab) -> Tab,
1154 max_width: Option<Pixels>,
1155 ) -> Vec<Pixels> {
1156 VARIANTS
1157 .into_iter()
1158 .map(|variant| {
1159 show(cx, variant, max_width, build)
1160 .debug_bounds("tab")
1161 .expect("tab not rendered")
1162 .size
1163 .width
1164 })
1165 .collect()
1166 }
1167
1168 #[gpui::test]
1169 fn a11y_label_defaults_to_visible_label(_cx: &mut TestAppContext) {
1170 let tab = Tab::new().label("Account");
1171
1172 assert_eq!(tab.a11y_label(), Some("Account".into()));
1173 }
1174
1175 #[gpui::test]
1176 fn explicit_a11y_label_overrides_visible_label(_cx: &mut TestAppContext) {
1177 let tab = Tab::new().label("Acct").aria_label("Account settings");
1178
1179 assert_eq!(tab.a11y_label(), Some("Account settings".into()));
1180 }
1181
1182 #[gpui::test]
1183 fn max_width_leaves_short_tabs_untouched(cx: &mut TestAppContext) {
1184 let build: fn(Tab) -> Tab = |tab| tab.label("Go");
1187
1188 let uncapped = variant_widths(cx, build, None);
1189 let capped = variant_widths(cx, build, Some(px(200.)));
1190
1191 assert_eq!(uncapped, capped, "a tab under the cap must not be resized");
1192 }
1193
1194 #[gpui::test]
1195 fn max_width_caps_long_tabs(cx: &mut TestAppContext) {
1196 let build: fn(Tab) -> Tab = |tab| tab.label(LONG_LABEL);
1197
1198 let uncapped = variant_widths(cx, build, None);
1199 let capped = variant_widths(cx, build, Some(px(120.)));
1200
1201 for (variant, (uncapped, capped)) in
1202 VARIANTS.into_iter().zip(uncapped.into_iter().zip(capped))
1203 {
1204 assert!(
1205 uncapped > px(120.),
1206 "{variant:?} is not long enough to exercise the cap ({uncapped:?})"
1207 );
1208 assert!(
1209 capped <= px(120.),
1210 "{variant:?} width {capped:?} exceeds max_width"
1211 );
1212 }
1213 }
1214
1215 #[gpui::test]
1216 fn max_width_keeps_prefix_and_suffix_intact(cx: &mut TestAppContext) {
1217 let cx = show(cx, TabVariant::Segmented, Some(px(140.)), |tab| {
1218 tab.prefix(Icon::new(IconName::BookOpen))
1219 .label(LONG_LABEL)
1220 .suffix(div().size(px(16.)).debug_selector(|| "suffix".to_string()))
1221 });
1222
1223 let tab = cx.debug_bounds("tab").expect("tab not rendered");
1224 let suffix = cx.debug_bounds("suffix").expect("suffix not rendered");
1225
1226 assert!(tab.size.width <= px(140.));
1227 assert_eq!(
1228 suffix.size.width,
1229 px(16.),
1230 "the label should absorb the truncation, not the suffix"
1231 );
1232 assert!(
1233 suffix.right() <= tab.right(),
1234 "suffix must stay within the tab"
1235 );
1236 assert!(
1240 suffix.left() > tab.center().x,
1241 "suffix must follow the label, not wrap below it"
1242 );
1243 }
1244
1245 #[gpui::test]
1246 fn a_right_to_left_tab_puts_its_prefix_on_the_right(cx: &mut TestAppContext) {
1247 cx.update(|cx| {
1248 crate::init(cx);
1249 gpui_base::LayoutDirection::Rtl.set_global(cx);
1250 });
1251 let cx = show(cx, TabVariant::Tab, None, |tab| {
1252 tab.prefix(div().size(px(16.)).debug_selector(|| "prefix".to_string()))
1253 .label("Label")
1254 .suffix(div().size(px(16.)).debug_selector(|| "suffix".to_string()))
1255 });
1256
1257 let prefix = cx.debug_bounds("prefix").expect("prefix not rendered");
1258 let suffix = cx.debug_bounds("suffix").expect("suffix not rendered");
1259 assert!(
1260 suffix.right() < prefix.left(),
1261 "the suffix ends the row on the left: {suffix:?} {prefix:?}"
1262 );
1263 }
1264
1265 #[gpui::test]
1268 fn max_width_exempts_icon_only_tabs(cx: &mut TestAppContext) {
1269 let build: fn(Tab) -> Tab = |tab| tab.icon(Icon::new(IconName::BookOpen));
1270 let width = |cx: &mut TestAppContext, max_width| {
1271 show(cx, TabVariant::Tab, max_width, build)
1272 .debug_bounds("tab")
1273 .expect("tab not rendered")
1274 .size
1275 .width
1276 };
1277
1278 let uncapped = width(cx, None);
1279 assert!(
1280 uncapped > px(16.),
1281 "the cap has to be narrower than the icon tab to be meaningful"
1282 );
1283 assert_eq!(
1284 width(cx, Some(px(16.))),
1285 uncapped,
1286 "an icon-only tab must ignore max_width"
1287 );
1288 }
1289
1290 #[gpui::test]
1293 fn icon_only_tabs_keep_their_width(cx: &mut TestAppContext) {
1294 let build: fn(Tab) -> Tab = |tab| tab.icon(Icon::new(IconName::BookOpen));
1295 for variant in VARIANTS {
1296 let width = show(cx, variant, None, build)
1297 .debug_bounds("tab")
1298 .expect("tab not rendered")
1299 .size
1300 .width;
1301 let content = variant.inner_height(Size::default()) * 1.25;
1302 assert!(
1303 width >= content,
1304 "{variant:?}: icon-only tab is {width:?}, narrower than its {content:?} content"
1305 );
1306 }
1307 }
1308}