Skip to main content

bhoo/systems/material/
toast.rs

1//! Lightweight transient floating Toast pill with deadline auto-dismiss.
2use crate::*;
3
4/// Caller-owned state for `Frame::toast`.
5#[derive(Clone, Debug, Default)]
6pub struct ToastState {
7    pub message: Option<String>,
8    pub expires_at: f64,
9}
10
11impl ToastState {
12    /// Show `message` for `duration_s` seconds starting at `now`.
13    pub fn show(&mut self, message: impl Into<String>, now: f64, duration_s: f64) {
14        self.message = Some(message.into());
15        self.expires_at = now + duration_s.max(0.1);
16    }
17
18    pub fn dismiss(&mut self) {
19        self.message = None;
20    }
21
22    pub fn is_visible(&self, now: f64) -> bool {
23        self.message.is_some() && now < self.expires_at
24    }
25}
26
27/// Visual options for `Frame::toast_with_options`.
28#[derive(Clone, Copy, Debug)]
29pub struct ToastOptions {
30    pub background: Option<Color>,
31    pub foreground: Option<Color>,
32    pub corner_radius: f32,
33}
34
35impl Default for ToastOptions {
36    fn default() -> Self {
37        Self {
38            background: None,
39            foreground: None,
40            corner_radius: 20.0,
41        }
42    }
43}
44
45impl Frame<'_> {
46    /// Paint an auto-sized centered floating Toast pill near the bottom of `container`
47    /// and schedule `wake_at` for dismissal. Clears `state.message` once expired.
48    pub fn toast(&mut self, state: &mut ToastState, container: Rect, bottom_margin: f32) {
49        self.toast_with_options(state, container, bottom_margin, ToastOptions::default());
50    }
51
52    /// Paint a floating Toast pill with optional custom background/foreground tint and radius.
53    pub fn toast_with_options(
54        &mut self,
55        state: &mut ToastState,
56        container: Rect,
57        bottom_margin: f32,
58        options: ToastOptions,
59    ) {
60        let now = self.time();
61        let Some(ref msg) = state.message else {
62            return;
63        };
64        if now >= state.expires_at {
65            state.message = None;
66            return;
67        }
68        self.wake_at(state.expires_at);
69        let t = self.theme();
70        let bg = options.background.unwrap_or(t.inverse_surface);
71        let fg = options.foreground.unwrap_or(t.inverse_on_surface);
72        let radius = options.corner_radius.max(0.0);
73        let style = TextStyle { size: 13.0, weight: 500, align: 1, ..TextStyle::default() };
74        let max_w = (container.width - 32.0).max(80.0);
75        let pill_w = (self.text_width(msg, style) + 36.0).clamp(96.0, max_w);
76        let pill_h = 40.0;
77        let rect = Rect {
78            x: container.x + (container.width - pill_w) * 0.5,
79            y: container.y + container.height - bottom_margin - pill_h,
80            width: pill_w,
81            height: pill_h,
82        };
83        self.elevation(rect, radius, 3);
84        self.paint_rect(rect, radius, bg);
85        self.styled_text(rect, msg, fg, style);
86    }
87}
88
89#[cfg(test)]
90#[path = "toast.spec.rs"]
91mod tests;