diff --git a/crates/core/src/alert.rs b/crates/core/src/alert.rs index a0b941c..9ff1e87 100644 --- a/crates/core/src/alert.rs +++ b/crates/core/src/alert.rs @@ -6,7 +6,7 @@ use gpui::{ IntoElement, ParentElement as _, RenderOnce, Role, SharedString, StatefulInteractiveElement as _, Styled as _, Window, deferred, div, ease_out_quint, }; -use gpui_native_icons::{Icon, IconRole, icon}; +use gpui_native_icons::{Icon, IconRole}; use gpui_native_theme::{ActiveTheme as _, ENTER_SLIDE_DISTANCE, Size, TextRole, Typeset as _}; use gpui_native_utils::ResultLogErr as _; @@ -16,7 +16,7 @@ use crate::common::Sizable as _; use crate::common::TOAST_LAYER; use crate::common::v_flex; -/// How alarming an [`Alert`] is; picks the icon and the accent color. +/// 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. @@ -30,6 +30,16 @@ pub enum AlertSeverity { 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] @@ -38,8 +48,8 @@ pub struct Alert { title: Option, message: Option, severity: AlertSeverity, + style: AlertStyle, icon: Option, - show_icon: bool, on_dismiss: Option, } @@ -53,8 +63,8 @@ impl Alert { title: None, message: None, severity: AlertSeverity::Info, + style: AlertStyle::default(), icon: None, - show_icon: true, on_dismiss: None, } } @@ -83,7 +93,13 @@ impl Alert { self } - /// Overrides the severity-derived icon. + /// Sets how the alert presents. + pub fn style(mut self, style: AlertStyle) -> Self { + self.style = style; + self + } + + /// Shows an icon before the text. pub fn icon(mut self, icon: I) -> Self where I: Into, @@ -92,12 +108,6 @@ impl Alert { self } - /// Hides the icon entirely. - pub fn no_icon(mut self) -> Self { - self.show_icon = false; - self - } - /// Runs when the user dismisses the alert. pub fn on_dismiss(mut self, handler: F) -> Self where @@ -124,89 +134,119 @@ impl RenderOnce for Alert { AlertSeverity::Danger => theme.ramps.destructive, }; - let icon_glyph = self.icon.unwrap_or_else(|| { - icon(match self.severity { - AlertSeverity::Info => IconRole::Info, - AlertSeverity::Success => IconRole::Check, - AlertSeverity::Warning | AlertSeverity::Danger => IconRole::Warning, - }) - }); - let foreground = theme.colors.foreground; let muted_foreground = theme.colors.muted_foreground; - let surface = theme.colors.surface; - let border = if theme.borders.overlays { - theme.colors.border - } else { - surface - }; - - let shadows = theme.elevations.popover.shadows(); let metrics = theme.sizes.resolve(Size::Medium); let icon_size = metrics.icon_size; + let dense_icon_size = metrics.dense_icon_size; let caption_size = metrics.caption_size; - 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(shadows) - .when(self.show_icon, |alert| { + let content = v_flex() + .flex_1() + .gap_0p5() + .when_some(self.title.clone(), |alert, title| { alert.child( div() - .flex_none() - .flex() - .items_center() - .h(window.line_height()) - .child(icon_glyph.size(icon_size).color(ramp.base)), + .typeset(TextRole::Headline, cx) + .text_color(foreground) + .child(title), ) }) - .child( - v_flex() - .flex_1() - .gap_0p5() - .when_some(self.title.clone(), |alert, title| { + .when_some(self.message.clone(), |alert, message| { + let color = if self.title.is_some() { + muted_foreground + } else { + foreground + }; + alert.child( + div() + .text_size(caption_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) + .small() + .accessibility_label("Dismiss") + .on_click(move |_, _window, cx| on_dismiss(cx)) + }); + + match self.style { + AlertStyle::Overlay => { + let surface = theme.colors.surface; + let border = if theme.borders.overlays { + theme.colors.border + } else { + surface + }; + + 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()) + .when_some(self.icon.clone(), |alert, icon_glyph| { alert.child( div() - .typeset(TextRole::Headline, cx) - .text_color(foreground) - .child(title), + .flex_none() + .flex() + .items_center() + .h(window.line_height()) + .child(icon_glyph.size(icon_size).color(ramp.base)), ) }) - .when_some(self.message.clone(), |alert, message| { - let color = if self.title.is_some() { - muted_foreground - } else { - foreground - }; - alert.child( - div() - .text_size(caption_size) - .text_color(color) - .child(message), - ) - }), - ) - .when_some(self.on_dismiss, |alert, on_dismiss| { - alert.child( - Button::new((self.id.clone(), "alert-close")) - .icon(IconRole::Close) - .variant(ButtonVariant::Ghost) - .small() - .accessibility_label("Dismiss") - .on_click(move |_, _window, cx| on_dismiss(cx)), + .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) + .child(div().flex_none().w_1p5().self_stretch().bg(ramp.base)) + .child( + div() + .flex_1() + .flex() + .items_start() + .gap_3() + .px_3() + .py_2p5() + .when_some(self.icon.clone(), |alert, icon_glyph| { + alert.child( + div() + .flex_none() + .flex() + .items_center() + .justify_center() + .size_6() + .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)) + }), + } } } diff --git a/examples/gallery/src/main.rs b/examples/gallery/src/main.rs index 3e343f2..e567a01 100644 --- a/examples/gallery/src/main.rs +++ b/examples/gallery/src/main.rs @@ -9,7 +9,7 @@ use gpui::{ SharedString, Styled as _, UniformListScrollHandle, WeakEntity, Window, WindowBounds, WindowOptions, actions, div, list, px, rems, size, uniform_list, }; -use gpui_native_core::alert::{Alert, AlertSeverity, Alerts}; +use gpui_native_core::alert::{Alert, AlertSeverity, AlertStyle, Alerts}; use gpui_native_core::avatar::Avatar; use gpui_native_core::badge::{Badge, BadgeVariant}; use gpui_native_core::button::{Button, ButtonVariant}; @@ -2609,17 +2609,31 @@ impl Render for AlertDemo { v_flex() .gap_2() .child( - Alert::new("alert-demo-update") - .title("Update available") - .message("Version 2.4.0 is ready to install.") - .severity(AlertSeverity::Info), + Alert::new("alert-demo-sent") + .style(AlertStyle::Inline) + .severity(AlertSeverity::Success) + .icon(IconRole::Check) + .title("Message sent") + .message("Your message successfully sent to the recipient."), + ) + .child( + Alert::new("alert-demo-hold") + .style(AlertStyle::Inline) + .severity(AlertSeverity::Info) + .icon(IconRole::Info) + .title("Keep in mind") + .message( + "Your message is temporarily on hold, please take action.", + ), ) .when(!self.dismissed, |section| { section.child( Alert::new("alert-demo-sync") - .title("Sync failed") - .message("Will retry when you're back online.") + .style(AlertStyle::Inline) .severity(AlertSeverity::Danger) + .icon(IconRole::Warning) + .title("Something went wrong") + .message("Will retry when you're back online.") .on_dismiss(move |cx| { view.update(cx, |demo, cx| { demo.dismissed = true;