use gpui_kit::{
Bounds, Corners, Pixels, Point, TransformationMatrix, bounds, point, px, radians, size,
};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TransformOrigin {
pub x: f32,
pub y: f32,
}
impl TransformOrigin {
pub const CENTER: Self = Self { x: 0.5, y: 0.5 };
pub const TOP_CENTER: Self = Self { x: 0.5, y: 0.0 };
pub const LEFT_CENTER: Self = Self { x: 0.0, y: 0.5 };
pub const RIGHT_CENTER: Self = Self { x: 1.0, y: 0.5 };
pub const BOTTOM_CENTER: Self = Self { x: 0.5, y: 1.0 };
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
}
impl Default for TransformOrigin {
fn default() -> Self {
Self::CENTER
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Transform2D {
pub translate_x: Pixels,
pub translate_y: Pixels,
pub scale_x: f32,
pub scale_y: f32,
pub rotation_radians: f32,
pub origin: TransformOrigin,
}
impl Transform2D {
pub const IDENTITY: Self = Self {
translate_x: px(0.0),
translate_y: px(0.0),
scale_x: 1.0,
scale_y: 1.0,
rotation_radians: 0.0,
origin: TransformOrigin::CENTER,
};
pub const fn translate(mut self, x: Pixels, y: Pixels) -> Self {
self.translate_x = x;
self.translate_y = y;
self
}
pub const fn scale(mut self, s: f32) -> Self {
self.scale_x = s;
self.scale_y = s;
self
}
pub const fn scale_xy(mut self, sx: f32, sy: f32) -> Self {
self.scale_x = sx;
self.scale_y = sy;
self
}
pub fn rotate_deg(mut self, degrees: f32) -> Self {
self.rotation_radians = degrees.to_radians();
self
}
pub const fn origin(mut self, origin: TransformOrigin) -> Self {
self.origin = origin;
self
}
pub fn apply_bounds(self, base: Bounds<Pixels>) -> Bounds<Pixels> {
let scaled = scaled_bounds(base, self.scale_x, self.scale_y, self.origin);
bounds(
point(
scaled.origin.x + self.translate_x,
scaled.origin.y + self.translate_y,
),
scaled.size,
)
}
pub fn pivot_swing_offset(self, height: Pixels) -> Point<Pixels> {
let h = f32::from(height);
let arm = (self.origin.y - 0.5) * h;
let dx = arm * self.rotation_radians.sin();
let dy = arm * (1.0 - self.rotation_radians.cos());
point(px(dx), px(dy))
}
}
impl Default for Transform2D {
fn default() -> Self {
Self::IDENTITY
}
}
pub fn scaled_bounds(
base: Bounds<Pixels>,
scale_x: f32,
scale_y: f32,
origin: TransformOrigin,
) -> Bounds<Pixels> {
let sx = scale_x.max(0.0);
let sy = scale_y.max(0.0);
let bw = f32::from(base.size.width);
let bh = f32::from(base.size.height);
let new_w = bw * sx;
let new_h = bh * sy;
let anchor_x = f32::from(base.origin.x) + bw * origin.x;
let anchor_y = f32::from(base.origin.y) + bh * origin.y;
let new_x = anchor_x - new_w * origin.x;
let new_y = anchor_y - new_h * origin.y;
bounds(point(px(new_x), px(new_y)), size(px(new_w), px(new_h)))
}
pub fn scaled_corners(corners: Corners<Pixels>, scale_x: f32, scale_y: f32) -> Corners<Pixels> {
let s = scale_x.min(scale_y).max(0.0);
Corners {
top_left: px(f32::from(corners.top_left) * s),
top_right: px(f32::from(corners.top_right) * s),
bottom_right: px(f32::from(corners.bottom_right) * s),
bottom_left: px(f32::from(corners.bottom_left) * s),
}
}
pub fn svg_transform_matrix(
rotation_radians: f32,
scale_x: f32,
scale_y: f32,
) -> TransformationMatrix {
let rot = TransformationMatrix::unit().rotate(radians(rotation_radians));
let scale = TransformationMatrix::unit().scale(gpui_kit::size(scale_x, scale_y));
rot.compose(scale)
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum RevealMask {
Radial { origin: Point<Pixels> },
Sweep,
Split,
Rise,
}
pub fn reveal_regions(
area: Bounds<Pixels>,
kind: RevealMask,
progress: f32,
band_height: Pixels,
) -> Vec<Bounds<Pixels>> {
let p = if progress.is_finite() {
progress.clamp(0.0, 1.0)
} else {
0.0
};
if p <= 0.0 {
return Vec::new();
}
if p >= 1.0 {
return vec![area];
}
let w = f32::from(area.size.width);
let h = f32::from(area.size.height);
match kind {
RevealMask::Split => vec![bounds(
point(area.left() + px(w * (1.0 - p) * 0.5), area.top()),
size(px(w * p), area.size.height),
)],
RevealMask::Rise => vec![bounds(
point(area.left(), area.bottom() - px(h * p)),
size(area.size.width, px(h * p)),
)],
RevealMask::Sweep | RevealMask::Radial { .. } => {
let (ox, oy, radius) = match kind {
RevealMask::Radial { origin } => {
let x = f32::from(origin.x - area.left()).clamp(0.0, w);
let y = f32::from(origin.y - area.top()).clamp(0.0, h);
(x, y, x.max(w - x).hypot(y.max(h - y)) * p)
}
_ => {
let r = w.hypot(h);
(-r + (w + r) * p, h * 0.5, r)
}
};
let step = f32::from(band_height).max(0.5);
let mut regions = Vec::new();
let mut y = 0.0;
while y < h {
let bh = step.min(h - y);
let dy = y + bh * 0.5 - oy;
if dy.abs() < radius {
let dx = (radius * radius - dy * dy).sqrt();
let l = (ox - dx).max(0.0);
let r = (ox + dx).min(w);
if r > l {
regions.push(bounds(
point(area.left() + px(l), area.top() + px(y)),
size(px(r - l), px(bh)),
));
}
}
y += bh;
}
regions
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reveal_masks_have_distinct_midpoints_and_exact_endpoints() {
let area = bounds(point(px(0.0), px(0.0)), size(px(240.0), px(140.0)));
let kinds = [
RevealMask::Radial {
origin: point(px(30.0), px(20.0)),
},
RevealMask::Sweep,
RevealMask::Split,
RevealMask::Rise,
];
let samples: Vec<_> = kinds
.into_iter()
.map(|kind| {
assert!(reveal_regions(area, kind, 0.0, px(1.0)).is_empty());
assert_eq!(reveal_regions(area, kind, 1.0, px(1.0)), vec![area]);
let mid = reveal_regions(area, kind, 0.5, px(1.0));
assert!(!mid.is_empty());
for rect in &mid {
assert!(rect.left() >= area.left() && rect.right() <= area.right());
assert!(rect.top() >= area.top() && rect.bottom() <= area.bottom());
}
mid
})
.collect();
for a in 0..samples.len() {
for b in a + 1..samples.len() {
assert_ne!(samples[a], samples[b]);
}
}
}
#[test]
fn reveal_masks_never_uncover_previously_revealed_pixels() {
let area = bounds(point(px(23.0), px(41.0)), size(px(240.0), px(140.0)));
for kind in [
RevealMask::Radial {
origin: area.bottom_right(),
},
RevealMask::Sweep,
RevealMask::Split,
RevealMask::Rise,
] {
let mut previous = vec![false; 240 * 140];
for frame in 0..=20 {
let regions = reveal_regions(area, kind, frame as f32 / 20.0, px(1.0));
for y in 0..140 {
for x in 0..240 {
let point = area.origin + point(px(x as f32 + 0.5), px(y as f32 + 0.5));
let covered = regions.iter().any(|region| region.contains(&point));
let index = y * 240 + x;
assert!(
!previous[index] || covered,
"{kind:?}: frame {frame}, ({x}, {y}) uncovered"
);
previous[index] = covered;
}
}
}
assert!(previous.iter().all(|covered| *covered));
}
}
#[test]
fn scaled_bounds_keeps_origin_point_fixed() {
let base = bounds(point(px(10.0), px(20.0)), size(px(100.0), px(40.0)));
let c = scaled_bounds(base, 0.8, 0.5, TransformOrigin::CENTER);
assert!((f32::from(c.origin.x) - 20.0).abs() < 1e-4);
assert!((f32::from(c.origin.y) - 30.0).abs() < 1e-4);
assert!((f32::from(c.size.width) - 80.0).abs() < 1e-4);
assert!((f32::from(c.size.height) - 20.0).abs() < 1e-4);
let top = scaled_bounds(base, 0.95, 0.95, TransformOrigin::TOP_CENTER);
assert!((f32::from(top.origin.y) - 20.0).abs() < 1e-4);
assert!((f32::from(top.origin.x) - 12.5).abs() < 1e-4);
let left = scaled_bounds(base, 1.15, 0.9, TransformOrigin::LEFT_CENTER);
assert!((f32::from(left.origin.x) - 10.0).abs() < 1e-4);
assert!((f32::from(left.size.width) - 115.0).abs() < 1e-4);
let right = scaled_bounds(base, 1.15, 0.9, TransformOrigin::RIGHT_CENTER);
assert!((f32::from(right.origin.x) + f32::from(right.size.width) - 110.0).abs() < 1e-4);
}
#[test]
fn svg_transform_matrix_composes_rotation_and_scale() {
let m = svg_transform_matrix(std::f32::consts::FRAC_PI_2, 1.5, 1.5);
assert!((m.rotation_scale[0][0]).abs() < 1e-4);
assert!((m.rotation_scale[1][0].abs() - 1.5).abs() < 1e-4);
}
}