Skip to main content

liora_components/
message_box.rs

1use crate::{Button, Dialog};
2use gpui::{App, SharedString, Window, div, prelude::*};
3use std::sync::Arc;
4
5pub struct MessageBox {
6    title: SharedString,
7    content: SharedString,
8    close_on_click_outside: bool,
9    close_on_escape: bool,
10}
11
12impl MessageBox {
13    pub fn new(title: impl Into<SharedString>, content: impl Into<SharedString>) -> Self {
14        Self {
15            title: title.into(),
16            content: content.into(),
17            close_on_click_outside: true,
18            close_on_escape: true,
19        }
20    }
21
22    pub fn close_on_click_outside(mut self, c: bool) -> Self {
23        self.close_on_click_outside = c;
24        self
25    }
26
27    pub fn close_on_escape(mut self, c: bool) -> Self {
28        self.close_on_escape = c;
29        self
30    }
31
32    pub fn alert(self, cx: &mut App) {
33        let content = self.content.clone();
34        Dialog::new()
35            .title(self.title)
36            .close_on_click_outside(self.close_on_click_outside)
37            .close_on_escape(self.close_on_escape)
38            .content(move |_, _| {
39                div()
40                    .flex()
41                    .flex_col()
42                    .gap_4()
43                    .child(content.clone())
44                    .child(
45                        div()
46                            .flex()
47                            .justify_end()
48                            .child(Button::new("OK").primary().on_click(|_, _, cx| {
49                                Dialog::close(cx);
50                            })),
51                    )
52            })
53            .show(cx);
54    }
55
56    pub fn confirm(self, on_confirm: impl Fn(&mut Window, &mut App) + 'static, cx: &mut App) {
57        let content = self.content.clone();
58        let on_confirm = Arc::new(on_confirm);
59
60        Dialog::new()
61            .title(self.title)
62            .close_on_click_outside(self.close_on_click_outside)
63            .close_on_escape(self.close_on_escape)
64            .content(move |_window, _cx| {
65                let on_confirm = on_confirm.clone();
66                div()
67                    .flex()
68                    .flex_col()
69                    .gap_4()
70                    .child(content.clone())
71                    .child(
72                        div()
73                            .flex()
74                            .justify_end()
75                            .gap_2()
76                            .child(Button::new("Cancel").on_click(|_, _, cx| {
77                                Dialog::close(cx);
78                            }))
79                            .child(Button::new("Confirm").primary().on_click(
80                                move |_, window, cx| {
81                                    on_confirm(window, cx);
82                                    Dialog::close(cx);
83                                },
84                            )),
85                    )
86            })
87            .show(cx);
88    }
89
90    pub fn close(cx: &mut App) {
91        Dialog::close(cx);
92    }
93}
94
95pub fn close(cx: &mut App) {
96    MessageBox::close(cx);
97}
98
99pub fn alert(title: impl Into<SharedString>, content: impl Into<SharedString>, cx: &mut App) {
100    MessageBox::new(title, content).alert(cx);
101}
102
103pub fn confirm(
104    title: impl Into<SharedString>,
105    content: impl Into<SharedString>,
106    on_confirm: impl Fn(&mut Window, &mut App) + 'static,
107    cx: &mut App,
108) {
109    MessageBox::new(title, content).confirm(on_confirm, cx);
110}