1use gpui_kit::{
6 Bounds, Corners, Edges, Hsla, PathBuilder, Pixels, Point, Window, bounds, linear_color_stop,
7 linear_gradient, point, px, quad, size,
8};
9
10#[derive(Clone, Copy, Debug, PartialEq)]
12pub struct GradientStop {
13 pub offset: f32,
14 pub color: Hsla,
15}
16
17impl GradientStop {
18 pub const fn new(offset: f32, color: Hsla) -> Self {
19 Self { offset, color }
20 }
21}
22
23pub fn sample_stops(stops: &[GradientStop], t: f32) -> Hsla {
25 if stops.is_empty() {
26 return Hsla::transparent_black();
27 }
28 let t = t.clamp(0.0, 1.0);
29 if t <= stops[0].offset || stops.len() == 1 {
30 return stops[0].color;
31 }
32 for pair in stops.windows(2) {
33 let a = pair[0];
34 let b = pair[1];
35 if t <= b.offset {
36 let span = (b.offset - a.offset).max(1e-6);
37 let local = ((t - a.offset) / span).clamp(0.0, 1.0);
38 return lerp_hsla(a.color, b.color, local);
39 }
40 }
41 stops[stops.len() - 1].color
42}
43
44fn lerp_hsla(a: Hsla, b: Hsla, t: f32) -> Hsla {
45 Hsla {
46 h: a.h + (b.h - a.h) * t,
47 s: a.s + (b.s - a.s) * t,
48 l: a.l + (b.l - a.l) * t,
49 a: a.a + (b.a - a.a) * t,
50 }
51}
52
53#[derive(Clone, Debug)]
55pub struct RadialGradientSpec {
56 pub center: Point<Pixels>,
57 pub radius: Pixels,
58 pub stops: Vec<GradientStop>,
59 pub rings: usize,
60 pub segments: usize,
61}
62
63impl RadialGradientSpec {
64 pub fn new(center: Point<Pixels>, radius: Pixels, stops: Vec<GradientStop>) -> Self {
65 Self {
66 center,
67 radius,
68 stops,
69 rings: 24,
70 segments: 48,
71 }
72 }
73}
74
75pub fn paint_radial_gradient(spec: &RadialGradientSpec, window: &mut Window) {
76 let radius = f32::from(spec.radius);
77 if radius <= 0.0 || spec.stops.is_empty() || spec.rings == 0 || spec.segments < 3 {
78 return;
79 }
80 let cx = f32::from(spec.center.x);
81 let cy = f32::from(spec.center.y);
82
83 for ring in (0..spec.rings).rev() {
85 let t = (ring + 1) as f32 / spec.rings as f32;
86 let r = radius * t;
87 let color = sample_stops(&spec.stops, t);
88 let mut builder = PathBuilder::fill();
89 for seg in 0..spec.segments {
90 let angle = std::f32::consts::TAU * (seg as f32 / spec.segments as f32);
91 let pt = point(px(cx + r * angle.cos()), px(cy + r * angle.sin()));
92 if seg == 0 {
93 builder.move_to(pt);
94 } else {
95 builder.line_to(pt);
96 }
97 }
98 builder.close();
99 if let Ok(path) = builder.build() {
100 window.paint_path(path, color);
101 }
102 }
103}
104
105#[derive(Clone, Debug)]
107pub struct ConicGradientSpec {
108 pub center: Point<Pixels>,
109 pub radius: Pixels,
110 pub start_angle_deg: f32,
111 pub stops: Vec<GradientStop>,
112 pub slices: usize,
113}
114
115impl ConicGradientSpec {
116 pub fn new(center: Point<Pixels>, radius: Pixels, stops: Vec<GradientStop>) -> Self {
117 Self {
118 center,
119 radius,
120 start_angle_deg: 0.0,
121 stops,
122 slices: 64,
123 }
124 }
125}
126
127pub fn paint_conic_gradient(spec: &ConicGradientSpec, window: &mut Window) {
128 let radius = f32::from(spec.radius);
129 if radius <= 0.0 || spec.stops.is_empty() || spec.slices < 3 {
130 return;
131 }
132 let cx = f32::from(spec.center.x);
133 let cy = f32::from(spec.center.y);
134 let start_rad = spec.start_angle_deg.to_radians();
135
136 for i in 0..spec.slices {
137 let t0 = i as f32 / spec.slices as f32;
138 let t1 = (i + 1) as f32 / spec.slices as f32;
139 let a0 = start_rad + std::f32::consts::TAU * t0;
140 let a1 = start_rad + std::f32::consts::TAU * t1;
141 let color = sample_stops(&spec.stops, 0.5 * (t0 + t1));
142
143 let mut builder = PathBuilder::fill();
144 builder.move_to(point(px(cx), px(cy)));
145 builder.line_to(point(
146 px(cx + radius * a0.cos()),
147 px(cy + radius * a0.sin()),
148 ));
149 builder.line_to(point(
150 px(cx + radius * a1.cos()),
151 px(cy + radius * a1.sin()),
152 ));
153 builder.close();
154 if let Ok(path) = builder.build() {
155 window.paint_path(path, color);
156 }
157 }
158}
159
160#[derive(Clone, Copy, Debug, Eq, PartialEq)]
162pub enum EdgeFadeSide {
163 Left,
164 Right,
165 Top,
166 Bottom,
167}
168
169#[derive(Clone, Copy, Debug, PartialEq)]
172pub struct EdgeFadeMask {
173 pub side: EdgeFadeSide,
174 pub extent: Pixels,
175 pub surface_color: Hsla,
176}
177
178impl EdgeFadeMask {
179 pub const fn new(side: EdgeFadeSide, extent: Pixels, surface_color: Hsla) -> Self {
180 Self {
181 side,
182 extent,
183 surface_color,
184 }
185 }
186
187 pub fn bounds_within(self, container: Bounds<Pixels>) -> Bounds<Pixels> {
188 let extent = f32::from(self.extent);
189 match self.side {
190 EdgeFadeSide::Left => bounds(
191 container.origin,
192 size(
193 px(extent.min(f32::from(container.size.width))),
194 container.size.height,
195 ),
196 ),
197 EdgeFadeSide::Right => {
198 let w = extent.min(f32::from(container.size.width));
199 bounds(
200 point(
201 container.origin.x + container.size.width - px(w),
202 container.origin.y,
203 ),
204 size(px(w), container.size.height),
205 )
206 }
207 EdgeFadeSide::Top => bounds(
208 container.origin,
209 size(
210 container.size.width,
211 px(extent.min(f32::from(container.size.height))),
212 ),
213 ),
214 EdgeFadeSide::Bottom => {
215 let h = extent.min(f32::from(container.size.height));
216 bounds(
217 point(
218 container.origin.x,
219 container.origin.y + container.size.height - px(h),
220 ),
221 size(container.size.width, px(h)),
222 )
223 }
224 }
225 }
226
227 pub const fn gradient_angle_deg(self) -> f32 {
229 match self.side {
230 EdgeFadeSide::Left => 90.0,
231 EdgeFadeSide::Right => 270.0,
232 EdgeFadeSide::Top => 180.0,
233 EdgeFadeSide::Bottom => 0.0,
234 }
235 }
236
237 pub fn paint(self, container: Bounds<Pixels>, window: &mut Window) {
238 let rect = self.bounds_within(container);
239 let transparent = Hsla {
240 a: 0.0,
241 ..self.surface_color
242 };
243 let bg = linear_gradient(
244 self.gradient_angle_deg(),
245 linear_color_stop(self.surface_color, 0.0),
246 linear_color_stop(transparent, 1.0),
247 );
248 window.paint_quad(quad(
249 rect,
250 Corners::default(),
251 bg,
252 Edges::default(),
253 Hsla::transparent_black(),
254 gpui_kit::BorderStyle::Solid,
255 ));
256 }
257}
258
259#[cfg(test)]
260mod tests {
261 use super::*;
262
263 #[test]
264 fn sample_stops_interpolates_midpoint_and_clamps() {
265 let stops = [
266 GradientStop::new(
267 0.0,
268 Hsla {
269 h: 0.0,
270 s: 1.0,
271 l: 0.0,
272 a: 1.0,
273 },
274 ),
275 GradientStop::new(
276 1.0,
277 Hsla {
278 h: 0.5,
279 s: 1.0,
280 l: 1.0,
281 a: 0.0,
282 },
283 ),
284 ];
285 let mid = sample_stops(&stops, 0.5);
286 assert!((mid.h - 0.25).abs() < 1e-5);
287 assert!((mid.l - 0.5).abs() < 1e-5);
288 assert!((mid.a - 0.5).abs() < 1e-5);
289 }
290
291 #[test]
292 fn edge_fade_mask_computes_exact_edge_rects() {
293 let container = bounds(point(px(10.0), px(20.0)), size(px(200.0), px(40.0)));
294 let left =
295 EdgeFadeMask::new(EdgeFadeSide::Left, px(35.0), Hsla::black()).bounds_within(container);
296 assert_eq!(left.origin, point(px(10.0), px(20.0)));
297 assert_eq!(left.size, size(px(35.0), px(40.0)));
298
299 let right = EdgeFadeMask::new(EdgeFadeSide::Right, px(35.0), Hsla::black())
300 .bounds_within(container);
301 assert_eq!(right.origin, point(px(175.0), px(20.0)));
302 assert_eq!(right.size, size(px(35.0), px(40.0)));
303 }
304}