subspace-dashboard 0.1.0

Subspace Framework board kit: the Dashboard trait, shared widgets and design tokens. Internal crate; apps depend on `subspace-framework`.
Documentation
//! Canonical dashboard treatment for native GPUI Kit tabs.

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};

/// Between a tab's glyph, title and status, as between a Button's icon and
/// label.
const CONTENT_GAP: f32 = 6.;

/// Optional visible and accessible state shown after a tab label.
#[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,
        }
    }
}

/// Board-owned tab facts. An icon is the default; accessibility defaults to
/// the visible label plus any status text.
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,
        }
    }

    /// A text-only tab for existing named regions whose titles need no glyph.
    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(),
        )
    }
}

/// Build a native GPUI Kit tab with the canonical dashboard treatment.
pub fn tab(spec: TabSpec) -> Tab {
    let accessible_label = spec.accessible_label();
    // GPUI Kit's icon slot is icon-only, and a segmented tab paints its
    // selected fill behind its children only, not behind a prefix or suffix.
    // The glyph, title and status are therefore one child.
    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)
}

/// Build the canonical native section/resource tab bar: GPUI Kit's
/// segmented treatment at its natural width.
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");
    }
}