1use gpui::{
2 AnyElement, App, Axis, BorderStyle, Bounds, ContentMask, Corners, Element, ElementId,
3 GlobalElementId, Hsla, InspectorElementId, IntoElement, LayoutId, PathBuilder, Pixels, Point,
4 Window, point, px, quad, size,
5};
6use gpui_material_foundation::{color, current_material_theme};
7use gpui_material_theme::{ColorRole, CornerRole, CornerShape};
8
9#[derive(Clone, Copy, Debug, PartialEq)]
11pub struct CarouselBorder {
12 pub width: Pixels,
14 pub color: Hsla,
16}
17
18pub struct CarouselParallax {
24 child: AnyElement,
25 axis: Axis,
26 viewport: Option<Bounds<Pixels>>,
27 shape: CornerShape,
28 border: Option<CarouselBorder>,
29 backdrop: Option<Hsla>,
30}
31
32impl CarouselParallax {
33 pub fn new(content: impl IntoElement) -> Self {
35 Self {
36 child: content.into_any_element(),
37 axis: Axis::Horizontal,
38 viewport: None,
39 shape: CornerRole::ExtraLarge.shape(),
40 border: None,
41 backdrop: None,
42 }
43 }
44
45 pub fn axis(mut self, axis: Axis) -> Self {
47 self.axis = axis;
48 self
49 }
50
51 pub fn viewport(mut self, viewport: Bounds<Pixels>) -> Self {
53 self.viewport = Some(viewport);
54 self
55 }
56
57 pub fn shape(mut self, shape: CornerShape) -> Self {
59 self.shape = shape;
60 self
61 }
62
63 pub fn border(mut self, border: CarouselBorder) -> Self {
65 self.border = Some(border);
66 self
67 }
68
69 pub fn backdrop(mut self, backdrop: Hsla) -> Self {
72 self.backdrop = Some(Hsla { a: 1., ..backdrop });
73 self
74 }
75}
76
77struct Mask {
78 bounds: Bounds<Pixels>,
79 offset: Point<Pixels>,
80}
81
82impl Mask {
83 fn new(
84 item: Bounds<Pixels>,
85 viewport: Bounds<Pixels>,
86 axis: Axis,
87 reduce_motion: bool,
88 ) -> Self {
89 let (start, end, clip_start, clip_end, cross) = match axis {
90 Axis::Horizontal => (
91 item.left(),
92 item.right(),
93 viewport.left(),
94 viewport.right(),
95 item.size.height,
96 ),
97 Axis::Vertical => (
98 item.top(),
99 item.bottom(),
100 viewport.top(),
101 viewport.bottom(),
102 item.size.width,
103 ),
104 };
105 let extent = end - start;
106 let mut crop_start = px(0.);
107 let mut crop_end = px(0.);
108 let mut translate = px(0.);
109 if !reduce_motion && extent > px(0.) && cross > px(0.) {
110 let ratio = f32::from(extent) / f32::from(cross);
111 let lerp = |a: f32, b: f32, lo, hi| {
112 let progress: f32 = (ratio - lo) / (hi - lo);
113 a + (b - a) * progress.clamp(0., 1.)
114 };
115 let intensity = if ratio < 1. {
116 lerp(0.25, 1. / 3., 9. / 16., 1.)
117 } else {
118 lerp(1. / 3., 0.5, 1., 16. / 9.)
119 };
120 if start < clip_start {
122 let distance = (clip_start - start).min(extent);
123 crop_start = distance * (1. - intensity);
124 translate = distance * intensity;
125 } else if end > clip_end {
126 let distance = (end - clip_end).min(extent);
127 crop_end = distance * (1. - intensity);
128 translate = -distance * intensity;
129 }
130 }
131 let (crop, cropped_size, offset) = match axis {
132 Axis::Horizontal => (
133 point(crop_start, px(0.)),
134 size(extent - crop_start - crop_end, item.size.height),
135 point(translate, px(0.)),
136 ),
137 Axis::Vertical => (
138 point(px(0.), crop_start),
139 size(item.size.width, extent - crop_start - crop_end),
140 point(px(0.), translate),
141 ),
142 };
143 Self {
144 bounds: Bounds::new(item.origin + crop, cropped_size),
145 offset,
146 }
147 }
148}
149
150#[doc(hidden)]
152pub struct CarouselParallaxPaint {
153 mask: Bounds<Pixels>,
154 clip: Bounds<Pixels>,
155}
156
157impl Element for CarouselParallax {
158 type RequestLayoutState = ();
159 type PrepaintState = Option<CarouselParallaxPaint>;
160
161 fn id(&self) -> Option<ElementId> {
162 None
163 }
164
165 fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
166 None
167 }
168
169 fn request_layout(
170 &mut self,
171 _: Option<&GlobalElementId>,
172 _: Option<&InspectorElementId>,
173 window: &mut Window,
174 cx: &mut App,
175 ) -> (LayoutId, ()) {
176 (self.child.request_layout(window, cx), ())
177 }
178
179 fn prepaint(
180 &mut self,
181 _: Option<&GlobalElementId>,
182 _: Option<&InspectorElementId>,
183 bounds: Bounds<Pixels>,
184 _: &mut (),
185 window: &mut Window,
186 cx: &mut App,
187 ) -> Self::PrepaintState {
188 let parent_clip = window.content_mask().bounds;
189 let viewport = self.viewport.unwrap_or(parent_clip);
190 let mask = Mask::new(bounds, viewport, self.axis, cx.reduce_motion());
191 let clip = mask.bounds.intersect(&viewport).intersect(&parent_clip);
192 if clip.is_empty() {
193 return None;
194 }
195 window.with_content_mask(Some(ContentMask { bounds: clip }), |window| {
196 window.with_element_offset(mask.offset, |window| {
197 self.child.prepaint(window, cx);
198 });
199 });
200 Some(CarouselParallaxPaint {
201 mask: mask.bounds,
202 clip,
203 })
204 }
205
206 fn paint(
207 &mut self,
208 _: Option<&GlobalElementId>,
209 _: Option<&InspectorElementId>,
210 _: Bounds<Pixels>,
211 _: &mut (),
212 paint: &mut Self::PrepaintState,
213 window: &mut Window,
214 cx: &mut App,
215 ) {
216 let Some(paint) = paint else { return };
217 let theme = current_material_theme(window, cx);
218 let backdrop = self
219 .backdrop
220 .unwrap_or_else(|| color(theme.colors.color(ColorRole::Surface)));
221 let radii = self
222 .shape
223 .radii(
224 paint.mask.size.width.into(),
225 paint.mask.size.height.into(),
226 theme.layout.direction,
227 )
228 .map(px);
229 window.with_content_mask(Some(ContentMask { bounds: paint.clip }), |window| {
230 self.child.paint(window, cx);
231 paint_corners(paint.mask, radii, backdrop, window);
232 if let Some(border) = self.border {
233 let width = px(super::nonnegative(border.width))
234 .min(paint.mask.size.width.min(paint.mask.size.height) / 2.);
235 if width > px(0.) {
236 let [top_left, top_right, bottom_right, bottom_left] = radii;
237 window.paint_quad(quad(
238 paint.mask,
239 Corners {
240 top_left,
241 top_right,
242 bottom_right,
243 bottom_left,
244 },
245 gpui::transparent_black(),
246 width,
247 border.color,
248 BorderStyle::Solid,
249 ));
250 }
251 }
252 });
253 }
254}
255
256fn paint_corners(bounds: Bounds<Pixels>, radii: [Pixels; 4], backdrop: Hsla, window: &mut Window) {
257 if radii.iter().all(|&r| r <= px(0.)) {
258 return;
259 }
260 let mut cutouts = PathBuilder::fill();
261 let corners = [
262 (point(bounds.left(), bounds.top()), 1., 1.),
263 (point(bounds.right(), bounds.top()), -1., 1.),
264 (point(bounds.right(), bounds.bottom()), -1., -1.),
265 (point(bounds.left(), bounds.bottom()), 1., -1.),
266 ];
267 for ((corner, x, y), radius) in corners.into_iter().zip(radii) {
268 if radius <= px(0.) {
269 continue;
270 }
271 let control = radius * (1. - 0.552_284_8);
273 cutouts.move_to(corner);
274 cutouts.line_to(corner + point(radius * x, px(0.)));
275 cutouts.cubic_bezier_to(
276 corner + point(px(0.), radius * y),
277 corner + point(control * x, px(0.)),
278 corner + point(px(0.), control * y),
279 );
280 cutouts.close();
281 }
282 if let Ok(path) = cutouts.build() {
283 window.paint_path(path, backdrop);
284 }
285}