Skip to main content

bhoo/systems/material/
banner.rs

1//! Material persistent inline Banner with animated reveal, optional leading icon, and action buttons.
2use crate::*;
3
4/// Configuration for `Frame::banner_with_options`.
5#[derive(Clone, Copy, Debug, Default)]
6pub struct BannerOptions {
7    pub leading_icon: Option<MaterialIcon>,
8}
9
10impl Frame<'_> {
11    /// Material inline Banner that reveals/collapses smoothly and returns the clicked action index (`0..actions.len()`).
12    pub fn banner(
13        &mut self,
14        id: u64,
15        open: bool,
16        message: &str,
17        actions: &[&str],
18    ) -> Option<usize> {
19        self.banner_with_options(id, open, message, actions, BannerOptions::default())
20    }
21
22    /// Material inline Banner with optional leading `MaterialIcon` and action buttons.
23    pub fn banner_with_options(
24        &mut self,
25        id: u64,
26        open: bool,
27        message: &str,
28        actions: &[&str],
29        options: BannerOptions,
30    ) -> Option<usize> {
31        let t = self.theme();
32        let mut clicked = None;
33        let height = if actions.is_empty() { 56.0 } else { 96.0 };
34        self.reveal(id, open, height, |f| {
35            let b = f.reserve(height);
36            f.paint_rect(b, 8.0, t.surface_low);
37            f.paint_rect(Rect { y: b.y + b.height - 1.0, height: 1.0, ..b }, 0.0, t.outline_variant);
38            let inner = Rect {
39                x: b.x + 16.0,
40                y: b.y + 8.0,
41                width: (b.width - 32.0).max(0.0),
42                height: (b.height - 16.0).max(0.0),
43            };
44            f.within(inner, |f| {
45                let msg_rect = f.reserve(36.0);
46                let text_rect = if let Some(icon) = options.leading_icon {
47                    let badge = Rect { x: msg_rect.x, y: msg_rect.y, width: 36.0, height: 36.0 };
48                    f.paint_rect(badge, 18.0, t.primary_container);
49                    f.paint_material_icon(badge.inset(6.0), icon, t.on_primary_container);
50                    Rect {
51                        x: msg_rect.x + 48.0,
52                        width: (msg_rect.width - 48.0).max(0.0),
53                        ..msg_rect
54                    }
55                } else {
56                    msg_rect
57                };
58                f.paragraph(text_rect, message, t.text, TextStyle { size: 14.0, ..TextStyle::default() }, 18.0);
59                if !actions.is_empty() {
60                    let cols = actions.len();
61                    f.row(40.0, cols, |f, i| {
62                        let r = f.button_with(
63                            id.wrapping_add(10 + i as u64),
64                            actions[i],
65                            Options {
66                                variant: if i + 1 == cols { ButtonVariant::Filled } else { ButtonVariant::Text },
67                                ..Options::default()
68                            },
69                        );
70                        if r.clicked {
71                            clicked = Some(i);
72                        }
73                    });
74                }
75            });
76        });
77        clicked
78    }
79}
80
81#[cfg(test)]
82#[path = "banner.spec.rs"]
83mod tests;