ui/surface.rs
1//! [`crate::surface`] — the backdrop surface a floating card sits on: wraps a
2//! popover/dialog card so its ENTIRE subtree paints inside one scene layer (a
3//! single draw order).
4//!
5//! The single layer order is the point: with per-primitive bounds-tree
6//! ordering, a hover repaint elsewhere can reassign the card's quads relative
7//! to siblings — inside one layer the card's stacking is structural.
8//!
9//! The blur is painted first, structurally under the content: inside one layer
10//! the order is blur, then shadow, tint, border, rows, text. It needs
11//! `Window::paint_backdrop_blur` from our gpui fork, which the Metal and wgpu
12//! renderers carry and the DirectX one does not; see [`theme::LENSED`]. Where
13//! it is missing the card fills with [`theme::SurfaceSpec::flat`] instead.
14//!
15//! Material and glass are different things — a material has thickness, a glass
16//! has a variant — and they meet only at the numbers they resolve to, which is
17//! why one element paints both and [`theme::SurfaceStyle`] names which.
18
19use gpui::{
20 AbsoluteLength, AnyElement, App, Bounds, Corners, Element, GlobalElementId, InspectorElementId,
21 IntoElement, LayoutId, Pixels, Styled, Window, fill, px,
22};
23
24use theme::{SurfaceSpec, SurfaceStyle, Theme};
25
26/// Paint an already-composed card as a surface, at the rounding the caller
27/// states — the shape the popover layers need, where the card arrives erased to
28/// an [`AnyElement`] and cannot be chained onto. The look comes off whichever
29/// theme paints it; a caller wanting its own hands one over through
30/// [`Surfaced::surface`].
31pub fn of(corner_radius: f32, style: SurfaceStyle, child: impl IntoElement) -> Surface {
32 Surface {
33 corners: uniform(corner_radius),
34 glass: Look::Shipped(style),
35 tint: None,
36 child: child.into_any_element(),
37 }
38}
39
40/// The same, on [`Theme::popover_surface`] — what the popover layers mount on, so an
41/// app moves every menu between frost and glass by moving one token.
42pub fn popover(corner_radius: f32, child: impl IntoElement) -> Surface {
43 Surface {
44 corners: uniform(corner_radius),
45 glass: Look::Popover,
46 tint: None,
47 child: child.into_any_element(),
48 }
49}
50
51/// One radius on all four corners.
52fn uniform(radius: f32) -> Corners<AbsoluteLength> {
53 let radius = AbsoluteLength::from(px(radius));
54 Corners {
55 top_left: radius,
56 top_right: radius,
57 bottom_right: radius,
58 bottom_left: radius,
59 }
60}
61
62/// Where a [`Material`]'s numbers come from. The popover layers carry no theme
63/// of their own, so they name a look and it resolves against whichever theme
64/// paints them; a caller tuning its own glass hands the numbers over instead.
65#[derive(Clone, Copy)]
66enum Look {
67 /// Whatever [`Theme::popover_surface`] says — the popover layers' choice.
68 Popover,
69 Shipped(SurfaceStyle),
70 Tuned(Tokens),
71}
72
73impl Look {
74 fn tokens(self, theme: &Theme) -> Tokens {
75 match self {
76 Look::Popover => Tokens::of(theme, theme.popover_surface),
77 Look::Shipped(style) => Tokens::of(theme, style),
78 Look::Tuned(tokens) => tokens,
79 }
80 }
81}
82
83/// The glass numbers, read off the theme the way every other widget reads its
84/// colours. Not parameters: a caller who wants different glass hands over a
85/// different theme.
86#[derive(Clone, Copy)]
87struct Tokens {
88 spec: SurfaceSpec,
89 magnify: f32,
90 dispersion: f32,
91}
92
93impl Tokens {
94 fn of(theme: &Theme, style: SurfaceStyle) -> Self {
95 Self {
96 spec: style.spec(theme),
97 magnify: theme.glass_magnify,
98 dispersion: theme.glass_dispersion,
99 }
100 }
101}
102
103/// The backdrop surfaces, on any element carrying a corner radius.
104pub trait Surfaced: Styled + IntoElement + Sized {
105 /// Paint this card as `style`'s surface.
106 ///
107 /// Blur, bevel and tint all come off [`Theme`], and the shape off the
108 /// card's own rounding: the lens dies if any of the three is wrong.
109 ///
110 /// It clears the card's `bg`, since the lens paints the fill. Where the
111 /// lens cannot run — the DirectX renderer, or [`Theme::glass`] off — it paints
112 /// [`SurfaceSpec::flat`] instead, so the surface is never invisible.
113 fn surface(mut self, theme: &Theme, style: SurfaceStyle) -> Surface {
114 let corners = corners_of(&mut self);
115 // The lens paints the fill, so the card's own is dropped here rather
116 // than at the call site: painting both is what buries the lens, and a
117 // caller who has to remember that is a caller who will forget.
118 self.style().background = None;
119 Surface {
120 corners,
121 glass: Look::Tuned(Tokens::of(theme, style)),
122 tint: None,
123 child: self.into_any_element(),
124 }
125 }
126}
127
128/// The rounding an element already carries, as the blur needs it.
129fn corners_of(styled: &mut impl Styled) -> Corners<AbsoluteLength> {
130 let square = AbsoluteLength::from(px(0.0));
131 let radii = &styled.style().corner_radii;
132 Corners {
133 top_left: radii.top_left.unwrap_or(square),
134 top_right: radii.top_right.unwrap_or(square),
135 bottom_right: radii.bottom_right.unwrap_or(square),
136 bottom_left: radii.bottom_left.unwrap_or(square),
137 }
138}
139
140impl<E: Styled + IntoElement> Surfaced for E {}
141
142pub struct Surface {
143 /// The glass's own tint, when the caller wants one. `None` is
144 /// [`Theme::glass_clear`]'s neutral lift.
145 tint: Option<gpui::Hsla>,
146 /// Held unresolved: a rem-rounded card only becomes pixels against the
147 /// window's rem size, which paint is the first place to have.
148 corners: Corners<AbsoluteLength>,
149 /// Which look to paint, and where its numbers come from.
150 glass: Look,
151 child: AnyElement,
152}
153
154/// Whether [`Surfaced::surface`] will actually refract here, or fall back to
155/// [`SurfaceSpec::flat`]. Capability and choice both: the primitive is macOS
156/// Metal's and wgpu's, and components with glass off paint no lens.
157pub fn lensed(theme: &Theme) -> bool {
158 theme::LENSED && theme.glass
159}
160
161impl Surface {
162 /// Tint the glass — SwiftUI's `Glass.tint(_:)`, for a control important
163 /// enough to carry colour. Pass the colour at the coverage you want; it
164 /// stands in for [`Theme::glass_clear`]'s neutral lift rather than adding
165 /// to it, so a heavy alpha reads as paint and a light one as glass.
166 ///
167 /// A plain material's fill is still its caller's, and this does not
168 /// reach it.
169 pub fn tint(mut self, color: gpui::Hsla) -> Self {
170 self.tint = Some(color);
171 self
172 }
173
174 /// The card's rounding in pixels, which only a rem size resolves — clamped
175 /// to the box, since gpui reads a radius past half of it as a sharp corner.
176 fn corners(&self, bounds: Bounds<Pixels>, rem: Pixels) -> Corners<Pixels> {
177 Corners {
178 top_left: self.corners.top_left.to_pixels(rem),
179 top_right: self.corners.top_right.to_pixels(rem),
180 bottom_right: self.corners.bottom_right.to_pixels(rem),
181 bottom_left: self.corners.bottom_left.to_pixels(rem),
182 }
183 .clamp_radii_for_quad_size(bounds.size)
184 }
185}
186
187impl Element for Surface {
188 type RequestLayoutState = ();
189 type PrepaintState = ();
190
191 fn id(&self) -> Option<gpui::ElementId> {
192 None
193 }
194
195 fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
196 None
197 }
198
199 fn request_layout(
200 &mut self,
201 _id: Option<&GlobalElementId>,
202 _inspector_id: Option<&InspectorElementId>,
203 window: &mut Window,
204 cx: &mut App,
205 ) -> (LayoutId, ()) {
206 (self.child.request_layout(window, cx), ())
207 }
208
209 fn prepaint(
210 &mut self,
211 _id: Option<&GlobalElementId>,
212 _inspector_id: Option<&InspectorElementId>,
213 bounds: Bounds<Pixels>,
214 _request_layout: &mut Self::RequestLayoutState,
215 window: &mut Window,
216 cx: &mut App,
217 ) {
218 crate::cover::record(bounds, window, cx);
219 self.child.prepaint(window, cx);
220 }
221
222 fn paint(
223 &mut self,
224 _id: Option<&GlobalElementId>,
225 _inspector_id: Option<&InspectorElementId>,
226 bounds: Bounds<Pixels>,
227 _request_layout: &mut Self::RequestLayoutState,
228 _prepaint: &mut Self::PrepaintState,
229 window: &mut Window,
230 cx: &mut App,
231 ) {
232 let theme = Theme::of(cx);
233 let glass = self.glass.tokens(theme);
234 let corners = self.corners(bounds, window.rem_size());
235 // The backdrop-blur primitive is macOS Metal's and wgpu's alone. The
236 // surface's fill lives inside it, so anywhere it will not run the fill
237 // is painted here — the tone the look settles on, at full coverage.
238 // The tint on its own is a coverage over a blur: painted flat it left
239 // the text behind a composer legible through it (user report).
240 if !lensed(theme) {
241 let tint = self.tint.unwrap_or(glass.spec.tint);
242 window.paint_quad(
243 fill(bounds, glass.spec.flat(tint).unwrap_or(tint)).corner_radii(corners),
244 );
245 }
246 if theme.glass {
247 let extent = f32::from(bounds.size.width.min(bounds.size.height));
248 let effect = gpui::GlassEffect {
249 blur_radius: px(glass.spec.blur),
250 // A length, not a share of the box — but two rims cannot meet
251 // in the middle of a small one.
252 lens: px(glass.spec.rim.min(extent / 2.0)),
253 // A length, like the lens: the measured drag is one curve of
254 // distance from the rim, the same on a 96pt box and a 320pt
255 // one. Held under half the box, where two reaches would cross.
256 reach: px(glass.spec.reach.min(extent / 2.0)),
257 gain: glass.spec.gain,
258 saturation: glass.spec.saturation,
259 magnify: glass.magnify,
260 dispersion: glass.dispersion,
261 tint: self.tint.unwrap_or(glass.spec.tint),
262 edge: glass.spec.edge,
263 edge_width: px(glass.spec.edge_width),
264 edge_aa: px(glass.spec.edge_aa),
265 };
266 window.paint_layer(bounds, |window| {
267 window.paint_backdrop_blur(bounds, corners, effect);
268 // After the blur, never before: the blur samples what is
269 // beneath it, so a shadow painted first is one the surface
270 // shows through itself. Cut to outside the shape, so it lands
271 // on the page and nowhere else.
272 if glass.spec.shadow {
273 window.paint_drop_shadows_outside(bounds, corners, &theme::surface_shadows());
274 }
275 self.child.paint(window, cx);
276 });
277 } else {
278 self.child.paint(window, cx);
279 }
280 }
281}
282
283impl IntoElement for Surface {
284 type Element = Self;
285
286 fn into_element(self) -> Self::Element {
287 self
288 }
289}
290
291/// Paint `child` in its own scene layer, giving it a fresh draw order above
292/// everything painted so far in the enclosing layer.
293///
294/// Needed for overlays INSIDE a material card: the card's single layer means
295/// every primitive shares one draw order, and equal orders render grouped by
296/// primitive kind (quads, then icons, then images) — so a close button's
297/// circle painted "after" a thumbnail still shows up UNDER the image. A
298/// nested layer restores the intended stacking.
299pub fn layered(child: impl IntoElement) -> Layered {
300 Layered {
301 child: child.into_any_element(),
302 }
303}
304
305pub struct Layered {
306 child: AnyElement,
307}
308
309impl Element for Layered {
310 type RequestLayoutState = ();
311 type PrepaintState = ();
312
313 fn id(&self) -> Option<gpui::ElementId> {
314 None
315 }
316
317 fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
318 None
319 }
320
321 fn request_layout(
322 &mut self,
323 _id: Option<&GlobalElementId>,
324 _inspector_id: Option<&InspectorElementId>,
325 window: &mut Window,
326 cx: &mut App,
327 ) -> (LayoutId, ()) {
328 (self.child.request_layout(window, cx), ())
329 }
330
331 fn prepaint(
332 &mut self,
333 _id: Option<&GlobalElementId>,
334 _inspector_id: Option<&InspectorElementId>,
335 _bounds: Bounds<Pixels>,
336 _request_layout: &mut Self::RequestLayoutState,
337 window: &mut Window,
338 cx: &mut App,
339 ) {
340 self.child.prepaint(window, cx);
341 }
342
343 fn paint(
344 &mut self,
345 _id: Option<&GlobalElementId>,
346 _inspector_id: Option<&InspectorElementId>,
347 bounds: Bounds<Pixels>,
348 _request_layout: &mut Self::RequestLayoutState,
349 _prepaint: &mut Self::PrepaintState,
350 window: &mut Window,
351 cx: &mut App,
352 ) {
353 window.paint_layer(bounds, |window| self.child.paint(window, cx));
354 }
355}
356
357impl IntoElement for Layered {
358 type Element = Self;
359
360 fn into_element(self) -> Self::Element {
361 self
362 }
363}