arc-motion 0.4.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
//! Gradients (linear via GPUI, radial/conic via PathBuilder tessellation) and `mask-image`
//! edge-fade quad overlays (used by scrollable `TabsList`, `ScrollArea`, etc.).

use gpui_kit::{
    Bounds, Corners, Edges, Hsla, PathBuilder, Pixels, Point, Window, bounds, linear_color_stop,
    linear_gradient, point, px, quad, size,
};

/// Color stop `(position in 0.0..=1.0, color)`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct GradientStop {
    pub offset: f32,
    pub color: Hsla,
}

impl GradientStop {
    pub const fn new(offset: f32, color: Hsla) -> Self {
        Self { offset, color }
    }
}

/// Samples a multi-stop gradient at `t` in `0.0..=1.0`.
pub fn sample_stops(stops: &[GradientStop], t: f32) -> Hsla {
    if stops.is_empty() {
        return Hsla::transparent_black();
    }
    let t = t.clamp(0.0, 1.0);
    if t <= stops[0].offset || stops.len() == 1 {
        return stops[0].color;
    }
    for pair in stops.windows(2) {
        let a = pair[0];
        let b = pair[1];
        if t <= b.offset {
            let span = (b.offset - a.offset).max(1e-6);
            let local = ((t - a.offset) / span).clamp(0.0, 1.0);
            return lerp_hsla(a.color, b.color, local);
        }
    }
    stops[stops.len() - 1].color
}

fn lerp_hsla(a: Hsla, b: Hsla, t: f32) -> Hsla {
    Hsla {
        h: a.h + (b.h - a.h) * t,
        s: a.s + (b.s - a.s) * t,
        l: a.l + (b.l - a.l) * t,
        a: a.a + (b.a - a.a) * t,
    }
}

/// Radial gradient painted with concentric polygon rings via GPUI `PathBuilder`.
#[derive(Clone, Debug)]
pub struct RadialGradientSpec {
    pub center: Point<Pixels>,
    pub radius: Pixels,
    pub stops: Vec<GradientStop>,
    pub rings: usize,
    pub segments: usize,
}

impl RadialGradientSpec {
    pub fn new(center: Point<Pixels>, radius: Pixels, stops: Vec<GradientStop>) -> Self {
        Self {
            center,
            radius,
            stops,
            rings: 24,
            segments: 48,
        }
    }
}

pub fn paint_radial_gradient(spec: &RadialGradientSpec, window: &mut Window) {
    let radius = f32::from(spec.radius);
    if radius <= 0.0 || spec.stops.is_empty() || spec.rings == 0 || spec.segments < 3 {
        return;
    }
    let cx = f32::from(spec.center.x);
    let cy = f32::from(spec.center.y);

    // Paint from outermost ring inward so inner rings overlay cleanly.
    for ring in (0..spec.rings).rev() {
        let t = (ring + 1) as f32 / spec.rings as f32;
        let r = radius * t;
        let color = sample_stops(&spec.stops, t);
        let mut builder = PathBuilder::fill();
        for seg in 0..spec.segments {
            let angle = std::f32::consts::TAU * (seg as f32 / spec.segments as f32);
            let pt = point(px(cx + r * angle.cos()), px(cy + r * angle.sin()));
            if seg == 0 {
                builder.move_to(pt);
            } else {
                builder.line_to(pt);
            }
        }
        builder.close();
        if let Ok(path) = builder.build() {
            window.paint_path(path, color);
        }
    }
}

/// Conic gradient painted with angular wedges via GPUI `PathBuilder`.
#[derive(Clone, Debug)]
pub struct ConicGradientSpec {
    pub center: Point<Pixels>,
    pub radius: Pixels,
    pub start_angle_deg: f32,
    pub stops: Vec<GradientStop>,
    pub slices: usize,
}

impl ConicGradientSpec {
    pub fn new(center: Point<Pixels>, radius: Pixels, stops: Vec<GradientStop>) -> Self {
        Self {
            center,
            radius,
            start_angle_deg: 0.0,
            stops,
            slices: 64,
        }
    }
}

pub fn paint_conic_gradient(spec: &ConicGradientSpec, window: &mut Window) {
    let radius = f32::from(spec.radius);
    if radius <= 0.0 || spec.stops.is_empty() || spec.slices < 3 {
        return;
    }
    let cx = f32::from(spec.center.x);
    let cy = f32::from(spec.center.y);
    let start_rad = spec.start_angle_deg.to_radians();

    for i in 0..spec.slices {
        let t0 = i as f32 / spec.slices as f32;
        let t1 = (i + 1) as f32 / spec.slices as f32;
        let a0 = start_rad + std::f32::consts::TAU * t0;
        let a1 = start_rad + std::f32::consts::TAU * t1;
        let color = sample_stops(&spec.stops, 0.5 * (t0 + t1));

        let mut builder = PathBuilder::fill();
        builder.move_to(point(px(cx), px(cy)));
        builder.line_to(point(
            px(cx + radius * a0.cos()),
            px(cy + radius * a0.sin()),
        ));
        builder.line_to(point(
            px(cx + radius * a1.cos()),
            px(cy + radius * a1.sin()),
        ));
        builder.close();
        if let Ok(path) = builder.build() {
            window.paint_path(path, color);
        }
    }
}

/// Which side an edge fade mask overlays.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum EdgeFadeSide {
    Left,
    Right,
    Top,
    Bottom,
}

/// Emulates CSS `mask-image: linear-gradient(...)` edge fades by painting a gradient quad
/// from the container's background color to transparent on the requested edge.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct EdgeFadeMask {
    pub side: EdgeFadeSide,
    pub extent: Pixels,
    pub surface_color: Hsla,
}

impl EdgeFadeMask {
    pub const fn new(side: EdgeFadeSide, extent: Pixels, surface_color: Hsla) -> Self {
        Self {
            side,
            extent,
            surface_color,
        }
    }

    pub fn bounds_within(self, container: Bounds<Pixels>) -> Bounds<Pixels> {
        let extent = f32::from(self.extent);
        match self.side {
            EdgeFadeSide::Left => bounds(
                container.origin,
                size(
                    px(extent.min(f32::from(container.size.width))),
                    container.size.height,
                ),
            ),
            EdgeFadeSide::Right => {
                let w = extent.min(f32::from(container.size.width));
                bounds(
                    point(
                        container.origin.x + container.size.width - px(w),
                        container.origin.y,
                    ),
                    size(px(w), container.size.height),
                )
            }
            EdgeFadeSide::Top => bounds(
                container.origin,
                size(
                    container.size.width,
                    px(extent.min(f32::from(container.size.height))),
                ),
            ),
            EdgeFadeSide::Bottom => {
                let h = extent.min(f32::from(container.size.height));
                bounds(
                    point(
                        container.origin.x,
                        container.origin.y + container.size.height - px(h),
                    ),
                    size(container.size.width, px(h)),
                )
            }
        }
    }

    /// Angle in GPUI `linear_gradient` convention (0° = top, 90° = right, 180° = bottom, 270° = left).
    pub const fn gradient_angle_deg(self) -> f32 {
        match self.side {
            EdgeFadeSide::Left => 90.0,
            EdgeFadeSide::Right => 270.0,
            EdgeFadeSide::Top => 180.0,
            EdgeFadeSide::Bottom => 0.0,
        }
    }

    pub fn paint(self, container: Bounds<Pixels>, window: &mut Window) {
        let rect = self.bounds_within(container);
        let transparent = Hsla {
            a: 0.0,
            ..self.surface_color
        };
        let bg = linear_gradient(
            self.gradient_angle_deg(),
            linear_color_stop(self.surface_color, 0.0),
            linear_color_stop(transparent, 1.0),
        );
        window.paint_quad(quad(
            rect,
            Corners::default(),
            bg,
            Edges::default(),
            Hsla::transparent_black(),
            gpui_kit::BorderStyle::Solid,
        ));
    }
}

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

    #[test]
    fn sample_stops_interpolates_midpoint_and_clamps() {
        let stops = [
            GradientStop::new(
                0.0,
                Hsla {
                    h: 0.0,
                    s: 1.0,
                    l: 0.0,
                    a: 1.0,
                },
            ),
            GradientStop::new(
                1.0,
                Hsla {
                    h: 0.5,
                    s: 1.0,
                    l: 1.0,
                    a: 0.0,
                },
            ),
        ];
        let mid = sample_stops(&stops, 0.5);
        assert!((mid.h - 0.25).abs() < 1e-5);
        assert!((mid.l - 0.5).abs() < 1e-5);
        assert!((mid.a - 0.5).abs() < 1e-5);
    }

    #[test]
    fn edge_fade_mask_computes_exact_edge_rects() {
        let container = bounds(point(px(10.0), px(20.0)), size(px(200.0), px(40.0)));
        let left =
            EdgeFadeMask::new(EdgeFadeSide::Left, px(35.0), Hsla::black()).bounds_within(container);
        assert_eq!(left.origin, point(px(10.0), px(20.0)));
        assert_eq!(left.size, size(px(35.0), px(40.0)));

        let right = EdgeFadeMask::new(EdgeFadeSide::Right, px(35.0), Hsla::black())
            .bounds_within(container);
        assert_eq!(right.origin, point(px(175.0), px(20.0)));
        assert_eq!(right.size, size(px(35.0), px(40.0)));
    }
}