Skip to main content

arc_motion/
blur.rs

1//! Ported from Arc (MIT) © 2026 Elia Kuratli
2//! Unified Blur & Backdrop fallback policy (see `docs/porting/P0-DECISIONS.md` §3).
3
4use arc_tokens::{ArcMotionTokens, ThemeMode};
5use gpui_kit::{Hsla, Pixels, px};
6
7/// Resolved fallback visual effect for a CSS `filter: blur(Npx)` transition.
8#[derive(Clone, Copy, Debug, PartialEq)]
9pub struct BlurEffect {
10    /// Additional opacity multiplier (`0.0..=1.0`) to combine with the element's own opacity.
11    pub opacity_multiplier: f32,
12    /// Subtle vertical micro-offset (`<= 2px`, zero under `reduced_motion`).
13    pub offset_y: Pixels,
14}
15
16/// Converts Arc's blur tokens (`subtle = 2px`, `soft = 4px`, `text = 8px`) into a consistent
17/// opacity + micro-shift fallback, avoiding ad-hoc per-component degradation.
18pub struct BlurFallback;
19
20impl BlurFallback {
21    pub fn evaluate(blur_radius: Pixels, reduced_motion: bool) -> BlurEffect {
22        let max_blur = f32::from(ArcMotionTokens::DEFAULT.blur.text).max(1.0);
23        let norm = (f32::from(blur_radius).max(0.0) / max_blur).clamp(0.0, 1.0);
24        // Smooth attenuation: 0px -> 1.0, 2px -> ~0.88, 4px -> ~0.72, 8px -> ~0.35
25        let opacity_multiplier = 1.0 - 0.65 * norm * (0.5 + 0.5 * norm);
26        let offset_y = if reduced_motion {
27            px(0.0)
28        } else {
29            px(2.0 * norm)
30        };
31        BlurEffect {
32            opacity_multiplier,
33            offset_y,
34        }
35    }
36}
37
38/// Converts CSS `backdrop-filter: blur(...)` surfaces into crisper high-opacity panels.
39pub struct BackdropFallback;
40
41impl BackdropFallback {
42    pub fn surface(base: Hsla, mode: ThemeMode) -> Hsla {
43        let alpha = match mode {
44            ThemeMode::Light => 0.95,
45            ThemeMode::Dark => 0.93,
46        };
47        Hsla {
48            a: base.a.max(alpha),
49            ..base
50        }
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn blur_fallback_is_monotonic_and_honors_reduced_motion() {
60        let b = ArcMotionTokens::DEFAULT.blur;
61        let zero = BlurFallback::evaluate(px(0.0), false);
62        let subtle = BlurFallback::evaluate(b.subtle, false);
63        let soft = BlurFallback::evaluate(b.soft, false);
64        let text = BlurFallback::evaluate(b.text, false);
65
66        assert_eq!(zero.opacity_multiplier, 1.0);
67        assert_eq!(zero.offset_y, px(0.0));
68        assert!(zero.opacity_multiplier > subtle.opacity_multiplier);
69        assert!(subtle.opacity_multiplier > soft.opacity_multiplier);
70        assert!(soft.opacity_multiplier > text.opacity_multiplier);
71        assert!(f32::from(text.offset_y) <= 2.0 + 1e-4);
72
73        let reduced = BlurFallback::evaluate(b.text, true);
74        assert_eq!(reduced.offset_y, px(0.0));
75    }
76
77    #[test]
78    fn backdrop_fallback_raises_alpha_for_translucent_surfaces() {
79        let base = Hsla {
80            h: 0.0,
81            s: 0.0,
82            l: 1.0,
83            a: 0.7,
84        };
85        let raised_light = BackdropFallback::surface(base, ThemeMode::Light);
86        let raised_dark = BackdropFallback::surface(base, ThemeMode::Dark);
87        assert!((raised_light.a - 0.95).abs() < 1e-5);
88        assert!((raised_dark.a - 0.93).abs() < 1e-5);
89    }
90}