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