gpui_material_components/navigation/rail/
state.rs1use 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
24pub 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 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 pub fn transition_duration(mut self, duration: Duration) -> Self {
56 self.duration = duration;
57 self
58 }
59
60 pub fn current_value(&self) -> RailMode {
62 self.current
63 }
64
65 pub fn target_value(&self) -> RailMode {
67 self.target
68 }
69
70 pub fn is_animating(&self) -> bool {
72 self.collapsed.is_animating() || self.expanded.is_animating()
73 }
74
75 pub fn progress(&self) -> f32 {
77 self.progress
78 }
79
80 pub fn expand(&mut self, cx: &mut Context<Self>) {
82 self.animate_to(RailMode::Expanded, cx);
83 }
84
85 pub fn collapse(&mut self, cx: &mut Context<Self>) {
87 self.animate_to(RailMode::Collapsed, cx);
88 }
89
90 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 pub fn hide(&mut self, cx: &mut Context<Self>) {
100 self.animate_to(RailMode::Hidden, cx);
101 }
102
103 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 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 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 }
143 }
144
145 fn settle(&mut self) {
146 self.current = self.target;
147 let weights = WidthWeights::for_mode(self.target);
148 self.collapsed.snap_to(weights.collapsed);
149 self.expanded.snap_to(weights.expanded);
150 self.progress = 1.;
151 }
152
153 pub(super) fn sample(&mut self, cx: &mut Context<Self>) -> WidthWeights {
154 if self.is_animating() {
155 let now = cx.background_executor().now();
156 let reduced = cx.reduce_motion();
157 let prev = (self.collapsed.value(), self.expanded.value());
158 let (c, c_run) = self.collapsed.sample(now, reduced);
159 let (e, e_run) = self.expanded.sample(now, reduced);
160 let notify = if !c_run && !e_run {
161 self.settle();
162 true
163 } else {
164 let target = WidthWeights::for_mode(self.target);
165 let dist = (c - target.collapsed)
166 .abs()
167 .max((e - target.expanded).abs());
168 self.progress = (1. - dist).clamp(0., 1.);
169 (c, e) != prev
170 };
171 if notify {
172 let state = cx.weak_entity();
173 cx.defer(move |cx| {
174 let _ = state.update(cx, |_, cx| cx.notify());
175 });
176 }
177 }
178 WidthWeights {
179 collapsed: self.collapsed.value(),
180 expanded: self.expanded.value(),
181 }
182 }
183}