From 6667c812597bd2adf415d29a4e50355bb3bf06ee Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Thu, 12 Nov 2026 09:31:08 +0700 Subject: [PATCH] core: add height option to dialog with scrolling body --- crates/core/Cargo.toml | 2 +- crates/core/src/dialog.rs | 50 +++++++++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 3be2ed7..55cc5d5 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -82,7 +82,7 @@ tooltip = [] menu = ["scroll_area"] dropdown_menu = ["menu", "button"] select = ["menu"] -dialog = ["button"] +dialog = ["button", "scroll_area"] empty_state = [] alert = [] toast = [] diff --git a/crates/core/src/dialog.rs b/crates/core/src/dialog.rs index 528ab42..0c34639 100644 --- a/crates/core/src/dialog.rs +++ b/crates/core/src/dialog.rs @@ -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; @@ -31,6 +32,7 @@ pub struct Dialog { content: Option, actions: Option, min_width: Option, + height: Option, } 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(mut self, height: H) -> Self + where + H: Into, + { + 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))