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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
use egui::{self, DragValue};
use egui_modal::{Icon, Modal, ModalStyle};
struct ExampleApp {
modal_style: ModalStyle,
modal_title: String,
modal_body: String,
nested_modal_text: String,
include_title: bool,
include_body: bool,
include_buttons: bool,
close_on_outside_click: bool,
dialog_icon: Option<Icon>,
}
impl Default for ExampleApp {
fn default() -> Self {
Self {
modal_style: ModalStyle::default(),
modal_title: "a modal".to_string(),
modal_body: "here is the modal body".to_string(),
nested_modal_text: String::new(),
include_title: true,
include_body: true,
include_buttons: true,
close_on_outside_click: false,
dialog_icon: Some(Icon::Info),
}
}
}
impl eframe::App for ExampleApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::Window::new("egui-modal").show(ctx, |ui| {
// you can put the modal creation and show logic wherever you want
// (though of course it needs to be created before it can be used)
let nested_modal = Modal::new(ctx, "nested_modal");
let modal = Modal::new(ctx, "modal")
.with_style(&self.modal_style)
.with_close_on_outside_click(self.close_on_outside_click || !self.include_buttons);
// the show function defines what is shown in the modal, but the modal
// won't actually show until you do modal.open()
modal.show(|ui| {
// these helper functions are NOT mandatory to use, they just
// help implement some styling with margins and separators
// you can put whatever you like in here
if self.include_title {
modal.title(ui, &mut self.modal_title);
}
// the "frame" of the modal refers to the container of the icon and body.
// this helper just applies a margin specified by the ModalStyle
modal.frame(ui, |ui| {
if self.include_body {
modal.body(ui, &self.modal_body);
}
});
if self.include_buttons {
modal.buttons(ui, |ui| {
if modal.button(ui, "close").clicked()
|| (self.close_on_outside_click && modal.was_outside_clicked())
{
// all buttons created with the helper functions automatically
// close the modal on click, but you can close it yourself with
// ['modal.close()']
println!("hello world!")
}
modal.caution_button(ui, "button, but caution");
if modal.suggested_button(ui, "open another modal").clicked() {
// always close your previous modal before opening a new one otherwise weird
// layering things will happen. again, the helper functions for the buttons automatically
// close the modal on click, so we don't have to manually do that here
nested_modal.open();
}
})
}
});
// a dialog is useful when you have a one-time occurance and you want to relay information to the user
let mut dialog_modal = Modal::new(ctx, "dialog_modal").with_style(&self.modal_style);
// make sure you don't forget to show the dialog!
dialog_modal.show_dialog();
ui.with_layout(egui::Layout::top_down_justified(egui::Align::Min), |ui| {
if ui.button("open modal").clicked() {
modal.open();
}
if ui.button("open dialog").clicked() {
// [`.dialog()`] can be used to both set the visual info for the dialog
// and open it at the same time
let mut dialog_builder = dialog_modal
.dialog()
.with_title("this is a dialog")
.with_body("this helps for showing information about one-time events");
if let Some(dialog_icon) = self.dialog_icon.clone() {
dialog_builder = dialog_builder.with_icon(dialog_icon);
}
dialog_builder.open();
}
ui.separator();
// to prevent locking the example window without any way to close the modal :)
// remember to implement this yourself if you don't use buttons in your modal
let mut cooc_enabled = self.close_on_outside_click || !self.include_buttons;
ui.add_enabled_ui(self.include_buttons, |ui| {
if ui
.checkbox(&mut cooc_enabled, "close if click outside modal")
.clicked()
{
self.close_on_outside_click = !self.close_on_outside_click
};
});
ui.checkbox(&mut self.include_title, "include title");
ui.checkbox(&mut self.include_body, "include body");
ui.checkbox(&mut self.include_buttons, "include buttons");
ui.separator();
egui::Grid::new("options_grid")
.min_col_width(200.)
.striped(true)
.show(ui, |ui| {
ui.label("title");
ui.text_edit_singleline(&mut self.modal_title);
ui.end_row();
ui.label("body");
ui.text_edit_singleline(&mut self.modal_body);
ui.end_row();
let mut has_height = self.modal_style.default_height.is_some();
let mut has_width = self.modal_style.default_width.is_some();
if ui.checkbox(&mut has_height, "default height").changed() {
if has_height {
self.modal_style.default_height = Some(100.)
} else {
self.modal_style.default_height = None
}
}
if let Some(modal_height) = self.modal_style.default_height.as_mut() {
let modal_height = DragValue::new(modal_height).range(0..=1000);
ui.add_sized(ui.available_rect_before_wrap().size(), modal_height);
}
ui.end_row();
if ui.checkbox(&mut has_width, "default width").changed() {
if has_width {
self.modal_style.default_width = Some(100.)
} else {
self.modal_style.default_width = None
}
}
if let Some(modal_width) = self.modal_style.default_width.as_mut() {
let modal_width = DragValue::new(modal_width).range(0..=1000);
ui.add_sized(ui.available_rect_before_wrap().size(), modal_width);
}
ui.end_row();
ui.label("body margin");
let body_margin =
DragValue::new(&mut self.modal_style.body_margin).range(0..=20);
ui.add_sized(ui.available_rect_before_wrap().size(), body_margin);
ui.end_row();
ui.label("frame margin");
let frame_margin =
DragValue::new(&mut self.modal_style.frame_margin).range(0..=20);
ui.add_sized(ui.available_rect_before_wrap().size(), frame_margin);
ui.end_row();
ui.label("icon margin");
let icon_margin =
DragValue::new(&mut self.modal_style.icon_margin).range(0..=20);
ui.add_sized(ui.available_rect_before_wrap().size(), icon_margin);
ui.end_row();
ui.label("icon size");
let icon_size =
DragValue::new(&mut self.modal_style.icon_size).range(8..=48);
ui.add_sized(ui.available_rect_before_wrap().size(), icon_size);
ui.end_row();
ui.label("dialog icon");
let mut use_icon = self.dialog_icon.is_some();
ui.horizontal(|ui| {
if ui.checkbox(&mut use_icon, "use a dialog icon").clicked() {
if use_icon {
self.dialog_icon = Some(Icon::Info);
} else {
self.dialog_icon = None;
}
}
if let Some(icon) = self.dialog_icon.as_mut() {
ui.selectable_value(icon, Icon::Info, "info");
ui.selectable_value(icon, Icon::Warning, "warning");
ui.selectable_value(icon, Icon::Success, "success");
ui.selectable_value(icon, Icon::Error, "error");
}
});
ui.end_row();
ui.label("body alignment");
ui.horizontal(|ui| {
ui.selectable_value(
&mut self.modal_style.body_alignment,
egui::Align::Min,
"min",
);
ui.selectable_value(
&mut self.modal_style.body_alignment,
egui::Align::Center,
"center",
);
ui.selectable_value(
&mut self.modal_style.body_alignment,
egui::Align::Max,
"max",
);
});
ui.end_row();
ui.label("overlay color");
ui.color_edit_button_srgba(&mut self.modal_style.overlay_color);
ui.end_row();
ui.label("caution button (fill, text)");
ui.horizontal(|ui| {
ui.color_edit_button_srgba(&mut self.modal_style.caution_button_fill);
ui.color_edit_button_srgba(
&mut self.modal_style.caution_button_text_color,
);
});
ui.end_row();
ui.label("suggested button (fill, text)");
ui.horizontal(|ui| {
ui.color_edit_button_srgba(&mut self.modal_style.suggested_button_fill);
ui.color_edit_button_srgba(
&mut self.modal_style.suggested_button_text_color,
);
});
ui.end_row();
ui.label("icon colors (info, warning, success, error)");
ui.horizontal(|ui| {
ui.color_edit_button_srgba(&mut self.modal_style.info_icon_color);
ui.color_edit_button_srgba(&mut self.modal_style.warning_icon_color);
ui.color_edit_button_srgba(&mut self.modal_style.success_icon_color);
ui.color_edit_button_srgba(&mut self.modal_style.error_icon_color);
});
ui.end_row();
});
});
// why is this down here?? just wanted to show that you can put
// the modal's [`.show()`] anywhere but we could have put this above
// modal if we wanted
nested_modal.show(|ui| {
nested_modal.frame(ui, |ui| {
nested_modal.body(ui, "hello there!");
// you can put textboxes in here.
ui.text_edit_singleline(&mut self.nested_modal_text);
});
nested_modal.buttons(ui, |ui| {
nested_modal.button(ui, "close");
})
});
});
}
}
fn main() {
let _ = eframe::run_native(
"egui-modal example",
eframe::NativeOptions::default(),
Box::new(|_cc| Ok(Box::new(ExampleApp::default()))),
);
}