use super::card;
use crate::t3::press::{self, rem};
use crate::{icons::CarbonIcon, theme::color, tokens::ColorRole};
use gpui_kit::{
AnyElement, App, FontWeight, Hsla, IntoElement, ParentElement, RenderOnce, SharedString,
Styled, Window, div, prelude::FluentBuilder as _, px,
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum DividerKind {
#[default]
Model,
Session,
Handoff,
Now,
Compact,
Worktree,
Unread,
}
impl DividerKind {
pub const ALL: [DividerKind; 7] = [
Self::Model,
Self::Session,
Self::Handoff,
Self::Now,
Self::Compact,
Self::Worktree,
Self::Unread,
];
pub const fn name(self) -> &'static str {
match self {
Self::Model => "model",
Self::Session => "session",
Self::Handoff => "handoff",
Self::Now => "now",
Self::Compact => "compact",
Self::Worktree => "worktree",
Self::Unread => "unread",
}
}
pub fn from_name(name: &str) -> Self {
Self::ALL
.into_iter()
.find(|k| k.name() == name)
.unwrap_or_default()
}
pub const fn icon(self) -> CarbonIcon {
match self {
Self::Model => CarbonIcon::Model,
Self::Session => CarbonIcon::Renew,
Self::Handoff => CarbonIcon::ArrowRight,
Self::Now | Self::Unread => CarbonIcon::Time,
Self::Compact => CarbonIcon::Subtract,
Self::Worktree => CarbonIcon::Branch,
}
}
pub fn colors(self) -> (Hsla, Hsla) {
if self == Self::Now {
let red = color(ColorRole::SupportError);
(red, red)
} else {
(
color(ColorRole::TextSecondary),
color(ColorRole::BorderSubtle01),
)
}
}
}
#[derive(IntoElement)]
pub struct TimelineDivider {
kind: DividerKind,
text: SharedString,
time: Option<SharedString>,
}
impl TimelineDivider {
pub fn new(kind: DividerKind, text: impl Into<SharedString>) -> Self {
Self {
kind,
text: text.into(),
time: None,
}
}
pub fn time(mut self, time: impl Into<SharedString>) -> Self {
self.time = Some(time.into());
self
}
}
impl RenderOnce for TimelineDivider {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let (ink, rule) = self.kind.colors();
let line = || div().flex_1().h(px(1.)).bg(rule);
div()
.flex()
.flex_row()
.items_center()
.w_full()
.gap(rem(8.))
.text_size(rem(12.))
.line_height(rem(16.))
.text_color(ink)
.child(line())
.child(press::icon(self.kind.icon(), 14., ink))
.child(div().flex_none().child(self.text))
.when_some(self.time, |this, t| {
this.child(
div()
.flex_none()
.text_color(color(ColorRole::TextHelper))
.child(t),
)
})
.child(line())
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ThreadBannerKind {
#[default]
Error,
Warning,
}
impl ThreadBannerKind {
pub const ALL: [ThreadBannerKind; 2] = [Self::Error, Self::Warning];
pub const fn name(self) -> &'static str {
match self {
Self::Error => "error",
Self::Warning => "warning",
}
}
pub fn look(self) -> (Hsla, Hsla, CarbonIcon) {
match self {
Self::Error => (
color(ColorRole::NotificationBackgroundError),
color(ColorRole::SupportError),
CarbonIcon::ErrorFilled,
),
Self::Warning => (
color(ColorRole::NotificationBackgroundWarning),
color(ColorRole::SupportWarning),
CarbonIcon::WarningAltFilled,
),
}
}
}
#[derive(IntoElement)]
pub struct ThreadErrorBanner {
kind: ThreadBannerKind,
title: SharedString,
body: Option<SharedString>,
actions: Vec<AnyElement>,
}
impl ThreadErrorBanner {
pub fn new(kind: ThreadBannerKind, title: impl Into<SharedString>) -> Self {
Self {
kind,
title: title.into(),
body: None,
actions: Vec::new(),
}
}
pub fn body(mut self, body: impl Into<SharedString>) -> Self {
self.body = Some(body.into());
self
}
pub fn action(mut self, action: impl IntoElement) -> Self {
self.actions.push(action.into_any_element());
self
}
}
impl RenderOnce for ThreadErrorBanner {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let (fill, accent, icon) = self.kind.look();
div()
.flex()
.flex_row()
.flex_wrap()
.items_center()
.w_full()
.gap(rem(12.))
.py(rem(12.))
.px(rem(16.))
.bg(fill)
.shadow(card::rail(accent))
.text_size(rem(14.))
.line_height(rem(18.))
.text_color(color(ColorRole::TextPrimary))
.child(if icon == CarbonIcon::WarningAltFilled {
crate::t3::base::warning_glyph(20.).into_any_element()
} else {
press::icon(icon, 20., accent).into_any_element()
})
.child(
div()
.flex()
.flex_col()
.flex_grow(1.)
.flex_shrink(1.)
.flex_basis(rem(256.))
.gap(px(2.))
.child(div().font_weight(FontWeight::SEMIBOLD).child(self.title))
.when_some(self.body, |this, b| {
this.child(div().text_color(color(ColorRole::TextSecondary)).child(b))
}),
)
.child(card::actions().children(self.actions))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
#[test]
fn unknown_divider_kinds_fall_back_to_model() {
assert_eq!(DividerKind::from_name("handoff"), DividerKind::Handoff);
assert_eq!(DividerKind::from_name("bogus"), DividerKind::Model);
assert_eq!(DividerKind::Unread.icon(), CarbonIcon::Time);
}
#[test]
fn the_now_line_is_red() {
crate::theme::tests_set(CarbonTheme::White);
let (ink, rule) = DividerKind::Now.colors();
assert_eq!(ink, color(ColorRole::SupportError));
assert_eq!(rule, ink);
let (ink, rule) = DividerKind::Model.colors();
assert_eq!(ink, color(ColorRole::TextSecondary));
assert_eq!(rule, color(ColorRole::BorderSubtle01));
}
#[test]
fn banners_tint_by_kind() {
crate::theme::tests_set(CarbonTheme::White);
let (fill, accent, _) = ThreadBannerKind::Warning.look();
assert_eq!(fill, color(ColorRole::NotificationBackgroundWarning));
assert_eq!(accent, color(ColorRole::SupportWarning));
}
}