update
Rust / build (macos-latest, stable) (push) Waiting to run
Rust / build (ubuntu-latest, stable) (push) Waiting to run
Rust / build (windows-latest, stable) (push) Waiting to run
Rust / build (macos-latest, stable) (pull_request) Waiting to run
Rust / build (ubuntu-latest, stable) (pull_request) Waiting to run
Rust / build (windows-latest, stable) (pull_request) Waiting to run

This commit is contained in:
2026-11-19 10:37:22 +07:00
parent c7c1d0c741
commit 2a9d42a612
29 changed files with 532 additions and 425 deletions
+2 -1
View File
@@ -25,6 +25,7 @@ use crate::components::common::{
};
use crate::components::spinner::Spinner;
use crate::icons::Icon;
use crate::spinner::SpinnerVariant;
use crate::theme::{ActiveTheme as _, ControlMetrics, DISABLED_OPACITY, Size};
/// The single activation handler, shared by pointer, Enter, and Space.
@@ -362,7 +363,7 @@ impl RenderOnce for Button {
h_flex().absolute().inset_0().justify_center().child(
Spinner::new((self.id.clone(), "spinner"))
.with_size(self.size)
.variant(crate::components::spinner::SpinnerVariant::Icon)
.variant(SpinnerVariant::Icon)
.color(foreground),
),
)
+110 -16
View File
@@ -1,5 +1,9 @@
//! Renders a centred panel over a dim scrim: the platform's modal.
//!
//! Give the dialog a [`DialogHandle`] to share its open state: dismissal
//! through escape, the scrim, or the close button updates every holder of a
//! clone, and they can open or close it in turn.
//!
//! # Examples
//!
//! ```no_run
@@ -17,6 +21,7 @@
//! .on_change(|open, _window, _cx| println!("dialog open: {open}"));
//! ```
use std::cell::Cell;
use std::rc::Rc;
use gpui::prelude::FluentBuilder as _;
@@ -32,6 +37,7 @@ use crate::components::common::{DIALOG_LAYER, Sizable, StyledExt as _, v_flex};
use crate::components::grain::grain;
use crate::components::scroll_area::ScrollArea;
use crate::icons::IconRole;
use crate::layout::h_flex;
use crate::theme::{ActiveTheme as _, DIALOG_MIN_WIDTH, TextRole, Typeset as _};
/// Requests the dialog's close: `false` closes.
@@ -39,6 +45,42 @@ pub type ChangeHandler = Rc<dyn Fn(bool, &mut Window, &mut App)>;
type ContentFn = Box<dyn Fn(&mut Window, &mut App) -> AnyElement>;
type ActionsFn = Box<dyn Fn(&mut Window, &mut App) -> Vec<AnyElement>>;
/// The shared open state of a dialog; clone it to open or close from anywhere.
#[derive(Clone, Default)]
pub struct DialogHandle {
open: Rc<Cell<bool>>,
}
impl DialogHandle {
/// Creates a handle with the given open state.
pub fn new(open: bool) -> Self {
Self {
open: Rc::new(Cell::new(open)),
}
}
/// Whether the dialog is open.
pub fn is_open(&self) -> bool {
self.open.get()
}
/// Opens the dialog.
pub fn open(&self, cx: &mut App) {
self.set_open(true, cx);
}
/// Closes the dialog.
pub fn close(&self, cx: &mut App) {
self.set_open(false, cx);
}
fn set_open(&self, open: bool, cx: &mut App) {
if self.open.replace(open) != open {
cx.refresh_windows();
}
}
}
/// A centred panel over a dim scrim: the platform's modal.
#[derive(IntoElement)]
#[must_use]
@@ -49,7 +91,10 @@ pub struct Dialog {
grain: bool,
title: Option<SharedString>,
a11y_label: Option<SharedString>,
handle: Option<DialogHandle>,
on_change: Option<ChangeHandler>,
close_on_escape: bool,
close_on_backdrop: bool,
content: Option<ContentFn>,
actions: Option<ActionsFn>,
min_width: Option<DefiniteLength>,
@@ -70,7 +115,10 @@ impl Dialog {
grain: false,
title: None,
a11y_label: None,
handle: None,
on_change: None,
close_on_escape: true,
close_on_backdrop: true,
content: None,
actions: None,
min_width: None,
@@ -79,7 +127,7 @@ impl Dialog {
}
}
/// Sets the current open state.
/// Sets the initial open state; a handle overrides it.
pub fn open(mut self, open: bool) -> Self {
self.open = open;
self
@@ -97,6 +145,24 @@ impl Dialog {
self
}
/// Shares the dialog's open state.
pub fn handle(mut self, handle: DialogHandle) -> Self {
self.handle = Some(handle);
self
}
/// Sets whether escape dismisses the dialog.
pub fn close_on_escape(mut self, close: bool) -> Self {
self.close_on_escape = close;
self
}
/// Sets whether clicking the scrim dismisses the dialog.
pub fn close_on_backdrop(mut self, close: bool) -> Self {
self.close_on_backdrop = close;
self
}
/// Requests close through `on_change` on escape, scrim clicks, and the close button.
pub fn on_change<F>(mut self, handler: F) -> Self
where
@@ -171,10 +237,24 @@ impl RenderOnce for Dialog {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let root = div().absolute().inset_0();
if !self.open {
let open = self
.handle
.as_ref()
.map_or(self.open, DialogHandle::is_open);
if !open {
return root.into_any_element();
}
// A handle routes dismissal through the shared state
let on_change = self.on_change.clone().or_else(|| {
self.handle.clone().map(|handle| {
Rc::new(move |open: bool, _window: &mut Window, cx: &mut App| {
handle.set_open(open, cx);
}) as ChangeHandler
})
});
let (scrim_color, border, foreground, surface, radius, shadows, enter_duration) = {
let theme = cx.theme();
(
@@ -200,9 +280,10 @@ impl RenderOnce for Dialog {
.or_else(|| self.title.clone())
.unwrap_or_else(|| SharedString::from(self.id.to_string()));
let on_change = self.on_change.clone();
let alert = self.alert;
let show_close = !alert && on_change.is_some();
let close_on_escape = self.close_on_escape;
let close_on_backdrop = self.close_on_backdrop;
let show_close = !alert && on_change.is_some() && (close_on_escape || close_on_backdrop);
// Keyboard focus rides the panel across frames.
let focus = crate::components::common::focus_handle(&self.id, window, cx);
@@ -227,8 +308,6 @@ impl RenderOnce for Dialog {
})
.max_w(relative(0.9))
.when_some(height, |panel, height| panel.h(height))
.p_4()
.gap_4()
.bg(surface)
.text_color(foreground)
.rounded(radius)
@@ -242,6 +321,7 @@ impl RenderOnce for Dialog {
move |event: &KeyDownEvent, window, cx| {
if event.keystroke.key == "escape"
&& !alert
&& close_on_escape
&& let Some(on_change) = &on_change
{
on_change(false, window, cx);
@@ -251,20 +331,27 @@ impl RenderOnce for Dialog {
})
.when(self.title.is_some() || show_close, |panel| {
panel.child(
div()
.flex()
.items_center()
.justify_end()
h_flex()
.h_12()
.px_2()
.gap_2()
.justify_between()
.child(div().size_5())
.when_some(self.title.clone(), |row, title| {
row.child(div().flex_1().typeset(TextRole::Title3, cx).child(title))
row.child(
div()
.flex_1()
.text_center()
.typeset(TextRole::Headline, cx)
.child(title),
)
})
.when(show_close, |row| {
row.child(
Button::new("dialog-close")
.icon(IconRole::Close)
.variant(ButtonVariant::Ghost)
.xsmall()
.small()
.debug_selector(|| "dialog-close".to_string())
.accessibility_label("Close")
.on_click({
@@ -287,15 +374,22 @@ impl RenderOnce for Dialog {
.handle(scroll)
.flex_1()
.min_h_0()
.child(content),
.child(div().p_2().child(content)),
)
} else {
this.child(content)
this.child(div().p_2().child(content))
}
})
})
.when_some(actions, |panel, actions| {
panel.child(div().flex().justify_end().gap_3().children(actions))
panel.child(
h_flex()
.px_2()
.h_12()
.justify_end()
.gap_2()
.children(actions),
)
})
.child(
canvas(|_, _, _| {}, {
@@ -315,7 +409,7 @@ impl RenderOnce for Dialog {
.bg(scrim_color)
.occlude()
.when(self.grain, |scrim| scrim.child(grain()))
.when(!alert, |scrim| {
.when(!alert && close_on_backdrop, |scrim| {
scrim.on_mouse_down(MouseButton::Left, {
let on_change = on_change.clone();
move |_, _window, cx| {
+1
View File
@@ -110,6 +110,7 @@ impl RenderOnce for ScrollArea {
let pane = div()
.id(self.id.clone())
.h_full()
.overflow_hidden()
.when(self.axes != ScrollAxes::Horizontal, |area| {
area.overflow_y_scroll()
+23 -25
View File
@@ -105,30 +105,28 @@ impl RenderOnce for Shell {
theme.colors.muted
};
let layout = v_flex()
.w_full()
.h_full()
.overflow_hidden()
.bg(background)
.refine_style(&self.style)
.when(self.transparent && self.grain, |layout| {
layout.child(grain_overlay())
});
let layout = layout
.when_some(self.titlebar, |layout, titlebar| {
layout.child(titlebar.transparent(self.transparent))
})
.child(self.panel.unwrap_or_else(|| div().into_any_element()));
let frame = frame(layout, window, cx);
frame
.when_some(self.toasts, |frame, toasts| {
frame.child(ToastsView::new((id.clone(), "toasts"), toasts))
})
.when_some(self.alerts, |frame, alerts| {
frame.child(AlertsView::new((id, "alerts"), alerts))
})
frame(
v_flex()
.w_full()
.h_full()
.overflow_hidden()
.bg(background)
.refine_style(&self.style)
.when(self.transparent && self.grain, |layout| {
layout.child(grain_overlay())
})
.when_some(self.titlebar, |layout, titlebar| {
layout.child(titlebar.transparent(self.transparent))
})
.child(self.panel.unwrap_or_else(|| div().into_any_element())),
window,
cx,
)
.when_some(self.toasts, |frame, toasts| {
frame.child(ToastsView::new((id.clone(), "toasts"), toasts))
})
.when_some(self.alerts, |frame, alerts| {
frame.child(AlertsView::new((id, "alerts"), alerts))
})
}
}
+1 -1
View File
@@ -34,7 +34,7 @@ pub enum TextRole {
Title2,
/// Sub-section and dialog headings.
Title3,
/// Body-sized emphasis — list row titles, group labels.
/// Body-sized emphasis, list row titles, group labels.
Headline,
/// Control labels and reading text.
Body,