1use gpui_kit::{
8 Bounds, Corners, Pixels, Point, TransformationMatrix, bounds, point, px, radians, size,
9};
10
11#[derive(Clone, Copy, Debug, PartialEq)]
13pub struct TransformOrigin {
14 pub x: f32,
15 pub y: f32,
16}
17
18impl TransformOrigin {
19 pub const CENTER: Self = Self { x: 0.5, y: 0.5 };
20 pub const TOP_CENTER: Self = Self { x: 0.5, y: 0.0 };
21 pub const LEFT_CENTER: Self = Self { x: 0.0, y: 0.5 };
22 pub const RIGHT_CENTER: Self = Self { x: 1.0, y: 0.5 };
23 pub const BOTTOM_CENTER: Self = Self { x: 0.5, y: 1.0 };
24
25 pub const fn new(x: f32, y: f32) -> Self {
26 Self { x, y }
27 }
28}
29
30impl Default for TransformOrigin {
31 fn default() -> Self {
32 Self::CENTER
33 }
34}
35
36#[derive(Clone, Copy, Debug, PartialEq)]
38pub struct Transform2D {
39 pub translate_x: Pixels,
40 pub translate_y: Pixels,
41 pub scale_x: f32,
42 pub scale_y: f32,
43 pub rotation_radians: f32,
44 pub origin: TransformOrigin,
45}
46
47impl Transform2D {
48 pub const IDENTITY: Self = Self {
49 translate_x: px(0.0),
50 translate_y: px(0.0),
51 scale_x: 1.0,
52 scale_y: 1.0,
53 rotation_radians: 0.0,
54 origin: TransformOrigin::CENTER,
55 };
56
57 pub const fn translate(mut self, x: Pixels, y: Pixels) -> Self {
58 self.translate_x = x;
59 self.translate_y = y;
60 self
61 }
62
63 pub const fn scale(mut self, s: f32) -> Self {
64 self.scale_x = s;
65 self.scale_y = s;
66 self
67 }
68
69 pub const fn scale_xy(mut self, sx: f32, sy: f32) -> Self {
70 self.scale_x = sx;
71 self.scale_y = sy;
72 self
73 }
74
75 pub fn rotate_deg(mut self, degrees: f32) -> Self {
76 self.rotation_radians = degrees.to_radians();
77 self
78 }
79
80 pub const fn origin(mut self, origin: TransformOrigin) -> Self {
81 self.origin = origin;
82 self
83 }
84
85 pub fn apply_bounds(self, base: Bounds<Pixels>) -> Bounds<Pixels> {
87 let scaled = scaled_bounds(base, self.scale_x, self.scale_y, self.origin);
88 bounds(
89 point(
90 scaled.origin.x + self.translate_x,
91 scaled.origin.y + self.translate_y,
92 ),
93 scaled.size,
94 )
95 }
96
97 pub fn pivot_swing_offset(self, height: Pixels) -> Point<Pixels> {
101 let h = f32::from(height);
102 let arm = (self.origin.y - 0.5) * h;
103 let dx = arm * self.rotation_radians.sin();
104 let dy = arm * (1.0 - self.rotation_radians.cos());
105 point(px(dx), px(dy))
106 }
107}
108
109impl Default for Transform2D {
110 fn default() -> Self {
111 Self::IDENTITY
112 }
113}
114
115pub fn scaled_bounds(
117 base: Bounds<Pixels>,
118 scale_x: f32,
119 scale_y: f32,
120 origin: TransformOrigin,
121) -> Bounds<Pixels> {
122 let sx = scale_x.max(0.0);
123 let sy = scale_y.max(0.0);
124 let bw = f32::from(base.size.width);
125 let bh = f32::from(base.size.height);
126 let new_w = bw * sx;
127 let new_h = bh * sy;
128 let anchor_x = f32::from(base.origin.x) + bw * origin.x;
129 let anchor_y = f32::from(base.origin.y) + bh * origin.y;
130 let new_x = anchor_x - new_w * origin.x;
131 let new_y = anchor_y - new_h * origin.y;
132 bounds(point(px(new_x), px(new_y)), size(px(new_w), px(new_h)))
133}
134
135pub fn scaled_corners(corners: Corners<Pixels>, scale_x: f32, scale_y: f32) -> Corners<Pixels> {
137 let s = scale_x.min(scale_y).max(0.0);
138 Corners {
139 top_left: px(f32::from(corners.top_left) * s),
140 top_right: px(f32::from(corners.top_right) * s),
141 bottom_right: px(f32::from(corners.bottom_right) * s),
142 bottom_left: px(f32::from(corners.bottom_left) * s),
143 }
144}
145
146pub fn svg_transform_matrix(
148 rotation_radians: f32,
149 scale_x: f32,
150 scale_y: f32,
151) -> TransformationMatrix {
152 let rot = TransformationMatrix::unit().rotate(radians(rotation_radians));
153 let scale = TransformationMatrix::unit().scale(gpui_kit::size(scale_x, scale_y));
154 rot.compose(scale)
155}
156
157#[derive(Clone, Copy, Debug, PartialEq)]
159pub enum RevealMask {
160 Radial { origin: Point<Pixels> },
161 Sweep,
162 Split,
163 Rise,
164}
165
166pub fn reveal_regions(
169 area: Bounds<Pixels>,
170 kind: RevealMask,
171 progress: f32,
172 band_height: Pixels,
173) -> Vec<Bounds<Pixels>> {
174 let p = if progress.is_finite() {
175 progress.clamp(0.0, 1.0)
176 } else {
177 0.0
178 };
179 if p <= 0.0 {
180 return Vec::new();
181 }
182 if p >= 1.0 {
183 return vec![area];
184 }
185 let w = f32::from(area.size.width);
186 let h = f32::from(area.size.height);
187 match kind {
188 RevealMask::Split => vec![bounds(
189 point(area.left() + px(w * (1.0 - p) * 0.5), area.top()),
190 size(px(w * p), area.size.height),
191 )],
192 RevealMask::Rise => vec![bounds(
193 point(area.left(), area.bottom() - px(h * p)),
194 size(area.size.width, px(h * p)),
195 )],
196 RevealMask::Sweep | RevealMask::Radial { .. } => {
197 let (ox, oy, radius) = match kind {
198 RevealMask::Radial { origin } => {
199 let x = f32::from(origin.x - area.left()).clamp(0.0, w);
200 let y = f32::from(origin.y - area.top()).clamp(0.0, h);
201 (x, y, x.max(w - x).hypot(y.max(h - y)) * p)
202 }
203 _ => {
205 let r = w.hypot(h);
206 (-r + (w + r) * p, h * 0.5, r)
209 }
210 };
211 let step = f32::from(band_height).max(0.5);
212 let mut regions = Vec::new();
213 let mut y = 0.0;
214 while y < h {
215 let bh = step.min(h - y);
216 let dy = y + bh * 0.5 - oy;
217 if dy.abs() < radius {
218 let dx = (radius * radius - dy * dy).sqrt();
219 let l = (ox - dx).max(0.0);
220 let r = (ox + dx).min(w);
221 if r > l {
222 regions.push(bounds(
223 point(area.left() + px(l), area.top() + px(y)),
224 size(px(r - l), px(bh)),
225 ));
226 }
227 }
228 y += bh;
229 }
230 regions
231 }
232 }
233}
234
235#[cfg(test)]
236mod tests {
237 use super::*;
238
239 #[test]
240 fn reveal_masks_have_distinct_midpoints_and_exact_endpoints() {
241 let area = bounds(point(px(0.0), px(0.0)), size(px(240.0), px(140.0)));
242 let kinds = [
243 RevealMask::Radial {
244 origin: point(px(30.0), px(20.0)),
245 },
246 RevealMask::Sweep,
247 RevealMask::Split,
248 RevealMask::Rise,
249 ];
250 let samples: Vec<_> = kinds
251 .into_iter()
252 .map(|kind| {
253 assert!(reveal_regions(area, kind, 0.0, px(1.0)).is_empty());
254 assert_eq!(reveal_regions(area, kind, 1.0, px(1.0)), vec![area]);
255 let mid = reveal_regions(area, kind, 0.5, px(1.0));
256 assert!(!mid.is_empty());
257 for rect in &mid {
258 assert!(rect.left() >= area.left() && rect.right() <= area.right());
259 assert!(rect.top() >= area.top() && rect.bottom() <= area.bottom());
260 }
261 mid
262 })
263 .collect();
264 for a in 0..samples.len() {
265 for b in a + 1..samples.len() {
266 assert_ne!(samples[a], samples[b]);
267 }
268 }
269 }
270
271 #[test]
272 fn reveal_masks_never_uncover_previously_revealed_pixels() {
273 let area = bounds(point(px(23.0), px(41.0)), size(px(240.0), px(140.0)));
274 for kind in [
275 RevealMask::Radial {
276 origin: area.bottom_right(),
277 },
278 RevealMask::Sweep,
279 RevealMask::Split,
280 RevealMask::Rise,
281 ] {
282 let mut previous = vec![false; 240 * 140];
283 for frame in 0..=20 {
284 let regions = reveal_regions(area, kind, frame as f32 / 20.0, px(1.0));
285 for y in 0..140 {
286 for x in 0..240 {
287 let point = area.origin + point(px(x as f32 + 0.5), px(y as f32 + 0.5));
288 let covered = regions.iter().any(|region| region.contains(&point));
289 let index = y * 240 + x;
290 assert!(
291 !previous[index] || covered,
292 "{kind:?}: frame {frame}, ({x}, {y}) uncovered"
293 );
294 previous[index] = covered;
295 }
296 }
297 }
298 assert!(previous.iter().all(|covered| *covered));
299 }
300 }
301
302 #[test]
303 fn scaled_bounds_keeps_origin_point_fixed() {
304 let base = bounds(point(px(10.0), px(20.0)), size(px(100.0), px(40.0)));
305
306 let c = scaled_bounds(base, 0.8, 0.5, TransformOrigin::CENTER);
308 assert!((f32::from(c.origin.x) - 20.0).abs() < 1e-4);
309 assert!((f32::from(c.origin.y) - 30.0).abs() < 1e-4);
310 assert!((f32::from(c.size.width) - 80.0).abs() < 1e-4);
311 assert!((f32::from(c.size.height) - 20.0).abs() < 1e-4);
312
313 let top = scaled_bounds(base, 0.95, 0.95, TransformOrigin::TOP_CENTER);
315 assert!((f32::from(top.origin.y) - 20.0).abs() < 1e-4);
316 assert!((f32::from(top.origin.x) - 12.5).abs() < 1e-4);
317
318 let left = scaled_bounds(base, 1.15, 0.9, TransformOrigin::LEFT_CENTER);
320 assert!((f32::from(left.origin.x) - 10.0).abs() < 1e-4);
321 assert!((f32::from(left.size.width) - 115.0).abs() < 1e-4);
322
323 let right = scaled_bounds(base, 1.15, 0.9, TransformOrigin::RIGHT_CENTER);
325 assert!((f32::from(right.origin.x) + f32::from(right.size.width) - 110.0).abs() < 1e-4);
326 }
327
328 #[test]
329 fn svg_transform_matrix_composes_rotation_and_scale() {
330 let m = svg_transform_matrix(std::f32::consts::FRAC_PI_2, 1.5, 1.5);
331 assert!((m.rotation_scale[0][0]).abs() < 1e-4);
332 assert!((m.rotation_scale[1][0].abs() - 1.5).abs() < 1e-4);
333 }
334}