use gpui::{
AnyElement, App, ElementId, Entity, InteractiveElement as _, IntoElement, ParentElement,
RenderOnce, ScrollHandle, SharedString, StyleRefinement, Styled, Window, div,
prelude::FluentBuilder as _, px,
};
use gpui_deck::{
Dialog, DialogBackdrop, DialogDescription, DialogPopup, DialogState, DialogTitle, ScrollArea,
StyledExt as _, TestSupportExt as _,
};
use super::super::Palette;
#[allow(dead_code)] #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(crate) enum SheetSide {
Top,
#[default]
Right,
Bottom,
Left,
}
#[derive(IntoElement)]
pub(crate) struct Sheet {
dialog: Dialog,
style: StyleRefinement,
side: SheetSide,
body: ScrollArea,
id: ElementId,
title: SharedString,
description: Option<SharedString>,
footer: Option<AnyElement>,
palette: Palette,
}
impl Sheet {
pub(crate) fn new(
id: impl Into<ElementId>,
state: &Entity<DialogState>,
scroll: &ScrollHandle,
title: impl Into<SharedString>,
palette: Palette,
) -> Self {
let id = id.into();
let title = title.into();
Self {
dialog: Dialog::new(state)
.accessibility_label(title.clone())
.backdrop(
DialogBackdrop::new()
.absolute()
.inset_0()
.bg(gpui::black())
.opacity(0.45),
),
style: StyleRefinement::default(),
side: SheetSide::default(),
body: ScrollArea::new((id.clone(), "body"), scroll)
.accessibility_label(format!("{title} content"))
.flex_1()
.scrollbar_styles(|styles| {
styles.thumb(|thumb| thumb.bg(palette.muted).radius(px(3.)))
})
.content_styles(|style| {
style.flex().flex_col().gap(palette.gap).p(palette.padding)
}),
id,
title,
description: None,
footer: None,
palette,
}
}
#[allow(dead_code)] pub(crate) fn side(mut self, side: SheetSide) -> Self {
self.side = side;
self
}
pub(crate) fn description(mut self, description: impl Into<SharedString>) -> Self {
let description = description.into();
self.dialog = self.dialog.accessibility_description(description.clone());
self.description = Some(description);
self
}
pub(crate) fn footer(mut self, footer: impl IntoElement) -> Self {
self.footer = Some(footer.into_any_element());
self
}
}
impl Styled for Sheet {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl ParentElement for Sheet {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.body.extend(elements);
}
}
impl RenderOnce for Sheet {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let palette = self.palette;
let popup = DialogPopup::new()
.max_w_full()
.max_h_full()
.min_w_0()
.min_h_0()
.flex_shrink_0()
.flex()
.flex_col()
.overflow_hidden()
.bg(palette.surface)
.text_color(palette.foreground)
.text_size(palette.font_size)
.border_color(palette.border);
let (dialog, popup) = match self.side {
SheetSide::Right => (
self.dialog.justify_end().items_stretch(),
popup.w(palette.dialog_width).h_full().border_l_1(),
),
SheetSide::Left => (
self.dialog.justify_start().items_stretch(),
popup.w(palette.dialog_width).h_full().border_r_1(),
),
SheetSide::Top => (
self.dialog.flex_col().justify_start().items_stretch(),
popup.w_full().h(palette.dialog_width).border_b_1(),
),
SheetSide::Bottom => (
self.dialog.flex_col().justify_end().items_stretch(),
popup.w_full().h(palette.dialog_width).border_t_1(),
),
};
let popup = popup.refine_style(&self.style);
let header = div()
.id((self.id.clone(), "header"))
.test_support()
.flex()
.flex_col()
.flex_shrink_0()
.gap(palette.gap)
.p(palette.padding)
.border_b_1()
.border_color(palette.border)
.child(DialogTitle::new().text_lg().child(self.title))
.when_some(self.description, |header, description| {
header.child(
DialogDescription::new()
.text_color(palette.muted)
.child(description),
)
});
dialog.popup(popup.child(header).child(self.body).when_some(
self.footer,
|popup, footer| {
popup.child(
div()
.id((self.id, "footer"))
.test_support()
.flex_shrink_0()
.p(palette.padding)
.border_t_1()
.border_color(palette.border)
.child(footer),
)
},
))
}
}