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
use std::time::Duration;
use crate::{Color, Context};
/// A transient notification stacked in the bottom-right corner of the viewport.
///
/// Toasts are passive overlays: they paint above windows, never take focus or intercept
/// the pointer, appear with a short fade and slide, and fade out when their time is up.
/// Queue one from anywhere with [`Context::toast`] (for example when a button was
/// clicked); the context keeps and paints it for its lifetime. Unset colors come from the
/// current style: window fill, hairline border, text and muted text.
///
/// ```
/// # let mut context = zaxis::Context::new();
/// context.toast(zaxis::Toast::new("Configurations").content("Saved aim.cfg"));
/// assert_eq!(context.toast_count(), 1);
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct Toast {
pub(crate) title: String,
pub(crate) content: String,
pub(crate) duration: Duration,
pub(crate) width: f32,
pub(crate) corner_radius: f32,
pub(crate) fill: Option<Color>,
pub(crate) border: Option<Color>,
pub(crate) title_color: Option<Color>,
pub(crate) content_color: Option<Color>,
pub(crate) title_size: f32,
pub(crate) content_size: f32,
}
impl Toast {
pub fn new(title: impl Into<String>) -> Self {
Self {
title: title.into(),
content: String::new(),
duration: Duration::from_secs(4),
width: 240.0,
corner_radius: 8.0,
fill: None,
border: None,
title_color: None,
content_color: None,
title_size: 16.0,
content_size: 15.0,
}
}
/// Body text below the title; wraps at the toast width.
pub fn content(mut self, content: impl Into<String>) -> Self {
self.content = content.into();
self
}
/// Time on screen before the fade-out starts; 4 seconds.
pub fn duration(mut self, duration: Duration) -> Self {
self.duration = duration;
self
}
/// Outer width, limited to the viewport; 240.
pub fn width(mut self, width: f32) -> Self {
self.width = width.max(80.0);
self
}
pub fn corner_radius(mut self, radius: f32) -> Self {
self.corner_radius = radius.max(0.0);
self
}
pub fn fill(mut self, color: Color) -> Self {
self.fill = Some(color);
self
}
pub fn border(mut self, color: Color) -> Self {
self.border = Some(color);
self
}
pub fn title_color(mut self, color: Color) -> Self {
self.title_color = Some(color);
self
}
pub fn content_color(mut self, color: Color) -> Self {
self.content_color = Some(color);
self
}
/// Font sizes of the title and the body; 16 and 15.
pub fn text_sizes(mut self, title: f32, content: f32) -> Self {
self.title_size = title.clamp(1.0, 256.0);
self.content_size = content.clamp(1.0, 256.0);
self
}
}
impl Context {
/// Show a [`Toast`]. Several toasts stack upward, the newest at the bottom.
pub fn toast(&mut self, toast: Toast) {
self.push_toast(toast);
}
/// Number of toasts still queued or fading out.
pub fn toast_count(&self) -> usize {
self.toast_len()
}
}