Skip to main content

gpui_material_components/
menu_appearance.rs

1#[cfg(any(feature = "inputs", feature = "feedback"))]
2use gpui::{Div, Hsla, Styled, div, px};
3use gpui::{Edges, Pixels};
4#[cfg(any(feature = "inputs", feature = "feedback"))]
5use gpui_material_foundation::{color, elevation_shadows};
6use gpui_material_theme::{ColorRole, CornerShape, Elevation};
7#[cfg(any(feature = "inputs", feature = "feedback"))]
8use gpui_material_theme::{CornerRole, Direction, MaterialTheme};
9
10#[cfg(any(feature = "inputs", feature = "feedback"))]
11use crate::motion::OverlayMotion;
12
13/// Semantic color presets for Material vertical menus.
14#[cfg(any(feature = "inputs", feature = "feedback"))]
15#[cfg_attr(not(feature = "feedback"), allow(dead_code))]
16#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
17pub enum MenuColor {
18    #[default]
19    Standard,
20    Vibrant,
21}
22
23/// Per-surface menu styling. Omitted values use defaults appropriate to the surface.
24#[derive(Clone, Copy, Debug, Default)]
25pub struct MenuSurfaceStyle {
26    /// The surface shape in logical corner order.
27    pub shape: Option<CornerShape>,
28    pub container: Option<ColorRole>,
29    /// The logical thickness and Material role of the outline.
30    pub border: Option<(Pixels, ColorRole)>,
31    pub tonal_elevation: Elevation,
32    pub shadow_elevation: Option<Elevation>,
33    pub padding: Option<Edges<Pixels>>,
34    pub spacing: Option<Pixels>,
35}
36
37#[cfg(feature = "inputs")]
38impl MenuSurfaceStyle {
39    pub(crate) fn dropdown_surface(self, theme: &MaterialTheme, motion: OverlayMotion) -> Div {
40        let palette = MenuColor::Standard.palette(false, self.container);
41        Paint {
42            theme,
43            motion,
44            direction: theme.layout.direction,
45        }
46        .surface(self, palette, false)
47    }
48
49    pub(crate) fn popup_vertical_insets(self) -> Pixels {
50        let padding = self.padding.unwrap_or_else(|| axis_padding(px(0.), px(8.)));
51        let length = |value: Pixels| {
52            if f32::from(value).is_finite() {
53                value.max(px(0.))
54            } else {
55                px(0.)
56            }
57        };
58        length(padding.top)
59            + length(padding.bottom)
60            + self.border.map_or(px(0.), |(width, _)| length(width) * 2.)
61    }
62}
63
64/// Per-row overrides. Unset shapes and colors follow the menu palette and layout.
65#[cfg(feature = "feedback")]
66#[derive(Clone, Copy, Debug, Default)]
67pub struct MenuItemStyle {
68    pub shape: Option<CornerShape>,
69    pub selected_shape: Option<CornerShape>,
70    pub padding: Option<Edges<Pixels>>,
71    pub spacing: Option<Pixels>,
72    pub container: Option<ColorRole>,
73    pub content: Option<ColorRole>,
74    pub supporting: Option<ColorRole>,
75    pub selected_container: Option<ColorRole>,
76    pub selected_content: Option<ColorRole>,
77}
78
79#[cfg(any(feature = "inputs", feature = "feedback"))]
80#[derive(Clone, Copy)]
81#[cfg_attr(not(feature = "feedback"), allow(dead_code))]
82pub(crate) struct Palette {
83    pub(crate) container: ColorRole,
84    pub(crate) ink: ColorRole,
85    pub(crate) muted: ColorRole,
86    pub(crate) selected: ColorRole,
87    pub(crate) selected_ink: ColorRole,
88}
89
90#[cfg(any(feature = "inputs", feature = "feedback"))]
91impl MenuColor {
92    pub(crate) fn palette(self, grouped: bool, container: Option<ColorRole>) -> Palette {
93        use ColorRole::*;
94        let (default_container, ink, muted, selected, selected_ink) = match (self, grouped) {
95            (Self::Standard, false) => (
96                SurfaceContainer,
97                OnSurface,
98                OnSurfaceVariant,
99                SecondaryContainer,
100                OnSecondaryContainer,
101            ),
102            (Self::Standard, true) => (
103                SurfaceContainerLow,
104                OnSurface,
105                OnSurfaceVariant,
106                TertiaryContainer,
107                OnTertiaryContainer,
108            ),
109            (Self::Vibrant, _) => (
110                TertiaryContainer,
111                OnTertiaryContainer,
112                OnTertiaryContainer,
113                Tertiary,
114                OnTertiary,
115            ),
116        };
117        Palette {
118            container: container.unwrap_or(default_container),
119            ink,
120            muted,
121            selected,
122            selected_ink,
123        }
124    }
125}
126
127#[cfg(any(feature = "inputs", feature = "feedback"))]
128#[derive(Clone, Copy)]
129pub(crate) struct Paint<'a> {
130    pub(crate) theme: &'a MaterialTheme,
131    pub(crate) motion: OverlayMotion,
132    pub(crate) direction: Direction,
133}
134
135#[cfg(any(feature = "inputs", feature = "feedback"))]
136impl Paint<'_> {
137    pub(crate) fn color(self, role: ColorRole) -> Hsla {
138        color(self.theme.colors.color(role))
139    }
140
141    pub(crate) fn padding<E: Styled>(self, element: E, padding: Edges<Pixels>) -> E {
142        element
143            .pt(self.length(padding.top))
144            .pr(self.length(padding.right))
145            .pb(self.length(padding.bottom))
146            .pl(self.length(padding.left))
147    }
148
149    pub(crate) fn length(self, value: Pixels) -> Pixels {
150        let value = f32::from(value);
151        self.motion
152            .px(if value.is_finite() { value.max(0.) } else { 0. })
153    }
154
155    pub(crate) fn shape<E: Styled>(self, element: E, shape: CornerShape) -> E {
156        if shape == CornerShape::Full {
157            return element.rounded_full();
158        }
159        let [tl, tr, br, bl] = shape.radii(f32::MAX, f32::MAX, self.direction);
160        element
161            .rounded_tl(self.motion.px(tl))
162            .rounded_tr(self.motion.px(tr))
163            .rounded_br(self.motion.px(br))
164            .rounded_bl(self.motion.px(bl))
165    }
166
167    pub(crate) fn surface(self, style: MenuSurfaceStyle, palette: Palette, group: bool) -> Div {
168        let background = if palette.container == ColorRole::Surface {
169            color(
170                self.theme
171                    .colors
172                    .surface_at_elevation(style.tonal_elevation),
173            )
174        } else {
175            self.color(palette.container)
176        };
177        let shadow = style.shadow_elevation.unwrap_or(if group {
178            Elevation::Level0
179        } else {
180            Elevation::Level2
181        });
182        let surface = div()
183            .flex()
184            .flex_col()
185            .gap(self.length(style.spacing.unwrap_or(if group { px(2.) } else { px(0.) })))
186            .bg(background)
187            .text_color(self.color(palette.ink))
188            .shadow(elevation_shadows(
189                shadow.dp(),
190                self.motion.scale,
191                self.color(ColorRole::Shadow),
192            ));
193        let surface = if let Some((width, role)) = style.border {
194            let mut surface = surface.border_color(self.color(role));
195            let width = Some(self.length(width).into());
196            let borders = &mut surface.style().border_widths;
197            borders.top = width;
198            borders.right = width;
199            borders.bottom = width;
200            borders.left = width;
201            surface
202        } else {
203            surface
204        };
205        let surface = self.shape(
206            surface,
207            style.shape.unwrap_or_else(|| {
208                if group {
209                    CornerRole::Large.shape()
210                } else {
211                    CornerRole::ExtraSmall.shape()
212                }
213            }),
214        );
215        self.padding(
216            surface,
217            style
218                .padding
219                .unwrap_or_else(|| axis_padding(px(0.), if group { px(4.) } else { px(8.) })),
220        )
221    }
222}
223
224#[cfg(any(feature = "inputs", feature = "feedback"))]
225pub(crate) fn axis_padding(x: Pixels, y: Pixels) -> Edges<Pixels> {
226    Edges {
227        top: y,
228        right: x,
229        bottom: y,
230        left: x,
231    }
232}