use crate::{
icons::{CarbonIcon, Icon, IconSize},
theme::color,
tokens::{self, ColorRole, TypeStyle},
typography::TypeStyled as _,
};
use gpui_kit::{App, IntoElement, ParentElement, RenderOnce, SharedString, Styled, Window, div};
pub const GAP: tokens::TokenLength = tokens::SPACING_03;
pub const DIFF_GAP: tokens::TokenLength = tokens::SPACING_02;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum IconIndicatorKind {
Failed,
CautionMajor,
CautionMinor,
Undefined,
Succeeded,
Normal,
InProgress,
Incomplete,
NotStarted,
Pending,
Unknown,
Informative,
}
impl IconIndicatorKind {
pub const ALL: [Self; 12] = [
Self::Failed,
Self::CautionMajor,
Self::CautionMinor,
Self::Undefined,
Self::Succeeded,
Self::Normal,
Self::InProgress,
Self::Incomplete,
Self::NotStarted,
Self::Pending,
Self::Unknown,
Self::Informative,
];
pub const fn name(self) -> &'static str {
match self {
Self::Failed => "failed",
Self::CautionMajor => "caution-major",
Self::CautionMinor => "caution-minor",
Self::Undefined => "undefined",
Self::Succeeded => "succeeded",
Self::Normal => "normal",
Self::InProgress => "in-progress",
Self::Incomplete => "incomplete",
Self::NotStarted => "not-started",
Self::Pending => "pending",
Self::Unknown => "unknown",
Self::Informative => "informative",
}
}
pub const fn icon(self) -> CarbonIcon {
match self {
Self::Failed => CarbonIcon::ErrorFilled,
Self::CautionMajor => CarbonIcon::WarningAltInvertedFilled,
Self::CautionMinor => CarbonIcon::WarningAltFilled,
Self::Undefined => CarbonIcon::UndefinedFilled,
Self::Succeeded => CarbonIcon::CheckmarkFilled,
Self::Normal => CarbonIcon::CheckmarkOutline,
Self::InProgress => CarbonIcon::InProgress,
Self::Incomplete => CarbonIcon::Incomplete,
Self::NotStarted => CarbonIcon::CircleDash,
Self::Pending => CarbonIcon::PendingFilled,
Self::Unknown => CarbonIcon::UnknownFilled,
Self::Informative => CarbonIcon::InformationSquareFilled,
}
}
pub const fn role(self) -> ColorRole {
match self {
Self::Failed => ColorRole::StatusRed,
Self::CautionMajor => ColorRole::StatusOrange,
Self::CautionMinor => ColorRole::StatusYellow,
Self::Undefined => ColorRole::StatusPurple,
Self::Succeeded => ColorRole::StatusGreen,
Self::Normal | Self::InProgress | Self::Incomplete | Self::Informative => {
ColorRole::StatusBlue
}
Self::NotStarted | Self::Pending | Self::Unknown => ColorRole::StatusGray,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum IndicatorSize {
#[default]
S16,
S20,
}
impl IndicatorSize {
pub const ALL: [Self; 2] = [Self::S16, Self::S20];
pub const fn name(self) -> &'static str {
match self {
Self::S16 => "16",
Self::S20 => "20",
}
}
pub const fn parts(self) -> (IconSize, TypeStyle) {
match self {
Self::S16 => (IconSize::S16, TypeStyle::BodyCompact01),
Self::S20 => (IconSize::S20, TypeStyle::BodyCompact02),
}
}
}
fn row(
icon: Icon,
label: SharedString,
style: TypeStyle,
gap: tokens::TokenLength,
) -> gpui_kit::Div {
div()
.flex()
.flex_row()
.items_center()
.gap(gap)
.type_style(style)
.text_color(color(ColorRole::TextSecondary))
.max_w_full()
.child(div().flex_none().flex().child(icon))
.child(div().min_w_0().child(label))
}
#[derive(IntoElement)]
pub struct IconIndicator {
kind: IconIndicatorKind,
label: SharedString,
size: IndicatorSize,
}
impl IconIndicator {
pub fn new(kind: IconIndicatorKind, label: impl Into<SharedString>) -> Self {
Self {
kind,
label: label.into(),
size: IndicatorSize::S16,
}
}
pub fn size(mut self, size: IndicatorSize) -> Self {
self.size = size;
self
}
}
impl RenderOnce for IconIndicator {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let (icon, style) = self.size.parts();
row(
Icon::carbon(self.kind.icon())
.size(icon)
.color(color(self.kind.role())),
self.label,
style,
GAP,
)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ShapeIndicatorKind {
Failed,
Critical,
High,
Medium,
Low,
Cautious,
Undefined,
Stable,
Informative,
Incomplete,
Draft,
}
impl ShapeIndicatorKind {
pub const ALL: [Self; 11] = [
Self::Failed,
Self::Critical,
Self::High,
Self::Medium,
Self::Low,
Self::Cautious,
Self::Undefined,
Self::Stable,
Self::Informative,
Self::Incomplete,
Self::Draft,
];
pub const fn name(self) -> &'static str {
match self {
Self::Failed => "failed",
Self::Critical => "critical",
Self::High => "high",
Self::Medium => "medium",
Self::Low => "low",
Self::Cautious => "cautious",
Self::Undefined => "undefined",
Self::Stable => "stable",
Self::Informative => "informative",
Self::Incomplete => "incomplete",
Self::Draft => "draft",
}
}
pub const fn icon(self) -> CarbonIcon {
match self {
Self::Failed => CarbonIcon::Critical,
Self::Critical => CarbonIcon::CriticalSeverity,
Self::High | Self::Cautious => CarbonIcon::Caution,
Self::Medium | Self::Undefined => CarbonIcon::DiamondFill,
Self::Low | Self::Informative => CarbonIcon::LowSeverity,
Self::Stable => CarbonIcon::CircleFill,
Self::Incomplete => CarbonIcon::Incomplete,
Self::Draft => CarbonIcon::CircleStroke,
}
}
pub const fn role(self) -> ColorRole {
match self {
Self::Failed | Self::Critical | Self::High => ColorRole::StatusRed,
Self::Medium => ColorRole::StatusOrangeOutline,
Self::Low => ColorRole::StatusYellow,
Self::Cautious => ColorRole::StatusYellowOutline,
Self::Undefined => ColorRole::StatusPurple,
Self::Stable => ColorRole::StatusGreen,
Self::Informative | Self::Incomplete => ColorRole::StatusBlue,
Self::Draft => ColorRole::StatusGray,
}
}
pub fn icon_element(self) -> Icon {
let icon = match self {
Self::Incomplete => Icon::from_path(INCOMPLETE_ASSET),
_ => Icon::carbon(self.icon()),
};
icon.size(IconSize::S16).color(color(self.role()))
}
}
pub const INCOMPLETE_ASSET: &str = "u0ui/shapes/incomplete.svg";
pub const INCOMPLETE_SVG: &str = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\"><path fill=\"#161616\" d=\"M8 2a6 6 0 1 0 0 12A6 6 0 0 0 8 2Zm0 2a4.004 4.004 0 0 1 4 4H4a4.004 4.004 0 0 1 4-4Z\"/></svg>";
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ShapeTextSize {
#[default]
S12,
S14,
}
impl ShapeTextSize {
pub const ALL: [Self; 2] = [Self::S12, Self::S14];
pub const fn name(self) -> &'static str {
match self {
Self::S12 => "12",
Self::S14 => "14",
}
}
pub const fn style(self) -> TypeStyle {
match self {
Self::S12 => TypeStyle::HelperText01,
Self::S14 => TypeStyle::BodyCompact01,
}
}
}
#[derive(IntoElement)]
pub struct ShapeIndicator {
kind: ShapeIndicatorKind,
label: SharedString,
text_size: ShapeTextSize,
}
impl ShapeIndicator {
pub fn new(kind: ShapeIndicatorKind, label: impl Into<SharedString>) -> Self {
Self {
kind,
label: label.into(),
text_size: ShapeTextSize::S12,
}
}
pub fn text_size(mut self, size: ShapeTextSize) -> Self {
self.text_size = size;
self
}
}
impl RenderOnce for ShapeIndicator {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
row(
self.kind.icon_element(),
self.label,
self.text_size.style(),
GAP,
)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Trend {
Up,
Down,
}
impl Trend {
pub const fn parts(self) -> (CarbonIcon, ColorRole) {
match self {
Self::Up => (CarbonIcon::ArrowUp, ColorRole::SupportSuccess),
Self::Down => (CarbonIcon::ArrowDown, ColorRole::SupportError),
}
}
}
#[derive(IntoElement)]
pub struct DiffIndicator {
trend: Trend,
text: SharedString,
}
impl DiffIndicator {
pub fn new(trend: Trend, text: impl Into<SharedString>) -> Self {
Self {
trend,
text: text.into(),
}
}
}
impl RenderOnce for DiffIndicator {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let (icon, role) = self.trend.parts();
row(
Icon::carbon(icon).size(IconSize::S16).color(color(role)),
self.text,
TypeStyle::BodyCompact01,
DIFF_GAP,
)
.text_color(color(ColorRole::TextPrimary))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn icon_kinds_take_their_status_colors() {
use IconIndicatorKind::*;
assert_eq!(Failed.role(), ColorRole::StatusRed);
assert_eq!(CautionMajor.role(), ColorRole::StatusOrange);
assert_eq!(CautionMinor.role(), ColorRole::StatusYellow);
assert_eq!(Undefined.role(), ColorRole::StatusPurple);
assert_eq!(Succeeded.role(), ColorRole::StatusGreen);
assert_eq!(InProgress.role(), ColorRole::StatusBlue);
assert_eq!(NotStarted.role(), ColorRole::StatusGray);
assert_eq!(NotStarted.icon(), CarbonIcon::CircleDash);
assert_eq!(InProgress.icon(), CarbonIcon::InProgress);
assert_eq!(IconIndicatorKind::ALL.len(), 12);
}
#[test]
fn shape_kinds_take_their_status_colors() {
use ShapeIndicatorKind::*;
assert_eq!(Critical.role(), ColorRole::StatusRed);
assert_eq!(Medium.role(), ColorRole::StatusOrangeOutline);
assert_eq!(Cautious.role(), ColorRole::StatusYellowOutline);
assert_eq!(Low.role(), ColorRole::StatusYellow);
assert_eq!(Undefined.role(), ColorRole::StatusPurple);
assert_eq!(Incomplete.role(), ColorRole::StatusBlue);
assert_eq!(Cautious.icon(), High.icon());
assert_eq!(Informative.icon(), Low.icon());
assert_eq!(Undefined.icon(), Medium.icon());
assert_eq!(
Incomplete.icon_element().asset_path().as_ref(),
INCOMPLETE_ASSET
);
assert!(INCOMPLETE_SVG.contains("H4a4.004"));
assert_eq!(Stable.role(), ColorRole::StatusGreen);
assert_eq!(Draft.role(), ColorRole::StatusGray);
assert_eq!(Medium.icon(), CarbonIcon::DiamondFill);
}
#[test]
fn sizes_follow_the_css() {
assert_eq!(
IndicatorSize::S20.parts(),
(IconSize::S20, TypeStyle::BodyCompact02)
);
assert_eq!(ShapeTextSize::S12.style(), TypeStyle::HelperText01);
assert_eq!(GAP.px_value(), 8.);
assert_eq!(DIFF_GAP.px_value(), 4.);
assert_eq!(Trend::Up.parts().1, ColorRole::SupportSuccess);
}
}