Skip to main content

bhoo/systems/material/
tile.rs

1//! Material 3 Expressive two-line pill Action Tile (Quick Settings / control tile).
2use crate::*;
3
4/// Configuration for `Frame::action_tile`.
5#[derive(Clone, Copy, Debug)]
6pub struct ActionTileOptions {
7    pub active: bool,
8    pub enabled: bool,
9    pub show_chevron: bool,
10    /// Optional 0..1 collapse factor for the subtitle (`1.0` = visible, `0.0` = centered single-line title).
11    pub subtitle_alpha: f32,
12}
13
14impl Default for ActionTileOptions {
15    fn default() -> Self {
16        Self {
17            active: false,
18            enabled: true,
19            show_chevron: false,
20            subtitle_alpha: 1.0,
21        }
22    }
23}
24
25impl Frame<'_> {
26    /// Two-line Material 3 pill toggle/action tile (Quick Settings style) at explicit `bounds`
27    /// with leading `SystemIcon`, title, collapsible subtitle, and optional trailing chevron.
28    pub fn action_tile(
29        &mut self,
30        id: u64,
31        bounds: Rect,
32        icon: SystemIcon,
33        title: &str,
34        subtitle: &str,
35        options: ActionTileOptions,
36    ) -> Response {
37        let t = self.theme();
38        let rad = (bounds.height * 0.42).min(bounds.width * 0.5);
39        let r = self.target(id, bounds, title, options.enabled, Role::ToggleButton);
40        let (bg, fg, sub_fg) = if !r.enabled {
41            (t.surface_low, t.text.opacity(0.38), t.on_surface_variant.opacity(0.38))
42        } else if options.active {
43            (t.primary, t.on_primary, t.on_primary.opacity(0.82))
44        } else {
45            (t.surface_high, t.text, t.on_surface_variant)
46        };
47        self.paint_rect(bounds, rad, bg);
48        self.state_layer(r, rad, fg);
49        self.focus_ring(r);
50
51        let icon_sz = 22.0_f32.min(bounds.height - 12.0).max(0.0);
52        let icon_rect = Rect {
53            x: bounds.x + 14.0,
54            y: bounds.y + (bounds.height - icon_sz) * 0.5,
55            width: icon_sz,
56            height: icon_sz,
57        };
58        self.paint_system_icon(icon_rect, icon, fg);
59
60        let trail_pad = if options.show_chevron { 28.0 } else { 12.0 };
61        let text_x = icon_rect.x + icon_sz + 10.0;
62        let text_w = (bounds.x + bounds.width - text_x - trail_pad).max(0.0);
63        let sa = if subtitle.is_empty() { 0.0 } else { options.subtitle_alpha.clamp(0.0, 1.0) };
64        let title_y = bounds.y + (bounds.height - 18.0) * 0.5 - 8.5 * sa;
65        self.styled_text(
66            Rect { x: text_x, y: title_y, width: text_w, height: 18.0 },
67            title,
68            fg,
69            TextStyle { size: 13.5, weight: 600, ..TextStyle::default() },
70        );
71        if sa > 0.02 {
72            self.styled_text(
73                Rect { x: text_x, y: title_y + 18.0, width: text_w, height: 16.0 },
74                subtitle,
75                sub_fg.opacity(sa),
76                TextStyle { size: 11.5, ..TextStyle::default() },
77            );
78        }
79        if options.show_chevron {
80            let cx = bounds.x + bounds.width - 18.0;
81            let cy = bounds.y + bounds.height * 0.5;
82            self.paint_line([cx - 2.5, cy - 4.0], [cx + 2.0, cy], 1.6, sub_fg);
83            self.paint_line([cx + 2.0, cy], [cx - 2.5, cy + 4.0], 1.6, sub_fg);
84        }
85        r
86    }
87}
88
89#[cfg(test)]
90#[path = "tile.spec.rs"]
91mod tests;