Skip to main content

bhoo/systems/material/
empty_state.rs

1//! Empty and error state panel with optional leading icon and primary CTA button.
2use crate::*;
3
4/// Configuration for `Frame::empty_state`.
5#[derive(Clone, Copy, Debug, Default)]
6pub struct EmptyStateOptions<'a> {
7    pub icon: Option<MaterialIcon>,
8    pub action_label: Option<&'a str>,
9    pub error: bool,
10}
11
12impl Frame<'_> {
13    /// Centered empty or error state panel with heading, supporting message, optional icon,
14    /// and optional action button. Returns `Some(Response)` when an action button is shown.
15    pub fn empty_state(
16        &mut self,
17        id: u64,
18        title: &str,
19        message: &str,
20        options: EmptyStateOptions<'_>,
21    ) -> Option<Response> {
22        let t = self.theme();
23        let has_icon = options.icon.is_some();
24        let has_action = options.action_label.is_some();
25        let height = 96.0 + if has_icon { 48.0 } else { 0.0 } + if has_action { 56.0 } else { 0.0 };
26        let b = self.reserve(height);
27        self.paint_rect(b, 16.0, t.surface_low);
28
29        let mut y = b.y + 18.0;
30        if let Some(icon) = options.icon {
31            let icon_rect = Rect { x: b.x + (b.width - 36.0) * 0.5, y, width: 36.0, height: 36.0 };
32            let col = if options.error { t.error } else { t.on_surface_variant };
33            self.paint_material_icon(icon_rect, icon, col);
34            y += 44.0;
35        }
36        self.styled_text(
37            Rect { x: b.x + 16.0, y, width: (b.width - 32.0).max(0.0), height: 28.0 },
38            title,
39            if options.error { t.error } else { t.text },
40            TextStyle { size: 18.0, weight: 600, align: 1, ..TextStyle::default() },
41        );
42        y += 30.0;
43        if !message.is_empty() {
44            self.styled_text(
45                Rect { x: b.x + 20.0, y, width: (b.width - 40.0).max(0.0), height: 24.0 },
46                message,
47                t.on_surface_variant,
48                TextStyle { size: 14.0, align: 1, ..TextStyle::default() },
49            );
50            y += 34.0;
51        }
52        options.action_label.map(|label| {
53            let bw = (self.text_width(label, TextStyle::LABEL) + 40.0).clamp(112.0, (b.width - 32.0).max(112.0));
54            let btn = Rect { x: b.x + (b.width - bw) * 0.5, y, width: bw, height: 40.0 };
55            self.action_at(
56                id,
57                btn,
58                label,
59                Options { variant: ButtonVariant::Filled, ..Options::default() },
60            )
61        })
62    }
63}
64
65#[cfg(test)]
66#[path = "empty_state.spec.rs"]
67mod tests;