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
//! A toast notification system for egui, roughly based on <https://github.com/urholaukkarinen/egui-toast>.

use std::collections::HashMap;

use egui::Color32;

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

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

#[derive(Clone)]
pub struct Toast {
    pub kind: ToastKind,
    pub text: String,
    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,

    /// Time to live in seconds.
    pub ttl_sec: f64,
}

impl ToastOptions {
    pub fn with_ttl_in_seconds(ttl_sec: f64) -> Self {
        Self {
            show_icon: true,
            ttl_sec,
        }
    }
}

impl Toast {
    pub fn close(&mut self) {
        self.options.ttl_sec = 0.0;
    }
}

pub type ToastContents = dyn Fn(&mut egui::Ui, &mut Toast) -> egui::Response;

pub struct Toasts {
    id: egui::Id,
    custom_toast_contents: HashMap<ToastKind, Box<ToastContents>>,
    toasts: Vec<Toast>,
}

impl Default for Toasts {
    fn default() -> Self {
        Self::new()
    }
}

impl Toasts {
    pub fn new() -> Self {
        Self {
            id: egui::Id::new("__toasts"),
            custom_toast_contents: Default::default(),
            toasts: Vec::new(),
        }
    }

    /// Adds a new toast
    pub fn add(&mut self, toast: Toast) -> &mut Self {
        self.toasts.push(toast);
        self
    }

    /// Shows and updates all toasts
    pub fn show(&mut self, egui_ctx: &egui::Context) {
        let Self {
            id,
            custom_toast_contents,
            toasts,
        } = self;

        let dt = egui_ctx.input(|i| i.unstable_dt) as f64;

        toasts.retain(|toast| 0.0 < toast.options.ttl_sec);

        let mut offset = egui::vec2(-8.0, 8.0);

        for (i, toast) in toasts.iter_mut().enumerate() {
            let response = egui::Area::new(id.with(i))
                .anchor(egui::Align2::RIGHT_TOP, offset)
                .order(egui::Order::Foreground)
                .interactable(true)
                .movable(false)
                .show(egui_ctx, |ui| {
                    if let Some(add_contents) = custom_toast_contents.get_mut(&toast.kind) {
                        add_contents(ui, toast);
                    } else {
                        default_toast_contents(ui, toast);
                    };
                })
                .response;

            let response = response.on_hover_text("Click to close and copy contents");

            if !response.hovered() {
                toast.options.ttl_sec -= dt;
                if toast.options.ttl_sec.is_finite() {
                    egui_ctx.request_repaint_after(std::time::Duration::from_secs_f64(
                        toast.options.ttl_sec.max(0.0),
                    ));
                }
            }

            if response.clicked() {
                egui_ctx.output_mut(|o| o.copied_text = toast.text.clone());
                toast.close();
            }

            offset.y += response.rect.height() + 8.0;
        }
    }
}

fn default_toast_contents(ui: &mut egui::Ui, toast: &Toast) -> egui::Response {
    egui::Frame::window(ui.style())
        .inner_margin(10.0)
        .show(ui, |ui| {
            ui.horizontal(|ui| {
                ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Wrap);
                ui.set_max_width(400.0);
                ui.spacing_mut().item_spacing = egui::Vec2::splat(5.0);

                if toast.options.show_icon {
                    let (icon, icon_color) = match toast.kind {
                        ToastKind::Warning => ("⚠", ui.style().visuals.warn_fg_color),
                        ToastKind::Error => ("❗", ui.style().visuals.error_fg_color),
                        ToastKind::Success => ("✔", SUCCESS_COLOR),
                        _ => ("ℹ", INFO_COLOR),
                    };
                    ui.label(egui::RichText::new(icon).color(icon_color));
                }
                ui.label(toast.text.clone());
            })
        })
        .response
}