Skip to main content

gpui_material_components/
selection.rs

1mod checkbox;
2mod radio;
3mod switch;
4
5use std::rc::Rc;
6
7use gpui::{
8    AnyElement, App, Div, Hsla, InteractiveElement, IntoElement, ParentElement, SharedString,
9    StatefulInteractiveElement, Styled, Window, div, prelude::FluentBuilder, px,
10};
11use gpui_base::TestSupportExt;
12use gpui_material_foundation::{color, current_material_theme};
13use gpui_material_theme::{ColorRole, Direction, StateLayer};
14
15use crate::action::{FeedbackState, ripple_ink};
16
17pub use checkbox::{Checkbox, CheckboxColors, CheckboxStateColors};
18pub use gpui_base::CheckboxState;
19pub use radio::{Radio, RadioColors, RadioGroup};
20pub use switch::{Switch, SwitchColors, SwitchStateColors};
21
22fn tone(role: ColorRole, window: &Window, cx: &App) -> Hsla {
23    color(current_material_theme(window, cx).colors.color(role))
24}
25
26fn selection_tone(selected: bool, disabled: bool, window: &Window, cx: &App) -> Hsla {
27    if disabled {
28        tone(ColorRole::OnSurface, window, cx).opacity(0.38)
29    } else if selected {
30        tone(ColorRole::Primary, window, cx)
31    } else {
32        tone(ColorRole::OnSurfaceVariant, window, cx)
33    }
34}
35
36fn row(direction: Direction, show_label: bool) -> Div {
37    div()
38        .flex()
39        .items_center()
40        .when(show_label, Styled::gap_2)
41        .when(direction == Direction::Rtl, Styled::flex_row_reverse)
42}
43
44fn selection_label(
45    label: SharedString,
46    content: Option<AnyElement>,
47    disabled: bool,
48    window: &Window,
49    cx: &App,
50) -> impl IntoElement + use<> {
51    div()
52        .id("selection-label")
53        .test_support()
54        .min_w_0()
55        .when(content.is_none(), Styled::truncate)
56        .when(disabled, |element| {
57            element.text_color(tone(ColorRole::OnSurface, window, cx).opacity(0.38))
58        })
59        .child(content.unwrap_or_else(|| label.into_any_element()))
60}
61
62fn selection_target(
63    id: SharedString,
64    indicator: impl IntoElement,
65    accent: Hsla,
66    enabled: bool,
67    ripple: Rc<std::cell::RefCell<FeedbackState>>,
68    direction: Direction,
69    reduced_motion: bool,
70) -> Div {
71    let static_pressed = enabled && reduced_motion && ripple.borrow().pressed();
72    div()
73        .size(px(48.))
74        .flex_none()
75        .flex()
76        .items_center()
77        .justify_center()
78        .child(
79            div()
80                .id("selection-indicator")
81                .relative()
82                .size(px(40.))
83                .rounded_full()
84                .flex()
85                .items_center()
86                .justify_center()
87                .when(enabled, |target| {
88                    let hover = id.clone();
89                    target
90                        .when(static_pressed, |target| {
91                            target.bg(accent.opacity(StateLayer::Pressed.opacity()))
92                        })
93                        .when(!static_pressed, |target| {
94                            target.group_hover(hover, |style| {
95                                style.bg(accent.opacity(StateLayer::Hover.opacity()))
96                            })
97                        })
98                        .when(reduced_motion, |target| {
99                            target.group_active(id, |style| {
100                                style.bg(accent.opacity(StateLayer::Pressed.opacity()))
101                            })
102                        })
103                })
104                .child(ripple_ink(
105                    ripple,
106                    accent,
107                    gpui_material_theme::CornerRole::Full.shape(),
108                    direction,
109                ))
110                .child(indicator),
111        )
112}
113
114fn focus_style(color: Hsla) -> impl FnOnce(gpui::StyleRefinement) -> gpui::StyleRefinement {
115    move |style| style.border_color(color)
116}