use gpui_kit::component::Sizable as _;
use gpui_kit::component::tab::{Tab, TabBar};
use gpui_kit::component::{Icon, h_flex};
use gpui_kit::prelude::FluentBuilder as _;
use gpui_kit::{ElementId, Hsla, ParentElement as _, SharedString, Styled as _, div, px};
const CONTENT_GAP: f32 = 6.;
#[derive(Debug, Clone, PartialEq)]
pub struct TabStatus {
visible: SharedString,
accessible: SharedString,
colour: Hsla,
}
impl TabStatus {
pub fn text(
visible: impl Into<SharedString>,
accessible: impl Into<SharedString>,
colour: Hsla,
) -> Self {
Self {
visible: visible.into(),
accessible: accessible.into(),
colour,
}
}
pub fn count(count: u64, colour: Hsla) -> Self {
let count = count.to_string();
Self {
visible: count.clone().into(),
accessible: format!("{count} items").into(),
colour,
}
}
pub fn busy(colour: Hsla) -> Self {
Self {
visible: "…".into(),
accessible: "Loading".into(),
colour,
}
}
pub fn count_and_busy(count: u64, colour: Hsla) -> Self {
Self {
visible: format!("{count} …").into(),
accessible: format!("{count} items, loading").into(),
colour,
}
}
}
pub struct TabSpec {
label: SharedString,
icon: Option<Icon>,
status: Option<TabStatus>,
disabled: bool,
}
impl TabSpec {
pub fn new(label: impl Into<SharedString>, icon: impl Into<Icon>) -> Self {
Self {
label: label.into(),
icon: Some(icon.into()),
status: None,
disabled: false,
}
}
pub fn plain(label: impl Into<SharedString>) -> Self {
Self {
label: label.into(),
icon: None,
status: None,
disabled: false,
}
}
pub fn status(mut self, status: TabStatus) -> Self {
self.status = Some(status);
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
fn accessible_label(&self) -> SharedString {
self.status.as_ref().map_or_else(
|| self.label.clone(),
|status| format!("{}, {}", self.label, status.accessible).into(),
)
}
}
pub fn tab(spec: TabSpec) -> Tab {
let accessible_label = spec.accessible_label();
let content = h_flex()
.gap(px(CONTENT_GAP))
.items_center()
.when_some(spec.icon, |row, icon| row.child(icon.size_3p5()))
.child(spec.label)
.when_some(spec.status, |row, status| {
row.child(
div()
.text_color(status.colour)
.child(status.visible.clone()),
)
});
Tab::new()
.aria_label(accessible_label)
.disabled(spec.disabled)
.child(content)
}
pub fn tab_bar(id: impl Into<ElementId>) -> TabBar {
TabBar::new(id).segmented().small().w_auto()
}
#[cfg(test)]
mod tests {
use gpui_kit::hsla;
use subspace_lucide::IconName;
use super::{TabSpec, TabStatus};
#[test]
fn count_and_busy_statuses_keep_visible_and_accessible_meaning() {
let count = TabStatus::count(12, hsla(0., 0., 0., 1.));
assert_eq!(count.visible, "12");
assert_eq!(count.accessible, "12 items");
let busy = TabStatus::busy(hsla(0., 0., 0., 1.));
assert_eq!(busy.visible, "…");
assert_eq!(busy.accessible, "Loading");
let both = TabStatus::count_and_busy(12, hsla(0., 0., 0., 1.));
assert_eq!(both.visible, "12 …");
assert_eq!(both.accessible, "12 items, loading");
let disabled = TabSpec::new("Open", IconName::CircleDot).disabled(true);
assert!(disabled.disabled);
assert_eq!(disabled.accessible_label(), "Open");
}
}