use crate::components::button::{Button, ButtonSize, ButtonVariant};
use crate::t3::base::{Spin, warning_glyph};
use crate::t3::press::{self, Handler, rem};
use crate::{icons::CarbonIcon, theme::color, tokens::ColorRole};
use gpui_kit::{
AnyElement, App, ElementId, FontWeight, Hsla, IntoElement, ParentElement, RenderOnce,
SharedString, Styled, Window, div, prelude::FluentBuilder as _, px, relative,
};
use std::rc::Rc;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum BannerKind {
#[default]
Info,
Warning,
Error,
Success,
Progress,
}
impl BannerKind {
pub const ALL: [BannerKind; 5] = [
Self::Info,
Self::Warning,
Self::Error,
Self::Success,
Self::Progress,
];
pub const fn name(self) -> &'static str {
match self {
Self::Info => "info",
Self::Warning => "warning",
Self::Error => "error",
Self::Success => "success",
Self::Progress => "progress",
}
}
pub fn accent(self) -> Hsla {
color(match self {
Self::Info => ColorRole::SupportInfo,
Self::Warning => ColorRole::SupportWarning,
Self::Error => ColorRole::SupportError,
Self::Success => ColorRole::SupportSuccess,
Self::Progress => ColorRole::Interactive,
})
}
pub const fn icon(self) -> CarbonIcon {
match self {
Self::Info => CarbonIcon::InformationFilled,
Self::Warning => CarbonIcon::WarningAltFilled,
Self::Error => CarbonIcon::ErrorFilled,
Self::Success => CarbonIcon::CheckmarkFilled,
Self::Progress => CarbonIcon::InProgress,
}
}
}
#[derive(IntoElement)]
pub struct ComposerBanner {
id: ElementId,
kind: BannerKind,
icon: Option<CarbonIcon>,
title: Option<SharedString>,
body: Option<SharedString>,
progress: Option<f32>,
actions: Vec<AnyElement>,
dismiss_label: SharedString,
progress_label: SharedString,
on_dismiss: Option<Handler>,
}
impl ComposerBanner {
pub fn new(id: impl Into<ElementId>, kind: BannerKind) -> Self {
Self {
id: id.into(),
kind,
icon: None,
title: None,
body: None,
progress: None,
actions: Vec::new(),
dismiss_label: "Dismiss".into(),
progress_label: "In progress".into(),
on_dismiss: None,
}
}
pub fn icon(mut self, icon: CarbonIcon) -> Self {
self.icon = Some(icon);
self
}
pub fn title(mut self, title: impl Into<SharedString>) -> Self {
self.title = Some(title.into());
self
}
pub fn body(mut self, body: impl Into<SharedString>) -> Self {
self.body = Some(body.into());
self
}
pub fn progress(mut self, percent: f32) -> Self {
self.progress = Some(percent);
self
}
pub fn action(mut self, action: impl IntoElement) -> Self {
self.actions.push(action.into_any_element());
self
}
pub fn labels(
mut self,
dismiss: impl Into<SharedString>,
progress: impl Into<SharedString>,
) -> Self {
self.dismiss_label = dismiss.into();
self.progress_label = progress.into();
self
}
pub fn on_dismiss(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_dismiss = Some(Rc::new(f));
self
}
}
impl RenderOnce for ComposerBanner {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let id = self.id.clone();
let accent = self.kind.accent();
let icon: AnyElement = match (self.kind, self.icon) {
(BannerKind::Progress, None) => Spin::new((id.clone(), "spin"))
.label(self.progress_label.clone())
.into_any_element(),
(BannerKind::Warning, None) => warning_glyph(16.).into_any_element(),
(kind, icon) => {
press::icon(icon.unwrap_or(kind.icon()), 16., accent).into_any_element()
}
};
let body = div()
.flex()
.flex_row()
.flex_wrap()
.flex_1()
.items_center()
.gap_x(rem(8.))
.min_w_0()
.py(rem(4.))
.when_some(self.title.clone(), |this, t| {
this.child(
div()
.min_w_0()
.whitespace_normal()
.font_weight(FontWeight::SEMIBOLD)
.child(t),
)
})
.when_some(self.body.clone(), |this, b| {
this.child(
div()
.min_w_0()
.whitespace_normal()
.text_color(color(ColorRole::TextSecondary))
.child(b),
)
})
.when_some(self.progress, |this, p| {
this.child(
div()
.w_full()
.h(px(4.))
.mt(rem(4.))
.bg(color(ColorRole::LayerAccent01))
.child(
div()
.h_full()
.w(relative((p / 100.).clamp(0., 1.)))
.bg(color(ColorRole::Interactive)),
),
)
});
let dismiss = self.on_dismiss.clone();
div()
.flex()
.flex_row()
.items_center()
.w_full()
.gap(rem(12.))
.min_h(rem(40.))
.pt(rem(4.))
.pb(rem(4.))
.pr(rem(4.))
.pl(rem(16.))
.bg(color(ColorRole::Layer01))
.shadow(crate::t3::timeline::card::rail(accent))
.text_size(rem(14.))
.line_height(rem(18.))
.text_color(color(ColorRole::TextPrimary))
.child(div().flex_none().child(icon))
.child(body)
.when(!self.actions.is_empty(), |this| {
this.child(
div()
.flex()
.flex_row()
.flex_none()
.gap(px(1.))
.children(self.actions),
)
})
.when_some(dismiss, |this, f| {
this.child(
Button::new((id.clone(), "dismiss"), self.dismiss_label.clone())
.icon(CarbonIcon::Close)
.icon_only(true)
.variant(ButtonVariant::Ghost)
.size(ButtonSize::Sm)
.on_click(move |_, w, cx| f(w, cx)),
)
})
}
}
#[derive(Clone)]
pub struct BannerStackTexts {
pub more: Rc<dyn Fn(usize) -> SharedString>,
pub fewer: SharedString,
}
impl Default for BannerStackTexts {
fn default() -> Self {
Self {
more: Rc::new(|n| format!("{n} more notices").into()),
fewer: "Show fewer".into(),
}
}
}
#[derive(IntoElement)]
pub struct ComposerBannerStack {
id: ElementId,
banners: Vec<AnyElement>,
collapsed: bool,
texts: BannerStackTexts,
}
impl ComposerBannerStack {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
banners: Vec::new(),
collapsed: false,
texts: BannerStackTexts::default(),
}
}
pub fn collapsed(mut self, collapsed: bool) -> Self {
self.collapsed = collapsed;
self
}
pub fn texts(mut self, texts: BannerStackTexts) -> Self {
self.texts = texts;
self
}
}
impl ParentElement for ComposerBannerStack {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.banners.extend(elements);
}
}
impl RenderOnce for ComposerBannerStack {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let collapsed = self.collapsed;
let open_state = window.use_keyed_state((id.clone(), "open"), cx, move |_, _| !collapsed);
let open = *open_state.read(cx);
let count = self.banners.len();
let foldable = collapsed && count > 1;
let (focus, ring) = press::focus((id.clone(), "more-focus"), window, cx);
let link = color(ColorRole::LinkPrimary);
let mut banners = self.banners;
if !open {
banners.truncate(1);
}
div()
.flex()
.flex_col()
.w_full()
.gap(px(1.))
.children(banners)
.when(foldable, |this| {
this.child(
gpui_kit::base::Button::new((id.clone(), "more"))
.track_focus(&focus)
.relative()
.flex()
.flex_row()
.items_center()
.self_start()
.gap(rem(4.))
.py(rem(4.))
.px(rem(8.))
.text_size(rem(12.))
.line_height(rem(16.))
.text_color(link)
.cursor_pointer()
.on_click(move |_, _, cx| {
open_state.update(cx, |o, cx| {
*o = !*o;
cx.notify();
})
})
.child(if open {
self.texts.fewer.clone()
} else {
(self.texts.more)(count - 1)
})
.child(press::icon(CarbonIcon::ChevronDown, 12., link))
.when(ring, |this| this.child(press::ring())),
)
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
#[test]
fn accents_follow_the_kind() {
crate::theme::tests_set(CarbonTheme::White);
assert_eq!(BannerKind::Info.accent(), color(ColorRole::SupportInfo));
assert_eq!(BannerKind::Progress.accent(), color(ColorRole::Interactive));
assert_eq!(BannerKind::Success.icon(), CarbonIcon::CheckmarkFilled);
assert_eq!((BannerStackTexts::default().more)(3), "3 more notices");
}
}