Skip to main content

bhoo/systems/material/
expansion_panel.rs

1//! Material ExpansionPanel and Accordion container with rotating chevron and split-corner shadow.
2use crate::*;
3
4/// Configuration for `Frame::expansion_panel`.
5#[derive(Clone, Copy, Debug)]
6pub struct ExpansionPanelOptions {
7    pub round_top: bool,
8    pub round_bottom: bool,
9    pub enabled: bool,
10    pub header_height: f32,
11}
12
13impl Default for ExpansionPanelOptions {
14    fn default() -> Self {
15        Self {
16            round_top: true,
17            round_bottom: true,
18            enabled: true,
19            header_height: 48.0,
20        }
21    }
22}
23
24fn expansion_panel_shadow(f: &mut Frame<'_>, panel: Rect, round_top: bool, round_bottom: bool) {
25    if round_top == round_bottom {
26        f.elevation(panel, if round_top { 12.0 } else { 0.0 }, 2);
27        return;
28    }
29    let half = panel.height * 0.5;
30    let margin = 24.0;
31    let top = Rect {
32        x: panel.x - margin,
33        y: panel.y - margin,
34        width: panel.width + 2.0 * margin,
35        height: half + margin,
36    };
37    let bottom = Rect {
38        x: panel.x - margin,
39        y: panel.y + half,
40        width: panel.width + 2.0 * margin,
41        height: panel.height - half + margin,
42    };
43    f.within(top, |f| f.elevation(panel, if round_top { 12.0 } else { 0.0 }, 2));
44    f.within(bottom, |f| f.elevation(panel, if round_bottom { 12.0 } else { 0.0 }, 2));
45}
46
47impl Frame<'_> {
48    /// Material ExpansionPanel with animated collapse/expand height, rotating chevron, and
49    /// independent top/bottom corner rounding for stacked accordions.
50    pub fn expansion_panel(
51        &mut self,
52        id: u64,
53        title: &str,
54        description: &str,
55        expanded: &mut bool,
56        body_height: f32,
57        options: ExpansionPanelOptions,
58        body: impl FnOnce(&mut Self),
59    ) -> Response {
60        let t = self.theme();
61        let progress = self
62            .transition_css(id, 40, f32::from(*expanded), f32::from(*expanded), 0.225, timing::Easing::Standard)
63            .clamp(0.0, 1.0);
64        let header_h = options.header_height.max(40.0) + 16.0 * progress;
65        let vis_body_h = (body_height.max(0.0) * progress).max(0.0);
66        let total_h = header_h + vis_body_h;
67        let panel = self.reserve(total_h);
68        let top_r = if options.round_top { 12.0 } else { 0.0 };
69        let bot_r = if options.round_bottom { 12.0 } else { 0.0 };
70        expansion_panel_shadow(self, panel, options.round_top, options.round_bottom);
71        self.paint_asym_rect(panel, top_r, bot_r, t.surface_low);
72
73        let header = Rect { height: header_h, ..panel };
74        let r = self.target(id, header, title, options.enabled, Role::Button);
75        self.state_layer(r, top_r.max(bot_r), t.text);
76        self.focus_ring(r);
77        let mut resp = r;
78        if r.clicked {
79            *expanded = !*expanded;
80            resp.changed = true;
81        }
82        let title_col = if r.enabled { t.text } else { t.text.opacity(0.38) };
83        let title_rect = Rect {
84            x: header.x + 20.0,
85            width: (header.width - 56.0).max(0.0),
86            ..header
87        };
88        if description.is_empty() {
89            self.styled_text(title_rect, title, title_col, TextStyle { size: 15.0, weight: 500, ..TextStyle::default() });
90        } else {
91            let split = title_rect.width * 0.45;
92            self.styled_text(
93                Rect { width: split, ..title_rect },
94                title,
95                title_col,
96                TextStyle { size: 15.0, weight: 500, ..TextStyle::default() },
97            );
98            self.styled_text(
99                Rect { x: title_rect.x + split + 8.0, width: (title_rect.width - split - 8.0).max(0.0), ..title_rect },
100                description,
101                t.on_surface_variant.opacity(if r.enabled { 1.0 } else { 0.38 }),
102                TextStyle { size: 14.0, ..TextStyle::default() },
103            );
104        }
105        // Rotating chevron indicator (0 deg down -> 180 deg up)
106        let cx = header.x + header.width - 24.0;
107        let cy = header.y + header.height * 0.5;
108        let flip = 1.0 - 2.0 * progress;
109        self.paint_line([cx - 5.0, cy - 2.0 * flip], [cx, cy + 3.0 * flip], 1.8, t.on_surface_variant);
110        self.paint_line([cx, cy + 3.0 * flip], [cx + 5.0, cy - 2.0 * flip], 1.8, t.on_surface_variant);
111
112        if vis_body_h > 0.5 {
113            let body_bounds = Rect {
114                x: panel.x + 20.0,
115                y: panel.y + header_h,
116                width: (panel.width - 40.0).max(0.0),
117                height: vis_body_h,
118            };
119            self.within(body_bounds, |f| {
120                f.with_opacity(progress, body);
121            });
122        }
123        resp
124    }
125
126    /// Stacked single- or multi-expand Material accordion over a slice of `(title, description)` panels.
127    pub fn accordion(
128        &mut self,
129        id: u64,
130        panels: &[(&str, &str)],
131        expanded_mask: &mut u64,
132        multi: bool,
133        body_height: f32,
134        mut build_body: impl FnMut(&mut Self, usize),
135    ) {
136        let count = panels.len();
137        self.with_gap(1.0, |f| {
138            for (i, &(title, desc)) in panels.iter().enumerate() {
139                let bit = 1u64 << (i.min(63));
140                let mut open = (*expanded_mask & bit) != 0;
141                let prev_open = i > 0 && (*expanded_mask & (1u64 << (i - 1).min(63))) != 0;
142                let next_open = i + 1 < count && (*expanded_mask & (1u64 << (i + 1).min(63))) != 0;
143                let opts = ExpansionPanelOptions {
144                    round_top: i == 0 || open || prev_open,
145                    round_bottom: i + 1 == count || open || next_open,
146                    ..ExpansionPanelOptions::default()
147                };
148                let resp = f.expansion_panel(id.wrapping_add(i as u64), title, desc, &mut open, body_height, opts, |f| {
149                    build_body(f, i);
150                });
151                if resp.changed {
152                    if open {
153                        if multi {
154                            *expanded_mask |= bit;
155                        } else {
156                            *expanded_mask = bit;
157                        }
158                    } else {
159                        *expanded_mask &= !bit;
160                    }
161                }
162            }
163        });
164    }
165}
166
167#[cfg(test)]
168#[path = "expansion_panel.spec.rs"]
169mod tests;