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, Material, 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    /// What a content card paints over [`Self::sidebar_bg`]:
33    /// [`Self::content_background`] at [`Self::content_opacity`] where
34    /// [`Self::vibrancy`] is on, and opaque [`Self::bg`](Self#structfield.bg)
35    /// where it is not.
36    pub fn content_bg(&self) -> Hsla {
37        match self.vibrancy {
38            true => self.content_background.opacity(self.content_opacity),
39            false => self.bg,
40        }
41    }
42
43    /// What a content card sitting back paints over [`Self::sidebar_bg`]:
44    /// [`Self::content_bg`]'s fill at [`Material::Thin`]'s share of
45    /// [`Material::Thick`]'s coverage.
46    pub fn content_resting_bg(&self) -> Hsla {
47        let share = Material::Thin.opacity() / Material::Thick.opacity();
48        match self.vibrancy {
49            true => self
50                .content_background
51                .opacity(self.content_opacity * share),
52            false => color::over(self.bg.opacity(share), self.surface),
53        }
54    }
55
56    /// A sidebar's fill: the frost where glass is on, [`Self::surface`] where
57    /// it is not.
58    pub fn sidebar_bg(&self) -> Hsla {
59        if self.vibrancy {
60            self.vibrancy_tint()
61        } else {
62            self.surface
63        }
64    }
65
66    /// The translucent tint floating cards paint over their backdrop blur
67    /// (see `bezel::material`). Dark: the reference `.glass-surface` menu tint
68    /// verbatim — `oklch(0.33 0 0 / 34%)`. Light: heavier, since white over a
69    /// bright backdrop separates a row from it by less.
70    ///
71    /// Some callers paint this *over* a [`Surface`](crate::SurfaceStyle) —
72    /// `control_bar` does — so coverage here hides the lens under it.
73    pub fn glass_overlay(&self) -> Hsla {
74        match self.appearance {
75            Appearance::Dark => color::oklch(0.33, 0.0, 0.0).opacity(0.34),
76            Appearance::Light => self.surface_overlay.opacity(0.55),
77        }
78    }
79
80    /// The composer pill / question panel fill. Light's `input_bg` is opaque
81    /// white (the elevation ladder on an opaque page) — over glass it read as
82    /// a solid slab in front of the blurred backdrop, so it thins to a
83    /// translucent tint there (0.6 and then 0.45 both still read too bright
84    /// over the 0.80 frost — lowered on user request). Dark's 3% white wash
85    /// is already glass-native.
86    pub fn input_glass_bg(&self) -> Hsla {
87        if self.glass && matches!(self.appearance, Appearance::Light) {
88            self.input_bg.opacity(0.30)
89        } else {
90            self.input_bg
91        }
92    }
93
94    /// Section-card fill — the group box, and the in-panel cards built like it.
95    ///
96    /// Each appearance plates in the direction it has room in: dark lifts on a
97    /// white wash (`../desktop`'s `--color-card`), light lands a white card
98    /// over whatever is beneath it. An opaque platform has no frost beneath the
99    /// card, so it takes the grey below its white page.
100    pub fn card_glass_bg(&self) -> Hsla {
101        if !self.glass {
102            return self.surface;
103        }
104        match self.appearance {
105            Appearance::Dark => hsla(0.0, 0.0, 1.0, 0.06),
106            Appearance::Light => self.surface_card.opacity(0.60),
107        }
108    }
109
110    /// The standard modal backdrop — see [`scrim`](crate::paint::scrim).
111    pub fn scrim(&self) -> Hsla {
112        paint::scrim_for(self.appearance, paint::SCRIM_ALPHA_DARK)
113    }
114
115    /// How the platform should composite the window behind our paint.
116    ///
117    /// Windows takes Mica, which is DWM's own backdrop; `Blurred` there is the
118    /// undocumented accent API's acrylic at a zero tint, which reads as plain
119    /// transparency. Mica tints from the desktop wallpaper rather than from
120    /// the windows behind, so what it carries is not what vibrancy carries.
121    ///
122    /// This is a method rather than a constant because it has to be *re-applied* after
123    /// every theme swap: gpui's macOS backend tears the `NSVisualEffectView`
124    /// out of the hierarchy whenever the value is anything but `Blurred`, and
125    /// the re-apply in `appearance::apply` is what restores vibrancy when the
126    /// user switches back to dark. See zed's `crates/zed/src/main.rs`, which
127    /// runs the same loop on every settings change.
128    pub fn window_background_appearance(&self) -> WindowBackgroundAppearance {
129        match (self.vibrancy, cfg!(target_os = "windows")) {
130            (false, _) => WindowBackgroundAppearance::Opaque,
131            (true, true) => WindowBackgroundAppearance::MicaBackdrop,
132            (true, false) => WindowBackgroundAppearance::Blurred,
133        }
134    }
135}