Skip to main content

gpui_material_components/navigation/rail/
state.rs

1use std::time::Duration;
2
3use gpui::Context;
4use web_time::Instant;
5
6use super::RailMode;
7
8#[derive(Clone, Copy)]
9pub(super) struct WidthWeights {
10    pub collapsed: f32,
11    pub expanded: f32,
12}
13
14impl WidthWeights {
15    fn for_mode(mode: RailMode) -> Self {
16        Self {
17            collapsed: f32::from(mode == RailMode::Collapsed),
18            expanded: f32::from(mode == RailMode::Expanded),
19        }
20    }
21
22    fn interpolate(self, target: Self, fraction: f32) -> Self {
23        let mix = |from, to| from + (to - from) * fraction;
24        Self {
25            collapsed: mix(self.collapsed, target.collapsed),
26            expanded: mix(self.expanded, target.expanded),
27        }
28    }
29}
30
31struct Transition {
32    from: WidthWeights,
33    started: Instant,
34    duration: Duration,
35}
36
37/// Retained presentation of a docked rail. Modal visibility uses DialogState.
38pub struct NavigationRailState {
39    current: RailMode,
40    target: RailMode,
41    width: WidthWeights,
42    progress: f32,
43    duration: Duration,
44    transition: Option<Transition>,
45}
46
47impl Default for NavigationRailState {
48    fn default() -> Self {
49        Self::new(RailMode::Collapsed)
50    }
51}
52
53impl NavigationRailState {
54    pub fn new(initial: RailMode) -> Self {
55        Self {
56            current: initial,
57            target: initial,
58            width: WidthWeights::for_mode(initial),
59            progress: 1.,
60            duration: Duration::from_millis(240),
61            transition: None,
62        }
63    }
64
65    /// Sets the duration used for new transitions.
66    pub fn transition_duration(mut self, duration: Duration) -> Self {
67        self.duration = duration;
68        self
69    }
70
71    /// The last settled mode, or the initial mode before the first transition.
72    pub fn current_value(&self) -> RailMode {
73        self.current
74    }
75
76    pub fn target_value(&self) -> RailMode {
77        self.target
78    }
79
80    pub fn is_animating(&self) -> bool {
81        self.transition.is_some()
82    }
83
84    /// Fraction of the active transition, or one at the target.
85    pub fn progress(&self) -> f32 {
86        self.progress
87    }
88
89    pub fn expand(&mut self, cx: &mut Context<Self>) {
90        self.animate_to(RailMode::Expanded, cx);
91    }
92
93    pub fn collapse(&mut self, cx: &mut Context<Self>) {
94        self.animate_to(RailMode::Collapsed, cx);
95    }
96
97    pub fn toggle(&mut self, cx: &mut Context<Self>) {
98        match self.target {
99            RailMode::Expanded => self.collapse(cx),
100            _ => self.expand(cx),
101        }
102    }
103
104    pub fn hide(&mut self, cx: &mut Context<Self>) {
105        self.animate_to(RailMode::Hidden, cx);
106    }
107
108    pub fn snap_to(&mut self, mode: RailMode, cx: &mut Context<Self>) {
109        if self.current != mode || self.target != mode || self.transition.is_some() {
110            self.target = mode;
111            self.settle();
112            cx.notify();
113        }
114    }
115
116    pub fn animate_to(&mut self, mode: RailMode, cx: &mut Context<Self>) {
117        if cx.reduce_motion() || self.duration.is_zero() {
118            self.snap_to(mode, cx);
119            return;
120        }
121        self.sample(cx);
122        if self.target != mode {
123            self.target = mode;
124            self.progress = 0.;
125            self.transition = Some(Transition {
126                from: self.width,
127                started: cx.background_executor().now(),
128                duration: self.duration,
129            });
130            cx.notify();
131        }
132    }
133
134    fn settle(&mut self) {
135        self.current = self.target;
136        self.width = WidthWeights::for_mode(self.target);
137        self.progress = 1.;
138        self.transition = None;
139    }
140
141    pub(super) fn sample(&mut self, cx: &mut Context<Self>) -> WidthWeights {
142        if let Some(transition) = &self.transition {
143            let elapsed = cx.background_executor().now() - transition.started;
144            if elapsed >= transition.duration || cx.reduce_motion() {
145                self.settle();
146                cx.notify();
147            } else {
148                let fraction = (elapsed.as_secs_f64() / transition.duration.as_secs_f64()) as f32;
149                if fraction != self.progress {
150                    let eased = fraction * fraction * (3. - 2. * fraction);
151                    self.width = transition
152                        .from
153                        .interpolate(WidthWeights::for_mode(self.target), eased);
154                    self.progress = fraction;
155                    cx.notify();
156                }
157            }
158        }
159        self.width
160    }
161}