1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#![warn(missing_docs)]
mod toast;
pub use toast::*;
mod anchor;
pub use anchor::*;
#[doc(hidden)]
pub use egui::__run_test_ctx;
use egui::{vec2, Color32, Context, FontId, Id, LayerId, Order, Rect, Rounding, Stroke, Vec2};
pub(crate) const TOAST_WIDTH: f32 = 180.;
pub(crate) const TOAST_HEIGHT: f32 = 34.;
const ERROR_COLOR: Color32 = Color32::from_rgb(200, 90, 90);
const INFO_COLOR: Color32 = Color32::from_rgb(150, 200, 210);
const WARNING_COLOR: Color32 = Color32::from_rgb(230, 220, 140);
const SUCCESS_COLOR: Color32 = Color32::from_rgb(140, 230, 140);
pub struct Toasts {
toasts: Vec<Toast>,
anchor: Anchor,
margin: Vec2,
spacing: f32,
padding: Vec2,
reverse: bool,
speed: f32,
held: bool,
}
impl Toasts {
pub const fn new() -> Self {
Self {
anchor: Anchor::TopRight,
margin: vec2(8., 8.),
toasts: vec![],
spacing: 8.,
padding: vec2(10., 10.),
held: false,
speed: 4.,
reverse: false,
}
}
pub fn add(&mut self, toast: Toast) -> &mut Toast {
if self.reverse {
self.toasts.insert(0, toast);
return self.toasts.get_mut(0).unwrap();
} else {
self.toasts.push(toast);
let l = self.toasts.len() - 1;
return self.toasts.get_mut(l).unwrap();
}
}
pub fn dismiss_oldest_toast(&mut self) {
if let Some(toast) = self.toasts.get_mut(0) {
toast.dismiss();
}
}
pub fn dismiss_latest_toast(&mut self) {
if let Some(toast) = self.toasts.last_mut() {
toast.dismiss();
}
}
pub fn dismiss_all_toasts(&mut self) {
for toast in self.toasts.iter_mut() {
toast.dismiss();
}
}
pub fn success(&mut self, caption: impl Into<String>) -> &mut Toast {
self.add(Toast::success(caption))
}
pub fn info(&mut self, caption: impl Into<String>) -> &mut Toast {
self.add(Toast::info(caption))
}
pub fn warning(&mut self, caption: impl Into<String>) -> &mut Toast {
self.add(Toast::warning(caption))
}
pub fn error(&mut self, caption: impl Into<String>) -> &mut Toast {
self.add(Toast::error(caption))
}
pub fn basic(&mut self, caption: impl Into<String>) -> &mut Toast {
self.add(Toast::basic(caption))
}
pub const fn reverse(mut self, reverse: bool) -> Self {
self.reverse = reverse;
self
}
pub const fn with_anchor(mut self, anchor: Anchor) -> Self {
self.anchor = anchor;
self
}
pub const fn with_spacing(mut self, spacing: f32) -> Self {
self.spacing = spacing;
self
}
pub const fn with_margin(mut self, margin: Vec2) -> Self {
self.margin = margin;
self
}
pub const fn with_padding(mut self, padding: Vec2) -> Self {
self.padding = padding;
self
}
}
impl Toasts {
pub fn show(&mut self, ctx: &Context) {
let Self {
anchor,
margin,
spacing,
padding,
toasts,
held,
speed,
..
} = self;
let mut pos = anchor.screen_corner(ctx.input(|i| i.screen_rect.max), *margin);
let p = ctx.layer_painter(LayerId::new(Order::Foreground, Id::new("toasts")));
let mut dismiss = None;
toasts.retain(|t| !t.state.disappeared());
toasts.iter_mut().for_each(|t| {
if let Some((_initial_d, current_d)) = t.duration {
if current_d <= 0. {
t.state = ToastState::Disapper
}
}
});
if ctx.input(|i| i.pointer.primary_released()) {
*held = false;
}
let visuals = ctx.style().visuals.widgets.noninteractive;
let mut update = false;
for (i, toast) in toasts.iter_mut().enumerate() {
if let Some((_, d)) = toast.duration.as_mut() {
if toast.state.idling() {
*d -= ctx.input(|i| i.stable_dt);
update = true;
}
}
let caption_galley = ctx.fonts(|f| {
f.layout(
toast.caption.clone(),
FontId::proportional(16.),
visuals.fg_stroke.color,
f32::INFINITY,
)
});
let (caption_width, caption_height) =
(caption_galley.rect.width(), caption_galley.rect.height());
let line_count = toast.caption.chars().filter(|c| *c == '\n').count() + 1;
let icon_width = caption_height / line_count as f32;
let icon_font = FontId::proportional(icon_width);
let icon_galley = if matches!(toast.level, ToastLevel::Info) {
Some(ctx.fonts(|f| f.layout("ℹ".into(), icon_font, INFO_COLOR, f32::INFINITY)))
} else if matches!(toast.level, ToastLevel::Warning) {
Some(ctx.fonts(|f| f.layout("⚠".into(), icon_font, WARNING_COLOR, f32::INFINITY)))
} else if matches!(toast.level, ToastLevel::Error) {
Some(ctx.fonts(|f| f.layout("!".into(), icon_font, ERROR_COLOR, f32::INFINITY)))
} else if matches!(toast.level, ToastLevel::Success) {
Some(ctx.fonts(|f| f.layout("✅".into(), icon_font, SUCCESS_COLOR, f32::INFINITY)))
} else {
None
};
let (action_width, action_height) = if let Some(icon_galley) = icon_galley.as_ref() {
(icon_galley.rect.width(), icon_galley.rect.height())
} else {
(0., 0.)
};
let cross_galley = if toast.closable {
let cross_fid = FontId::proportional(icon_width);
let cross_galley = ctx.fonts(|f| {
f.layout(
"❌".into(),
cross_fid,
visuals.fg_stroke.color,
f32::INFINITY,
)
});
Some(cross_galley)
} else {
None
};
let (cross_width, cross_height) = if let Some(cross_galley) = cross_galley.as_ref() {
(cross_galley.rect.width(), cross_galley.rect.height())
} else {
(0., 0.)
};
let icon_x_padding = (0., 7.);
let cross_x_padding = (7., 0.);
let icon_width_padded = if icon_width == 0. {
0.
} else {
icon_width + icon_x_padding.0 + icon_x_padding.1
};
let cross_width_padded = if cross_width == 0. {
0.
} else {
cross_width + cross_x_padding.0 + cross_x_padding.1
};
toast.width = icon_width_padded + caption_width + cross_width_padded + (padding.x * 2.);
toast.height = action_height.max(caption_height).max(cross_height) + padding.y * 2.;
let anim_offset = toast.width * (1. - ease_in_cubic(toast.value));
pos.x += anim_offset * anchor.anim_side();
let rect = toast.calc_anchored_rect(pos, *anchor);
pos.x -= anim_offset * anchor.anim_side();
p.rect_filled(rect, Rounding::same(4.), visuals.bg_fill);
if let Some((icon_galley, true)) =
icon_galley.zip(Some(toast.level != ToastLevel::None))
{
let oy = toast.height / 2. - action_height / 2.;
let ox = padding.x + icon_x_padding.0;
p.galley(rect.min + vec2(ox, oy), icon_galley);
}
let oy = toast.height / 2. - caption_height / 2.;
let o_from_icon = if action_width == 0. {
0.
} else {
action_width + icon_x_padding.1
};
let o_from_cross = if cross_width == 0. {
0.
} else {
cross_width + cross_x_padding.0
};
let ox = (toast.width / 2. - caption_width / 2.) + o_from_icon / 2. - o_from_cross / 2.;
p.galley(rect.min + vec2(ox, oy), caption_galley);
if let Some(cross_galley) = cross_galley {
let cross_rect = cross_galley.rect;
let oy = toast.height / 2. - cross_height / 2.;
let ox = toast.width - cross_width - cross_x_padding.1 - padding.x;
let cross_pos = rect.min + vec2(ox, oy);
p.galley(cross_pos, cross_galley);
let screen_cross = Rect {
max: cross_pos + cross_rect.max.to_vec2(),
min: cross_pos,
};
if let Some(pos) = ctx.input(|i| i.pointer.press_origin()) {
if screen_cross.contains(pos) && !*held {
dismiss = Some(i);
*held = true;
}
}
}
if let Some((initial, current)) = toast.duration {
if !toast.state.disappearing() {
p.line_segment(
[
rect.min + vec2(0., toast.height),
rect.max - vec2((1. - (current / initial)) * toast.width, 0.),
],
Stroke::new(4., visuals.fg_stroke.color),
);
}
}
toast.adjust_next_pos(&mut pos, *anchor, *spacing);
if toast.state.appearing() {
update = true;
toast.value += ctx.input(|i| i.stable_dt) * (*speed);
if toast.value >= 1. {
toast.value = 1.;
toast.state = ToastState::Idle;
}
} else if toast.state.disappearing() {
update = true;
toast.value -= ctx.input(|i| i.stable_dt) * (*speed);
if toast.value <= 0. {
toast.state = ToastState::Disappeared;
}
}
}
if update {
ctx.request_repaint();
}
if let Some(i) = dismiss {
self.toasts[i].dismiss();
}
}
}
impl Default for Toasts {
fn default() -> Self {
Self::new()
}
}
fn ease_in_cubic(x: f32) -> f32 {
1. - (1. - x).powi(3)
}