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
//! Transient toast notifications — a small overlay for engine notices (e.g. a
//! sketch solve that failed after an edit). The engine only QUEUES notices
//! ([`brep_render::engine_state::EngineState::take_notices`]); this drains them
//! and shows each as an auto-expiring card anchored bottom-center. Time comes
//! from egui (`ctx.input().time`) — never `std::time::Instant`, which aborts on
//! wasm.
use eframe::egui;
/// Seconds a toast stays on screen before it fades out of the list.
const TTL: f64 = 6.0;
/// Most toasts kept on screen at once (older ones drop off the top).
const MAX_ON_SCREEN: usize = 6;
/// A queue of transient messages, each stamped with its egui birth time.
#[derive(Default)]
pub struct Toasts {
items: Vec<(String, f64)>,
}
impl Toasts {
pub fn new() -> Self {
Self::default()
}
/// Queue `messages` (drained from the engine this frame), stamped with `now`
/// (`ctx.input().time`).
pub fn extend(&mut self, messages: impl IntoIterator<Item = String>, now: f64) {
for message in messages {
self.items.push((message, now));
}
let overflow = self.items.len().saturating_sub(MAX_ON_SCREEN);
if overflow > 0 {
self.items.drain(0..overflow);
}
}
/// Draw the unexpired toasts. Call once per frame at ctx level (after the
/// panels, so the cards float over the shell).
pub fn show(&mut self, ctx: &egui::Context) {
let now = ctx.input(|i| i.time);
self.items.retain(|(_, born)| now - born < TTL);
if self.items.is_empty() {
return;
}
// Keep the frame loop alive so a toast expires on time without new input.
ctx.request_repaint();
egui::Area::new(egui::Id::new("brep-toasts"))
.anchor(egui::Align2::CENTER_BOTTOM, egui::vec2(0.0, -28.0))
.order(egui::Order::Foreground)
.interactable(false)
.show(ctx, |ui| {
for (message, _) in &self.items {
egui::Frame::popup(ui.style())
.fill(egui::Color32::from_rgb(0x3a, 0x24, 0x24))
.stroke(egui::Stroke::new(
1.0,
egui::Color32::from_rgb(0xff, 0x6b, 0x6b),
))
.show(ui, |ui| {
ui.set_max_width(460.0);
ui.label(
egui::RichText::new(message)
.color(egui::Color32::from_rgb(0xff, 0x9b, 0x9b)),
);
});
ui.add_space(6.0);
}
});
}
}