tabs: collapse same-kind tabs into group chips

This commit is contained in:
2026-11-15 14:50:48 +07:00
parent f6ba9c0d47
commit daca373f77
5 changed files with 668 additions and 90 deletions
+96
View File
@@ -0,0 +1,96 @@
#![cfg(feature = "tabs")]
//! Grouped tabs: same-kind tabs collapse into one chip that opens a popover
//! listing the members; picking one selects it by its original index.
use std::cell::RefCell;
use std::rc::Rc;
use gpui::{
Context, IntoElement, Modifiers, ParentElement as _, Render, Styled as _, TestAppContext,
VisualTestContext, Window, div, px, size,
};
use gpui_native_data::tabs::{Tab, Tabs, TabsVariant};
use gpui_native_theme::Theme;
struct StripHarness {
changed: Rc<RefCell<Vec<usize>>>,
}
impl Render for StripHarness {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
let changed = self.changed.clone();
div().size_full().child(
Tabs::new("strip")
.variant(TabsVariant::Pill)
.tabs(|_, _| {
vec![
Tab::new("a", "Alpha").kind("Kind"),
Tab::new("b", "Beta").kind("Kind"),
Tab::new("c", "Gamma"),
Tab::new("d", "Delta").kind("Lone"),
]
})
.on_change(move |index, _, _| changed.borrow_mut().push(index)),
)
}
}
fn strip_window(cx: &mut TestAppContext, changed: Rc<RefCell<Vec<usize>>>) -> VisualTestContext {
cx.update(Theme::init);
let window = cx.open_window(size(px(800.), px(600.)), |_, _| StripHarness { changed });
let cx = VisualTestContext::from_window(window.into(), cx);
cx.run_until_parked();
cx
}
#[gpui::test]
fn same_kind_tabs_collapse_into_one_group(cx: &mut TestAppContext) {
let mut cx = strip_window(cx, Rc::new(RefCell::new(Vec::new())));
assert!(
cx.debug_bounds("strip-group-Kind").is_some(),
"two same-kind tabs draw one group chip"
);
assert!(
cx.debug_bounds("strip-tab-Alpha").is_none(),
"grouped members do not draw inline"
);
assert!(cx.debug_bounds("strip-tab-Beta").is_none());
// A tab outside any two-member kind group draws itself, kinded or not.
assert!(cx.debug_bounds("strip-tab-Gamma").is_some());
assert!(cx.debug_bounds("strip-tab-Delta").is_some());
assert!(
cx.debug_bounds("strip-group-Lone").is_none(),
"a lone kinded tab is not grouped"
);
}
#[gpui::test]
fn picking_a_group_member_selects_it(cx: &mut TestAppContext) {
let changed = Rc::new(RefCell::new(Vec::new()));
let mut cx = strip_window(cx, changed.clone());
let chip = cx
.debug_bounds("strip-group-Kind")
.expect("group chip drawn");
cx.simulate_click(chip.center(), Modifiers::none());
cx.run_until_parked();
let beta = cx
.debug_bounds("strip-tab-Beta")
.expect("the popover lists the members");
cx.simulate_click(beta.center(), Modifiers::none());
cx.run_until_parked();
assert_eq!(
changed.borrow().as_slice(),
&[1],
"picking a member reports its original index"
);
assert!(
cx.debug_bounds("strip-tab-Beta").is_none(),
"a selection dismisses the popover"
);
}