use std::rc::Rc; use gpui::prelude::FluentBuilder as _; use gpui::{ Animation, AnimationExt as _, App, Context, ElementId, Entity, InteractiveElement as _, IntoElement, ParentElement as _, RenderOnce, Role, SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled as _, Window, deferred, div, ease_out_quint, }; use gpui_native_icons::{Icon, IconRole}; use gpui_native_theme::{ ALERT_EDGE_WIDTH, ALERT_STACK_WIDTH, ActiveTheme as _, ENTER_SLIDE_DISTANCE, Size, TextRole, Typeset as _, }; use gpui_native_utils::ResultLogErr as _; use crate::button::{Button, ButtonVariant}; use crate::common::DismissHandler; use crate::common::Sizable as _; use crate::common::StyledExt as _; use crate::common::TOAST_LAYER; use crate::common::v_flex; /// How alarming an [`Alert`] is; picks the accent color. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub enum AlertSeverity { /// The default; uses the info role. #[default] Info, /// Uses the success role. Success, /// Uses the warning role. Warning, /// Uses the destructive role. Danger, } /// How an [`Alert`] presents. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub enum AlertStyle { /// A floating banner styled like a popover; the default. #[default] Overlay, /// A tinted callout embedded in the page. Inline, } /// A static callout banner. #[derive(Clone, IntoElement)] #[must_use] pub struct Alert { id: ElementId, title: Option, message: Option, severity: AlertSeverity, presentation: AlertStyle, icon: Option, on_dismiss: Option, style: StyleRefinement, } impl Alert { pub fn new(id: I) -> Self where I: Into, { Self { id: id.into(), title: None, message: None, severity: AlertSeverity::Info, presentation: AlertStyle::default(), icon: None, on_dismiss: None, style: StyleRefinement::default(), } } /// Sets the primary line. pub fn title(mut self, title: L) -> Self where L: Into, { self.title = Some(title.into()); self } /// Sets a secondary line under the title. pub fn message(mut self, message: L) -> Self where L: Into, { self.message = Some(message.into()); self } /// Sets how alarming the alert is. pub fn severity(mut self, severity: AlertSeverity) -> Self { self.severity = severity; self } /// Sets how the alert presents. pub fn style(mut self, style: AlertStyle) -> Self { self.presentation = style; self } /// Shows an icon before the text. pub fn icon(mut self, icon: I) -> Self where I: Into, { self.icon = Some(icon.into()); self } /// Runs when the user dismisses the alert. pub fn on_dismiss(mut self, handler: F) -> Self where F: Fn(&mut App) + 'static, { self.on_dismiss = Some(Rc::new(handler)); self } fn rekey(mut self, id: usize) -> Self { self.id = ElementId::from(id); self } } impl gpui::Styled for Alert { fn style(&mut self) -> &mut StyleRefinement { &mut self.style } } impl RenderOnce for Alert { fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { let theme = cx.theme(); let ramp = match self.severity { AlertSeverity::Info => theme.ramps.info, AlertSeverity::Success => theme.ramps.success, AlertSeverity::Warning => theme.ramps.warning, AlertSeverity::Danger => theme.ramps.destructive, }; let foreground = theme.colors.foreground; let muted_foreground = theme.colors.muted_foreground; let metrics = theme.sizes.resolve(Size::Medium); let icon_size = metrics.icon_size; let dense_icon_size = metrics.dense_icon_size; let content = v_flex() .flex_1() .gap_0p5() .when_some(self.title.clone(), |alert, title| { alert.child( div() .typeset(TextRole::Headline, cx) .text_color(foreground) .child(title), ) }) .when_some(self.message.clone(), |alert, message| { let color = if self.title.is_some() { muted_foreground } else { foreground }; alert.child( div() .text_size(metrics.text_size) .text_color(color) .child(message), ) }); let dismiss = self.on_dismiss.clone().map(|on_dismiss| { Button::new((self.id.clone(), "alert-close")) .icon(IconRole::Close) .variant(ButtonVariant::Ghost) .xsmall() .accessibility_label("Dismiss") .on_click(move |_, _window, cx| on_dismiss(cx)) }); match self.presentation { AlertStyle::Overlay => { let surface = theme.colors.surface; let border = theme.colors.border; div() .id(self.id.clone()) .role(Role::Alert) .w_full() .flex() .items_start() .gap_2() .px_3() .py_2() .rounded(theme.radius.overlay) .bg(surface) .border_1() .border_color(border) .shadow(theme.elevations.popover.shadows()) .refine_style(&self.style) .when_some(self.icon.clone(), |alert, icon_glyph| { alert.child( div() .flex_none() .flex() .items_center() .h(window.line_height()) .child(icon_glyph.size(icon_size).color(ramp.base)), ) }) .child(content) .when_some(dismiss, |alert, dismiss| alert.child(dismiss)) } AlertStyle::Inline => div() .id(self.id.clone()) .role(Role::Alert) .w_full() .flex() .overflow_hidden() .bg(ramp.subtle) .refine_style(&self.style) .child( div() .flex_none() .w(ALERT_EDGE_WIDTH) .self_stretch() .bg(ramp.base), ) .child( div() .flex_1() .flex() .items_start() .gap_3() .px_3() .py_2() .when_some(self.icon.clone(), |alert, icon_glyph| { alert.child( div() .flex_none() .flex() .items_center() .justify_center() .size(theme.sizes.small.avatar_size) .rounded(theme.radius.full) .bg(ramp.base) .child(icon_glyph.size(dense_icon_size).color(ramp.foreground)), ) }) .child(content), ) .when_some(dismiss, |alert, dismiss| { alert.child(div().flex_none().self_center().pr_3().child(dismiss)) }), } } } /// One pushed alert, with its identity in the stack. #[derive(Clone)] pub struct AlertEntry { pub id: usize, pub alert: Alert, on_dismiss: Option, } /// The stack of pushed alerts. #[derive(Default)] pub struct Alerts { entries: Vec, next_id: usize, } impl Alerts { /// Pushes an alert onto the stack. pub fn push(&mut self, mut alert: Alert, cx: &mut Context) -> usize { let id = self.next_id; self.next_id += 1; let on_dismiss = alert.on_dismiss.take(); let stack = cx.entity().downgrade(); alert.on_dismiss = Some(Rc::new(move |cx: &mut App| { stack .update(cx, |alerts, cx| alerts.dismiss(id, cx)) .log_err(); })); self.entries.push(AlertEntry { id, alert: alert.rekey(id), on_dismiss, }); cx.notify(); id } /// Dismisses one alert by id, running its dismiss handler. pub fn dismiss(&mut self, id: usize, cx: &mut Context) { if let Some(position) = self.entries.iter().position(|entry| entry.id == id) { let entry = self.entries.remove(position); cx.notify(); if let Some(on_dismiss) = &entry.on_dismiss { (on_dismiss.clone())(cx); } } } /// Dismisses every alert, running each entry's `on_dismiss` handler. pub fn clear(&mut self, cx: &mut Context) { if self.entries.is_empty() { return; } let entries = std::mem::take(&mut self.entries); cx.notify(); for entry in entries { if let Some(on_dismiss) = entry.on_dismiss { on_dismiss(cx); } } } /// The stacked alerts, oldest first. pub fn entries(&self) -> &[AlertEntry] { &self.entries } /// How many alerts are stacked. pub fn count(&self) -> usize { self.entries.len() } } /// The floating stack of pushed alerts, topmost right. #[derive(IntoElement)] #[must_use] pub struct AlertsView { alerts: Entity, id: ElementId, } impl AlertsView { /// Binds the view to an alert stack. pub fn new(id: I, alerts: Entity) -> Self where I: Into, { Self { id: id.into(), alerts, } } } impl RenderOnce for AlertsView { fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement { let theme = cx.theme(); let enter_duration = theme.motion.overlay; let entries = self.alerts.read(cx).entries(); deferred( div() .id(self.id) .absolute() .top_4() .right_4() .w(ALERT_STACK_WIDTH) .flex() .flex_col() .items_end() .gap_2() .children(entries.iter().rev().map(|entry| { let id = entry.id; let ease_slide = ease_out_quint(); div() .relative() .w_full() .debug_selector(|| "alert-entry".to_string()) .child(entry.alert.clone()) .with_animation( ("alert-enter", id), Animation::new(enter_duration), move |layer, delta| { layer.left(ENTER_SLIDE_DISTANCE * (1. - ease_slide(delta))) }, ) })), ) .with_priority(TOAST_LAYER) } }