Skip to main content

gpui_component/
sheet.rs

1use std::{rc::Rc, time::Duration};
2
3use gpui::{
4    Animation, AnimationExt as _, AnyElement, App, ClickEvent, DefiniteLength, DismissEvent, Edges,
5    EventEmitter, FocusHandle, InteractiveElement as _, IntoElement, ParentElement, Pixels,
6    RenderOnce, StyleRefinement, Styled, Window, anchored, div, point, prelude::FluentBuilder as _,
7    px,
8};
9use gpui_base::{ElementExt as _, Sheet as BaseSheet, TextSelectionScopeId, actions::Cancel};
10use schemars::JsonSchema;
11use serde::{Deserialize, Serialize};
12
13use crate::{
14    ActiveTheme, IconName, Placement, Sizable, StyledExt as _, WindowExt as _,
15    button::{Button, ButtonVariants as _},
16    dialog::overlay_color,
17    h_flex,
18    scroll::ScrollableElement as _,
19    title_bar::TITLE_BAR_HEIGHT,
20    v_flex,
21};
22
23pub(crate) fn init(_: &mut App) {}
24
25/// The settings for sheets.
26#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
27pub struct SheetSettings {
28    /// The margin top for the sheet, default is [`TITLE_BAR_HEIGHT`].
29    pub margin_top: Pixels,
30}
31
32impl Default for SheetSettings {
33    fn default() -> Self {
34        Self {
35            margin_top: TITLE_BAR_HEIGHT,
36        }
37    }
38}
39
40/// Sheet component that slides in from the side of the window.
41#[derive(IntoElement)]
42pub struct Sheet {
43    pub(crate) focus_handle: FocusHandle,
44    pub(crate) placement: Placement,
45    pub(crate) size: DefiniteLength,
46    pub(crate) selection_scope: TextSelectionScopeId,
47    resizable: bool,
48    on_close: Rc<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>,
49    title: Option<AnyElement>,
50    footer: Option<AnyElement>,
51    style: StyleRefinement,
52    children: Vec<AnyElement>,
53    overlay: bool,
54    overlay_closable: bool,
55}
56
57impl Sheet {
58    /// Creates a new Sheet.
59    pub fn new(_: &mut Window, cx: &mut App) -> Self {
60        Self {
61            focus_handle: cx.focus_handle(),
62            placement: Placement::Right,
63            size: DefiniteLength::Absolute(px(350.).into()),
64            selection_scope: TextSelectionScopeId::default(),
65            resizable: true,
66            title: None,
67            footer: None,
68            style: StyleRefinement::default(),
69            children: Vec::new(),
70            overlay: true,
71            overlay_closable: true,
72            on_close: Rc::new(|_, _, _| {}),
73        }
74    }
75
76    /// Sets the title of the sheet.
77    pub fn title(mut self, title: impl IntoElement) -> Self {
78        self.title = Some(title.into_any_element());
79        self
80    }
81
82    /// Set the footer of the sheet.
83    pub fn footer(mut self, footer: impl IntoElement) -> Self {
84        self.footer = Some(footer.into_any_element());
85        self
86    }
87
88    /// Sets the size of the sheet, default is 350px.
89    pub fn size(mut self, size: impl Into<DefiniteLength>) -> Self {
90        self.size = size.into();
91        self
92    }
93
94    /// Sets whether the sheet is resizable, default is `true`.
95    pub fn resizable(mut self, resizable: bool) -> Self {
96        self.resizable = resizable;
97        self
98    }
99
100    /// Set whether the sheet should have an overlay, default is `true`.
101    pub fn overlay(mut self, overlay: bool) -> Self {
102        self.overlay = overlay;
103        self
104    }
105
106    /// Set whether the sheet should be closable by clicking the overlay, default is `true`.
107    pub fn overlay_closable(mut self, overlay_closable: bool) -> Self {
108        self.overlay_closable = overlay_closable;
109        self
110    }
111
112    /// Listen to the close event of the sheet.
113    pub fn on_close(
114        mut self,
115        on_close: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
116    ) -> Self {
117        self.on_close = Rc::new(on_close);
118        self
119    }
120}
121
122impl EventEmitter<DismissEvent> for Sheet {}
123impl ParentElement for Sheet {
124    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
125        self.children.extend(elements);
126    }
127}
128impl Styled for Sheet {
129    fn style(&mut self) -> &mut gpui::StyleRefinement {
130        &mut self.style
131    }
132}
133
134impl RenderOnce for Sheet {
135    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
136        let placement = self.placement;
137        let selection_scope = self.selection_scope;
138        let window_paddings = crate::window_border::window_paddings(window);
139        let size = window.viewport_size()
140            - gpui::size(
141                window_paddings.left + window_paddings.right,
142                window_paddings.top + window_paddings.bottom,
143            );
144        let top = cx.theme().sheet.margin_top;
145        let base_size = window.text_style().font_size;
146        let rem_size = window.rem_size();
147        let mut paddings = Edges::all(px(16.));
148        if let Some(pl) = self.style.padding.left {
149            paddings.left = pl.to_pixels(base_size, rem_size);
150        }
151        if let Some(pr) = self.style.padding.right {
152            paddings.right = pr.to_pixels(base_size, rem_size);
153        }
154        if let Some(pt) = self.style.padding.top {
155            paddings.top = pt.to_pixels(base_size, rem_size);
156        }
157        if let Some(pb) = self.style.padding.bottom {
158            paddings.bottom = pb.to_pixels(base_size, rem_size);
159        }
160
161        let overlay = div()
162            .occlude()
163            .w(size.width)
164            .h(size.height)
165            .bg(overlay_color(self.overlay, cx));
166
167        let surface = v_flex()
168            .id("sheet")
169            .absolute()
170            .occlude()
171            .bg(cx.theme().tokens.background)
172            .border_color(cx.theme().border)
173            .shadow_xl()
174            .refine_style(&self.style)
175            .map(|this| {
176                // Set the size of the sheet.
177                if placement.is_horizontal() {
178                    this.w(self.size)
179                } else {
180                    this.h(self.size)
181                }
182            })
183            .map(|this| match self.placement {
184                Placement::Top => this.top(top).left_0().right_0().border_b_1(),
185                Placement::Right => this.top(top).right_0().bottom_0().border_l_1(),
186                Placement::Bottom => this.bottom_0().left_0().right_0().border_t_1(),
187                Placement::Left => this.top(top).left_0().bottom_0().border_r_1(),
188            })
189            .child(
190                // TitleBar
191                h_flex()
192                    .justify_between()
193                    .pl_4()
194                    .pr_3()
195                    .py_2()
196                    .w_full()
197                    .font_semibold()
198                    .child(self.title.unwrap_or(div().into_any_element()))
199                    .child(
200                        Button::new("close")
201                            .small()
202                            .ghost()
203                            .icon(IconName::Close)
204                            .on_click(|_, window, cx| {
205                                window.dispatch_action(Box::new(Cancel), cx);
206                            }),
207                    ),
208            )
209            .child(
210                div().flex_1().overflow_hidden().child(
211                    // Body
212                    v_flex()
213                        .size_full()
214                        .overflow_y_scrollbar()
215                        .pl(paddings.left)
216                        .pr(paddings.right)
217                        .children(self.children),
218                ),
219            )
220            .when_some(self.footer, |this, footer| {
221                // Footer
222                this.child(
223                    h_flex()
224                        .justify_between()
225                        .px_4()
226                        .py_3()
227                        .w_full()
228                        .child(footer),
229                )
230            })
231            .with_animation(
232                "slide",
233                Animation::new(Duration::from_secs_f64(0.15)),
234                move |this, delta| {
235                    let y = px(-100.) + delta * px(100.);
236                    this.map(|this| match placement {
237                        Placement::Top => this.top(top + y),
238                        Placement::Right => this.right(y),
239                        Placement::Bottom => this.bottom(y),
240                        Placement::Left => this.left(y),
241                    })
242                },
243            );
244        let surface = surface.text_selection_scope(selection_scope);
245
246        anchored()
247            .position(point(window_paddings.left, window_paddings.top))
248            .snap_to_window()
249            .child(
250                BaseSheet::new(cx)
251                    .w(size.width)
252                    .h(size.height)
253                    .focus_handle(self.focus_handle)
254                    .overlay_interactive(self.overlay)
255                    .overlay_closable(self.overlay && self.overlay_closable)
256                    .request_close(|window, cx| window.close_sheet(cx))
257                    .on_close(move |event, window, cx| (self.on_close)(event, window, cx))
258                    .overlay(overlay)
259                    .surface(surface),
260            )
261    }
262}