1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use eframe::{
self,
egui::{
Area, Button, Context, Id, Layout, Response, RichText, Sense, Ui,
Window,
},
emath::{Align, Align2},
epaint::{Color32, Pos2, Rounding},
};
pub enum ModalButtonStyle {
None,
Suggested,
Caution,
}
#[derive(Clone)]
struct ModalState {
is_open: bool,
}
#[derive(Clone)]
pub struct ModalStyle {
pub body_margin: f32,
pub overlay_color: Color32,
pub caution_button_fill: Color32,
pub suggested_button_fill: Color32,
pub caution_button_text_color: Color32,
pub suggested_button_text_color: Color32,
}
impl ModalState {
fn load(ctx: &Context, id: Id) -> Self {
ctx.data().get_persisted(id).unwrap_or_default()
}
fn save(self, ctx: &Context, id: Id) {
ctx.data().insert_persisted(id, self)
}
}
impl Default for ModalState {
fn default() -> Self {
Self { is_open: false }
}
}
impl Default for ModalStyle {
fn default() -> Self {
Self {
body_margin: 5.,
overlay_color: Color32::from_rgba_unmultiplied(0, 0, 0, 200),
caution_button_fill: Color32::from_rgb(87, 38, 34),
suggested_button_fill: Color32::from_rgb(33, 54, 84),
caution_button_text_color: Color32::from_rgb(242, 148, 148),
suggested_button_text_color: Color32::from_rgb(141, 182, 242),
}
}
}
pub struct Modal {
close_on_outside_click: bool,
style: ModalStyle,
ctx: Context,
id: Id,
window_id: Id,
}
fn ui_with_margin<R>(ui: &mut Ui, margin: f32, add_contents: impl FnOnce(&mut Ui) -> R) {
ui.vertical(|ui| {
ui.add_space(margin);
ui.horizontal(|ui| {
ui.add_space(margin);
add_contents(ui);
ui.add_space(margin);
});
ui.add_space(margin);
});
}
impl Modal {
pub fn new(ctx: &Context, id_source: impl std::fmt::Display) -> Self {
Self {
id: Id::new(id_source.to_string()),
style: ModalStyle::default(),
ctx: ctx.clone(),
close_on_outside_click: false,
window_id: Id::new("window_".to_string() + &id_source.to_string()),
}
}
fn set_open_state(&self, is_open: bool) {
let mut modal_state = ModalState::load(&self.ctx, self.id);
modal_state.is_open = is_open;
modal_state.save(&self.ctx, self.id)
}
pub fn open(&self) {
self.set_open_state( true)
}
pub fn close(&self) {
self.set_open_state(false)
}
pub fn with_close_on_outside_click(mut self, do_close_on_click_ouside: bool) -> Self {
self.close_on_outside_click = do_close_on_click_ouside;
self
}
pub fn with_style(mut self, style: &ModalStyle) -> Self {
self.style = style.clone();
self
}
pub fn title(&self, ui: &mut Ui, text: impl Into<RichText>) {
let text: RichText = text.into();
ui.vertical_centered(|ui| {
ui.heading(text);
});
ui.separator();
}
pub fn body(&self, ui: &mut Ui, text: impl Into<RichText>) {
let text: RichText = text.into();
ui_with_margin(ui, self.style.body_margin, |ui| {
ui.label(text);
})
}
pub fn buttons<R>(&self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) {
ui.separator();
ui.with_layout(Layout::right_to_left(Align::Min), add_contents);
}
pub fn button(&self, ui: &mut Ui, text: impl Into<RichText>) -> Response {
self.styled_button(ui, text, ModalButtonStyle::None)
}
pub fn caution_button(&self, ui: &mut Ui, text: impl Into<RichText>) -> Response {
self.styled_button(ui, text, ModalButtonStyle::Caution)
}
pub fn suggested_button(&self, ui: &mut Ui, text: impl Into<RichText>) -> Response {
self.styled_button(ui, text, ModalButtonStyle::Suggested)
}
fn styled_button(&self, ui: &mut Ui, text: impl Into<RichText>, button_style: ModalButtonStyle) -> Response {
let button = match button_style {
ModalButtonStyle::Suggested => {
let text: RichText = text.into().color(self.style.suggested_button_text_color);
Button::new(text).fill(self.style.suggested_button_fill)
}
ModalButtonStyle::Caution => {
let text: RichText = text.into().color(self.style.caution_button_text_color);
Button::new(text).fill(self.style.caution_button_fill)
}
ModalButtonStyle::None => Button::new(text.into()),
};
let response = ui.add(button);
if response.clicked() {
self.close()
}
response
}
pub fn show<R>(&self, add_contents: impl FnOnce(&mut Ui) -> R) {
let mut modal_state = ModalState::load(&self.ctx, self.id);
if modal_state.is_open {
let ctx_clone = self.ctx.clone();
Area::new(self.id).interactable(true).fixed_pos(Pos2::ZERO).show(&self.ctx, |ui: &mut Ui| {
let screen_rect = ui.ctx().input().screen_rect;
let area_response = ui.allocate_response(screen_rect.size(), Sense::click());
if area_response.clicked() && self.close_on_outside_click {
self.close();
}
ui.painter().rect_filled(screen_rect, Rounding::none(), self.style.overlay_color);
});
let window = Window::new("")
.id(self.window_id)
.open(&mut modal_state.is_open)
.title_bar(false)
.anchor(Align2::CENTER_CENTER, [0., 0.])
.resizable(false);
let response = window.show(&ctx_clone, add_contents);
if let Some(inner_response) = response {
inner_response.response.request_focus();
ctx_clone.move_to_top(inner_response.response.layer_id);
}
}
}
}