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
384
//! This crate provides a convenient interface for showing toast notifications with
//! the [egui](https://github.com/emilk/egui) library.
//!
//! For a complete example, see <https://github.com/urholaukkarinen/egui-toast/tree/main/examples>.
//!
//! # Usage
//!
//! To get started, create a `Toasts` instance in your rendering code and specify the anchor position and
//! direction for the notifications. Toast notifications will show up starting from the specified
//! anchor position and stack up in the specified direction.
//!
//! To add a toast, you can use one of the convenience methods for different [ToastKinds](ToastKind),
//! e.g. [`Toasts::info()`] for info notifications. You can also use [`Toasts::add()`] if you would like to specify the toast kind
//! manually.
//!
//! ```
//! # use std::time::Duration;
//! # use egui_toast::{Toasts, ToastKind, ToastOptions};
//! # egui_toast::__run_test_ui(|ui, ctx| {
//! let mut toasts = Toasts::new()
//!     .anchor((300.0, 300.0))
//!     .direction(egui::Direction::BottomUp)
//!     .align_to_end(true);
//!
//! toasts.info(ui, "Hello, World!", Duration::from_secs(5));
//! // or
//! toasts.info(ui, "Hello, World!", ToastOptions {
//!     show_icon: true,
//!     ..ToastOptions::with_duration(Duration::from_secs(5))
//! });
//! // or
//! toasts.add(ui, "Hello, World!", ToastKind::Info, Duration::from_secs(5));
//!
//! // Show all toasts
//! toasts.show(ctx);
//! # })
//! ```
//!
//! Look of the notifications can be fully customized by specifying a custom rendering function for a specific toast kind
//! with [`Toasts::custom_contents`]. [`ToastKind::Custom`] can be used if the default kinds are not sufficient.
//!
//! ```
//! # use std::time::Duration;
//! # use egui_toast::{Toast, ToastKind, ToastOptions, Toasts};
//! # egui_toast::__run_test_ui(|ui, ctx| {
//! const MY_CUSTOM_TOAST: u32 = 0;
//!
//! fn custom_toast_contents(ui: &mut egui::Ui, toast: &mut Toast) {
//!     egui::Frame::window(ui.style()).show(ui, |ui| {
//!         ui.label(toast.text.clone());
//!     });
//! }
//!
//! let mut toasts = Toasts::new()
//!     .custom_contents(MY_CUSTOM_TOAST, &custom_toast_contents)
//!     .custom_contents(ToastKind::Info, &|ui, toast| {
//!         ui.label(toast.text.clone());
//!     });
//!
//! // Add a custom toast that never expires
//! toasts.add(ui, "Hello, World", MY_CUSTOM_TOAST, ToastOptions::with_duration(None));
//!
//! # })
//! ```
//!
#![deny(clippy::all)]

use std::collections::HashMap;
use std::ops::Add;
use std::time::{Duration, Instant};

use egui::{
    Align, Area, Color32, Context, Direction, Id, Layout, Order, Pos2, Rect, RichText, Ui, Vec2,
    WidgetText,
};

pub const INFO_COLOR: Color32 = Color32::from_rgb(0, 155, 255);
pub const WARNING_COLOR: Color32 = Color32::from_rgb(255, 212, 0);
pub const ERROR_COLOR: Color32 = Color32::from_rgb(255, 32, 0);
pub const SUCCESS_COLOR: Color32 = Color32::from_rgb(0, 255, 32);

#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub enum ToastKind {
    Info,
    Warning,
    Error,
    Success,
    Custom(u32),
}

impl From<u32> for ToastKind {
    fn from(value: u32) -> ToastKind {
        ToastKind::Custom(value)
    }
}

#[derive(Clone)]
pub struct Toast {
    pub kind: ToastKind,
    pub text: WidgetText,
    pub options: ToastOptions,
}

#[derive(Copy, Clone)]
pub struct ToastOptions {
    /// This can be used to show or hide the toast type icon.
    pub show_icon: bool,
    /// If defined, the toast is removed when it expires.
    pub expires_at: Option<Instant>,
}

impl Default for ToastOptions {
    fn default() -> Self {
        Self {
            show_icon: true,
            expires_at: None,
        }
    }
}

impl From<Duration> for ToastOptions {
    fn from(duration: Duration) -> Self {
        ToastOptions::with_duration(duration)
    }
}

impl ToastOptions {
    pub fn with_duration(duration: impl Into<Option<Duration>>) -> Self {
        Self {
            expires_at: duration.into().map(|duration| Instant::now().add(duration)),
            ..Default::default()
        }
    }
}

impl Toast {
    pub fn close(&mut self) {
        self.options.expires_at = Some(Instant::now());
    }
}

pub struct Toasts<'a> {
    id: Id,
    anchor: Pos2,
    direction: Direction,
    align_to_end: bool,
    custom_toast_contents: HashMap<ToastKind, &'a dyn Fn(&mut Ui, &mut Toast)>,
}

impl<'a> Default for Toasts<'a> {
    fn default() -> Self {
        Self {
            id: Id::new("__toasts"),
            anchor: Pos2::new(0.0, 0.0),
            direction: Direction::TopDown,
            align_to_end: false,
            custom_toast_contents: HashMap::new(),
        }
    }
}

impl<'a> Toasts<'a> {
    pub fn new() -> Self {
        Self::default()
    }

    /// Starting position for the toasts
    pub fn anchor(mut self, anchor: impl Into<Pos2>) -> Self {
        self.anchor = anchor.into();
        self
    }

    /// Direction where the toasts stack up
    pub fn direction(mut self, direction: impl Into<Direction>) -> Self {
        self.direction = direction.into();
        self
    }

    /// Whether to align toasts to right/bottom depending on the direction
    pub fn align_to_end(mut self, align_to_end: bool) -> Self {
        self.align_to_end = align_to_end;
        self
    }

    /// Can be used to specify a custom rendering function for toasts for given kind
    pub fn custom_contents(
        mut self,
        kind: impl Into<ToastKind>,
        add_contents: &'a dyn Fn(&mut Ui, &mut Toast),
    ) -> Self {
        self.custom_toast_contents.insert(kind.into(), add_contents);
        self
    }

    /// Adds a new info toast
    pub fn info(
        &mut self,
        ui: &mut Ui,
        text: impl Into<WidgetText>,
        options: impl Into<ToastOptions>,
    ) -> &mut Self {
        self.add(ui, text, ToastKind::Info, options)
    }

    /// Adds a new warning toast
    pub fn warning(
        &mut self,
        ui: &mut Ui,
        text: impl Into<WidgetText>,
        options: impl Into<ToastOptions>,
    ) -> &mut Self {
        self.add(ui, text, ToastKind::Warning, options)
    }

    /// Adds a new error toast
    pub fn error(
        &mut self,
        ui: &mut Ui,
        text: impl Into<WidgetText>,
        options: impl Into<ToastOptions>,
    ) -> &mut Self {
        self.add(ui, text, ToastKind::Error, options)
    }

    /// Adds a new success toast
    pub fn success(
        &mut self,
        ui: &mut Ui,
        text: impl Into<WidgetText>,
        options: impl Into<ToastOptions>,
    ) -> &mut Self {
        self.add(ui, text, ToastKind::Success, options)
    }

    /// Adds a new toast
    pub fn add(
        &mut self,
        ui: &mut Ui,
        text: impl Into<WidgetText>,
        kind: impl Into<ToastKind>,
        options: impl Into<ToastOptions>,
    ) -> &mut Self {
        ui.ctx()
            .data()
            .get_persisted_mut_or_default::<Vec<Toast>>(self.id)
            .push(Toast {
                kind: kind.into(),
                text: text.into(),
                options: options.into(),
            });
        self
    }

    /// Shows and updates all toasts
    pub fn show(mut self, ctx: &Context) {
        Area::new("__toasts")
            .default_pos((0.0, 0.0))
            .order(Order::Background)
            .interactable(true)
            .movable(false)
            .show(ctx, |ui| {
                let now = Instant::now();

                let rect = match (self.direction, self.align_to_end) {
                    (Direction::LeftToRight | Direction::TopDown, false) => Rect::from_min_size(
                        self.anchor,
                        ui.available_size() - self.anchor.to_vec2(),
                    ),
                    (Direction::RightToLeft | Direction::BottomUp, true) => Rect::from_min_size(
                        Pos2::new(0.0, 0.0),
                        Vec2::new(self.anchor.x, self.anchor.y),
                    ),
                    (Direction::BottomUp, false) | (Direction::LeftToRight, true) => {
                        Rect::from_min_size(
                            Pos2::new(self.anchor.x, 0.0),
                            Vec2::new(ui.available_width() - self.anchor.x, self.anchor.y),
                        )
                    }
                    (Direction::RightToLeft, false) | (Direction::TopDown, true) => {
                        Rect::from_min_size(
                            Pos2::new(0.0, self.anchor.y),
                            Vec2::new(self.anchor.x, ui.available_height() - self.anchor.y),
                        )
                    }
                };

                let cross_align = if self.align_to_end {
                    Align::Max
                } else {
                    Align::Min
                };

                ui.allocate_ui_at_rect(rect, |ui| {
                    ui.with_layout(
                        Layout::from_main_dir_and_cross_align(self.direction, cross_align),
                        |ui| {
                            ui.spacing_mut().item_spacing = Vec2::splat(5.0);

                            let mut toasts: Vec<Toast> =
                                ui.ctx().data().get_persisted(self.id).unwrap_or_default();

                            for toast in toasts.iter_mut() {
                                if let Some(add_contents) =
                                    self.custom_toast_contents.get_mut(&toast.kind)
                                {
                                    add_contents(ui, toast);
                                } else {
                                    default_toast_contents(ui, toast);
                                }
                            }

                            toasts.retain(|toast| {
                                toast
                                    .options
                                    .expires_at
                                    .filter(|&expires_at| expires_at <= now)
                                    .is_none()
                            });

                            ui.ctx().data().insert_persisted(self.id, toasts);
                        },
                    );
                });
            });
    }
}

fn default_toast_contents(ui: &mut Ui, toast: &mut Toast) {
    egui::Frame::window(ui.style())
        .inner_margin(10.0)
        .show(ui, |ui| {
            ui.horizontal(|ui| {
                let (icon, color) = match toast.kind {
                    ToastKind::Warning => ("⚠", WARNING_COLOR),
                    ToastKind::Error => ("❗", ERROR_COLOR),
                    ToastKind::Success => ("✔", SUCCESS_COLOR),
                    _ => ("ℹ", INFO_COLOR),
                };

                let a = |ui: &mut Ui, toast: &mut Toast| {
                    if toast.options.show_icon {
                        ui.label(RichText::new(icon).color(color));
                    }
                };
                let b = |ui: &mut Ui, toast: &mut Toast| ui.label(toast.text.clone());
                let c = |ui: &mut Ui, toast: &mut Toast| {
                    if ui.button("🗙").clicked() {
                        toast.close();
                    }
                };

                // Draw the contents in the reverse order on right-to-left layouts
                // to keep the same look.
                if ui.layout().prefer_right_to_left() {
                    c(ui, toast);
                    b(ui, toast);
                    a(ui, toast);
                } else {
                    a(ui, toast);
                    b(ui, toast);
                    c(ui, toast);
                }
            });
        });
}

pub fn __run_test_ui(mut add_contents: impl FnMut(&mut Ui, &Context)) {
    let ctx = Context::default();
    let _ = ctx.run(Default::default(), |ctx| {
        egui::CentralPanel::default().show(ctx, |ui| {
            add_contents(ui, ctx);
        });
    });
}

pub fn __run_test_ui_with_toasts(mut add_contents: impl FnMut(&mut Ui, &mut Toasts)) {
    let ctx = Context::default();
    let _ = ctx.run(Default::default(), |ctx| {
        egui::CentralPanel::default().show(ctx, |ui| {
            let mut toasts = Toasts::new();
            add_contents(ui, &mut toasts);
        });
    });
}