use super::{button::Button, Response, Ui};
use crate::{context::Paint, AccessRole, CornerRadius, Id, Rect, Vec2};
use std::hash::Hash;
impl Ui<'_> {
pub fn tab_bar<T: PartialEq + Hash, L: Into<String>>(
&mut self,
selected: &mut T,
tabs: impl IntoIterator<Item = (T, L)>,
) -> Vec<Response> {
let tabs: Vec<(T, String)> = tabs
.into_iter()
.map(|(value, label)| (value, label.into()))
.collect();
let style = self.style().clone();
let tab_style = super::theme::ButtonStyle {
surface: super::theme::ControlStyle {
idle: super::theme::SurfaceStyle {
foreground: Some(style.muted_text),
..Default::default()
},
hover: super::theme::SurfaceStyle {
foreground: Some(style.text_color),
..Default::default()
},
selected: super::theme::SurfaceStyle {
foreground: Some(style.text_color),
..super::theme::SurfaceStyle::fill(crate::Color::TRANSPARENT)
},
..Default::default()
},
..Default::default()
};
let height = 36.0_f32.max(style.control_height);
let weights = style.typography.weights;
let tab_button = |value: &T, label: &str, active: bool| {
Button::new(label)
.id_source(value)
.variant(super::button_variant::ButtonVariant::Ghost)
.style(super::theme::ButtonStyle {
font_weight: Some(if active {
weights.selected()
} else {
weights.control
}),
..tab_style
})
.selected(active)
.corner_radius(CornerRadius::ZERO)
};
let count = tabs.len().max(1) as f32;
let gaps = style.spacing * tabs.len().saturating_sub(1) as f32;
let available = self.available_width();
let equal = ((available - gaps) / count).max(0.0);
let natural: Vec<f32> = tabs
.iter()
.map(|(value, label)| tab_button(value, label, true).natural_size(self).x)
.collect();
let first = tabs.first().map(|(value, _)| Id::new(value));
let total = natural.iter().sum::<f32>() + gaps;
let (widths, content_width) = if natural.iter().all(|width| *width <= equal) {
(vec![equal; tabs.len()], None)
} else if total <= available {
let extra = (available - total) / count;
(natural.iter().map(|width| width + extra).collect(), None)
} else {
(natural, Some(total))
};
let build = |ui: &mut Ui<'_>| -> Vec<(Response, bool)> {
let list = ui.a11y_begin(
ui.scope.with(("tab-list", first)),
AccessRole::TabList,
|_| {},
);
let mut nodes = Vec::new();
let rows: Vec<(Response, bool)> = tabs
.into_iter()
.zip(&widths)
.map(|((value, label), width)| {
let active = *selected == value;
if ui.context.a11y_on() {
nodes.push(ui.context.a11y_len());
}
let mut response = ui.add(
tab_button(&value, &label, active).min_size(Vec2::new(*width, height)),
);
if response.clicked() && *selected != value {
*selected = value;
response.changed = true;
}
if response.changed && content_width.is_some() {
ui.scroll_to_response(&response);
}
(response, active)
})
.collect();
let picked = rows.iter().position(|(response, _)| response.changed);
for (index, node) in nodes.iter().enumerate() {
let Some(node) = ui.context.a11y_node_mut(*node) else {
continue;
};
if node.role != AccessRole::Button {
continue;
}
let selected = picked.map_or(rows[index].1, |picked| picked == index);
node.toggled = None;
node.role(AccessRole::Tab)
.selected(selected)
.position_in_set(index, rows.len());
}
ui.a11y_end(list, None);
if let (Some((first, _)), Some((last, _))) = (rows.first(), rows.last()) {
let line = |from: f32, to: f32, top: f32, bottom: f32, color| {
Paint::Shape(
crate::Shape::rect(
Rect::from_min_max(Vec2::new(from, top), Vec2::new(to, bottom)),
color,
)
.into(),
)
};
let bottom = last.rect.max.y;
let mut shapes = vec![line(
first.rect.min.x,
last.rect.max.x,
bottom - 1.0,
bottom,
style.border.color,
)];
for (response, active) in &rows {
if *active {
let r = response.rect;
shapes.push(line(r.min.x, r.max.x, bottom - 2.0, bottom, style.accent));
}
}
ui.context
.paint(first.id.with("tab-line"), ui.window, ui.clip, shapes);
}
rows
};
let rows = match content_width {
None => self.horizontal(build),
Some(width) => {
let mut scroll = style.scroll;
scroll.padding = crate::Padding::all(0.0);
scroll.spacing = style.spacing;
crate::ScrollArea::horizontal()
.id_source(("tab-bar", first))
.style(scroll)
.max_height(height)
.content_width(width)
.overlay_scrollbars(true)
.show(self, build)
.inner
}
};
rows.into_iter().map(|(response, _)| response).collect()
}
}