Skip to main content

theme/theme/
glass.rs

1//! Glass recipes: translucent chrome over the blurred window background, and
2//! the modal scrim.
3
4use gpui::{Hsla, WindowBackgroundAppearance, hsla};
5
6use crate::{Appearance, color, paint, theme::Theme};
7
8impl Theme {
9    /// The frost tint painted over the blurred window background (macOS glass):
10    /// [`Self::vibrancy_tone`] at [`Brand::vibrancy_alpha`](crate::Brand::vibrancy_alpha).
11    /// Opaque, this IS the surface tone.
12    pub fn vibrancy_tint(&self) -> Hsla {
13        let alpha = self.vibrancy_alpha;
14        if alpha >= 1.0 {
15            return self.surface;
16        }
17        self.vibrancy_tone.opacity(alpha)
18    }
19
20    /// The app's root fill: the frost where glass is on, the opaque panel
21    /// where it is not. What a root element paints instead of
22    /// [`Self::bg`](Self#structfield.bg), so a window that opens blurred is not
23    /// then covered over by the paint that made the blur pointless.
24    pub fn window_bg(&self) -> Hsla {
25        if self.vibrancy {
26            self.vibrancy_tint()
27        } else {
28            self.bg
29        }
30    }
31
32    /// The translucent tint floating cards paint over their backdrop blur
33    /// (see `bezel::material`). Dark: the reference `.glass-surface` menu tint
34    /// verbatim — `oklch(0.33 0 0 / 34%)`. Light: heavier, since white over a
35    /// bright backdrop separates a row from it by less.
36    ///
37    /// Some callers paint this *over* a [`Surface`](crate::SurfaceStyle) —
38    /// `control_bar` does — so coverage here hides the lens under it.
39    pub fn glass_overlay(&self) -> Hsla {
40        match self.appearance {
41            Appearance::Dark => color::oklch(0.33, 0.0, 0.0).opacity(0.34),
42            Appearance::Light => self.surface_overlay.opacity(0.55),
43        }
44    }
45
46    /// The composer pill / question panel fill. Light's `input_bg` is opaque
47    /// white (the elevation ladder on an opaque page) — over glass it read as
48    /// a solid slab in front of the frosted blur, so it thins to a
49    /// translucent tint there (0.6 and then 0.45 both still read too bright
50    /// over the 0.80 frost — lowered on user request). Dark's 3% white wash
51    /// is already glass-native.
52    pub fn input_glass_bg(&self) -> Hsla {
53        if self.glass && matches!(self.appearance, Appearance::Light) {
54            self.input_bg.opacity(0.30)
55        } else {
56            self.input_bg
57        }
58    }
59
60    /// Section-card fill — the group box, and the in-panel cards built like it.
61    ///
62    /// Each appearance plates in the direction it has room in: dark lifts on a
63    /// white wash (`../desktop`'s `--color-card`), light lands a white card
64    /// over whatever is beneath it. An opaque platform has no frost beneath the
65    /// card, so it takes the grey below its white page.
66    pub fn card_glass_bg(&self) -> Hsla {
67        if !self.glass {
68            return self.surface;
69        }
70        match self.appearance {
71            Appearance::Dark => hsla(0.0, 0.0, 1.0, 0.06),
72            Appearance::Light => self.surface_card.opacity(0.60),
73        }
74    }
75
76    /// The standard modal backdrop — see [`scrim`](crate::paint::scrim).
77    pub fn scrim(&self) -> Hsla {
78        paint::scrim_for(self.appearance, paint::SCRIM_ALPHA_DARK)
79    }
80
81    /// How the platform should composite the window behind our paint.
82    ///
83    /// Windows takes Mica, which is DWM's own backdrop; `Blurred` there is the
84    /// undocumented accent API's acrylic at a zero tint, which reads as plain
85    /// transparency. Mica tints from the desktop wallpaper rather than from
86    /// the windows behind, so what it carries is not what vibrancy carries.
87    ///
88    /// This is a method rather than a constant because it has to be *re-applied* after
89    /// every theme swap: gpui's macOS backend tears the `NSVisualEffectView`
90    /// out of the hierarchy whenever the value is anything but `Blurred`, and
91    /// the re-apply in `appearance::apply` is what restores vibrancy when the
92    /// user switches back to dark. See zed's `crates/zed/src/main.rs`, which
93    /// runs the same loop on every settings change.
94    pub fn window_background_appearance(&self) -> WindowBackgroundAppearance {
95        match (self.vibrancy, cfg!(target_os = "windows")) {
96            (false, _) => WindowBackgroundAppearance::Opaque,
97            (true, true) => WindowBackgroundAppearance::MicaBackdrop,
98            (true, false) => WindowBackgroundAppearance::Blurred,
99        }
100    }
101}