1use iced::widget::{column, container, row, text};
4use iced::{Background, Border, Color, Element, Length, Shadow};
5
6use crate::icon::{Glyph, themed};
7use crate::theme::{Tokens, mix, radius, space, text_size};
8
9const ICON_SIZE: f32 = 16.0;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
13pub enum Variant {
14 #[default]
15 Info,
16 Success,
17 Warning,
18 Destructive,
19}
20
21impl Variant {
22 pub const ALL: [Variant; 4] = [
23 Variant::Info,
24 Variant::Success,
25 Variant::Warning,
26 Variant::Destructive,
27 ];
28
29 pub const fn glyph(self) -> Glyph {
31 match self {
32 Variant::Info => crate::lucide!(Info),
33 Variant::Success => crate::lucide!(CircleCheck),
34 Variant::Warning => crate::lucide!(TriangleAlert),
35 Variant::Destructive => crate::lucide!(CircleAlert),
36 }
37 }
38}
39
40#[derive(Debug)]
42pub struct Alert<'a> {
43 title: text::Fragment<'a>,
44 description: Option<text::Fragment<'a>>,
45 icon: Option<Glyph>,
46 variant: Variant,
47 width: Length,
48}
49
50pub fn alert<'a>(title: impl text::IntoFragment<'a>) -> Alert<'a> {
52 Alert {
53 title: title.into_fragment(),
54 description: None,
55 icon: None,
56 variant: Variant::default(),
57 width: Length::Fill,
58 }
59}
60
61impl<'a> Alert<'a> {
62 pub fn variant(mut self, variant: Variant) -> Self {
63 self.variant = variant;
64 self
65 }
66
67 pub fn description(mut self, description: impl text::IntoFragment<'a>) -> Self {
69 self.description = Some(description.into_fragment());
70 self
71 }
72
73 pub fn icon(mut self, glyph: Glyph) -> Self {
75 self.icon = Some(glyph);
76 self
77 }
78
79 pub fn width(mut self, width: impl Into<Length>) -> Self {
81 self.width = width.into();
82 self
83 }
84
85 pub fn glyph(&self) -> Glyph {
87 self.icon.unwrap_or(self.variant.glyph())
88 }
89}
90
91impl<'a, Message: 'a> From<Alert<'a>> for Element<'a, Message> {
92 fn from(alert: Alert<'a>) -> Self {
93 let variant = alert.variant;
94 let glyph = alert.glyph();
95 #[cfg(feature = "icedwright")]
96 let (name, about) = (alert.title.clone(), alert.description.clone());
97
98 let icon = themed(glyph, ICON_SIZE, 1.0, move |theme| {
99 colours(&Tokens::of(theme), variant).accent
100 });
101
102 let title = text(alert.title)
103 .size(text_size::SM)
104 .style(move |theme| text::Style {
105 color: Some(colours(&Tokens::of(theme), variant).title),
106 });
107
108 let mut body = column![title].spacing(space::XS).width(Length::Fill);
109 if let Some(description) = alert.description {
110 body =
111 body.push(
112 text(description)
113 .size(text_size::SM)
114 .style(move |theme| text::Style {
115 color: Some(colours(&Tokens::of(theme), variant).description),
116 }),
117 );
118 }
119
120 let element: Element<'a, Message> =
121 container(row![container(icon).padding([2, 0]), body].spacing(space::MD))
122 .padding([space::MD, space::LG])
123 .width(alert.width)
124 .style(move |theme| style(&Tokens::of(theme), variant))
125 .into();
126 #[cfg(feature = "icedwright")]
127 let element = {
128 let mut annotated = icedwright_probe::semantic(element)
129 .role(icedwright_probe::role::Alert)
130 .name(name)
131 .component("iced_cube::alert");
132 if let Some(about) = about {
133 annotated = annotated.description(about);
134 }
135 annotated.into()
136 };
137 element
138 }
139}
140
141#[derive(Debug, Clone, Copy, PartialEq)]
143pub struct Colours {
144 pub background: Color,
145 pub border: Color,
146 pub accent: Color,
148 pub title: Color,
149 pub description: Color,
150}
151
152pub fn colours(tokens: &Tokens, variant: Variant) -> Colours {
154 let accent = match variant {
155 Variant::Info => tokens.foreground,
156 Variant::Success => tokens.success,
157 Variant::Warning => tokens.warning,
158 Variant::Destructive => tokens.destructive,
159 };
160 let tint = if tokens.is_dark { 0.12 } else { 0.06 };
161 let edge = if tokens.is_dark { 0.35 } else { 0.3 };
162 let (background, border) = match variant {
163 Variant::Info => (tokens.background, tokens.border),
164 _ => (
165 mix(tokens.background, accent, tint),
166 mix(tokens.background, accent, edge),
167 ),
168 };
169
170 Colours {
171 background,
172 border,
173 accent,
174 title: mix(accent, tokens.foreground, 0.25),
175 description: match variant {
176 Variant::Info => tokens.muted_foreground,
177 _ => mix(accent, tokens.foreground, 0.55),
178 },
179 }
180}
181
182pub fn style(tokens: &Tokens, variant: Variant) -> container::Style {
184 let colours = colours(tokens, variant);
185 container::Style {
186 text_color: Some(colours.title),
187 background: Some(Background::Color(colours.background)),
188 border: Border {
189 color: colours.border,
190 width: 1.0,
191 radius: radius::LG.into(),
192 },
193 shadow: Shadow::default(),
194 snap: true,
195 }
196}
197
198#[cfg(test)]
199mod tests {
200 use super::*;
201 use crate::theme::{dark, light};
202
203 #[test]
204 fn default_builder_is_info_fill_width_with_variant_icon() {
205 let a = alert("Heads up");
206 assert_eq!(a.variant, Variant::Info);
207 assert_eq!(a.width, Length::Fill);
208 assert!(a.description.is_none());
209 assert_eq!(a.glyph().name(), "info");
210 }
211
212 #[test]
213 fn each_variant_has_its_own_icon() {
214 let mut names: Vec<_> = Variant::ALL.map(|v| v.glyph().name()).to_vec();
215 names.sort_unstable();
216 names.dedup();
217 assert_eq!(names.len(), Variant::ALL.len());
218 }
219
220 #[test]
221 fn custom_icon_overrides_variant_icon() {
222 let a = alert("Saved")
223 .variant(Variant::Success)
224 .icon(crate::lucide!(Save));
225 assert_eq!(a.glyph().name(), "save");
226 }
227
228 #[test]
229 fn status_variants_tint_the_surface() {
230 for theme in [light(), dark()] {
231 let tokens = Tokens::of(&theme);
232 for variant in [Variant::Success, Variant::Warning, Variant::Destructive] {
233 let colours = colours(&tokens, variant);
234 assert_ne!(colours.background, tokens.background, "{variant:?}");
235 assert_ne!(colours.border, tokens.border, "{variant:?}");
236 }
237 }
238 }
239
240 #[test]
241 fn info_uses_neutral_surface() {
242 for theme in [light(), dark()] {
243 let tokens = Tokens::of(&theme);
244 let colours = colours(&tokens, Variant::Info);
245 assert_eq!(colours.background, tokens.background);
246 assert_eq!(colours.accent, tokens.foreground);
247 }
248 }
249
250 #[test]
251 fn text_is_distinct_from_surface_in_every_variant() {
252 for theme in [light(), dark()] {
253 let tokens = Tokens::of(&theme);
254 for variant in Variant::ALL {
255 let colours = colours(&tokens, variant);
256 assert_ne!(colours.title, colours.background, "{variant:?}");
257 assert_ne!(colours.description, colours.background, "{variant:?}");
258 assert_eq!(style(&tokens, variant).border.width, 1.0);
259 }
260 }
261 }
262}