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
use crate::{Anchor, TOAST_HEIGHT, TOAST_WIDTH};
use egui::{pos2, vec2, Pos2, Rect};
use std::{fmt::Debug, time::Duration};
#[derive(Debug, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum ToastLevel {
Info,
Warning,
Error,
Success,
None,
}
impl Default for ToastLevel {
fn default() -> Self {
ToastLevel::Info
}
}
#[derive(Debug)]
pub(crate) enum ToastState {
Appear,
Disapper,
Disappeared,
Idle,
}
impl ToastState {
pub fn appearing(&self) -> bool {
matches!(self, Self::Appear)
}
pub fn disappearing(&self) -> bool {
matches!(self, Self::Disapper)
}
pub fn disappeared(&self) -> bool {
matches!(self, Self::Disappeared)
}
pub fn idling(&self) -> bool {
matches!(self, Self::Idle)
}
}
pub struct ToastOptions {
duration: Option<Duration>,
level: ToastLevel,
closable: bool,
}
#[derive(Debug)]
pub struct Toast {
pub(crate) level: ToastLevel,
pub(crate) caption: String,
pub(crate) duration: Option<(f32, f32)>,
pub(crate) height: f32,
pub(crate) width: f32,
pub(crate) closable: bool,
pub(crate) state: ToastState,
pub(crate) value: f32,
}
impl Default for ToastOptions {
fn default() -> Self {
Self {
duration: Some(Duration::from_millis(3500)),
level: ToastLevel::None,
closable: true,
}
}
}
fn duration_to_seconds_f32(duration: Duration) -> f32 {
duration.as_nanos() as f32 * 1e-9
}
impl Toast {
fn new(caption: impl Into<String>, options: ToastOptions) -> Self {
Self {
caption: caption.into(),
height: TOAST_HEIGHT,
width: TOAST_WIDTH,
duration: if let Some(dur) = options.duration {
let max_dur = duration_to_seconds_f32(dur);
Some((max_dur, max_dur))
} else {
None
},
closable: options.closable,
level: options.level,
value: 0.,
state: ToastState::Appear,
}
}
pub fn basic(caption: impl Into<String>) -> Self {
Self::new(caption, ToastOptions::default())
}
pub fn success(caption: impl Into<String>) -> Self {
Self::new(
caption,
ToastOptions {
level: ToastLevel::Success,
..ToastOptions::default()
},
)
}
pub fn info(caption: impl Into<String>) -> Self {
Self::new(
caption,
ToastOptions {
level: ToastLevel::Info,
..ToastOptions::default()
},
)
}
pub fn warning(caption: impl Into<String>) -> Self {
Self::new(
caption,
ToastOptions {
level: ToastLevel::Warning,
..ToastOptions::default()
},
)
}
pub fn error(caption: impl Into<String>) -> Self {
Self::new(
caption,
ToastOptions {
closable: false,
level: ToastLevel::Error,
..ToastOptions::default()
},
)
}
pub fn set_options(&mut self, options: ToastOptions) -> &mut Self {
self.set_closable(options.closable);
self.set_duration(options.duration);
self.set_level(options.level);
self
}
pub fn set_level(&mut self, level: ToastLevel) -> &mut Self {
self.level = level;
self
}
pub fn set_closable(&mut self, closable: bool) -> &mut Self {
self.closable = closable;
self
}
pub fn set_duration(&mut self, duration: Option<Duration>) -> &mut Self {
if let Some(duration) = duration {
let max_dur = duration_to_seconds_f32(duration);
self.duration = Some((max_dur, max_dur));
} else {
self.duration = None;
}
self
}
pub fn set_height(&mut self, height: f32) -> &mut Self {
self.height = height;
self
}
pub fn set_width(&mut self, width: f32) -> &mut Self {
self.width = width;
self
}
pub fn dismiss(&mut self) {
self.state = ToastState::Disapper;
}
pub(crate) fn calc_anchored_rect(&self, pos: Pos2, anchor: Anchor) -> Rect {
match anchor {
Anchor::TopRight => Rect {
min: pos2(pos.x - self.width, pos.y),
max: pos2(pos.x, pos.y + self.height),
},
Anchor::TopLeft => Rect {
min: pos,
max: pos + vec2(self.width, self.height),
},
Anchor::BottomRight => Rect {
min: pos - vec2(self.width, self.height),
max: pos,
},
Anchor::BottomLeft => Rect {
min: pos2(pos.x, pos.y - self.height),
max: pos2(pos.x + self.width, pos.y),
},
}
}
pub(crate) fn adjust_next_pos(&self, pos: &mut Pos2, anchor: Anchor, spacing: f32) {
match anchor {
Anchor::TopRight | Anchor::TopLeft => pos.y += self.height + spacing,
Anchor::BottomRight | Anchor::BottomLeft => pos.y -= self.height + spacing,
}
}
}