Skip to main content

bhoo/systems/material/
button.rs

1use crate::*;
2
3#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
4pub enum ButtonVariant {
5    #[default]
6    Neutral,
7    Filled,
8    Tonal,
9    Outlined,
10    Text,
11    Elevated,
12}
13
14#[derive(Clone, Copy, Debug)]
15pub struct Options {
16    pub enabled: bool,
17    pub selected: bool,
18    pub variant: ButtonVariant,
19}
20impl Default for Options {
21    fn default() -> Self {
22        Self {
23            enabled: true,
24            selected: false,
25            variant: ButtonVariant::Neutral,
26        }
27    }
28}
29
30impl Frame<'_> {
31    pub fn button(&mut self, id: u64, text: impl AsRef<str>) -> Response {
32        self.button_with(id, text, Options::default())
33    }
34    pub fn button_with(&mut self, id: u64, text: impl AsRef<str>, options: Options) -> Response {
35        let b = self.allocate(40.0);
36        self.button_at(id, b, text.as_ref(), options)
37    }
38    pub(crate) fn button_at(&mut self, id: u64, b: Rect, text: &str, options: Options) -> Response {
39        let response = self.interact(id, b, options.enabled);
40        let t = self.ui.theme;
41        let radius = (b.height * 0.5).min(20.0);
42        let transparent = matches!(
43            options.variant,
44            ButtonVariant::Text | ButtonVariant::Neutral | ButtonVariant::Outlined
45        );
46        let base = match options.variant {
47            ButtonVariant::Filled => t.primary,
48            ButtonVariant::Tonal => t.secondary,
49            ButtonVariant::Elevated => t.background,
50            _ => t.background,
51        };
52        let foreground = if !response.enabled {
53            t.text.opacity(0.38)
54        } else {
55            match options.variant {
56                ButtonVariant::Filled => t.on_primary,
57                ButtonVariant::Tonal => t.on_secondary,
58                _ => t.primary,
59            }
60        };
61        if options.variant == ButtonVariant::Elevated && response.enabled {
62            self.elevation(b, radius, if response.hovered { 2 } else { 1 });
63        }
64        if !transparent {
65            self.rect(
66                b,
67                radius,
68                if response.enabled {
69                    base
70                } else {
71                    t.text.opacity(0.12)
72                },
73            );
74        }
75        if options.variant == ButtonVariant::Outlined {
76            self.rect(
77                b,
78                radius,
79                if response.enabled {
80                    t.outline
81                } else {
82                    t.text.opacity(0.12)
83                },
84            );
85            self.rect(b.inset(1.0), (radius - 1.0).max(0.0), base);
86        }
87        let hover = if self.feedback_blocked {
88            0.0
89        } else {
90            self.animate(id, 0, f32::from(response.hovered), 0.0)
91        };
92        if response.enabled {
93            let opacity = if response.focused && self.ui.keyboard_focus
94                || response.pressed && self.ui.motion_preference == MotionPreference::Reduced
95            {
96                0.12
97            } else {
98                0.08 * hover
99            };
100            if opacity > 0.001 {
101                self.rect(b, radius, foreground.opacity(opacity));
102            }
103        }
104        if !self.feedback_blocked {
105            self.ink(response, radius, foreground);
106        }
107        self.styled_text(b, text, foreground, TextStyle::LABEL);
108        self.focus_ring(response);
109        self.semantic(
110            response,
111            Role::Button,
112            text,
113            options.selected,
114            None,
115            None,
116            None,
117        );
118        response
119    }
120    /// An accessible icon button using bundled official Material Symbols geometry.
121    pub fn material_icon_button(&mut self, id:u64, icon:MaterialIcon, label:&str, options:Options) -> Response {
122        let r = self.icon_button(id, "", label, options);
123        let t = self.ui.theme;
124        let color = if !r.enabled { t.text.opacity(0.38) } else {
125            match options.variant {
126                ButtonVariant::Filled => t.on_primary,
127                ButtonVariant::Tonal => t.on_secondary,
128                _ => if options.selected { t.primary } else { t.on_surface_variant },
129            }
130        };
131        self.paint_material_icon(r.bounds.inset(8.0), icon, color);
132        r
133    }
134    /// The glyph is presentation; `label` names the action for assistive technology.
135    pub fn icon_button(&mut self, id: u64, glyph: &str, label: &str, options: Options) -> Response {
136        let mut b = self.allocate(40.0);
137        b.width = b.width.min(40.0);
138        let r = self.interact(id, b, options.enabled);
139        let t = self.ui.theme;
140        let color = if !r.enabled {
141            t.text.opacity(0.38)
142        } else {
143            match options.variant {
144                ButtonVariant::Filled => t.on_primary,
145                ButtonVariant::Tonal => t.on_secondary,
146                _ => {
147                    if options.selected {
148                        t.primary
149                    } else {
150                        t.on_surface_variant
151                    }
152                }
153            }
154        };
155        match options.variant {
156            ButtonVariant::Filled | ButtonVariant::Tonal => self.rect(
157                b,
158                20.0,
159                if !r.enabled {
160                    t.text.opacity(0.12)
161                } else if options.variant == ButtonVariant::Filled {
162                    t.primary
163                } else {
164                    t.secondary
165                },
166            ),
167            ButtonVariant::Outlined => {
168                self.rect(
169                    b,
170                    20.0,
171                    if r.enabled {
172                        t.outline
173                    } else {
174                        t.text.opacity(0.12)
175                    },
176                );
177                self.rect(b.inset(1.0), 19.0, t.background);
178            }
179            _ => {}
180        }
181        self.state_layer(r, 20.0, color);
182        self.styled_text(
183            b,
184            glyph,
185            color,
186            TextStyle {
187                size: 24.0,
188                align: 1,
189                ..TextStyle::default()
190            },
191        );
192        self.focus_ring(r);
193        self.semantic(r, Role::Button, label, options.selected, None, None, None);
194        r
195    }
196
197    /// Material icon button placed at caller-supplied `bounds` using a bundled `MaterialIcon`.
198    pub fn material_icon_button_at(
199        &mut self,
200        id: u64,
201        bounds: Rect,
202        icon: MaterialIcon,
203        label: &str,
204        options: Options,
205    ) -> Response {
206        let r = self.interact(id, bounds, options.enabled);
207        let t = self.ui.theme;
208        let radius = bounds.width.min(bounds.height) * 0.5;
209        let color = if !r.enabled {
210            t.text.opacity(0.38)
211        } else {
212            match options.variant {
213                ButtonVariant::Filled => t.on_primary,
214                ButtonVariant::Tonal => t.on_secondary,
215                _ => {
216                    if options.selected {
217                        t.primary
218                    } else {
219                        t.on_surface_variant
220                    }
221                }
222            }
223        };
224        match options.variant {
225            ButtonVariant::Filled | ButtonVariant::Tonal => self.rect(
226                bounds,
227                radius,
228                if !r.enabled {
229                    t.text.opacity(0.12)
230                } else if options.variant == ButtonVariant::Filled {
231                    t.primary
232                } else {
233                    t.secondary
234                },
235            ),
236            ButtonVariant::Outlined => {
237                self.rect(bounds, radius, if r.enabled { t.outline } else { t.text.opacity(0.12) });
238                self.rect(bounds.inset(1.0), (radius - 1.0).max(0.0), t.background);
239            }
240            _ => {}
241        }
242        self.state_layer(r, radius, color);
243        let inset = (bounds.width.min(bounds.height) - 24.0).max(0.0) * 0.5;
244        self.paint_material_icon(bounds.inset(inset), icon, color);
245        self.focus_ring(r);
246        self.semantic(r, Role::Button, label, options.selected, None, None, None);
247        r
248    }
249
250    /// Pill button with Beer CSS / Material hover-wave effect.
251    pub fn hover_wave_button(&mut self, id: u64, label: &str, enabled: bool) -> Response {
252        let b = self.allocate(40.0);
253        let r = self.target(id, b, label, enabled, Role::Button);
254        let t = self.theme();
255        let radius = (b.height * 0.5).min(20.0);
256        let bg = if r.enabled { t.primary } else { t.text.opacity(0.12) };
257        let fg = if r.enabled { t.on_primary } else { t.text.opacity(0.38) };
258        self.paint_rect(b, radius, bg);
259        self.wave(r, radius, fg);
260        self.styled_text(b, label, fg, TextStyle::LABEL);
261        self.focus_ring(r);
262        r
263    }
264}
265
266#[cfg(test)]
267#[path = "button.spec.rs"]
268mod tests;