arc-motion 0.2.0

Physics-first animation & motion engine for GPUI: analytical ODE springs, cubic bezier, FLIP shared layout, presence, drag/rubber-band, inertia, and SVG path morphing.
Documentation
//! Ported from Arc (MIT) © 2026 Elia Kuratli
//! Hybrid Transform primitives (see `docs/porting/P0-DECISIONS.md` §2):
//! - Translation: exact via `Window::with_element_offset` or positioning
//! - Box scaling: analytical bounds + corner-radius scaling around `TransformOrigin`
//! - SVG/Icon rotation & scale: native GPUI `TransformationMatrix`

use gpui_kit::{
    Bounds, Corners, Pixels, Point, TransformationMatrix, bounds, point, px, radians, size,
};

/// Normalized transform origin (`(0.5, 0.5)` = center, `(0.5, 0.0)` = top-center, `(0.0, 0.5)` = left).
#[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
    }
}

/// Complete 2D transform description for Arc elements.
#[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
    }

    /// Computes the transformed axis-aligned bounds (translation + anchored scaling).
    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,
        )
    }

    /// Computes the pivot displacement caused by rotating a box whose pivot sits at `origin`
    /// (used for subtle tilt fallback on text-bearing containers when GPUI text sprites
    /// cannot rotate as a group).
    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
    }
}

/// Scales `base` by `(scale_x, scale_y)` keeping the normalized `origin` point fixed.
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)))
}

/// Scales corner radii proportionally to the smaller axis scale factor.
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),
    }
}

/// Builds a native GPUI `TransformationMatrix` for SVG/icon rotation & scaling.
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)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[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)));

        // Center origin
        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);

        // Top-center origin (ToastStack tucked cards: `transform-origin: 50% 0`)
        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);

        // Left-center origin (ElasticSlider right overscroll: `originX = 0`)
        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);

        // Right-center origin (ElasticSlider left overscroll: `originX = 1`)
        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);
    }
}