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    pub damping_ratio: f32,
7    pub stiffness: f32,
8}
9
10/// The two control points of a cubic B?zier easing from (0, 0) to (1, 1).
11#[derive(Clone, Copy, Debug, PartialEq)]
12pub struct CubicBezier {
13    pub x1: f32,
14    pub y1: f32,
15    pub x2: f32,
16    pub y2: f32,
17}
18
19#[derive(Clone, Copy, Debug, PartialEq)]
20pub enum MotionSpec {
21    Instant,
22    Spring(Spring),
23    Tween {
24        duration_ms: u16,
25        easing: CubicBezier,
26    },
27}
28
29/// Spatial springs move or reshape an element. Effects springs drive
30/// properties such as color and opacity and use critical damping.
31#[derive(Clone, Copy, Debug, PartialEq, Eq)]
32pub enum MotionKind {
33    Spatial,
34    Effects,
35}
36
37#[derive(Clone, Copy, Debug, PartialEq, Eq)]
38pub enum MotionSpeed {
39    Fast,
40    Default,
41    Slow,
42}
43
44/// Standard and Expressive schemes from the fixed Material 3 reference.
45#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
46#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
47pub enum MotionScheme {
48    #[default]
49    Standard,
50    Expressive,
51}
52
53impl MotionScheme {
54    pub const fn spec(
55        self,
56        kind: MotionKind,
57        speed: MotionSpeed,
58        reduced_motion: bool,
59    ) -> MotionSpec {
60        use MotionKind::{Effects, Spatial};
61        use MotionSpeed::{Default, Fast, Slow};
62
63        if reduced_motion {
64            return MotionSpec::Instant;
65        }
66
67        let spring = match (self, kind, speed) {
68            (Self::Standard, Spatial, Fast) => MD_SYS_MOTION_STANDARD_SPRING_FAST_SPATIAL,
69            (Self::Standard, Spatial, Default) => MD_SYS_MOTION_STANDARD_SPRING_DEFAULT_SPATIAL,
70            (Self::Standard, Spatial, Slow) => MD_SYS_MOTION_STANDARD_SPRING_SLOW_SPATIAL,
71            (Self::Expressive, Spatial, Fast) => MD_SYS_MOTION_EXPRESSIVE_SPRING_FAST_SPATIAL,
72            (Self::Expressive, Spatial, Default) => MD_SYS_MOTION_EXPRESSIVE_SPRING_DEFAULT_SPATIAL,
73            (Self::Expressive, Spatial, Slow) => MD_SYS_MOTION_EXPRESSIVE_SPRING_SLOW_SPATIAL,
74            (Self::Standard, Effects, Fast) => MD_SYS_MOTION_STANDARD_SPRING_FAST_EFFECTS,
75            (Self::Standard, Effects, Default) => MD_SYS_MOTION_STANDARD_SPRING_DEFAULT_EFFECTS,
76            (Self::Standard, Effects, Slow) => MD_SYS_MOTION_STANDARD_SPRING_SLOW_EFFECTS,
77            (Self::Expressive, Effects, Fast) => MD_SYS_MOTION_EXPRESSIVE_SPRING_FAST_EFFECTS,
78            (Self::Expressive, Effects, Default) => MD_SYS_MOTION_EXPRESSIVE_SPRING_DEFAULT_EFFECTS,
79            (Self::Expressive, Effects, Slow) => MD_SYS_MOTION_EXPRESSIVE_SPRING_SLOW_EFFECTS,
80        };
81        MotionSpec::Spring(spring)
82    }
83}