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
//! Unified Blur & Backdrop fallback policy (see `docs/porting/P0-DECISIONS.md` §3).

use arc_tokens::{ArcMotionTokens, ThemeMode};
use gpui_kit::{Hsla, Pixels, px};

/// Resolved fallback visual effect for a CSS `filter: blur(Npx)` transition.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BlurEffect {
    /// Additional opacity multiplier (`0.0..=1.0`) to combine with the element's own opacity.
    pub opacity_multiplier: f32,
    /// Subtle vertical micro-offset (`<= 2px`, zero under `reduced_motion`).
    pub offset_y: Pixels,
}

/// Converts Arc's blur tokens (`subtle = 2px`, `soft = 4px`, `text = 8px`) into a consistent
/// opacity + micro-shift fallback, avoiding ad-hoc per-component degradation.
pub struct BlurFallback;

impl BlurFallback {
    pub fn evaluate(blur_radius: Pixels, reduced_motion: bool) -> BlurEffect {
        let max_blur = f32::from(ArcMotionTokens::DEFAULT.blur.text).max(1.0);
        let norm = (f32::from(blur_radius).max(0.0) / max_blur).clamp(0.0, 1.0);
        // Smooth attenuation: 0px -> 1.0, 2px -> ~0.88, 4px -> ~0.72, 8px -> ~0.35
        let opacity_multiplier = 1.0 - 0.65 * norm * (0.5 + 0.5 * norm);
        let offset_y = if reduced_motion {
            px(0.0)
        } else {
            px(2.0 * norm)
        };
        BlurEffect {
            opacity_multiplier,
            offset_y,
        }
    }
}

/// Converts CSS `backdrop-filter: blur(...)` surfaces into crisper high-opacity panels.
pub struct BackdropFallback;

impl BackdropFallback {
    pub fn surface(base: Hsla, mode: ThemeMode) -> Hsla {
        let alpha = match mode {
            ThemeMode::Light => 0.95,
            ThemeMode::Dark => 0.93,
        };
        Hsla {
            a: base.a.max(alpha),
            ..base
        }
    }
}

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

    #[test]
    fn blur_fallback_is_monotonic_and_honors_reduced_motion() {
        let b = ArcMotionTokens::DEFAULT.blur;
        let zero = BlurFallback::evaluate(px(0.0), false);
        let subtle = BlurFallback::evaluate(b.subtle, false);
        let soft = BlurFallback::evaluate(b.soft, false);
        let text = BlurFallback::evaluate(b.text, false);

        assert_eq!(zero.opacity_multiplier, 1.0);
        assert_eq!(zero.offset_y, px(0.0));
        assert!(zero.opacity_multiplier > subtle.opacity_multiplier);
        assert!(subtle.opacity_multiplier > soft.opacity_multiplier);
        assert!(soft.opacity_multiplier > text.opacity_multiplier);
        assert!(f32::from(text.offset_y) <= 2.0 + 1e-4);

        let reduced = BlurFallback::evaluate(b.text, true);
        assert_eq!(reduced.offset_y, px(0.0));
    }

    #[test]
    fn backdrop_fallback_raises_alpha_for_translucent_surfaces() {
        let base = Hsla {
            h: 0.0,
            s: 0.0,
            l: 1.0,
            a: 0.7,
        };
        let raised_light = BackdropFallback::surface(base, ThemeMode::Light);
        let raised_dark = BackdropFallback::surface(base, ThemeMode::Dark);
        assert!((raised_light.a - 0.95).abs() < 1e-5);
        assert!((raised_dark.a - 0.93).abs() < 1e-5);
    }
}