fc_ui/capabilities/navigation/
drawer_navigation.rs1use gpui::{prelude::FluentBuilder as _, *};
4use std::rc::Rc;
5use std::time::Duration;
6
7use crate::capabilities::foundation::theme::use_theme;
8use crate::capabilities::motion::animations::{durations, easings};
9
10#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
11pub enum DrawerSide {
12 #[default]
13 Left,
14 Right,
15}
16
17pub struct DrawerState {
18 is_open: bool,
19 is_animating: bool,
20 is_dismissing: bool,
21 animation_version: usize,
22 duration: Duration,
23}
24
25impl DrawerState {
26 pub fn new() -> Self {
27 Self {
28 is_open: false,
29 is_animating: false,
30 is_dismissing: false,
31 animation_version: 0,
32 duration: durations::NORMAL,
33 }
34 }
35
36 pub fn is_open(&self) -> bool {
37 self.is_open
38 }
39
40 pub fn set_duration(&mut self, duration: Duration) {
41 self.duration = duration;
42 }
43
44 pub fn open(&mut self, cx: &mut Context<Self>) {
45 if !self.is_open {
46 self.is_open = true;
47 self.is_dismissing = false;
48 self.is_animating = true;
49 self.animation_version = self.animation_version.wrapping_add(1);
50 cx.notify();
51 }
52 }
53
54 pub fn close(&mut self, cx: &mut Context<Self>) {
55 if self.is_open && !self.is_dismissing {
56 self.is_dismissing = true;
57 self.animation_version = self.animation_version.wrapping_add(1);
58 cx.notify();
59
60 let duration = self.duration;
61 cx.spawn(async move |this, cx| {
62 cx.background_executor().timer(duration).await;
63 _ = this.update(cx, |state, cx| {
64 state.is_open = false;
65 state.is_dismissing = false;
66 state.is_animating = false;
67 cx.notify();
68 });
69 })
70 .detach();
71 }
72 }
73
74 pub fn toggle(&mut self, cx: &mut Context<Self>) {
75 if self.is_open {
76 self.close(cx);
77 } else {
78 self.open(cx);
79 }
80 }
81}
82
83impl Default for DrawerState {
84 fn default() -> Self {
85 Self::new()
86 }
87}
88
89#[derive(IntoElement)]
90pub struct DrawerNavigation {
91 id: ElementId,
92 state: Entity<DrawerState>,
93 side: DrawerSide,
94 drawer_width: Pixels,
95 show_backdrop: bool,
96 children: Vec<AnyElement>,
97 on_close: Option<Rc<dyn Fn(&mut Window, &mut App)>>,
98 duration: Duration,
99 style: StyleRefinement,
100}
101
102impl DrawerNavigation {
103 pub fn new(id: impl Into<ElementId>, state: Entity<DrawerState>) -> Self {
104 Self {
105 id: id.into(),
106 state,
107 side: DrawerSide::default(),
108 drawer_width: px(280.0),
109 show_backdrop: true,
110 children: Vec::new(),
111 on_close: None,
112 duration: durations::NORMAL,
113 style: StyleRefinement::default(),
114 }
115 }
116
117 pub fn side(mut self, side: DrawerSide) -> Self {
118 self.side = side;
119 self
120 }
121
122 pub fn width(mut self, width: impl Into<Pixels>) -> Self {
123 self.drawer_width = width.into();
124 self
125 }
126
127 pub fn show_backdrop(mut self, show: bool) -> Self {
128 self.show_backdrop = show;
129 self
130 }
131
132 pub fn duration(mut self, duration: Duration) -> Self {
133 self.duration = duration;
134 self
135 }
136
137 pub fn on_close<F>(mut self, handler: F) -> Self
138 where
139 F: Fn(&mut Window, &mut App) + 'static,
140 {
141 self.on_close = Some(Rc::new(handler));
142 self
143 }
144}
145
146impl Styled for DrawerNavigation {
147 fn style(&mut self) -> &mut StyleRefinement {
148 &mut self.style
149 }
150}
151
152impl ParentElement for DrawerNavigation {
153 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
154 self.children.extend(elements);
155 }
156}
157
158impl RenderOnce for DrawerNavigation {
159 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
160 let theme = use_theme(cx);
161 let user_style = self.style;
162 let duration = self.duration;
163 self.state.update(cx, |state, _| {
164 if state.duration != duration {
165 state.duration = duration;
166 }
167 });
168 let state = self.state.read(cx);
169 let is_open = state.is_open;
170 let is_dismissing = state.is_dismissing;
171 let animation_version = state.animation_version;
172 let drawer_width = self.drawer_width;
173 let side = self.side;
174
175 if !is_open {
176 return div().into_any_element();
177 }
178
179 let state_for_backdrop = self.state.clone();
180 let on_close = self.on_close.clone();
181
182 deferred(
183 div()
184 .id(self.id)
185 .absolute()
186 .inset_0()
187 .when(self.show_backdrop, |this| {
188 this.child(
189 div()
190 .id("drawer-backdrop")
191 .absolute()
192 .inset_0()
193 .bg(hsla(0.0, 0.0, 0.0, 0.5))
194 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
195 state_for_backdrop.update(cx, |s, cx| s.close(cx));
196 if let Some(handler) = on_close.as_ref() {
197 handler(window, cx);
198 }
199 })
200 .with_animation(
201 ElementId::Name(
202 format!("drawer-backdrop-{}", animation_version).into(),
203 ),
204 Animation::new(duration).with_easing(easings::ease_out_cubic),
205 move |el, delta| {
206 if is_dismissing {
207 el.opacity(1.0 - delta)
208 } else {
209 el.opacity(delta)
210 }
211 },
212 ),
213 )
214 })
215 .child(
216 div()
217 .id("drawer-panel")
218 .occlude()
219 .absolute()
220 .top_0()
221 .bottom_0()
222 .w(drawer_width)
223 .when(side == DrawerSide::Left, |this| this.left_0())
224 .when(side == DrawerSide::Right, |this| this.right_0())
225 .bg(theme.tokens.background)
226 .border_color(theme.tokens.border)
227 .when(side == DrawerSide::Left, |this| this.border_r_1())
228 .when(side == DrawerSide::Right, |this| this.border_l_1())
229 .shadow(smallvec::smallvec![BoxShadow {
230 color: hsla(0.0, 0.0, 0.0, 0.15),
231 offset: point(
232 if side == DrawerSide::Left {
233 px(4.0)
234 } else {
235 px(-4.0)
236 },
237 px(0.0)
238 ),
239 blur_radius: px(16.0),
240 spread_radius: px(0.0),
241 inset: false,
242 }])
243 .flex()
244 .flex_col()
245 .overflow_hidden()
246 .children(self.children)
247 .map(|this| {
248 let mut el = this;
249 el.style().refine(&user_style);
250 el
251 })
252 .on_mouse_down(MouseButton::Left, |_, _, _| {})
253 .with_animation(
254 ElementId::Name(format!("drawer-slide-{}", animation_version).into()),
255 Animation::new(duration).with_easing(easings::ease_out_cubic),
256 move |el, delta| {
257 let offset = drawer_width / px(1.0);
258 if is_dismissing {
259 match side {
260 DrawerSide::Left => el.ml(px(-offset * delta)),
261 DrawerSide::Right => el.mr(px(-offset * delta)),
262 }
263 } else {
264 match side {
265 DrawerSide::Left => el.ml(px(-offset * (1.0 - delta))),
266 DrawerSide::Right => el.mr(px(-offset * (1.0 - delta))),
267 }
268 }
269 },
270 ),
271 ),
272 )
273 .with_priority(1)
274 .into_any_element()
275 }
276}