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