Skip to main content

brep_app/panels/
toasts.rs

1//! Transient toast notifications — a small overlay for engine notices (e.g. a
2//! sketch solve that failed after an edit). The engine only QUEUES notices
3//! ([`brep_render::engine_state::EngineState::take_notices`]); this drains them
4//! and shows each as an auto-expiring card anchored bottom-center. Time comes
5//! from egui (`ctx.input().time`) — never `std::time::Instant`, which aborts on
6//! wasm.
7
8use eframe::egui;
9
10/// Seconds a toast stays on screen before it fades out of the list.
11const TTL: f64 = 6.0;
12/// Most toasts kept on screen at once (older ones drop off the top).
13const MAX_ON_SCREEN: usize = 6;
14
15/// A queue of transient messages, each stamped with its egui birth time.
16#[derive(Default)]
17pub struct Toasts {
18    items: Vec<(String, f64)>,
19}
20
21impl Toasts {
22    /// Drop every queued toast (an automation host does this before a
23    /// deterministic capture; the texts were already published as notices).
24    pub fn dismiss_all(&mut self) -> usize {
25        let n = self.items.len();
26        self.items.clear();
27        n
28    }
29
30    pub fn new() -> Self {
31        Self::default()
32    }
33
34    /// Queue `messages` (drained from the engine this frame), stamped with `now`
35    /// (`ctx.input().time`).
36    pub fn extend(&mut self, messages: impl IntoIterator<Item = String>, now: f64) {
37        for message in messages {
38            self.items.push((message, now));
39        }
40        let overflow = self.items.len().saturating_sub(MAX_ON_SCREEN);
41        if overflow > 0 {
42            self.items.drain(0..overflow);
43        }
44    }
45
46    /// The queued toast texts, oldest first, as a JSON array — the
47    /// `__brepNotices` global the headed verifier reads to see a refusal the
48    /// app only ever shows as a transient card.
49    pub fn texts_json(&self) -> String {
50        serde_json::Value::Array(
51            self.items
52                .iter()
53                .map(|(text, _)| serde_json::Value::String(text.clone()))
54                .collect(),
55        )
56        .to_string()
57    }
58
59    /// Draw the unexpired toasts. Call once per frame at ctx level (after the
60    /// panels, so the cards float over the shell).
61    pub fn show(&mut self, ctx: &egui::Context) {
62        let now = ctx.input(|i| i.time);
63        self.items.retain(|(_, born)| now - born < TTL);
64        if self.items.is_empty() {
65            return;
66        }
67        // Keep the frame loop alive so a toast expires on time without new input.
68        ctx.request_repaint();
69
70        egui::Area::new(egui::Id::new("brep-toasts"))
71            .anchor(egui::Align2::CENTER_BOTTOM, egui::vec2(0.0, -28.0))
72            .order(egui::Order::Foreground)
73            .interactable(false)
74            .show(ctx, |ui| {
75                for (message, _) in &self.items {
76                    egui::Frame::popup(ui.style())
77                        .fill(egui::Color32::from_rgb(0x3a, 0x24, 0x24))
78                        .stroke(egui::Stroke::new(
79                            1.0,
80                            egui::Color32::from_rgb(0xff, 0x6b, 0x6b),
81                        ))
82                        .show(ui, |ui| {
83                            ui.set_max_width(460.0);
84                            ui.label(
85                                egui::RichText::new(message)
86                                    .color(egui::Color32::from_rgb(0xff, 0x9b, 0x9b)),
87                            );
88                        });
89                    ui.add_space(6.0);
90                }
91            });
92    }
93}