core: add height option to dialog with scrolling body

This commit is contained in:
2026-11-12 09:31:08 +07:00
parent 9678a7676f
commit 6667c81259
2 changed files with 36 additions and 16 deletions
+1 -1
View File
@@ -82,7 +82,7 @@ tooltip = []
menu = ["scroll_area"]
dropdown_menu = ["menu", "button"]
select = ["menu"]
dialog = ["button"]
dialog = ["button", "scroll_area"]
empty_state = []
alert = []
toast = []
+35 -15
View File
@@ -4,14 +4,15 @@ use std::rc::Rc;
use gpui::{
Animation, AnimationExt as _, AnyElement, App, DefiniteLength, ElementId, FontWeight,
InteractiveElement as _, IntoElement, KeyDownEvent, MouseButton, ParentElement as _,
RenderOnce, Role, SharedString, StatefulInteractiveElement as _, Styled as _, Window, canvas,
div, ease_out_quint, relative,
RenderOnce, Role, ScrollHandle, SharedString, StatefulInteractiveElement as _, Styled as _,
Window, canvas, div, ease_out_quint, relative,
};
use gpui_native_icons::IconRole;
use gpui_native_theme::ActiveTheme as _;
use crate::button::{Button, ButtonVariant};
use crate::common::{Sizable, v_flex};
use crate::scroll_area::ScrollArea;
/// Requests the dialog's close: `false` closes.
pub type ChangeHandler = Rc<dyn Fn(bool, &mut Window, &mut App)>;
@@ -31,6 +32,7 @@ pub struct Dialog {
content: Option<ContentFn>,
actions: Option<ActionsFn>,
min_width: Option<DefiniteLength>,
height: Option<DefiniteLength>,
}
impl Dialog {
@@ -49,6 +51,7 @@ impl Dialog {
content: None,
actions: None,
min_width: None,
height: None,
}
}
@@ -117,6 +120,15 @@ impl Dialog {
self.min_width = Some(width.into());
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 {
@@ -127,17 +139,7 @@ impl RenderOnce for Dialog {
return root;
}
let (
scrim_color,
border,
foreground,
muted_foreground,
surface,
radius,
shadows,
title_size,
enter_duration,
) = {
let (scrim_color, border, foreground, surface, radius, shadows, title_size, enter_duration) = {
let theme = cx.theme();
(
theme.colors.overlay,
@@ -147,7 +149,6 @@ impl RenderOnce for Dialog {
theme.colors.surface
},
theme.colors.foreground,
theme.colors.muted_foreground,
theme.colors.surface,
theme.radius.overlay,
theme.elevations.dialog.shadows(),
@@ -177,6 +178,12 @@ impl RenderOnce for Dialog {
let content = self.content.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()
.id("dialog-panel")
@@ -186,6 +193,7 @@ impl RenderOnce for Dialog {
.when_some(self.min_width, |panel, width| panel.min_w(width))
.when(self.min_width.is_none(), |panel| panel.min_w_80())
.max_w(relative(0.9))
.when_some(height, |panel, height| panel.h(height))
.p_4()
.gap_3()
.bg(surface)
@@ -245,7 +253,19 @@ impl RenderOnce for Dialog {
)
})
.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| {
panel.child(div().flex().justify_end().gap_2().children(actions))