Skip to main content

fc_ui/capabilities/overlays/
alert_dialog.rs

1//! Alert dialog component for confirmations.
2
3use gpui::{prelude::FluentBuilder as _, *};
4use std::rc::Rc;
5
6const ALERT_DIALOG_CONTEXT: &str = "AlertDialog";
7
8use crate::capabilities::foundation::theme::use_theme;
9use crate::capabilities::primitives::button::{Button, ButtonSize, ButtonVariant};
10
11actions!(alert_dialog, [AlertDialogCancel]);
12
13pub struct AlertDialog {
14    focus_handle: FocusHandle,
15    title: SharedString,
16    description: SharedString,
17    cancel_text: SharedString,
18    action_text: SharedString,
19    destructive: bool,
20    on_cancel: Option<Rc<dyn Fn(&mut Window, &mut App)>>,
21    on_action: Option<Rc<dyn Fn(&mut Window, &mut App)>>,
22    focused: bool,
23    previous_focus: Option<FocusHandle>,
24    _focus_lost_subscription: Option<Subscription>,
25    style: StyleRefinement,
26}
27
28impl AlertDialog {
29    pub fn new(cx: &mut Context<Self>) -> Self {
30        Self {
31            focus_handle: cx.focus_handle(),
32            title: "Are you sure?".into(),
33            description: "This action cannot be undone.".into(),
34            cancel_text: "Cancel".into(),
35            action_text: "Continue".into(),
36            destructive: false,
37            on_cancel: None,
38            on_action: None,
39            focused: false,
40            previous_focus: None,
41            _focus_lost_subscription: None,
42            style: StyleRefinement::default(),
43        }
44    }
45
46    pub fn title(mut self, title: impl Into<SharedString>) -> Self {
47        self.title = title.into();
48        self
49    }
50
51    pub fn description(mut self, description: impl Into<SharedString>) -> Self {
52        self.description = description.into();
53        self
54    }
55
56    pub fn cancel_text(mut self, text: impl Into<SharedString>) -> Self {
57        self.cancel_text = text.into();
58        self
59    }
60
61    pub fn action_text(mut self, text: impl Into<SharedString>) -> Self {
62        self.action_text = text.into();
63        self
64    }
65
66    pub fn destructive(mut self, destructive: bool) -> Self {
67        self.destructive = destructive;
68        self
69    }
70
71    pub fn on_cancel<F>(mut self, handler: F) -> Self
72    where
73        F: Fn(&mut Window, &mut App) + 'static,
74    {
75        self.on_cancel = Some(Rc::new(handler));
76        self
77    }
78
79    pub fn on_action<F>(mut self, handler: F) -> Self
80    where
81        F: Fn(&mut Window, &mut App) + 'static,
82    {
83        self.on_action = Some(Rc::new(handler));
84        self
85    }
86
87    fn restore_previous_focus(&mut self, window: &mut Window, cx: &App) {
88        if let Some(previous_focus) = self.previous_focus.take() {
89            window.focus(&previous_focus);
90        } else if let Some(restore_target) = window.focus_lost_restore_target(cx) {
91            window.focus(&restore_target);
92        }
93    }
94
95    fn handle_cancel(&mut self, window: &mut Window, cx: &mut Context<Self>) {
96        self.restore_previous_focus(window, cx);
97        if let Some(handler) = &self.on_cancel {
98            handler(window, cx);
99        }
100    }
101
102    fn handle_action(&mut self, window: &mut Window, cx: &mut Context<Self>) {
103        self.restore_previous_focus(window, cx);
104        if let Some(handler) = &self.on_action {
105            handler(window, cx);
106        }
107    }
108
109    fn handle_escape(
110        &mut self,
111        _: &AlertDialogCancel,
112        window: &mut Window,
113        cx: &mut Context<Self>,
114    ) {
115        self.handle_cancel(window, cx);
116    }
117}
118
119pub fn init_alert_dialog(cx: &mut App) {
120    if !crate::capabilities::foundation::initialization::begin(cx, "alert_dialog") {
121        return;
122    }
123    cx.bind_keys([KeyBinding::new(
124        "escape",
125        AlertDialogCancel,
126        Some(ALERT_DIALOG_CONTEXT),
127    )]);
128}
129
130impl Styled for AlertDialog {
131    fn style(&mut self) -> &mut StyleRefinement {
132        &mut self.style
133    }
134}
135
136impl Render for AlertDialog {
137    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
138        let theme = use_theme(cx);
139        let user_style = self.style.clone();
140        let title = self.title.clone();
141        let description = self.description.clone();
142        let cancel_text = self.cancel_text.clone();
143        let action_text = self.action_text.clone();
144        let destructive = self.destructive;
145
146        if !self.focused {
147            self.previous_focus = window.focused(cx);
148            window.focus(&self.focus_handle);
149            self.focused = true;
150            self._focus_lost_subscription = Some(cx.on_focus_lost(window, |this, window, cx| {
151                if this.focused {
152                    this.focused = false;
153                    this.restore_previous_focus(window, cx);
154                    cx.notify();
155                }
156            }));
157        }
158
159        div()
160            .track_focus(&self.focus_handle)
161            .key_context(ALERT_DIALOG_CONTEXT)
162            .on_action(cx.listener(Self::handle_escape))
163            .absolute()
164            .inset_0()
165            .flex()
166            .items_center()
167            .justify_center()
168            .bg(hsla(0.0, 0.0, 0.0, 0.5))
169            .child(
170                div()
171                    .w(px(500.0))
172                    .bg(theme.tokens.card)
173                    .border_1()
174                    .border_color(theme.tokens.border)
175                    .rounded(theme.tokens.radius_lg)
176                    .shadow(smallvec::smallvec![BoxShadow {
177                        color: hsla(0.0, 0.0, 0.0, 0.25),
178                        offset: point(px(0.0), px(8.0)),
179                        blur_radius: px(24.0),
180                        spread_radius: px(0.0),
181                        inset: false,
182                    }])
183                    .overflow_hidden()
184                    .child(
185                        div()
186                            .flex()
187                            .flex_col()
188                            .gap(px(16.0))
189                            .p(px(24.0))
190                            .child(
191                                div()
192                                    .text_size(px(18.0))
193                                    .font_weight(FontWeight::SEMIBOLD)
194                                    .text_color(theme.tokens.foreground)
195                                    .child(title),
196                            )
197                            .child(
198                                div()
199                                    .text_size(px(14.0))
200                                    .text_color(theme.tokens.muted_foreground)
201                                    .line_height(relative(1.5))
202                                    .child(description),
203                            )
204                            .child(
205                                div()
206                                    .flex()
207                                    .gap(px(12.0))
208                                    .justify_end()
209                                    .child(
210                                        Button::new("alert-cancel-btn", cancel_text)
211                                            .variant(ButtonVariant::Outline)
212                                            .size(ButtonSize::Md)
213                                            .on_click(cx.listener(|this, _, window, cx| {
214                                                this.handle_cancel(window, cx);
215                                            })),
216                                    )
217                                    .child(
218                                        Button::new("alert-action-btn", action_text)
219                                            .variant(if destructive {
220                                                ButtonVariant::Destructive
221                                            } else {
222                                                ButtonVariant::Default
223                                            })
224                                            .size(ButtonSize::Md)
225                                            .on_click(cx.listener(|this, _, window, cx| {
226                                                this.handle_action(window, cx);
227                                            })),
228                                    ),
229                            ),
230                    )
231                    .map(|this| {
232                        let mut div = this;
233                        div.style().refine(&user_style);
234                        div
235                    }),
236            )
237    }
238}