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 {
42 let weights = WidthWeights::for_mode(initial);
43 Self {
44 current: initial,
45 target: initial,
46 collapsed: RetargetTransition::settled(weights.collapsed),
47 expanded: RetargetTransition::settled(weights.expanded),
48 progress: 1.,
49 duration: Duration::from_millis(240),
50 }
51 }
52
53 pub fn transition_duration(mut self, duration: Duration) -> Self {
55 self.duration = duration;
56 self
57 }
58
59 pub fn current_value(&self) -> RailMode {
61 self.current
62 }
63
64 pub fn target_value(&self) -> RailMode {
65 self.target
66 }
67
68 pub fn is_animating(&self) -> bool {
69 self.collapsed.is_animating() || self.expanded.is_animating()
70 }
71
72 pub fn progress(&self) -> f32 {
74 self.progress
75 }
76
77 pub fn expand(&mut self, cx: &mut Context<Self>) {
78 self.animate_to(RailMode::Expanded, cx);
79 }
80
81 pub fn collapse(&mut self, cx: &mut Context<Self>) {
82 self.animate_to(RailMode::Collapsed, cx);
83 }
84
85 pub fn toggle(&mut self, cx: &mut Context<Self>) {
86 match self.target {
87 RailMode::Expanded => self.collapse(cx),
88 _ => self.expand(cx),
89 }
90 }
91
92 pub fn hide(&mut self, cx: &mut Context<Self>) {
93 self.animate_to(RailMode::Hidden, cx);
94 }
95
96 pub fn snap_to(&mut self, mode: RailMode, cx: &mut Context<Self>) {
97 if self.current != mode || self.target != mode || self.is_animating() {
98 self.target = mode;
99 self.settle();
100 cx.notify();
101 }
102 }
103
104 pub fn animate_to(&mut self, mode: RailMode, cx: &mut Context<Self>) {
105 if cx.reduce_motion() || self.duration.is_zero() {
106 self.snap_to(mode, cx);
107 return;
108 }
109 self.sample(cx);
110 if self.target != mode {
111 self.target = mode;
112 let weights = WidthWeights::for_mode(mode);
113 let now = cx.background_executor().now();
114 self.collapsed.animate_to(
115 weights.collapsed,
116 self.duration,
117 Easing::EaseInOut,
118 now,
119 false,
120 );
121 self.expanded.animate_to(
122 weights.expanded,
123 self.duration,
124 Easing::EaseInOut,
125 now,
126 false,
127 );
128 self.progress = if self.is_animating() { 0. } else { 1. };
129 if !self.is_animating() {
130 self.current = mode;
131 }
132 cx.notify();
133 }
134 }
135
136 fn settle(&mut self) {
137 self.current = self.target;
138 let weights = WidthWeights::for_mode(self.target);
139 self.collapsed.snap_to(weights.collapsed);
140 self.expanded.snap_to(weights.expanded);
141 self.progress = 1.;
142 }
143
144 pub(super) fn sample(&mut self, cx: &mut Context<Self>) -> WidthWeights {
145 if self.is_animating() {
146 let now = cx.background_executor().now();
147 let reduced = cx.reduce_motion();
148 let prev = (self.collapsed.value(), self.expanded.value());
149 let (c, c_run) = self.collapsed.sample(now, reduced);
150 let (e, e_run) = self.expanded.sample(now, reduced);
151 let notify = if !c_run && !e_run {
152 self.settle();
153 true
154 } else {
155 let target = WidthWeights::for_mode(self.target);
156 let dist = (c - target.collapsed)
157 .abs()
158 .max((e - target.expanded).abs());
159 self.progress = (1. - dist).clamp(0., 1.);
160 (c, e) != prev
161 };
162 if notify {
163 let state = cx.weak_entity();
164 cx.defer(move |cx| {
165 let _ = state.update(cx, |_, cx| cx.notify());
166 });
167 }
168 }
169 WidthWeights {
170 collapsed: self.collapsed.value(),
171 expanded: self.expanded.value(),
172 }
173 }
174}