1use std::rc::Rc;
2
3use gpui::{
4 AnyElement, App, Edges, ElementId, Entity, Hsla, IntoElement, Pixels, SharedString, Size,
5 Window, px, radians,
6};
7use gpui_material_theme::{ColorRole, CornerShape, Elevation};
8
9use crate::{Icon, IconButton, IconName, ScaffoldInsets};
10
11use super::bottom_sheet::{BottomSheetState, BottomSheetThresholds};
12use super::dialog::{
13 DialogCloseReason, DialogProperties, DialogState, ModalClosed, ModalDismissRequest,
14 ModalSession,
15};
16
17mod gesture;
18mod render;
19
20#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
21pub enum SheetEdge {
22 #[default]
23 Bottom,
24 Start,
25 End,
26}
27
28#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
29pub enum SheetMode {
30 #[default]
31 Standard,
32 Modal,
33}
34
35#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
37pub enum SheetPlacement {
38 #[default]
39 Docked,
40 Detached,
41}
42
43#[derive(Clone, Copy, Debug, PartialEq)]
45pub struct SheetMargins {
46 pub top: Pixels,
47 pub bottom: Pixels,
48 pub start: Pixels,
49 pub end: Pixels,
50}
51
52impl Default for SheetMargins {
53 fn default() -> Self {
54 Self {
55 top: px(16.),
56 bottom: px(16.),
57 start: px(16.),
58 end: px(16.),
59 }
60 }
61}
62
63impl SheetMargins {
64 fn fit(self, viewport: Size<Pixels>, rtl: bool) -> Edges<Pixels> {
65 let (start, end) =
66 fit_margins(self.start, self.end, (viewport.width - px(48.)).max(px(0.)));
67 let (top, bottom) = fit_margins(
68 self.top,
69 self.bottom,
70 (viewport.height - px(48.)).max(px(0.)),
71 );
72 let (left, right) = if rtl { (end, start) } else { (start, end) };
73 Edges {
74 top,
75 bottom,
76 left,
77 right,
78 }
79 }
80}
81
82fn fit_margins(a: Pixels, b: Pixels, available: Pixels) -> (Pixels, Pixels) {
83 let sanitize = |margin: Pixels| {
84 if f32::from(margin).is_finite() {
85 margin.clamp(px(0.), available)
86 } else {
87 px(0.)
88 }
89 };
90 let (a, b) = (sanitize(a), sanitize(b));
91 if a + b > available {
92 let factor = available / (a + b);
93 (a * factor, b * factor)
94 } else {
95 (a, b)
96 }
97}
98
99#[derive(Clone, Copy, Debug, Default)]
101pub struct SheetColors {
102 pub container: Option<Hsla>,
103 pub content: Option<Hsla>,
104 pub header: Option<Hsla>,
105 pub divider: Option<Hsla>,
106 pub scrim: Option<Hsla>,
107}
108
109#[derive(Clone)]
111pub struct SheetClose(CloseAction);
112
113#[derive(Clone)]
114enum CloseAction {
115 Modal(ModalSession),
116 Standard(Option<ModalDismissRequest>, Option<ModalClosed>),
117}
118
119impl SheetClose {
120 pub fn activate(&self, window: &mut Window, cx: &mut App) {
122 self.request(window, cx);
123 }
124
125 pub(super) fn request(&self, window: &mut Window, cx: &mut App) -> bool {
126 let reason = DialogCloseReason::Cancel;
127 match &self.0 {
128 CloseAction::Modal(session) => session.dismiss(reason, window, cx),
129 CloseAction::Standard(request, Some(closed))
130 if request
131 .as_ref()
132 .is_none_or(|request| request(reason, window, cx)) =>
133 {
134 closed(reason, window, cx);
135 true
136 }
137 CloseAction::Standard(_, _) => false,
138 }
139 }
140}
141
142type CloseSlot = Box<dyn FnOnce(SheetClose) -> AnyElement>;
143
144type Change = Rc<dyn Fn(bool, &mut Window, &mut App)>;
145
146enum SheetBinding {
147 Immediate(Entity<DialogState>),
148 Retained(Entity<BottomSheetState>),
149}
150
151#[derive(IntoElement)]
154pub struct Sheet {
155 id: ElementId,
156 state: SheetBinding,
157 title: SharedString,
158 content: AnyElement,
159 edge: SheetEdge,
160 mode: SheetMode,
161 extent: Pixels,
162 expansion: Option<(bool, Change)>,
163 placement: SheetPlacement,
164 margins: SheetMargins,
165 header: Option<AnyElement>,
166 back: Option<AnyElement>,
167 close_content: Option<CloseSlot>,
168 close_label: SharedString,
169 show_close: bool,
170 footer: Option<AnyElement>,
171 footer_divider: bool,
172 edge_divider: bool,
173 properties: DialogProperties,
174 on_dismiss_request: Option<ModalDismissRequest>,
175 on_close: Option<ModalClosed>,
176 layer: usize,
177 shape: Option<CornerShape>,
178 container_role: Option<ColorRole>,
179 content_role: Option<ColorRole>,
180 colors: SheetColors,
181 tonal_elevation: Elevation,
182 shadow_elevation: Option<Elevation>,
183 scrim_opacity: f32,
184 presence: Option<gpui_base::motion::PresenceSample>,
185 layout_size: Option<Size<Pixels>>,
186 peek_height: Option<Pixels>,
187 max_width: Pixels,
188 drag_enabled: bool,
189 show_handle: bool,
190 handle_content: Option<AnyElement>,
191 handle_label: SharedString,
192 thresholds: BottomSheetThresholds,
193 content_insets: ScaffoldInsets,
194}
195
196impl Sheet {
197 pub fn new(
198 id: impl Into<ElementId>,
199 state: &Entity<DialogState>,
200 title: impl Into<SharedString>,
201 content: impl IntoElement,
202 ) -> Self {
203 Self::with_state(
204 id.into(),
205 SheetBinding::Immediate(state.clone()),
206 title.into(),
207 content.into_any_element(),
208 )
209 }
210
211 pub fn bottom(
213 id: impl Into<ElementId>,
214 state: &Entity<BottomSheetState>,
215 title: impl Into<SharedString>,
216 content: impl IntoElement,
217 ) -> Self {
218 Self::with_state(
219 id.into(),
220 SheetBinding::Retained(state.clone()),
221 title.into(),
222 content.into_any_element(),
223 )
224 }
225
226 fn with_state(
227 id: ElementId,
228 state: SheetBinding,
229 title: SharedString,
230 content: AnyElement,
231 ) -> Self {
232 Self {
233 id,
234 state,
235 title,
236 content,
237 edge: SheetEdge::Bottom,
238 mode: SheetMode::Standard,
239 extent: px(320.),
240 expansion: None,
241 placement: SheetPlacement::Docked,
242 margins: SheetMargins::default(),
243 header: None,
244 back: None,
245 close_content: None,
246 close_label: "Close".into(),
247 show_close: true,
248 footer: None,
249 footer_divider: false,
250 edge_divider: false,
251 properties: DialogProperties::default(),
252 on_dismiss_request: None,
253 on_close: None,
254 layer: 0,
255 shape: None,
256 container_role: None,
257 content_role: None,
258 colors: SheetColors::default(),
259 tonal_elevation: Elevation::Level0,
260 shadow_elevation: None,
261 scrim_opacity: 0.32,
262 presence: None,
263 layout_size: None,
264 peek_height: None,
265 max_width: px(640.),
266 drag_enabled: true,
267 show_handle: true,
268 handle_content: None,
269 handle_label: "Resize bottom sheet".into(),
270 thresholds: BottomSheetThresholds::default(),
271 content_insets: ScaffoldInsets::default(),
272 }
273 }
274
275 #[cfg_attr(not(feature = "navigation"), allow(dead_code))]
276 pub(crate) fn in_scaffold(mut self, size: Size<Pixels>) -> Self {
277 self.edge = SheetEdge::Bottom;
278 self.mode = SheetMode::Standard;
279 self.layout_size = Some(size);
280 self
281 }
282
283 pub fn peek_height(mut self, height: Pixels) -> Self {
285 if f32::from(height).is_finite() {
286 self.peek_height = Some(height.max(px(48.)));
287 }
288 self
289 }
290
291 pub fn max_width(mut self, width: Pixels) -> Self {
293 if f32::from(width).is_finite() && width > px(0.) {
294 self.max_width = width;
295 }
296 self
297 }
298
299 pub fn drag_enabled(mut self, enabled: bool) -> Self {
300 self.drag_enabled = enabled;
301 self
302 }
303
304 pub fn drag_thresholds(mut self, thresholds: BottomSheetThresholds) -> Self {
305 self.thresholds = thresholds.normalized();
306 self
307 }
308
309 pub fn handle_content(mut self, content: impl IntoElement) -> Self {
311 self.handle_content = Some(content.into_any_element());
312 self
313 }
314
315 pub fn handle_label(mut self, label: impl Into<SharedString>) -> Self {
317 self.handle_label = label.into();
318 self
319 }
320
321 pub fn show_handle(mut self, show: bool) -> Self {
323 self.show_handle = show;
324 self
325 }
326
327 pub fn content_insets(mut self, insets: ScaffoldInsets) -> Self {
329 self.content_insets = insets.normalized();
330 self
331 }
332
333 pub fn edge(mut self, edge: SheetEdge) -> Self {
334 if matches!(self.state, SheetBinding::Immediate(_)) {
335 self.edge = edge;
336 }
337 self
338 }
339
340 pub fn mode(mut self, mode: SheetMode) -> Self {
341 self.mode = mode;
342 self
343 }
344
345 pub fn extent(mut self, extent: Pixels) -> Self {
347 self.extent = extent.max(px(48.));
348 self
349 }
350
351 pub fn expandable(
353 mut self,
354 expanded: bool,
355 on_change: impl Fn(bool, &mut Window, &mut App) + 'static,
356 ) -> Self {
357 self.expansion = Some((expanded, Rc::new(on_change)));
358 self
359 }
360
361 pub fn placement(mut self, placement: SheetPlacement) -> Self {
362 self.placement = placement;
363 self
364 }
365
366 pub fn margins(mut self, margins: SheetMargins) -> Self {
367 self.margins = margins;
368 self
369 }
370
371 pub fn header_content(mut self, heading: impl IntoElement) -> Self {
373 self.header = Some(heading.into_any_element());
374 self
375 }
376
377 pub fn back(
379 mut self,
380 label: impl Into<SharedString>,
381 on_back: impl Fn(&mut Window, &mut App) + 'static,
382 ) -> Self {
383 self.back = Some(
384 IconButton::new(
385 "back",
386 label,
387 Icon::new(IconName::ArrowForward)
388 .rotate(radians(std::f32::consts::PI))
389 .mirror_in_rtl(true),
390 )
391 .on_activate(on_back)
392 .into_any_element(),
393 );
394 self
395 }
396
397 pub fn back_content(mut self, back: impl IntoElement) -> Self {
398 self.back = Some(back.into_any_element());
399 self
400 }
401
402 pub fn close_label(mut self, label: impl Into<SharedString>) -> Self {
403 self.close_label = label.into();
404 self
405 }
406
407 pub fn show_close(mut self, show: bool) -> Self {
408 self.show_close = show;
409 self
410 }
411
412 pub fn close_content<E: IntoElement>(
414 mut self,
415 close: impl FnOnce(SheetClose) -> E + 'static,
416 ) -> Self {
417 self.close_content = Some(Box::new(move |action| close(action).into_any_element()));
418 self
419 }
420
421 pub fn footer(mut self, actions: impl IntoElement) -> Self {
422 self.footer = Some(actions.into_any_element());
423 self
424 }
425
426 pub fn footer_divider(mut self, visible: bool) -> Self {
427 self.footer_divider = visible;
428 self
429 }
430
431 pub fn edge_divider(mut self, visible: bool) -> Self {
433 self.edge_divider = visible;
434 self
435 }
436
437 pub fn properties(mut self, properties: DialogProperties) -> Self {
438 self.properties = properties;
439 self
440 }
441
442 pub fn on_dismiss_request(
444 mut self,
445 request: impl Fn(DialogCloseReason, &mut Window, &mut App) -> bool + 'static,
446 ) -> Self {
447 self.on_dismiss_request = Some(Rc::new(request));
448 self
449 }
450
451 pub fn on_close(
453 mut self,
454 close: impl Fn(DialogCloseReason, &mut Window, &mut App) + 'static,
455 ) -> Self {
456 self.on_close = Some(Rc::new(close));
457 self
458 }
459
460 pub fn layer(mut self, layer: usize) -> Self {
462 self.layer = layer;
463 self
464 }
465
466 pub fn shape(mut self, shape: CornerShape) -> Self {
467 self.shape = Some(shape);
468 self
469 }
470
471 pub fn container_role(mut self, role: ColorRole) -> Self {
472 self.container_role = Some(role);
473 self
474 }
475
476 pub fn content_role(mut self, role: ColorRole) -> Self {
477 self.content_role = Some(role);
478 self
479 }
480
481 pub fn colors(mut self, colors: SheetColors) -> Self {
482 self.colors = colors;
483 self
484 }
485
486 pub fn tonal_elevation(mut self, elevation: Elevation) -> Self {
487 self.tonal_elevation = elevation;
488 self
489 }
490
491 pub fn shadow_elevation(mut self, elevation: Elevation) -> Self {
492 self.shadow_elevation = Some(elevation);
493 self
494 }
495
496 #[cfg_attr(not(feature = "navigation"), allow(dead_code))]
498 pub(crate) fn sampled_presence(mut self, presence: gpui_base::motion::PresenceSample) -> Self {
499 self.presence = Some(presence);
500 self
501 }
502
503 pub fn scrim_opacity(mut self, opacity: f32) -> Self {
504 self.scrim_opacity = if opacity.is_nan() {
505 0.32
506 } else {
507 opacity.clamp(0., 1.)
508 };
509 self
510 }
511}