use gpui_kit::component::Icon;
use gpui_kit::{AnyElement, ElementId, Hsla, SharedString};
pub(super) 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,
key: Option<SharedString>,
icon: Option<Icon>,
status: Option<TabStatus>,
disabled: bool,
unread: bool,
content: Option<AnyElement>,
}
impl TabSpec {
pub fn new(label: impl Into<SharedString>, icon: impl Into<Icon>) -> Self {
Self {
label: label.into(),
key: None,
icon: Some(icon.into()),
status: None,
disabled: false,
unread: false,
content: None,
}
}
pub fn plain(label: impl Into<SharedString>) -> Self {
Self {
label: label.into(),
key: None,
icon: None,
status: None,
disabled: false,
unread: false,
content: None,
}
}
pub fn key(mut self, key: impl Into<SharedString>) -> Self {
self.key = Some(key.into());
self
}
pub fn unread(mut self, unread: bool) -> Self {
self.unread = unread;
self
}
pub fn content(mut self, content: AnyElement) -> Self {
self.content = Some(content);
self
}
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 {
let name: SharedString = match &self.key {
Some(key) => format!("{key} {}", self.label).into(),
None => self.label.clone(),
};
let label = self.status.as_ref().map_or_else(
|| name.clone(),
|status| format!("{name}, {}", status.accessible).into(),
);
if self.unread {
format!("{label}, unread items").into()
} else {
label
}
}
}
impl gpui_kit::prelude::FluentBuilder for TabSpec {}
mod bar;
pub mod dock;
#[cfg(test)]
mod interaction_tests;
mod paint;
#[cfg(test)]
mod state_tests;
pub use bar::{Tab, TabBar};
pub use paint::unread_dot;
pub fn tab(spec: TabSpec) -> Tab {
Tab::new(spec)
}
pub fn tab_bar(id: impl Into<ElementId>) -> TabBar {
TabBar::new(id)
}
pub fn pane_tab_bar(id: impl Into<ElementId>) -> TabBar {
TabBar::new(id).pane()
}
#[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");
let item = TabSpec::new("Course correction", IconName::CircleDot).key("SUB-12");
assert_eq!(item.accessible_label(), "SUB-12 Course correction");
}
}