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#[cfg(test)]
158mod tests {
159 use super::*;
160
161 #[test]
162 fn scaled_bounds_keeps_origin_point_fixed() {
163 let base = bounds(point(px(10.0), px(20.0)), size(px(100.0), px(40.0)));
164
165 let c = scaled_bounds(base, 0.8, 0.5, TransformOrigin::CENTER);
167 assert!((f32::from(c.origin.x) - 20.0).abs() < 1e-4);
168 assert!((f32::from(c.origin.y) - 30.0).abs() < 1e-4);
169 assert!((f32::from(c.size.width) - 80.0).abs() < 1e-4);
170 assert!((f32::from(c.size.height) - 20.0).abs() < 1e-4);
171
172 let top = scaled_bounds(base, 0.95, 0.95, TransformOrigin::TOP_CENTER);
174 assert!((f32::from(top.origin.y) - 20.0).abs() < 1e-4);
175 assert!((f32::from(top.origin.x) - 12.5).abs() < 1e-4);
176
177 let left = scaled_bounds(base, 1.15, 0.9, TransformOrigin::LEFT_CENTER);
179 assert!((f32::from(left.origin.x) - 10.0).abs() < 1e-4);
180 assert!((f32::from(left.size.width) - 115.0).abs() < 1e-4);
181
182 let right = scaled_bounds(base, 1.15, 0.9, TransformOrigin::RIGHT_CENTER);
184 assert!((f32::from(right.origin.x) + f32::from(right.size.width) - 110.0).abs() < 1e-4);
185 }
186
187 #[test]
188 fn svg_transform_matrix_composes_rotation_and_scale() {
189 let m = svg_transform_matrix(std::f32::consts::FRAC_PI_2, 1.5, 1.5);
190 assert!((m.rotation_scale[0][0]).abs() < 1e-4);
191 assert!((m.rotation_scale[1][0].abs() - 1.5).abs() < 1e-4);
192 }
193}