1use gpui_base::TestSupportExt as _;
2use std::{rc::Rc, time::Duration};
3
4use gpui::{
5 Animation, AnimationExt as _, AnyElement, App, ClickEvent, DefiniteLength, DismissEvent, Edges,
6 EventEmitter, FocusHandle, InteractiveElement as _, IntoElement, ParentElement, Pixels,
7 RenderOnce, StyleRefinement, Styled, Window, div, prelude::FluentBuilder as _, px, rems,
8};
9use gpui_base::{
10 ElementExt as _, LayoutDirection, LogicalStyledExt as _, Sheet as BaseSheet,
11 TextSelectionScopeId, actions::Cancel,
12};
13use schemars::JsonSchema;
14use serde::{Deserialize, Serialize};
15
16use crate::{
17 ActiveTheme, IconName, Placement, Sizable, StyledExt as _, WindowExt as _,
18 button::{Button, ButtonVariants as _},
19 dialog::overlay_color,
20 h_flex,
21 scroll::ScrollableElement as _,
22 title_bar::TITLE_BAR_HEIGHT,
23 v_flex,
24};
25
26pub(crate) fn init(_: &mut App) {}
27
28pub(crate) fn physical_placement(placement: Placement, direction: LayoutDirection) -> Placement {
33 match (placement, direction) {
34 (Placement::Left, LayoutDirection::Rtl) => Placement::Right,
35 (Placement::Right, LayoutDirection::Rtl) => Placement::Left,
36 (placement, _) => placement,
37 }
38}
39
40#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
42#[non_exhaustive]
43pub struct SheetSettings {
44 pub margin_top: Pixels,
46}
47
48impl Default for SheetSettings {
49 fn default() -> Self {
50 Self {
51 margin_top: TITLE_BAR_HEIGHT,
52 }
53 }
54}
55
56#[derive(IntoElement)]
58pub struct Sheet {
59 pub(crate) focus_handle: FocusHandle,
60 pub(crate) placement: Placement,
61 pub(crate) size: DefiniteLength,
62 pub(crate) selection_scope: TextSelectionScopeId,
63 resizable: bool,
64 on_close: Rc<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>,
65 title: Option<AnyElement>,
66 footer: Option<AnyElement>,
67 style: StyleRefinement,
68 children: Vec<AnyElement>,
69 overlay: bool,
70 overlay_closable: bool,
71}
72
73impl Sheet {
74 pub fn new(_: &mut Window, cx: &mut App) -> Self {
76 Self {
77 focus_handle: cx.focus_handle(),
78 placement: Placement::Right,
79 size: DefiniteLength::Absolute(px(350.).into()),
80 selection_scope: TextSelectionScopeId::default(),
81 resizable: true,
82 title: None,
83 footer: None,
84 style: StyleRefinement::default(),
85 children: Vec::new(),
86 overlay: true,
87 overlay_closable: true,
88 on_close: Rc::new(|_, _, _| {}),
89 }
90 }
91
92 pub fn title(mut self, title: impl IntoElement) -> Self {
94 self.title = Some(title.into_any_element());
95 self
96 }
97
98 pub fn footer(mut self, footer: impl IntoElement) -> Self {
100 self.footer = Some(footer.into_any_element());
101 self
102 }
103
104 pub fn size(mut self, size: impl Into<DefiniteLength>) -> Self {
106 self.size = size.into();
107 self
108 }
109
110 pub fn resizable(mut self, resizable: bool) -> Self {
112 self.resizable = resizable;
113 self
114 }
115
116 pub fn overlay(mut self, overlay: bool) -> Self {
118 self.overlay = overlay;
119 self
120 }
121
122 pub fn overlay_closable(mut self, overlay_closable: bool) -> Self {
124 self.overlay_closable = overlay_closable;
125 self
126 }
127
128 pub fn on_close(
130 mut self,
131 on_close: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
132 ) -> Self {
133 self.on_close = Rc::new(on_close);
134 self
135 }
136}
137
138impl EventEmitter<DismissEvent> for Sheet {}
139impl ParentElement for Sheet {
140 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
141 self.children.extend(elements);
142 }
143}
144impl Styled for Sheet {
145 fn style(&mut self) -> &mut gpui::StyleRefinement {
146 &mut self.style
147 }
148}
149
150impl RenderOnce for Sheet {
151 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
152 let placement = self.placement;
153 let selection_scope = self.selection_scope;
154 let frame_insets = crate::window_border::window_content_insets(window);
155 let safe_insets = crate::window_border::window_safe_insets(window);
156 let frame_insets = Edges {
157 top: frame_insets.top.max(safe_insets.top),
158 right: frame_insets.right.max(safe_insets.right),
159 bottom: frame_insets.bottom.max(safe_insets.bottom),
160 left: frame_insets.left.max(safe_insets.left),
161 };
162 let size = window.viewport_size()
163 - gpui::size(
164 frame_insets.left + frame_insets.right,
165 frame_insets.top + frame_insets.bottom,
166 );
167 let top = cx.theme().sheet.margin_top;
168 let base_size = window.text_style().font_size;
169 let rem_size = window.rem_size();
170 let mut paddings = Edges::all(px(16.));
171 if let Some(pl) = self.style.padding.left {
172 paddings.left = pl.to_pixels(base_size, rem_size);
173 }
174 if let Some(pr) = self.style.padding.right {
175 paddings.right = pr.to_pixels(base_size, rem_size);
176 }
177 if let Some(pt) = self.style.padding.top {
178 paddings.top = pt.to_pixels(base_size, rem_size);
179 }
180 if let Some(pb) = self.style.padding.bottom {
181 paddings.bottom = pb.to_pixels(base_size, rem_size);
182 }
183
184 let overlay = div()
185 .occlude()
186 .w(size.width)
187 .h(size.height)
188 .bg(overlay_color(self.overlay, cx));
189
190 let surface = v_flex()
191 .id("sheet-content")
192 .test_support()
193 .absolute()
194 .occlude()
195 .bg(cx.theme().tokens.background)
196 .border_color(cx.theme().border)
197 .shadow_xl()
198 .refine_style(&self.style)
199 .map(|this| {
200 if placement.is_horizontal() {
202 this.w(self.size)
203 } else {
204 this.h(self.size)
205 }
206 })
207 .map(|this| match self.placement {
208 Placement::Top => this.top(top).left_0().right_0().border_b_1(),
209 Placement::Right => this.top(top).right_0().bottom_0().border_l_1(),
210 Placement::Bottom => this.bottom_0().left_0().right_0().border_t_1(),
211 Placement::Left => this.top(top).left_0().bottom_0().border_r_1(),
212 })
213 .child(
214 h_flex()
216 .flex_row_dir(cx)
217 .justify_between()
218 .ps(rems(1.), cx)
219 .pe(rems(0.75), cx)
220 .py_2()
221 .w_full()
222 .font_semibold()
223 .child(self.title.unwrap_or(div().into_any_element()))
224 .child(
225 Button::new("close")
226 .small()
227 .ghost()
228 .icon(IconName::Close)
229 .accessibility_label("Close")
230 .on_click(|_, window, cx| {
231 window.dispatch_action(Box::new(Cancel), cx);
232 }),
233 ),
234 )
235 .child(
236 div().flex_1().overflow_hidden().child(
237 v_flex()
239 .size_full()
240 .overflow_y_scrollbar()
241 .ps(paddings.left, cx)
242 .pe(paddings.right, cx)
243 .children(self.children),
244 ),
245 )
246 .when_some(self.footer, |this, footer| {
247 this.child(
249 h_flex()
250 .justify_between()
251 .px_4()
252 .py_3()
253 .w_full()
254 .child(footer),
255 )
256 })
257 .with_animation(
258 "slide",
259 Animation::new(Duration::from_secs_f64(0.15)),
260 move |this, delta| {
261 let y = px(-100.) + delta * px(100.);
262 this.map(|this| match placement {
263 Placement::Top => this.top(top + y),
264 Placement::Right => this.right(y),
265 Placement::Bottom => this.bottom(y),
266 Placement::Left => this.left(y),
267 })
268 },
269 );
270 let surface = surface.text_selection_scope(selection_scope);
271
272 BaseSheet::new(cx)
273 .top(frame_insets.top)
274 .left(frame_insets.left)
275 .w(size.width)
276 .h(size.height)
277 .focus_handle(self.focus_handle)
278 .overlay_interactive(self.overlay)
279 .overlay_closable(self.overlay && self.overlay_closable)
280 .request_close(|window, cx| window.close_sheet(cx))
281 .on_close(move |event, window, cx| (self.on_close)(event, window, cx))
282 .overlay(overlay)
283 .surface(surface)
284 }
285}
286
287#[cfg(test)]
288mod tests {
289 use super::*;
290
291 #[test]
292 fn rtl_mirrors_left_and_right_sheets() {
293 let rtl = LayoutDirection::Rtl;
294 assert_eq!(physical_placement(Placement::Right, rtl), Placement::Left);
295 assert_eq!(physical_placement(Placement::Left, rtl), Placement::Right);
296 assert_eq!(
297 physical_placement(Placement::Bottom, rtl),
298 Placement::Bottom
299 );
300 assert_eq!(
301 physical_placement(Placement::Right, LayoutDirection::Ltr),
302 Placement::Right
303 );
304 }
305}