Skip to main content

repose_material/material3/
advbuttons.rs

1#![allow(non_snake_case)]
2
3use std::rc::Rc;
4
5use super::SplitButtonDefaults;
6use super::util::apply_m3_clickable;
7use repose_core::{locals::with_content_color, *};
8use repose_ui::{Box, Row, Text, TextStyle, ViewExt};
9
10/// Configuration for [`SplitButtonLayout`].
11#[derive(Clone)]
12pub struct SplitButtonConfig {
13    pub modifier: Modifier,
14    pub spacing: f32,
15}
16
17impl Default for SplitButtonConfig {
18    fn default() -> Self {
19        Self {
20            modifier: Modifier::new(),
21            spacing: SplitButtonDefaults::SPACING,
22        }
23    }
24}
25
26/// M3 SplitButtonLayout -> places leading and trailing buttons side by side.
27///
28/// Pass styled buttons (via [`Button`], [`FilledTonalButton`], etc.) or use
29/// the factory helpers below for properly-shaped split button parts.
30pub fn SplitButtonLayout(
31    leading_button: View,
32    trailing_button: View,
33    config: SplitButtonConfig,
34) -> View {
35    Row(config
36        .modifier
37        .gap(config.spacing)
38        .align_items(AlignItems::CENTER))
39    .child((leading_button, trailing_button))
40}
41
42fn split_leading_shape_radii() -> [f32; 4] {
43    [
44        SplitButtonDefaults::OUTER_CORNER_SIZE,
45        SplitButtonDefaults::SMALL_INNER_CORNER_SIZE,
46        SplitButtonDefaults::SMALL_INNER_CORNER_SIZE,
47        SplitButtonDefaults::OUTER_CORNER_SIZE,
48    ]
49}
50
51fn split_trailing_shape_radii() -> [f32; 4] {
52    [
53        SplitButtonDefaults::SMALL_INNER_CORNER_SIZE,
54        SplitButtonDefaults::OUTER_CORNER_SIZE,
55        SplitButtonDefaults::OUTER_CORNER_SIZE,
56        SplitButtonDefaults::SMALL_INNER_CORNER_SIZE,
57    ]
58}
59
60fn split_button_impl(
61    outer_modifier: Modifier,
62    on_click: impl Fn() + 'static,
63    content: impl FnOnce() -> View,
64    content_color: Color,
65    container_color: Option<Color>,
66    state_colors: StateColors,
67    state_elevation: Option<StateElevation>,
68    border: Option<(f32, Color, f32)>,
69    pad_left: f32,
70    pad_right: f32,
71    height: f32,
72    enabled: bool,
73    interaction_source: Option<MutableInteractionSource>,
74) -> View {
75    let mut m = Modifier::new()
76        .height(height)
77        .min_width(48.0)
78        .padding_values(PaddingValues {
79            left: pad_left,
80            right: pad_right,
81            top: 0.0,
82            bottom: 0.0,
83        })
84        .align_items(AlignItems::CENTER)
85        .justify_content(JustifyContent::CENTER);
86    if let Some(bg) = container_color {
87        m = m.background(bg);
88    }
89    m = m.state_colors(if enabled {
90        state_colors
91    } else {
92        StateColors {
93            default: Color::TRANSPARENT,
94            hovered: Color::TRANSPARENT,
95            focused: Color::TRANSPARENT,
96            pressed: Color::TRANSPARENT,
97            dragged: Color::TRANSPARENT,
98            disabled: state_colors.disabled,
99        }
100    });
101    if let Some(se) = state_elevation {
102        m = m.state_elevation(se);
103    }
104    if let Some((w, c, r)) = border {
105        m = m.border(w, c, r);
106    }
107    let source: Rc<MutableInteractionSource> = interaction_source
108        .map(Rc::new)
109        .unwrap_or_else(|| remember(MutableInteractionSource::new));
110    m = m.interaction_source(&source);
111    m = m.indication(crate::ripple::ripple(crate::ripple::RippleConfig {
112        color: Some(content_color),
113        bounded: true,
114        ..Default::default()
115    }));
116    if enabled {
117        m = m.clickable().on_click(on_click);
118    }
119    m = m.then(outer_modifier);
120    let effective = if enabled {
121        content_color
122    } else {
123        content_color.with_alpha_f32(0.38)
124    };
125    with_content_color(effective, || Box(m).child(content()))
126}
127
128pub fn SplitButtonLeadingButton(
129    modifier: Modifier,
130    on_click: impl Fn() + 'static,
131    config: super::ButtonConfig,
132    content: impl FnOnce() -> View,
133) -> View {
134    let def = super::ButtonColors {
135        container_color: super::ButtonDefaults::container_color(),
136        content_color: super::ButtonDefaults::content_color(),
137        disabled_container_color: super::ButtonDefaults::container_color()
138            .with_alpha_f32(0.12)
139            .composite_over(theme().surface_container_low),
140        disabled_content_color: super::ButtonDefaults::content_color().with_alpha_f32(0.38),
141    };
142    let (cc, bg, sc, se) = resolve_button_colors(&config, def);
143    let pad = config
144        .content_padding
145        .unwrap_or(SplitButtonDefaults::small_leading_content_padding());
146    split_button_impl(
147        modifier.clip_rounded_radii(split_leading_shape_radii()),
148        on_click,
149        content,
150        cc,
151        bg,
152        sc,
153        se.or(Some(super::ButtonDefaults::state_elevation_default())),
154        config.border,
155        pad.left,
156        pad.right,
157        config.height,
158        config.enabled,
159        config.interaction_source.clone(),
160    )
161}
162
163pub fn SplitButtonTrailingButton(
164    modifier: Modifier,
165    on_click: impl Fn() + 'static,
166    config: super::ButtonConfig,
167    content: impl FnOnce() -> View,
168) -> View {
169    let def = super::ButtonColors {
170        container_color: super::ButtonDefaults::container_color(),
171        content_color: super::ButtonDefaults::content_color(),
172        disabled_container_color: super::ButtonDefaults::container_color()
173            .with_alpha_f32(0.12)
174            .composite_over(theme().surface_container_low),
175        disabled_content_color: super::ButtonDefaults::content_color().with_alpha_f32(0.38),
176    };
177    let (cc, bg, sc, se) = resolve_button_colors(&config, def);
178    let pad = config
179        .content_padding
180        .unwrap_or(SplitButtonDefaults::small_trailing_content_padding());
181    split_button_impl(
182        modifier.clip_rounded_radii(split_trailing_shape_radii()),
183        on_click,
184        content,
185        cc,
186        bg,
187        sc,
188        se.or(Some(super::ButtonDefaults::state_elevation_default())),
189        config.border,
190        pad.left,
191        pad.right,
192        config.height,
193        config.enabled,
194        config.interaction_source.clone(),
195    )
196}
197
198pub fn SplitButtonTrailingToggleButton(
199    checked: bool,
200    on_checked_change: impl Fn(bool) + 'static,
201    modifier: Modifier,
202    config: super::ToggleButtonConfig,
203    content: impl FnOnce(bool) -> View,
204) -> View {
205    let _th = theme();
206    let cc = config
207        .content_color
208        .unwrap_or_else(super::ToggleButtonDefaults::content_color);
209    let checked_cc = config
210        .checked_content_color
211        .unwrap_or_else(super::ToggleButtonDefaults::checked_content_color);
212    let checked_bg = config
213        .checked_container_color
214        .unwrap_or_else(super::ToggleButtonDefaults::checked_container_color);
215    let bg = if checked {
216        checked_bg
217    } else {
218        config.container_color.unwrap_or(Color::TRANSPARENT)
219    };
220    let fg = if checked { checked_cc } else { cc };
221    let se = config
222        .state_elevation
223        .unwrap_or_else(super::ToggleButtonDefaults::state_elevation_default);
224    let pad_l = config
225        .content_padding
226        .map(|p| p.left)
227        .unwrap_or(super::ToggleButtonDefaults::HORIZONTAL_PADDING);
228    let pad_r = config
229        .content_padding
230        .map(|p| p.right)
231        .unwrap_or(super::ToggleButtonDefaults::HORIZONTAL_PADDING);
232    let mut m = Modifier::new()
233        .height(config.height)
234        .min_width(48.0)
235        .padding_values(PaddingValues {
236            left: pad_l,
237            right: pad_r,
238            top: 0.0,
239            bottom: 0.0,
240        })
241        .background(bg)
242        .align_items(AlignItems::CENTER)
243        .justify_content(JustifyContent::CENTER)
244        .state_colors(config.state_colors)
245        .state_elevation(se);
246    let tg_source: Rc<MutableInteractionSource> = config
247        .interaction_source
248        .clone()
249        .map(Rc::new)
250        .unwrap_or_else(|| remember(MutableInteractionSource::new));
251    m = m.interaction_source(&tg_source);
252    if let Some((w, c, r)) = config.border {
253        m = m.border(w, c, r);
254    }
255    if config.enabled {
256        let cb = on_checked_change;
257        m = apply_m3_clickable(m, &tg_source, fg, true, move || cb(!checked));
258    } else {
259        m = m.alpha(0.38);
260    }
261    m = m.then(modifier.clip_rounded_radii(split_trailing_shape_radii()));
262    with_content_color(fg, || Box(m).child(content(checked)))
263}
264
265pub fn SplitButtonTonalLeadingButton(
266    modifier: Modifier,
267    on_click: impl Fn() + 'static,
268    config: super::ButtonConfig,
269    content: impl FnOnce() -> View,
270) -> View {
271    let def = super::ButtonColors {
272        container_color: super::ButtonDefaults::tonal_container_color(),
273        content_color: super::ButtonDefaults::tonal_content_color(),
274        disabled_container_color: theme()
275            .on_surface
276            .with_alpha_f32(0.12)
277            .composite_over(theme().surface_container_low),
278        disabled_content_color: theme().on_surface.with_alpha_f32(0.38),
279    };
280    let (cc, bg, sc, se) = resolve_button_colors(&config, def);
281    let pad = config
282        .content_padding
283        .unwrap_or(SplitButtonDefaults::small_leading_content_padding());
284    split_button_impl(
285        modifier.clip_rounded_radii(split_leading_shape_radii()),
286        on_click,
287        content,
288        cc,
289        bg,
290        sc,
291        se.or(Some(super::ButtonDefaults::elevated_state_elevation())),
292        config.border,
293        pad.left,
294        pad.right,
295        config.height,
296        config.enabled,
297        config.interaction_source.clone(),
298    )
299}
300
301pub fn SplitButtonTonalTrailingToggleButton(
302    checked: bool,
303    on_checked_change: impl Fn(bool) + 'static,
304    modifier: Modifier,
305    config: super::ToggleButtonConfig,
306    content: impl FnOnce(bool) -> View,
307) -> View {
308    let _th = theme();
309    let cc = config
310        .content_color
311        .unwrap_or_else(super::ToggleButtonDefaults::tonal_content_color);
312    let checked_cc = config
313        .checked_content_color
314        .unwrap_or_else(super::ToggleButtonDefaults::tonal_checked_content_color);
315    let checked_bg = config
316        .checked_container_color
317        .unwrap_or_else(super::ToggleButtonDefaults::tonal_checked_container_color);
318    let bg = if checked {
319        checked_bg
320    } else {
321        config.container_color.unwrap_or(Color::TRANSPARENT)
322    };
323    let fg = if checked { checked_cc } else { cc };
324    let se = config
325        .state_elevation
326        .unwrap_or_else(super::ToggleButtonDefaults::state_elevation_default);
327    let pad_l = config
328        .content_padding
329        .map(|p| p.left)
330        .unwrap_or(super::ToggleButtonDefaults::HORIZONTAL_PADDING);
331    let pad_r = config
332        .content_padding
333        .map(|p| p.right)
334        .unwrap_or(super::ToggleButtonDefaults::HORIZONTAL_PADDING);
335    let mut m = Modifier::new()
336        .height(config.height)
337        .min_width(48.0)
338        .padding_values(PaddingValues {
339            left: pad_l,
340            right: pad_r,
341            top: 0.0,
342            bottom: 0.0,
343        })
344        .background(bg)
345        .align_items(AlignItems::CENTER)
346        .justify_content(JustifyContent::CENTER)
347        .state_colors(config.state_colors)
348        .state_elevation(se);
349    let tg_source: Rc<MutableInteractionSource> = config
350        .interaction_source
351        .clone()
352        .map(Rc::new)
353        .unwrap_or_else(|| remember(MutableInteractionSource::new));
354    m = m.interaction_source(&tg_source);
355    if let Some((w, c, r)) = config.border {
356        m = m.border(w, c, r);
357    }
358    if config.enabled {
359        let cb = on_checked_change;
360        m = apply_m3_clickable(m, &tg_source, fg, true, move || cb(!checked));
361    } else {
362        m = m.alpha(0.38);
363    }
364    m = m.then(modifier.clip_rounded_radii(split_trailing_shape_radii()));
365    with_content_color(fg, || Box(m).child(content(checked)))
366}
367
368/// State for the overflow menu in [`ButtonGroup`].
369pub struct ButtonGroupMenuState {
370    pub is_showing: bool,
371}
372
373impl ButtonGroupMenuState {
374    pub fn dismiss(&mut self) {
375        self.is_showing = false;
376    }
377    pub fn show(&mut self) {
378        self.is_showing = true;
379    }
380}
381
382/// Scope passed to [`ButtonGroup`]'s content closure.
383pub struct ButtonGroupScope {
384    items: Vec<ButtonGroupItem>,
385    connected: bool,
386}
387
388/// Internal item held by `ButtonGroupScope`.
389struct ButtonGroupItem {
390    button_group_content: Box<dyn FnOnce() -> View>,
391    menu_content: Option<Box<dyn FnOnce(&mut ButtonGroupMenuState) -> View>>,
392}
393
394impl ButtonGroupScope {
395    fn new() -> Self {
396        Self {
397            items: Vec::new(),
398            connected: false,
399        }
400    }
401
402    /// Add a clickable item (rendered as a [`Button`] internally).
403    pub fn clickable_item(
404        &mut self,
405        on_click: impl Fn() + 'static,
406        label: String,
407        icon: Option<View>,
408    ) {
409        let cb = Rc::new(on_click);
410        let cb2 = cb.clone();
411        self.items.push(ButtonGroupItem {
412            button_group_content: Box::new(move || {
413                let cb = cb2.clone();
414                let config = super::ButtonConfig {
415                    shape_radius: 0.0,
416                    ..Default::default()
417                };
418                super::Button(
419                    Modifier::new().flex_grow(1.0),
420                    move || (cb)(),
421                    config,
422                    move || {
423                        let label = label.clone();
424                        let t = Text(label).single_line();
425                        match icon.clone() {
426                            Some(ic) => Box(Modifier::new()).child((ic, t)),
427                            None => t,
428                        }
429                    },
430                )
431            }),
432            menu_content: None,
433        });
434    }
435
436    /// Add a toggleable item (rendered as a [`ToggleButton`] internally).
437    pub fn toggleable_item(
438        &mut self,
439        checked: bool,
440        on_checked_change: impl Fn(bool) + 'static,
441        label: String,
442        icon: Option<View>,
443    ) {
444        let cb = Rc::new(on_checked_change);
445        let cb2 = cb.clone();
446        self.items.push(ButtonGroupItem {
447            button_group_content: Box::new(move || {
448                let cb = cb2.clone();
449                let config = super::ToggleButtonConfig {
450                    shape_radius: 0.0,
451                    ..Default::default()
452                };
453                super::ToggleButton(
454                    checked,
455                    move |b| (cb)(b),
456                    config,
457                    move |_| {
458                        let label = label.clone();
459                        let t = Text(label).single_line();
460                        match icon.clone() {
461                            Some(ic) => Box(Modifier::new()).child((ic, t)),
462                            None => t,
463                        }
464                    },
465                )
466            }),
467            menu_content: None,
468        });
469    }
470
471    /// Add a custom item with a button group composable and an optional overflow menu content.
472    pub fn custom_item(
473        &mut self,
474        button_group_content: impl FnOnce() -> View + 'static,
475        menu_content: Option<impl FnOnce(&mut ButtonGroupMenuState) -> View + 'static>,
476    ) {
477        self.items.push(ButtonGroupItem {
478            button_group_content: Box::new(button_group_content),
479            menu_content: menu_content.map(|f| {
480                let b: Box<dyn FnOnce(&mut ButtonGroupMenuState) -> View> = Box::new(f);
481                b
482            }),
483        });
484    }
485}
486
487/// M3 ButtonGroup -> a horizontal sequence of related action items.
488///
489/// Items are added via [`ButtonGroupScope::clickable_item`] and
490/// [`ButtonGroupScope::toggleable_item`].
491pub fn ButtonGroup(
492    modifier: Modifier,
493    gap: f32,
494    content: impl FnOnce(&mut ButtonGroupScope),
495) -> View {
496    let mut scope = ButtonGroupScope::new();
497    content(&mut scope);
498    Row(modifier.gap(gap).align_items(AlignItems::CENTER)).with_children(
499        scope
500            .items
501            .into_iter()
502            .map(|item| (item.button_group_content)())
503            .collect::<Vec<View>>(),
504    )
505}
506
507fn resolve_button_colors(
508    config: &super::ButtonConfig,
509    def: super::ButtonColors,
510) -> (Color, Option<Color>, StateColors, Option<StateElevation>) {
511    if let Some(colors) = &config.colors {
512        let bg = if config.enabled {
513            colors.container_color
514        } else {
515            colors.disabled_container_color
516        };
517        let cc = if config.enabled {
518            colors.content_color
519        } else {
520            colors.disabled_content_color
521        };
522        let sc = StateColors {
523            default: Color::TRANSPARENT,
524            hovered: Color::TRANSPARENT,
525            focused: Color::TRANSPARENT,
526            pressed: Color::TRANSPARENT,
527            dragged: colors.content_color.with_alpha_f32(0.12),
528            disabled: Color::TRANSPARENT,
529        };
530        let se = config.elevation.map(|e| StateElevation {
531            default: e.default,
532            hovered: e.hovered,
533            focused: e.focused,
534            pressed: e.pressed,
535            dragged: e.pressed,
536            disabled: e.disabled,
537        });
538        (cc, Some(bg), sc, se)
539    } else {
540        let cc = config.content_color.unwrap_or(def.content_color);
541        let bg = Some(config.container_color.unwrap_or(def.container_color));
542        let sc = if config.enabled {
543            config.state_colors
544        } else {
545            StateColors {
546                default: Color::TRANSPARENT,
547                hovered: Color::TRANSPARENT,
548                focused: Color::TRANSPARENT,
549                pressed: Color::TRANSPARENT,
550                dragged: Color::TRANSPARENT,
551                disabled: config.state_colors.disabled,
552            }
553        };
554        let se = config.state_elevation;
555        (cc, bg, sc, se)
556    }
557}