alert: add inline style with severity accent bar
This commit is contained in:
+98
-58
@@ -6,7 +6,7 @@ use gpui::{
|
|||||||
IntoElement, ParentElement as _, RenderOnce, Role, SharedString,
|
IntoElement, ParentElement as _, RenderOnce, Role, SharedString,
|
||||||
StatefulInteractiveElement as _, Styled as _, Window, deferred, div, ease_out_quint,
|
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_theme::{ActiveTheme as _, ENTER_SLIDE_DISTANCE, Size, TextRole, Typeset as _};
|
||||||
use gpui_native_utils::ResultLogErr as _;
|
use gpui_native_utils::ResultLogErr as _;
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ use crate::common::Sizable as _;
|
|||||||
use crate::common::TOAST_LAYER;
|
use crate::common::TOAST_LAYER;
|
||||||
use crate::common::v_flex;
|
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)]
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||||
pub enum AlertSeverity {
|
pub enum AlertSeverity {
|
||||||
/// The default; uses the info role.
|
/// The default; uses the info role.
|
||||||
@@ -30,6 +30,16 @@ pub enum AlertSeverity {
|
|||||||
Danger,
|
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.
|
/// A static callout banner.
|
||||||
#[derive(Clone, IntoElement)]
|
#[derive(Clone, IntoElement)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
@@ -38,8 +48,8 @@ pub struct Alert {
|
|||||||
title: Option<SharedString>,
|
title: Option<SharedString>,
|
||||||
message: Option<SharedString>,
|
message: Option<SharedString>,
|
||||||
severity: AlertSeverity,
|
severity: AlertSeverity,
|
||||||
|
style: AlertStyle,
|
||||||
icon: Option<Icon>,
|
icon: Option<Icon>,
|
||||||
show_icon: bool,
|
|
||||||
on_dismiss: Option<DismissHandler>,
|
on_dismiss: Option<DismissHandler>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,8 +63,8 @@ impl Alert {
|
|||||||
title: None,
|
title: None,
|
||||||
message: None,
|
message: None,
|
||||||
severity: AlertSeverity::Info,
|
severity: AlertSeverity::Info,
|
||||||
|
style: AlertStyle::default(),
|
||||||
icon: None,
|
icon: None,
|
||||||
show_icon: true,
|
|
||||||
on_dismiss: None,
|
on_dismiss: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -83,7 +93,13 @@ impl Alert {
|
|||||||
self
|
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<I>(mut self, icon: I) -> Self
|
pub fn icon<I>(mut self, icon: I) -> Self
|
||||||
where
|
where
|
||||||
I: Into<Icon>,
|
I: Into<Icon>,
|
||||||
@@ -92,12 +108,6 @@ impl Alert {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Hides the icon entirely.
|
|
||||||
pub fn no_icon(mut self) -> Self {
|
|
||||||
self.show_icon = false;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Runs when the user dismisses the alert.
|
/// Runs when the user dismisses the alert.
|
||||||
pub fn on_dismiss<F>(mut self, handler: F) -> Self
|
pub fn on_dismiss<F>(mut self, handler: F) -> Self
|
||||||
where
|
where
|
||||||
@@ -124,55 +134,15 @@ impl RenderOnce for Alert {
|
|||||||
AlertSeverity::Danger => theme.ramps.destructive,
|
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 foreground = theme.colors.foreground;
|
||||||
let muted_foreground = theme.colors.muted_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 metrics = theme.sizes.resolve(Size::Medium);
|
||||||
let icon_size = metrics.icon_size;
|
let icon_size = metrics.icon_size;
|
||||||
|
let dense_icon_size = metrics.dense_icon_size;
|
||||||
let caption_size = metrics.caption_size;
|
let caption_size = metrics.caption_size;
|
||||||
|
|
||||||
div()
|
let content = v_flex()
|
||||||
.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| {
|
|
||||||
alert.child(
|
|
||||||
div()
|
|
||||||
.flex_none()
|
|
||||||
.flex()
|
|
||||||
.items_center()
|
|
||||||
.h(window.line_height())
|
|
||||||
.child(icon_glyph.size(icon_size).color(ramp.base)),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.child(
|
|
||||||
v_flex()
|
|
||||||
.flex_1()
|
.flex_1()
|
||||||
.gap_0p5()
|
.gap_0p5()
|
||||||
.when_some(self.title.clone(), |alert, title| {
|
.when_some(self.title.clone(), |alert, title| {
|
||||||
@@ -195,18 +165,88 @@ impl RenderOnce for Alert {
|
|||||||
.text_color(color)
|
.text_color(color)
|
||||||
.child(message),
|
.child(message),
|
||||||
)
|
)
|
||||||
}),
|
});
|
||||||
)
|
|
||||||
.when_some(self.on_dismiss, |alert, on_dismiss| {
|
let dismiss = self.on_dismiss.clone().map(|on_dismiss| {
|
||||||
alert.child(
|
|
||||||
Button::new((self.id.clone(), "alert-close"))
|
Button::new((self.id.clone(), "alert-close"))
|
||||||
.icon(IconRole::Close)
|
.icon(IconRole::Close)
|
||||||
.variant(ButtonVariant::Ghost)
|
.variant(ButtonVariant::Ghost)
|
||||||
.small()
|
.small()
|
||||||
.accessibility_label("Dismiss")
|
.accessibility_label("Dismiss")
|
||||||
.on_click(move |_, _window, cx| on_dismiss(cx)),
|
.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()
|
||||||
|
.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)
|
||||||
|
.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))
|
||||||
|
}),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ use gpui::{
|
|||||||
SharedString, Styled as _, UniformListScrollHandle, WeakEntity, Window, WindowBounds,
|
SharedString, Styled as _, UniformListScrollHandle, WeakEntity, Window, WindowBounds,
|
||||||
WindowOptions, actions, div, list, px, rems, size, uniform_list,
|
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::avatar::Avatar;
|
||||||
use gpui_native_core::badge::{Badge, BadgeVariant};
|
use gpui_native_core::badge::{Badge, BadgeVariant};
|
||||||
use gpui_native_core::button::{Button, ButtonVariant};
|
use gpui_native_core::button::{Button, ButtonVariant};
|
||||||
@@ -2609,17 +2609,31 @@ impl Render for AlertDemo {
|
|||||||
v_flex()
|
v_flex()
|
||||||
.gap_2()
|
.gap_2()
|
||||||
.child(
|
.child(
|
||||||
Alert::new("alert-demo-update")
|
Alert::new("alert-demo-sent")
|
||||||
.title("Update available")
|
.style(AlertStyle::Inline)
|
||||||
.message("Version 2.4.0 is ready to install.")
|
.severity(AlertSeverity::Success)
|
||||||
.severity(AlertSeverity::Info),
|
.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| {
|
.when(!self.dismissed, |section| {
|
||||||
section.child(
|
section.child(
|
||||||
Alert::new("alert-demo-sync")
|
Alert::new("alert-demo-sync")
|
||||||
.title("Sync failed")
|
.style(AlertStyle::Inline)
|
||||||
.message("Will retry when you're back online.")
|
|
||||||
.severity(AlertSeverity::Danger)
|
.severity(AlertSeverity::Danger)
|
||||||
|
.icon(IconRole::Warning)
|
||||||
|
.title("Something went wrong")
|
||||||
|
.message("Will retry when you're back online.")
|
||||||
.on_dismiss(move |cx| {
|
.on_dismiss(move |cx| {
|
||||||
view.update(cx, |demo, cx| {
|
view.update(cx, |demo, cx| {
|
||||||
demo.dismissed = true;
|
demo.dismissed = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user