core: add height option to dialog with scrolling body
This commit is contained in:
@@ -82,7 +82,7 @@ tooltip = []
|
|||||||
menu = ["scroll_area"]
|
menu = ["scroll_area"]
|
||||||
dropdown_menu = ["menu", "button"]
|
dropdown_menu = ["menu", "button"]
|
||||||
select = ["menu"]
|
select = ["menu"]
|
||||||
dialog = ["button"]
|
dialog = ["button", "scroll_area"]
|
||||||
empty_state = []
|
empty_state = []
|
||||||
alert = []
|
alert = []
|
||||||
toast = []
|
toast = []
|
||||||
|
|||||||
+35
-15
@@ -4,14 +4,15 @@ use std::rc::Rc;
|
|||||||
use gpui::{
|
use gpui::{
|
||||||
Animation, AnimationExt as _, AnyElement, App, DefiniteLength, ElementId, FontWeight,
|
Animation, AnimationExt as _, AnyElement, App, DefiniteLength, ElementId, FontWeight,
|
||||||
InteractiveElement as _, IntoElement, KeyDownEvent, MouseButton, ParentElement as _,
|
InteractiveElement as _, IntoElement, KeyDownEvent, MouseButton, ParentElement as _,
|
||||||
RenderOnce, Role, SharedString, StatefulInteractiveElement as _, Styled as _, Window, canvas,
|
RenderOnce, Role, ScrollHandle, SharedString, StatefulInteractiveElement as _, Styled as _,
|
||||||
div, ease_out_quint, relative,
|
Window, canvas, div, ease_out_quint, relative,
|
||||||
};
|
};
|
||||||
use gpui_native_icons::IconRole;
|
use gpui_native_icons::IconRole;
|
||||||
use gpui_native_theme::ActiveTheme as _;
|
use gpui_native_theme::ActiveTheme as _;
|
||||||
|
|
||||||
use crate::button::{Button, ButtonVariant};
|
use crate::button::{Button, ButtonVariant};
|
||||||
use crate::common::{Sizable, v_flex};
|
use crate::common::{Sizable, v_flex};
|
||||||
|
use crate::scroll_area::ScrollArea;
|
||||||
|
|
||||||
/// Requests the dialog's close: `false` closes.
|
/// Requests the dialog's close: `false` closes.
|
||||||
pub type ChangeHandler = Rc<dyn Fn(bool, &mut Window, &mut App)>;
|
pub type ChangeHandler = Rc<dyn Fn(bool, &mut Window, &mut App)>;
|
||||||
@@ -31,6 +32,7 @@ pub struct Dialog {
|
|||||||
content: Option<ContentFn>,
|
content: Option<ContentFn>,
|
||||||
actions: Option<ActionsFn>,
|
actions: Option<ActionsFn>,
|
||||||
min_width: Option<DefiniteLength>,
|
min_width: Option<DefiniteLength>,
|
||||||
|
height: Option<DefiniteLength>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Dialog {
|
impl Dialog {
|
||||||
@@ -49,6 +51,7 @@ impl Dialog {
|
|||||||
content: None,
|
content: None,
|
||||||
actions: None,
|
actions: None,
|
||||||
min_width: None,
|
min_width: None,
|
||||||
|
height: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,6 +120,15 @@ impl Dialog {
|
|||||||
self.min_width = Some(width.into());
|
self.min_width = Some(width.into());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets the panel's height, body content taller than the panel scrolls.
|
||||||
|
pub fn height<H>(mut self, height: H) -> Self
|
||||||
|
where
|
||||||
|
H: Into<DefiniteLength>,
|
||||||
|
{
|
||||||
|
self.height = Some(height.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RenderOnce for Dialog {
|
impl RenderOnce for Dialog {
|
||||||
@@ -127,17 +139,7 @@ impl RenderOnce for Dialog {
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
let (
|
let (scrim_color, border, foreground, surface, radius, shadows, title_size, enter_duration) = {
|
||||||
scrim_color,
|
|
||||||
border,
|
|
||||||
foreground,
|
|
||||||
muted_foreground,
|
|
||||||
surface,
|
|
||||||
radius,
|
|
||||||
shadows,
|
|
||||||
title_size,
|
|
||||||
enter_duration,
|
|
||||||
) = {
|
|
||||||
let theme = cx.theme();
|
let theme = cx.theme();
|
||||||
(
|
(
|
||||||
theme.colors.overlay,
|
theme.colors.overlay,
|
||||||
@@ -147,7 +149,6 @@ impl RenderOnce for Dialog {
|
|||||||
theme.colors.surface
|
theme.colors.surface
|
||||||
},
|
},
|
||||||
theme.colors.foreground,
|
theme.colors.foreground,
|
||||||
theme.colors.muted_foreground,
|
|
||||||
theme.colors.surface,
|
theme.colors.surface,
|
||||||
theme.radius.overlay,
|
theme.radius.overlay,
|
||||||
theme.elevations.dialog.shadows(),
|
theme.elevations.dialog.shadows(),
|
||||||
@@ -177,6 +178,12 @@ impl RenderOnce for Dialog {
|
|||||||
|
|
||||||
let content = self.content.map(|build| build(window, cx));
|
let content = self.content.map(|build| build(window, cx));
|
||||||
let actions = self.actions.map(|build| build(window, cx));
|
let actions = self.actions.map(|build| build(window, cx));
|
||||||
|
let height = self.height;
|
||||||
|
|
||||||
|
let scroll = window
|
||||||
|
.use_keyed_state("dialog-content-scroll", cx, |_, _| ScrollHandle::new())
|
||||||
|
.read(cx)
|
||||||
|
.clone();
|
||||||
|
|
||||||
let panel = v_flex()
|
let panel = v_flex()
|
||||||
.id("dialog-panel")
|
.id("dialog-panel")
|
||||||
@@ -186,6 +193,7 @@ impl RenderOnce for Dialog {
|
|||||||
.when_some(self.min_width, |panel, width| panel.min_w(width))
|
.when_some(self.min_width, |panel, width| panel.min_w(width))
|
||||||
.when(self.min_width.is_none(), |panel| panel.min_w_80())
|
.when(self.min_width.is_none(), |panel| panel.min_w_80())
|
||||||
.max_w(relative(0.9))
|
.max_w(relative(0.9))
|
||||||
|
.when_some(height, |panel, height| panel.h(height))
|
||||||
.p_4()
|
.p_4()
|
||||||
.gap_3()
|
.gap_3()
|
||||||
.bg(surface)
|
.bg(surface)
|
||||||
@@ -245,7 +253,19 @@ impl RenderOnce for Dialog {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
.when_some(content, |panel, content| {
|
.when_some(content, |panel, content| {
|
||||||
panel.child(div().text_color(muted_foreground).child(content))
|
panel.map(|this| {
|
||||||
|
if height.is_some() {
|
||||||
|
this.child(
|
||||||
|
ScrollArea::new("dialog-content")
|
||||||
|
.handle(scroll)
|
||||||
|
.flex_1()
|
||||||
|
.min_h_0()
|
||||||
|
.child(content),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
this.child(content)
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.when_some(actions, |panel, actions| {
|
.when_some(actions, |panel, actions| {
|
||||||
panel.child(div().flex().justify_end().gap_2().children(actions))
|
panel.child(div().flex().justify_end().gap_2().children(actions))
|
||||||
|
|||||||
Reference in New Issue
Block a user