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
25/// Resolved animation timing specification for a state transition.
26#[derive(Clone, Copy, Debug, PartialEq)]
27pub enum MotionSpec {
28    /// Complete the transition immediately without interpolation (used for reduced motion).
29    Instant,
30    /// Physics-based spring transition.
31    Spring(Spring),
32    /// Fixed-duration cubic Bézier tween.
33    Tween {
34        /// Tween duration in milliseconds.
35        duration_ms: u16,
36        /// Easing curve applied across the duration.
37        easing: CubicBezier,
38    },
39}
40
41/// Spatial springs move or reshape an element. Effects springs drive
42/// properties such as color and opacity and use critical damping.
43#[derive(Clone, Copy, Debug, PartialEq, Eq)]
44pub enum MotionKind {
45    /// Motion affecting position, size, or shape.
46    Spatial,
47    /// Motion affecting color, opacity, or elevation effects without overshoot.
48    Effects,
49}
50
51/// Relative speed tier for a Material motion token.
52#[derive(Clone, Copy, Debug, PartialEq, Eq)]
53pub enum MotionSpeed {
54    /// Quick transitions for small controls and immediate feedback.
55    Fast,
56    /// Balanced transitions for standard component state changes.
57    Default,
58    /// Slower transitions for large surfaces, sheets, or full-screen changes.
59    Slow,
60}
61
62/// Standard and Expressive schemes from the fixed Material 3 reference.
63#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
64#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
65pub enum MotionScheme {
66    /// Utility-focused motion with subtle or no overshoot (default).
67    #[default]
68    Standard,
69    /// Playful, energetic Material 3 Expressive spring physics.
70    Expressive,
71}
72
73impl MotionScheme {
74    /// Resolves the [`MotionSpec`] for the given kind and speed, collapsing to
75    /// [`MotionSpec::Instant`] when `reduced_motion` is `true`.
76    pub const fn spec(
77        self,
78        kind: MotionKind,
79        speed: MotionSpeed,
80        reduced_motion: bool,
81    ) -> MotionSpec {
82        use MotionKind::{Effects, Spatial};
83        use MotionSpeed::{Default, Fast, Slow};
84
85        if reduced_motion {
86            return MotionSpec::Instant;
87        }
88
89        let spring = match (self, kind, speed) {
90            (Self::Standard, Spatial, Fast) => MD_SYS_MOTION_STANDARD_SPRING_FAST_SPATIAL,
91            (Self::Standard, Spatial, Default) => MD_SYS_MOTION_STANDARD_SPRING_DEFAULT_SPATIAL,
92            (Self::Standard, Spatial, Slow) => MD_SYS_MOTION_STANDARD_SPRING_SLOW_SPATIAL,
93            (Self::Expressive, Spatial, Fast) => MD_SYS_MOTION_EXPRESSIVE_SPRING_FAST_SPATIAL,
94            (Self::Expressive, Spatial, Default) => MD_SYS_MOTION_EXPRESSIVE_SPRING_DEFAULT_SPATIAL,
95            (Self::Expressive, Spatial, Slow) => MD_SYS_MOTION_EXPRESSIVE_SPRING_SLOW_SPATIAL,
96            (Self::Standard, Effects, Fast) => MD_SYS_MOTION_STANDARD_SPRING_FAST_EFFECTS,
97            (Self::Standard, Effects, Default) => MD_SYS_MOTION_STANDARD_SPRING_DEFAULT_EFFECTS,
98            (Self::Standard, Effects, Slow) => MD_SYS_MOTION_STANDARD_SPRING_SLOW_EFFECTS,
99            (Self::Expressive, Effects, Fast) => MD_SYS_MOTION_EXPRESSIVE_SPRING_FAST_EFFECTS,
100            (Self::Expressive, Effects, Default) => MD_SYS_MOTION_EXPRESSIVE_SPRING_DEFAULT_EFFECTS,
101            (Self::Expressive, Effects, Slow) => MD_SYS_MOTION_EXPRESSIVE_SPRING_SLOW_EFFECTS,
102        };
103        MotionSpec::Spring(spring)
104    }
105}