use crate::components::{
Button, ButtonSize, ButtonVariant, alert::NotificationKind, alert::NotificationLook,
};
use crate::{
icons::{CarbonIcon, Icon, IconSize},
theme,
tokens::{CarbonTheme, TypeStyle},
typography::TypeStyled as _,
};
use gpui_kit::{
App, BoxShadow, ElementId, IntoElement, ParentElement, RenderOnce, SharedString, Styled,
Window, div, prelude::FluentBuilder as _, px, rems,
};
use std::rc::Rc;
pub const WIDTH_REM: f32 = 18.;
pub const ACCENT_PX: f32 = 3.;
pub const DETAILS_START_REM: f32 = 0.8125;
pub const TEXT_PADDING_REM: (f32, f32) = (0.9375, 1.4375);
pub const ACTION_INSET_REM: (f32, f32) = (2. + 1.25 - 0.125, 1.);
pub const CLOSE_REM: f32 = 3.;
type Handler = Rc<dyn Fn(&mut Window, &mut App)>;
pub fn inverse_theme(theme: CarbonTheme) -> CarbonTheme {
match theme {
CarbonTheme::White | CarbonTheme::G10 => CarbonTheme::G100,
CarbonTheme::G90 | CarbonTheme::G100 => CarbonTheme::White,
}
}
#[derive(IntoElement)]
pub struct ActionableToast {
id: ElementId,
kind: NotificationKind,
title: SharedString,
subtitle: Option<SharedString>,
action_label: SharedString,
on_action: Option<Handler>,
close_label: SharedString,
on_close: Option<Handler>,
low_contrast: bool,
}
impl ActionableToast {
pub fn new(
id: impl Into<ElementId>,
title: impl Into<SharedString>,
action_label: impl Into<SharedString>,
) -> Self {
Self {
id: id.into(),
kind: NotificationKind::Info,
title: title.into(),
subtitle: None,
action_label: action_label.into(),
on_action: None,
close_label: SharedString::default(),
on_close: None,
low_contrast: false,
}
}
pub fn kind(mut self, kind: NotificationKind) -> Self {
self.kind = kind;
self
}
pub fn subtitle(mut self, text: impl Into<SharedString>) -> Self {
self.subtitle = Some(text.into());
self
}
pub fn on_action(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_action = Some(Rc::new(f));
self
}
pub fn close_label(mut self, label: impl Into<SharedString>) -> Self {
self.close_label = label.into();
self
}
pub fn on_close(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_close = Some(Rc::new(f));
self
}
pub fn low_contrast(mut self, low: bool) -> Self {
self.low_contrast = low;
self
}
}
impl RenderOnce for ActionableToast {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let open = window.use_keyed_state((id.clone(), "open"), cx, |_, _| true);
if !*open.read(cx) {
return div().into_any_element();
}
let look = NotificationLook::of(self.kind, self.low_contrast);
let zone = if self.low_contrast {
theme::current()
} else {
inverse_theme(theme::current())
};
let action_id = ElementId::from((id.clone(), "action"));
let label = self.action_label.clone();
let on_action = self.on_action.clone();
let action = theme::zone(zone, move |_, _| {
Button::new(action_id, label)
.variant(ButtonVariant::Tertiary)
.size(ButtonSize::Sm)
.label_top(true)
.when_some(on_action, |b, f| b.on_click(move |_, w, cx| f(w, cx)))
.into_any_element()
});
let close_id = ElementId::from((id.clone(), "close"));
let close_label = self.close_label.clone();
let on_close = self.on_close.clone();
let close = theme::zone(zone, move |_, _| {
Button::new(close_id, close_label)
.variant(ButtonVariant::Ghost)
.size(ButtonSize::Lg)
.density(crate::density::Density::Comfortable)
.icon(CarbonIcon::Close)
.icon_only(true)
.tooltip(false)
.on_click(move |_, w, cx| {
open.update(cx, |o, cx| {
*o = false;
cx.notify();
});
if let Some(f) = &on_close {
f(w, cx);
}
})
.into_any_element()
});
div()
.relative()
.flex()
.flex_col()
.flex_none()
.w(rems(WIDTH_REM))
.bg(look.background)
.border_l(px(ACCENT_PX))
.border_color(look.accent)
.text_color(look.text)
.shadow(vec![
BoxShadow::new(px(0.), px(2.), gpui_kit::black().opacity(0.2)).blur_radius(px(6.)),
])
.child(
div()
.flex()
.flex_row()
.pl(rems(DETAILS_START_REM))
.pr(rems(CLOSE_REM))
.child(
div().flex_none().mt(rems(1.)).mr(rems(1.)).child(
Icon::carbon(self.kind.icon())
.size(IconSize::S20)
.color(look.accent),
),
)
.child(
div()
.flex()
.flex_col()
.flex_1()
.min_w_0()
.pt(rems(TEXT_PADDING_REM.0))
.pb(rems(TEXT_PADDING_REM.1))
.child(
div()
.type_style(TypeStyle::HeadingCompact01)
.child(self.title),
)
.when_some(self.subtitle, |d, t| {
d.child(div().type_style(TypeStyle::BodyCompact01).child(t))
}),
),
)
.child(
div()
.flex()
.ml(rems(ACTION_INSET_REM.0))
.mb(rems(ACTION_INSET_REM.1))
.child(action),
)
.child(div().absolute().top_0().right_0().child(close))
.into_any_element()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn geometry_follows_the_css() {
assert_eq!(WIDTH_REM * 16., 288.);
assert_eq!(ACTION_INSET_REM.0 * 16., 50.);
assert_eq!(TEXT_PADDING_REM, (0.9375, 1.4375));
}
#[test]
fn actions_render_in_the_opposite_theme() {
assert_eq!(inverse_theme(CarbonTheme::White), CarbonTheme::G100);
assert_eq!(inverse_theme(CarbonTheme::G10), CarbonTheme::G100);
assert_eq!(inverse_theme(CarbonTheme::G90), CarbonTheme::White);
assert_eq!(inverse_theme(CarbonTheme::G100), CarbonTheme::White);
}
}