alert: add inline style with severity accent bar

This commit is contained in:
2026-11-16 10:18:47 +07:00
parent 1df87c65a1
commit 573d8a8303
2 changed files with 139 additions and 85 deletions
+118 -78
View File
@@ -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<SharedString>,
message: Option<SharedString>,
severity: AlertSeverity,
style: AlertStyle,
icon: Option<Icon>,
show_icon: bool,
on_dismiss: Option<DismissHandler>,
}
@@ -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<I>(mut self, icon: I) -> Self
where
I: Into<Icon>,
@@ -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<F>(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))
}),
}
}
}