Skip to main content

fc_ui/capabilities/effects/
glass_morphism.rs

1use gpui::prelude::FluentBuilder as _;
2use gpui::*;
3
4use crate::capabilities::foundation::theme::use_theme;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
7pub enum GlassIntensity {
8    Light,
9    #[default]
10    Medium,
11    Heavy,
12}
13
14#[derive(IntoElement)]
15pub struct GlassMorphism {
16    base: Div,
17    opacity: f32,
18    tint: Option<Hsla>,
19    show_border: bool,
20    intensity: GlassIntensity,
21    noise: bool,
22}
23
24impl GlassMorphism {
25    pub fn new() -> Self {
26        Self {
27            base: div(),
28            opacity: 0.8,
29            tint: None,
30            show_border: true,
31            intensity: GlassIntensity::default(),
32            noise: false,
33        }
34    }
35
36    pub fn opacity(mut self, opacity: f32) -> Self {
37        self.opacity = opacity.clamp(0.0, 1.0);
38        self
39    }
40
41    pub fn tint(mut self, color: Hsla) -> Self {
42        self.tint = Some(color);
43        self
44    }
45
46    pub fn border(mut self, show: bool) -> Self {
47        self.show_border = show;
48        self
49    }
50
51    pub fn intensity(mut self, intensity: GlassIntensity) -> Self {
52        self.intensity = intensity;
53        self
54    }
55
56    pub fn noise(mut self, noise: bool) -> Self {
57        self.noise = noise;
58        self
59    }
60}
61
62impl RenderOnce for GlassMorphism {
63    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
64        let theme = use_theme(cx);
65
66        let (bg_alpha, border_alpha) = match self.intensity {
67            GlassIntensity::Light => (0.05, 0.08),
68            GlassIntensity::Medium => (0.10, 0.15),
69            GlassIntensity::Heavy => (0.20, 0.25),
70        };
71
72        let bg_alpha = bg_alpha * self.opacity;
73        let border_alpha = border_alpha * self.opacity;
74
75        let bg_color = if let Some(tint) = self.tint {
76            hsla(tint.h, tint.s, tint.l, bg_alpha)
77        } else {
78            hsla(0.0, 0.0, 1.0, bg_alpha)
79        };
80
81        let border_color = hsla(0.0, 0.0, 1.0, border_alpha);
82
83        let shadow = BoxShadow {
84            offset: point(px(0.0), px(4.0)),
85            blur_radius: px(12.0),
86            spread_radius: px(0.0),
87            inset: false,
88            color: hsla(0.0, 0.0, 0.0, 0.08 * self.opacity),
89        };
90
91        let mut element = self
92            .base
93            .bg(bg_color)
94            .rounded(theme.tokens.radius_lg)
95            .shadow(smallvec::smallvec![shadow]);
96
97        // gpui 不支持单边异色边框,统一使用单一 border_color。
98        if self.show_border {
99            element = element.border_1().border_color(border_color);
100        }
101
102        let noise = self.noise;
103        let noise_opacity = self.opacity;
104        element.relative().when(noise, |el| {
105            let dot_color = hsla(0.0, 0.0, 1.0, 0.03 * noise_opacity);
106            let mut noise_layer = div().absolute().inset_0().overflow_hidden();
107            let positions: [(f32, f32); 24] = [
108                (0.07, 0.13),
109                (0.23, 0.05),
110                (0.41, 0.19),
111                (0.59, 0.08),
112                (0.77, 0.22),
113                (0.91, 0.11),
114                (0.15, 0.37),
115                (0.33, 0.44),
116                (0.52, 0.31),
117                (0.68, 0.47),
118                (0.84, 0.39),
119                (0.03, 0.55),
120                (0.19, 0.63),
121                (0.45, 0.57),
122                (0.61, 0.69),
123                (0.79, 0.61),
124                (0.93, 0.73),
125                (0.11, 0.81),
126                (0.29, 0.87),
127                (0.47, 0.79),
128                (0.65, 0.91),
129                (0.83, 0.85),
130                (0.37, 0.95),
131                (0.71, 0.03),
132            ];
133            for (px_pct, py_pct) in positions {
134                noise_layer = noise_layer.child(
135                    div()
136                        .absolute()
137                        .left(relative(px_pct))
138                        .top(relative(py_pct))
139                        .w(px(1.0))
140                        .h(px(1.0))
141                        .rounded_full()
142                        .bg(dot_color),
143                );
144            }
145            el.child(noise_layer)
146        })
147    }
148}
149
150impl Default for GlassMorphism {
151    fn default() -> Self {
152        Self::new()
153    }
154}
155
156impl Styled for GlassMorphism {
157    fn style(&mut self) -> &mut StyleRefinement {
158        self.base.style()
159    }
160}
161
162impl InteractiveElement for GlassMorphism {
163    fn interactivity(&mut self) -> &mut Interactivity {
164        self.base.interactivity()
165    }
166}
167
168impl StatefulInteractiveElement for GlassMorphism {}
169
170impl ParentElement for GlassMorphism {
171    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
172        self.base.extend(elements)
173    }
174}