gpui_deck/alert_dialog.rs
1//! A confirmation surface composed from the retained Dialog host.
2//! Adapted from GPUI Kit v0.9.0's AlertDialog specialization; GPUI Deck reuses
3//! DialogState and the existing Dialog parts instead of callback-owned lifecycle.
4
5use gpui::{
6 App, Entity, IntoElement, RenderOnce, Role, SharedString, StyleRefinement, Styled, Window,
7};
8
9use crate::{Dialog, DialogState};
10
11/// An unstyled alert dialog for a short decision that needs an explicit response.
12///
13/// Supply an accessible name and description, a backdrop, and a popup containing
14/// cancellation and confirmation controls. Reuse [`crate::DialogBackdrop`],
15/// [`crate::DialogPopup`], [`crate::DialogTitle`], and [`crate::DialogDescription`]
16/// for presentation. All visual styles belong to the application.
17///
18/// Create the retained state with [`DialogState::with_initial_focus`] pointing at
19/// the cancellation button. [`crate::DialogClose`] supplies its close behavior;
20/// an ordinary [`crate::Button`] can perform the application's confirmation and
21/// call [`DialogState::close`]. Escape cancels, while backdrop presses never
22/// dismiss this host. Closing alone does not execute the confirmation action.
23///
24/// Keep this host mounted while closed and host the window in [`crate::Root`].
25/// The existing Dialog owns focus containment, initial focus, and restoration.
26#[derive(IntoElement)]
27pub struct AlertDialog {
28 dialog: Dialog,
29}
30
31impl AlertDialog {
32 pub fn new(state: &Entity<DialogState>) -> Self {
33 Self {
34 dialog: Dialog::new(state)
35 .role(Role::AlertDialog)
36 .close_on_backdrop_press(false),
37 }
38 }
39
40 pub fn accessibility_label(mut self, label: impl Into<SharedString>) -> Self {
41 self.dialog = self.dialog.accessibility_label(label);
42 self
43 }
44
45 pub fn accessibility_description(mut self, description: impl Into<SharedString>) -> Self {
46 self.dialog = self.dialog.accessibility_description(description);
47 self
48 }
49
50 pub fn backdrop(mut self, element: impl IntoElement) -> Self {
51 self.dialog = self.dialog.backdrop(element);
52 self
53 }
54
55 pub fn popup(mut self, element: impl IntoElement) -> Self {
56 self.dialog = self.dialog.popup(element);
57 self
58 }
59}
60
61impl Styled for AlertDialog {
62 fn style(&mut self) -> &mut StyleRefinement {
63 self.dialog.style()
64 }
65}
66
67impl RenderOnce for AlertDialog {
68 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
69 self.dialog.render(window, cx)
70 }
71}