1use crate::*;
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4pub enum AlertSeverity {
5 Success,
6 Info,
7 Warning,
8 Error,
9}
10
11#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
13pub enum AlertVariant {
14 #[default]
15 Standard,
16 Filled,
17 Outlined,
18}
19
20#[derive(Clone, Copy, Debug)]
22pub struct AlertOptions<'a> {
23 pub variant: AlertVariant,
24 pub action_label: Option<&'a str>,
25 pub dismissible: bool,
26}
27
28impl Default for AlertOptions<'_> {
29 fn default() -> Self {
30 Self {
31 variant: AlertVariant::Standard,
32 action_label: None,
33 dismissible: true,
34 }
35 }
36}
37
38impl Frame<'_> {
39 pub fn alert(&mut self, id: u64, message: &str, severity: AlertSeverity) -> Response {
41 self.alert_with_options(id, message, severity, AlertOptions::default()).0
42 }
43
44 pub fn alert_with_options(
48 &mut self,
49 id: u64,
50 message: &str,
51 severity: AlertSeverity,
52 options: AlertOptions<'_>,
53 ) -> (Response, bool) {
54 let b = self.reserve(48.0);
55 let tone = match severity {
56 AlertSeverity::Success => Color(46, 125, 50, 255),
57 AlertSeverity::Info => Color(2, 136, 209, 255),
58 AlertSeverity::Warning => Color(237, 108, 2, 255),
59 AlertSeverity::Error => Color(211, 47, 47, 255),
60 };
61 let (icon_fg, text_fg) = match options.variant {
62 AlertVariant::Standard => {
63 self.paint_rect(b, 4.0, tone.opacity(0.10));
64 (tone, self.ui.theme.text)
65 }
66 AlertVariant::Filled => {
67 self.paint_rect(b, 4.0, tone);
68 let on_tone = Color(255, 255, 255, 255);
69 (on_tone, on_tone)
70 }
71 AlertVariant::Outlined => {
72 self.paint_rect(b, 4.0, self.ui.theme.background);
73 self.commands.push(DrawCommand::Outline {
74 bounds: b,
75 radius: 4.0,
76 width: 1.0,
77 color: tone,
78 });
79 (tone, self.ui.theme.text)
80 }
81 };
82 self.styled_text(
83 Rect { x: b.x + 16.0, width: 24.0, ..b },
84 match severity {
85 AlertSeverity::Success => "✓",
86 AlertSeverity::Info => "i",
87 AlertSeverity::Warning => "!",
88 AlertSeverity::Error => "×",
89 },
90 icon_fg,
91 TextStyle { size: 20.0, align: 1, ..TextStyle::default() },
92 );
93
94 let mut trailing_w = 12.0;
95 let mut dismiss_resp = Response { id, bounds: b, enabled: self.enabled, ..Response::default() };
96 if options.dismissible {
97 let button = Rect {
98 x: b.x + (b.width - 40.0).max(0.0),
99 y: b.y + 4.0,
100 width: 40.0,
101 height: 40.0,
102 };
103 dismiss_resp = self.target(id, button, "Dismiss alert", true, Role::Button);
104 self.state_layer(dismiss_resp, 20.0, text_fg);
105 self.styled_text(
106 button,
107 "×",
108 text_fg,
109 TextStyle { size: 20.0, align: 1, ..TextStyle::default() },
110 );
111 trailing_w = 48.0;
112 }
113 let mut action_clicked = false;
114 if let Some(action) = options.action_label.filter(|s| !s.is_empty()) {
115 let act_w = (self.text_width(action, TextStyle::LABEL) + 20.0).clamp(52.0, (b.width * 0.4).max(52.0));
116 let act_rect = Rect {
117 x: (b.x + b.width - trailing_w - act_w).max(b.x + 52.0),
118 y: b.y + 6.0,
119 width: act_w,
120 height: 36.0,
121 };
122 let ar = self.target(id.wrapping_add(1), act_rect, action, true, Role::Button);
123 self.state_layer(ar, 18.0, text_fg);
124 self.focus_ring(ar);
125 self.styled_text(act_rect, action, if options.variant == AlertVariant::Filled { text_fg } else { tone }, TextStyle::LABEL);
126 action_clicked = ar.clicked;
127 trailing_w += act_w + 8.0;
128 }
129 self.styled_text(
130 Rect { x: b.x + 52.0, width: (b.width - 52.0 - trailing_w).max(0.0), ..b },
131 message,
132 text_fg,
133 TextStyle { size: 14.0, ..TextStyle::default() },
134 );
135 (dismiss_resp, action_clicked)
136 }
137}
138
139#[cfg(test)]
140#[path = "alert.spec.rs"]
141mod tests;