1use gpui_base::LogicalStyledExt as _;
2use std::rc::Rc;
3
4use gpui::{
5 App, ClickEvent, ElementId, Empty, Hsla, InteractiveElement, IntoElement, ParentElement as _,
6 RenderOnce, Role, SharedString, StatefulInteractiveElement, StyleRefinement, Styled, Window,
7 div, prelude::FluentBuilder as _, px, rems, transparent_white,
8};
9
10use crate::{
11 ActiveTheme as _, Colorize, Icon, IconName, Sizable, Size, StyledExt, h_flex,
12 text::{Text, TextViewStyle},
13};
14
15#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
17pub enum AlertVariant {
18 #[default]
19 Default,
20 Info,
21 Success,
22 Warning,
23 Error,
24}
25
26impl AlertVariant {
27 fn fg(&self, cx: &App) -> Hsla {
28 match self {
29 Self::Default => cx.theme().foreground,
30 Self::Info => cx.theme().info,
31 Self::Success => cx.theme().success,
32 Self::Warning => cx.theme().warning,
33 Self::Error => cx.theme().danger,
34 }
35 }
36
37 fn bg(&self, cx: &App) -> Hsla {
38 match self {
39 Self::Default => cx.theme().background,
40 Self::Info => cx.theme().info.mix_oklab(transparent_white(), 0.04),
41 Self::Success => cx.theme().success.mix_oklab(transparent_white(), 0.04),
42 Self::Warning => cx.theme().warning.mix_oklab(transparent_white(), 0.04),
43 Self::Error => cx.theme().danger.mix_oklab(transparent_white(), 0.04),
44 }
45 }
46
47 fn border_color(&self, cx: &App) -> Hsla {
48 match self {
49 Self::Default => cx.theme().border,
50 Self::Info => cx.theme().info.mix_oklab(transparent_white(), 0.3),
51 Self::Success => cx.theme().success.mix_oklab(transparent_white(), 0.3),
52 Self::Warning => cx.theme().warning.mix_oklab(transparent_white(), 0.3),
53 Self::Error => cx.theme().danger.mix_oklab(transparent_white(), 0.3),
54 }
55 }
56}
57
58#[derive(IntoElement)]
60pub struct Alert {
61 id: ElementId,
62 style: StyleRefinement,
63 variant: AlertVariant,
64 icon: Icon,
65 title: Option<SharedString>,
66 message: Text,
67 size: Size,
68 banner: bool,
69 on_close: Option<Rc<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>>,
70 visible: bool,
71}
72
73impl Alert {
74 pub fn new(id: impl Into<ElementId>, message: impl Into<Text>) -> Self {
76 Self {
77 id: id.into(),
78 style: StyleRefinement::default(),
79 variant: AlertVariant::default(),
80 icon: Icon::new(IconName::Info),
81 title: None,
82 message: message.into(),
83 size: Size::default(),
84 banner: false,
85 visible: true,
86 on_close: None,
87 }
88 }
89
90 pub fn info(id: impl Into<ElementId>, message: impl Into<Text>) -> Self {
92 Self::new(id, message)
93 .with_variant(AlertVariant::Info)
94 .icon(IconName::Info)
95 }
96
97 pub fn success(id: impl Into<ElementId>, message: impl Into<Text>) -> Self {
99 Self::new(id, message)
100 .with_variant(AlertVariant::Success)
101 .icon(IconName::CircleCheck)
102 }
103
104 pub fn warning(id: impl Into<ElementId>, message: impl Into<Text>) -> Self {
106 Self::new(id, message)
107 .with_variant(AlertVariant::Warning)
108 .icon(IconName::TriangleAlert)
109 }
110
111 pub fn error(id: impl Into<ElementId>, message: impl Into<Text>) -> Self {
113 Self::new(id, message)
114 .with_variant(AlertVariant::Error)
115 .icon(IconName::CircleX)
116 }
117
118 pub fn with_variant(mut self, variant: AlertVariant) -> Self {
120 self.variant = variant;
121 self
122 }
123
124 pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
126 self.icon = icon.into();
127 self
128 }
129
130 pub fn title(mut self, title: impl Into<SharedString>) -> Self {
132 self.title = Some(title.into());
133 self
134 }
135
136 pub fn banner(mut self) -> Self {
141 self.banner = true;
142 self
143 }
144
145 pub fn visible(mut self, visible: bool) -> Self {
147 self.visible = visible;
148 self
149 }
150
151 pub fn on_close(
153 mut self,
154 on_close: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
155 ) -> Self {
156 self.on_close = Some(Rc::new(on_close));
157 self
158 }
159}
160
161impl Sizable for Alert {
162 fn with_size(mut self, size: impl Into<Size>) -> Self {
163 self.size = size.into();
164 self
165 }
166}
167
168impl Styled for Alert {
169 fn style(&mut self) -> &mut gpui::StyleRefinement {
170 &mut self.style
171 }
172}
173
174impl RenderOnce for Alert {
175 fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
176 if !self.visible {
177 return Empty.into_any_element();
178 }
179
180 let (radius, padding_x, padding_y, gap) = match self.size {
181 Size::XSmall => (cx.theme().radius, px(12.), px(6.), px(6.)),
182 Size::Small => (cx.theme().radius, px(12.), px(8.), px(6.)),
183 Size::Large => (cx.theme().radius_lg, px(20.), px(14.), px(12.)),
184 _ => (cx.theme().radius, px(16.), px(10.), px(12.)),
185 };
186
187 let bg = self.variant.bg(cx);
188 let fg = self.variant.fg(cx);
189 let border_color = self.variant.border_color(cx);
190
191 h_flex()
192 .flex_row_dir(cx)
193 .id(self.id)
194 .role(Role::Alert)
195 .w_full()
196 .text_color(fg)
197 .bg(bg)
198 .px(padding_x)
199 .py(padding_y)
200 .gap(gap)
201 .justify_between()
202 .text_sm()
203 .border_1()
204 .border_color(border_color)
205 .when(!self.banner, |this| this.rounded(radius).items_start())
206 .refine_style(&self.style)
207 .child(
208 div()
209 .flex_row_dir(cx)
210 .flex_1()
211 .when(self.banner, |this| this.items_center())
212 .overflow_hidden()
213 .gap(gap)
214 .child(
215 div()
216 .when(!self.banner, |this| this.mt(px(5.)))
217 .child(self.icon),
218 )
219 .child(
220 div()
221 .flex_1()
222 .overflow_hidden()
223 .gap_3()
224 .when(!self.banner, |this| {
225 this.when_some(self.title, |this, title| {
226 this.child(
227 div().w_full().truncate().font_semibold().child(title),
228 )
229 })
230 })
231 .child(
232 self.message
233 .style(TextViewStyle::default().paragraph_gap(rems(0.2))),
234 ),
235 ),
236 )
237 .when_some(self.on_close, |this, on_close| {
238 this.child(
239 div()
240 .id("close")
241 .p_0p5()
242 .rounded(cx.theme().radius)
243 .hover(|this| this.bg(bg.opacity(0.8)))
244 .active(|this| this.bg(bg.opacity(0.9)))
245 .on_click(move |ev, window, cx| {
246 on_close(ev, window, cx);
247 })
248 .child(
249 Icon::new(IconName::Close)
250 .with_size(self.size.max(Size::Medium))
251 .flex_shrink_0(),
252 ),
253 )
254 })
255 .into_any_element()
256 }
257}