Skip to main content

gpui_material_components/navigation/rail/
state.rs

1use std::time::Duration;
2
3use gpui::Context;
4use gpui_base::motion::Easing;
5use gpui_material_foundation::RetargetTransition;
6
7use super::RailMode;
8
9#[derive(Clone, Copy)]
10pub(super) struct WidthWeights {
11    pub collapsed: f32,
12    pub expanded: f32,
13}
14
15impl WidthWeights {
16    fn for_mode(mode: RailMode) -> Self {
17        Self {
18            collapsed: f32::from(mode == RailMode::Collapsed),
19            expanded: f32::from(mode == RailMode::Expanded),
20        }
21    }
22}
23
24/// Retained presentation of a docked rail. Modal visibility uses DialogState.
25pub struct NavigationRailState {
26    current: RailMode,
27    target: RailMode,
28    collapsed: RetargetTransition,
29    expanded: RetargetTransition,
30    progress: f32,
31    duration: Duration,
32}
33
34impl Default for NavigationRailState {
35    fn default() -> Self {
36        Self::new(RailMode::Collapsed)
37    }
38}
39
40impl NavigationRailState {
41    /// Creates retained docked rail state starting in `initial` mode with a 240 ms default transition.
42    pub fn new(initial: RailMode) -> Self {
43        let weights = WidthWeights::for_mode(initial);
44        Self {
45            current: initial,
46            target: initial,
47            collapsed: RetargetTransition::settled(weights.collapsed),
48            expanded: RetargetTransition::settled(weights.expanded),
49            progress: 1.,
50            duration: Duration::from_millis(240),
51        }
52    }
53
54    /// Sets the duration used for new transitions.
55    pub fn transition_duration(mut self, duration: Duration) -> Self {
56        self.duration = duration;
57        self
58    }
59
60    /// The last settled mode, or the initial mode before the first transition.
61    pub fn current_value(&self) -> RailMode {
62        self.current
63    }
64
65    /// Returns the mode currently settled at or transitioning toward.
66    pub fn target_value(&self) -> RailMode {
67        self.target
68    }
69
70    /// Returns whether a rail width transition is currently running.
71    pub fn is_animating(&self) -> bool {
72        self.collapsed.is_animating() || self.expanded.is_animating()
73    }
74
75    /// Fraction of the active transition, or one at the target.
76    pub fn progress(&self) -> f32 {
77        self.progress
78    }
79
80    /// Animates the rail to [`RailMode::Expanded`].
81    pub fn expand(&mut self, cx: &mut Context<Self>) {
82        self.animate_to(RailMode::Expanded, cx);
83    }
84
85    /// Animates the rail to [`RailMode::Collapsed`].
86    pub fn collapse(&mut self, cx: &mut Context<Self>) {
87        self.animate_to(RailMode::Collapsed, cx);
88    }
89
90    /// Toggles between [`RailMode::Expanded`] and [`RailMode::Collapsed`].
91    pub fn toggle(&mut self, cx: &mut Context<Self>) {
92        match self.target {
93            RailMode::Expanded => self.collapse(cx),
94            _ => self.expand(cx),
95        }
96    }
97
98    /// Animates the rail to [`RailMode::Hidden`].
99    pub fn hide(&mut self, cx: &mut Context<Self>) {
100        self.animate_to(RailMode::Hidden, cx);
101    }
102
103    /// Moves immediately to `mode` without animation.
104    pub fn snap_to(&mut self, mode: RailMode, cx: &mut Context<Self>) {
105        if self.current != mode || self.target != mode || self.is_animating() {
106            self.target = mode;
107            self.settle();
108            cx.notify();
109        }
110    }
111
112    /// Animates the rail to `mode`, respecting reduced-motion settings.
113    pub fn animate_to(&mut self, mode: RailMode, cx: &mut Context<Self>) {
114        if cx.reduce_motion() || self.duration.is_zero() {
115            self.snap_to(mode, cx);
116            return;
117        }
118        let (_, changed) = self.sample(cx);
119        if self.target != mode {
120            self.target = mode;
121            let weights = WidthWeights::for_mode(mode);
122            let now = cx.background_executor().now();
123            self.collapsed.animate_to(
124                weights.collapsed,
125                self.duration,
126                Easing::EaseInOut,
127                now,
128                false,
129            );
130            self.expanded.animate_to(
131                weights.expanded,
132                self.duration,
133                Easing::EaseInOut,
134                now,
135                false,
136            );
137            self.progress = if self.is_animating() { 0. } else { 1. };
138            if !self.is_animating() {
139                self.current = mode;
140            }
141            cx.notify();
142        } else if changed {
143            cx.notify();
144        }
145    }
146
147    fn settle(&mut self) {
148        self.current = self.target;
149        let weights = WidthWeights::for_mode(self.target);
150        self.collapsed.snap_to(weights.collapsed);
151        self.expanded.snap_to(weights.expanded);
152        self.progress = 1.;
153    }
154
155    /// Samples the active width transition and returns `(weights, changed)` without calling `cx.notify()`.
156    pub(super) fn sample(&mut self, cx: &mut Context<Self>) -> (WidthWeights, bool) {
157        let mut changed = false;
158        if self.is_animating() {
159            let now = cx.background_executor().now();
160            let reduced = cx.reduce_motion();
161            let prev = (self.collapsed.value(), self.expanded.value());
162            let (c, c_run) = self.collapsed.sample(now, reduced);
163            let (e, e_run) = self.expanded.sample(now, reduced);
164            changed = if !c_run && !e_run {
165                self.settle();
166                true
167            } else {
168                let target = WidthWeights::for_mode(self.target);
169                let dist = (c - target.collapsed)
170                    .abs()
171                    .max((e - target.expanded).abs());
172                self.progress = (1. - dist).clamp(0., 1.);
173                (c, e) != prev
174            };
175        }
176        (
177            WidthWeights {
178                collapsed: self.collapsed.value(),
179                expanded: self.expanded.value(),
180            },
181            changed,
182        )
183    }
184}