Skip to main content

gpui_material_theme/
motion.rs

1use crate::generated::*;
2
3/// Material spring parameters for unit mass.
4#[derive(Clone, Copy, Debug, PartialEq)]
5pub struct Spring {
6    /// Damping ratio (`< 1.0` underdamped/bouncy, `1.0` critically damped).
7    pub damping_ratio: f32,
8    /// Spring stiffness constant.
9    pub stiffness: f32,
10}
11
12/// The two control points of a cubic Bézier easing from `(0, 0)` to `(1, 1)`.
13#[derive(Clone, Copy, Debug, PartialEq)]
14pub struct CubicBezier {
15    /// First control point X coordinate in `0.0..=1.0`.
16    pub x1: f32,
17    /// First control point Y coordinate.
18    pub y1: f32,
19    /// Second control point X coordinate in `0.0..=1.0`.
20    pub x2: f32,
21    /// Second control point Y coordinate.
22    pub y2: f32,
23}
24
25impl CubicBezier {
26    /// Constant-rate linear interpolation (`(0.0, 0.0, 1.0, 1.0)`).
27    pub const LINEAR: Self = Self {
28        x1: 0.0,
29        y1: 0.0,
30        x2: 1.0,
31        y2: 1.0,
32    };
33
34    /// Standard CSS `ease` curve (`(0.25, 0.1, 0.25, 1.0)`).
35    pub const EASE: Self = Self {
36        x1: 0.25,
37        y1: 0.1,
38        x2: 0.25,
39        y2: 1.0,
40    };
41
42    /// Standard CSS `ease-in` curve (`(0.42, 0.0, 1.0, 1.0)`).
43    pub const EASE_IN: Self = Self {
44        x1: 0.42,
45        y1: 0.0,
46        x2: 1.0,
47        y2: 1.0,
48    };
49
50    /// Standard CSS `ease-out` curve (`(0.0, 0.0, 0.58, 1.0)`).
51    pub const EASE_OUT: Self = Self {
52        x1: 0.0,
53        y1: 0.0,
54        x2: 0.58,
55        y2: 1.0,
56    };
57
58    /// Standard CSS `ease-in-out` curve (`(0.42, 0.0, 0.58, 1.0)`).
59    pub const EASE_IN_OUT: Self = Self {
60        x1: 0.42,
61        y1: 0.0,
62        x2: 0.58,
63        y2: 1.0,
64    };
65}
66
67/// Resolved animation timing specification for a state transition.
68#[derive(Clone, Copy, Debug, PartialEq)]
69pub enum MotionSpec {
70    /// Complete the transition immediately without interpolation (used for reduced motion).
71    Instant,
72    /// Physics-based spring transition.
73    Spring(Spring),
74    /// Fixed-duration cubic Bézier tween.
75    Tween {
76        /// Tween duration in milliseconds.
77        duration_ms: u16,
78        /// Easing curve applied across the duration.
79        easing: CubicBezier,
80    },
81}
82
83/// Spatial springs move or reshape an element. Effects springs drive
84/// properties such as color and opacity and use critical damping.
85#[derive(Clone, Copy, Debug, PartialEq, Eq)]
86pub enum MotionKind {
87    /// Motion affecting position, size, or shape.
88    Spatial,
89    /// Motion affecting color, opacity, or elevation effects without overshoot.
90    Effects,
91}
92
93/// Relative speed tier for a Material motion token.
94#[derive(Clone, Copy, Debug, PartialEq, Eq)]
95pub enum MotionSpeed {
96    /// Quick transitions for small controls and immediate feedback.
97    Fast,
98    /// Balanced transitions for standard component state changes.
99    Default,
100    /// Slower transitions for large surfaces, sheets, or full-screen changes.
101    Slow,
102}
103
104/// Standard and Expressive schemes from the fixed Material 3 reference.
105#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
106#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
107pub enum MotionScheme {
108    /// Utility-focused motion with subtle or no overshoot (default).
109    #[default]
110    Standard,
111    /// Playful, energetic Material 3 Expressive spring physics.
112    Expressive,
113}
114
115impl MotionScheme {
116    /// Resolves the [`MotionSpec`] for the given kind and speed, collapsing to
117    /// [`MotionSpec::Instant`] when `reduced_motion` is `true`.
118    pub const fn spec(
119        self,
120        kind: MotionKind,
121        speed: MotionSpeed,
122        reduced_motion: bool,
123    ) -> MotionSpec {
124        use MotionKind::{Effects, Spatial};
125        use MotionSpeed::{Default, Fast, Slow};
126
127        if reduced_motion {
128            return MotionSpec::Instant;
129        }
130
131        let spring = match (self, kind, speed) {
132            (Self::Standard, Spatial, Fast) => MD_SYS_MOTION_STANDARD_SPRING_FAST_SPATIAL,
133            (Self::Standard, Spatial, Default) => MD_SYS_MOTION_STANDARD_SPRING_DEFAULT_SPATIAL,
134            (Self::Standard, Spatial, Slow) => MD_SYS_MOTION_STANDARD_SPRING_SLOW_SPATIAL,
135            (Self::Expressive, Spatial, Fast) => MD_SYS_MOTION_EXPRESSIVE_SPRING_FAST_SPATIAL,
136            (Self::Expressive, Spatial, Default) => MD_SYS_MOTION_EXPRESSIVE_SPRING_DEFAULT_SPATIAL,
137            (Self::Expressive, Spatial, Slow) => MD_SYS_MOTION_EXPRESSIVE_SPRING_SLOW_SPATIAL,
138            (Self::Standard, Effects, Fast) => MD_SYS_MOTION_STANDARD_SPRING_FAST_EFFECTS,
139            (Self::Standard, Effects, Default) => MD_SYS_MOTION_STANDARD_SPRING_DEFAULT_EFFECTS,
140            (Self::Standard, Effects, Slow) => MD_SYS_MOTION_STANDARD_SPRING_SLOW_EFFECTS,
141            (Self::Expressive, Effects, Fast) => MD_SYS_MOTION_EXPRESSIVE_SPRING_FAST_EFFECTS,
142            (Self::Expressive, Effects, Default) => MD_SYS_MOTION_EXPRESSIVE_SPRING_DEFAULT_EFFECTS,
143            (Self::Expressive, Effects, Slow) => MD_SYS_MOTION_EXPRESSIVE_SPRING_SLOW_EFFECTS,
144        };
145        MotionSpec::Spring(spring)
146    }
147}