chore: audit components

This commit is contained in:
2026-11-10 15:06:16 +07:00
parent af2177c6c8
commit 03369f501e
92 changed files with 2744 additions and 1947 deletions
+139 -137
View File
@@ -31,6 +31,7 @@ pub enum TabsVariant {
}
/// One tab of a [`Tabs`] strip.
#[must_use]
pub struct Tab {
/// The stable identity for element state; must survive reordering.
key: ElementId,
@@ -102,6 +103,7 @@ impl FluentBuilder for Tab {}
/// A tab strip.
#[derive(IntoElement)]
#[must_use]
pub struct Tabs {
id: ElementId,
selected: usize,
@@ -129,7 +131,7 @@ impl Tabs {
}
}
/// Sets the current selection.
/// Sets the current selection, as an index into the `tabs` vec — not a tab key.
pub fn selected(mut self, selected: usize) -> Self {
self.selected = selected;
self
@@ -141,7 +143,7 @@ impl Tabs {
self
}
/// Reports the next selected tab, from clicks and keys alike.
/// Reports the next selected tab, as an index into the `tabs` vec — not a tab key; indices shift on reorder.
pub fn on_change<F>(mut self, handler: F) -> Self
where
F: Fn(usize, &mut Window, &mut App) + 'static,
@@ -150,7 +152,7 @@ impl Tabs {
self
}
/// Reports a closed tab by index.
/// Reports a closed tab, as an index into the `tabs` vec — not a tab key; indices shift on reorder.
pub fn on_close<F>(mut self, handler: F) -> Self
where
F: Fn(usize, &mut Window, &mut App) + 'static,
@@ -159,7 +161,7 @@ impl Tabs {
self
}
/// Sets the tabs, built fresh each render.
/// Sets the tabs; the closure runs during render and must not mutate entities or notify.
pub fn tabs<F>(mut self, tabs: F) -> Self
where
F: Fn(&mut Window, &mut App) -> Vec<Tab> + 'static,
@@ -204,10 +206,9 @@ impl RenderOnce for Tabs {
.into_iter()
.enumerate()
.map(|(index, tab)| {
render_tab(
tab.render_tab(
&self.id,
cx,
tab,
index,
self.selected,
self.variant,
@@ -257,145 +258,146 @@ impl RenderOnce for Tabs {
}
}
#[allow(clippy::too_many_arguments)]
fn render_tab(
tabs_id: &ElementId,
cx: &App,
tab: Tab,
index: usize,
selected_index: usize,
variant: TabsVariant,
on_change: &Option<ChangeHandler>,
on_close: &Option<CloseHandler>,
focus: &gpui::FocusHandle,
) -> AnyElement {
let mut tab = tab;
let selected = index == selected_index;
impl Tab {
#[allow(clippy::too_many_arguments)]
fn render_tab(
mut self,
tabs_id: &ElementId,
cx: &App,
index: usize,
selected_index: usize,
variant: TabsVariant,
on_change: &Option<ChangeHandler>,
on_close: &Option<CloseHandler>,
focus: &gpui::FocusHandle,
) -> AnyElement {
let selected = index == selected_index;
let theme = cx.theme();
let metrics = theme.sizes.resolve(Size::default());
let small = theme.sizes.small;
let theme = cx.theme();
let metrics = theme.sizes.resolve(Size::default());
let small = theme.sizes.small;
let label_color = match variant {
TabsVariant::Underline => {
if tab.disabled {
theme.colors.muted_foreground
} else if selected {
theme.ramps.primary.base
} else {
theme.colors.foreground
let label_color = match variant {
TabsVariant::Underline => {
if self.disabled {
theme.colors.muted_foreground
} else if selected {
theme.ramps.primary.base
} else {
theme.colors.foreground
}
}
}
TabsVariant::Pill => {
if tab.disabled {
theme.colors.muted_foreground
} else {
theme.colors.foreground
TabsVariant::Pill => {
if self.disabled {
theme.colors.muted_foreground
} else {
theme.colors.foreground
}
}
}
};
};
let strip_tab = div()
.id((tab.key.clone(), "tab"))
.debug_selector({
let tabs_id = tabs_id.clone();
let label = tab.label.clone();
move || format!("{tabs_id}-tab-{}", label)
})
.flex()
.items_center()
.gap_1p5()
.role(Role::Tab)
.aria_selected(selected)
.text_color(label_color)
.when(!tab.disabled && !selected, |strip_tab| {
strip_tab.hover(|strip_tab| strip_tab.bg(theme.colors.muted))
})
.map(|strip_tab| match variant {
TabsVariant::Underline => strip_tab
.px_2()
.py_1()
.rounded_tl(theme.radius.sm)
.rounded_tr(theme.radius.sm)
.border_b_2()
.border_color(gpui::transparent_black())
.when(selected, |strip_tab| {
strip_tab.border_color(theme.ramps.primary.base)
}),
TabsVariant::Pill => strip_tab
.h(small.control_height + metrics.plate_inset * 2.0)
.px(metrics.dense_padding_x)
.pr(metrics.plate_inset)
.rounded(theme.radius.control)
.when(selected, |strip_tab| {
strip_tab
.bg(theme.colors.muted)
.hover(|strip_tab| strip_tab.bg(theme.colors.muted_hover))
}),
})
.when_some(tab.lead.take(), |strip_tab, lead| strip_tab.child(lead))
.child(tab.label.clone())
.when(variant == TabsVariant::Pill, |strip_tab| {
strip_tab.child(div().flex_none().w(metrics.padding_x * 1.5))
})
.when(tab.busy && !tab.disabled, |strip_tab| {
strip_tab.child(
Spinner::new((tab.key.clone(), "tab-busy"))
.small()
.accessibility_label("Loading"),
)
})
.when(
tab.closeable && on_close.is_some() && !tab.disabled,
|strip_tab| strip_tab.child(render_tab_close(tabs_id, &tab, index, on_close)),
)
.when(!tab.disabled, |strip_tab| {
strip_tab.cursor_pointer().on_click({
let on_change = on_change.clone();
let focus = focus.clone();
let close = (self.closeable && on_close.is_some() && !self.disabled)
.then(|| self.render_tab_close(tabs_id, index, on_close));
let strip_tab = div()
.id((self.key.clone(), "tab"))
.debug_selector({
let tabs_id = tabs_id.clone();
let label = self.label.clone();
move || format!("{tabs_id}-tab-{}", label)
})
.flex()
.items_center()
.gap_1p5()
.role(Role::Tab)
.aria_selected(selected)
.text_color(label_color)
.when(!self.disabled && !selected, |strip_tab| {
strip_tab.hover(|strip_tab| strip_tab.bg(theme.colors.muted))
})
.map(|strip_tab| match variant {
TabsVariant::Underline => strip_tab
.px_2()
.py_1()
.rounded_tl(theme.radius.sm)
.rounded_tr(theme.radius.sm)
.border_b_2()
.border_color(gpui::transparent_black())
.when(selected, |strip_tab| {
strip_tab.border_color(theme.ramps.primary.base)
}),
TabsVariant::Pill => strip_tab
.h(small.control_height + metrics.plate_inset * 2.0)
.px(metrics.dense_padding_x)
.pr(metrics.plate_inset)
.rounded(theme.radius.control)
.when(selected, |strip_tab| {
strip_tab
.bg(theme.colors.muted)
.hover(|strip_tab| strip_tab.bg(theme.colors.muted_hover))
}),
})
.when_some(self.lead.take(), |strip_tab, lead| strip_tab.child(lead))
.child(self.label.clone())
.when(variant == TabsVariant::Pill, |strip_tab| {
strip_tab.child(div().flex_none().w(metrics.padding_x * 1.5))
})
.when(self.busy && !self.disabled, |strip_tab| {
strip_tab.child(
Spinner::new((self.key.clone(), "tab-busy"))
.small()
.accessibility_label("Loading"),
)
})
.when_some(close, |strip_tab, close| strip_tab.child(close))
.when(!self.disabled, |strip_tab| {
strip_tab.cursor_pointer().on_click({
let on_change = on_change.clone();
let focus = focus.clone();
move |_, window, cx| {
window.focus(&focus, cx);
if index != selected_index
&& let Some(on_change) = &on_change
{
on_change(index, window, cx);
}
}
})
});
match self.wrap {
Some(wrap) => wrap(strip_tab),
None => strip_tab.into_any_element(),
}
}
fn render_tab_close(
&self,
tabs_id: &ElementId,
index: usize,
on_close: &Option<CloseHandler>,
) -> AnyElement {
Button::new((self.key.clone(), "tab-close"))
.icon(IconRole::Close)
.variant(ButtonVariant::Ghost)
.small()
.tab_stop(false)
.debug_selector({
let tabs_id = tabs_id.clone();
let label = self.label.clone();
move || format!("{tabs_id}-close-{}", label)
})
.tooltip(Tooltip::text("Close"))
.accessibility_label(SharedString::from(format!("Close {}", self.label)))
.on_click({
let on_close = on_close.clone();
move |_, window, cx| {
window.focus(&focus, cx);
if index != selected_index
&& let Some(on_change) = &on_change
{
on_change(index, window, cx);
cx.stop_propagation();
if let Some(on_close) = &on_close {
on_close(index, window, cx);
}
}
})
});
match tab.wrap {
Some(wrap) => wrap(strip_tab),
None => strip_tab.into_any_element(),
.into_any_element()
}
}
fn render_tab_close(
tabs_id: &ElementId,
tab: &Tab,
index: usize,
on_close: &Option<CloseHandler>,
) -> AnyElement {
Button::new((tab.key.clone(), "tab-close"))
.icon(IconRole::Close)
.variant(ButtonVariant::Ghost)
.small()
.tab_stop(false)
.debug_selector({
let tabs_id = tabs_id.clone();
let label = tab.label.clone();
move || format!("{tabs_id}-close-{}", label)
})
.tooltip(Tooltip::text("Close"))
.accessibility_label(SharedString::from(format!("Close {}", tab.label)))
.on_click({
let on_close = on_close.clone();
move |_, window, cx| {
cx.stop_propagation();
if let Some(on_close) = &on_close {
on_close(index, window, cx);
}
}
})
.into_any_element()
}