egui_dialogs 0.3.8

Platform-agnostic, customizable dialogs for egui library.
Documentation
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use egui::{
    include_image, vec2, Align, Align2, FontId, Image, ImageSource, Label, Layout, ScrollArea,
    Vec2, WidgetText,
};
use sys_locale::get_locales;

use crate::*;

// standard dialog icons
const ICON_INFO: ImageSource = include_image!("assets/info.svg");
const ICON_SUCCESS: ImageSource = include_image!("assets/success.svg");
const ICON_CONFIRM: ImageSource = include_image!("assets/confirm.svg");
const ICON_WARNING: ImageSource = include_image!("assets/warning.svg");
const ICON_ERROR: ImageSource = include_image!("assets/error.svg");

// we offer the same language supports as those offered by rust-lang.org
type StandardReplyTranslation = [(&'static str, &'static str); 10];

const STANDARD_OK_REPLY: StandardReplyTranslation = [
    ("en-US", "OK"),
    ("zh-CN", "确定"),
    ("zh-TW", "確定"),
    ("es", "OK"),
    ("fr", "D'accord"),
    ("it", "OK"),
    ("ja", "はい"),
    ("pt-BR", "OK"),
    ("ru", "Хорошо"),
    ("tr", "Tamam"),
];

const STANDARD_CANCEL_REPLY: StandardReplyTranslation = [
    ("en-US", "Cancel"),
    ("zh-CN", "取消"),
    ("zh-TW", "取消"),
    ("es", "Cancelar"),
    ("fr", "Annuler"),
    ("it", "Annulla"),
    ("ja", "キャンセル"),
    ("pt-BR", "Cancelar"),
    ("ru", "Отмена"),
    ("tr", "İptal"),
];

const STANDARD_YES_REPLY: StandardReplyTranslation = [
    ("en-US", "Yes"),
    ("zh-CN", ""),
    ("zh-TW", ""),
    ("es", ""),
    ("fr", "Oui"),
    ("it", ""),
    ("ja", "はい"),
    ("pt-BR", "Sim"),
    ("ru", "Да"),
    ("tr", "Evet"),
];

const STANDARD_NO_REPLY: [(&'static str, &'static str); 10] = [
    ("en-US", "No"),
    ("zh-CN", ""),
    ("zh-TW", ""),
    ("es", "No"),
    ("fr", "Non"),
    ("it", "No"),
    ("ja", "いいえ"),
    ("pt-BR", "Não"),
    ("ru", "Нет"),
    ("tr", "Hayır"),
];

#[inline]
fn find_translation(source: StandardReplyTranslation, locale: String) -> Option<String> {
    for (locale_key, reply) in source {
        if locale_key == locale {
            return Some(reply.to_string());
        }
    }

    None
}

#[inline]
fn translate_standard_reply(source: StandardReplyTranslation) -> String {
    let locales = get_locales();
    for locale in locales {
        if let Some(reply) = find_translation(source, locale) {
            return reply;
        }
    }

    source[0].1.to_string()
}

/// Standard dialog replies.
/// Can be translated to the current locale.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StandardReply {
    Ok,
    Cancel,
    Yes,
    No,
}

impl StandardReply {
    pub fn localize(self) -> String {
        match self {
            StandardReply::Ok => translate_standard_reply(STANDARD_OK_REPLY),
            StandardReply::Cancel => translate_standard_reply(STANDARD_CANCEL_REPLY),
            StandardReply::Yes => translate_standard_reply(STANDARD_YES_REPLY),
            StandardReply::No => translate_standard_reply(STANDARD_NO_REPLY),
        }
    }

    #[inline]
    pub fn accepted(self) -> bool {
        match self {
            StandardReply::Ok | StandardReply::Yes => true,
            _ => false,
        }
    }

    #[inline]
    pub fn rejected(self) -> bool {
        match self {
            StandardReply::Cancel | StandardReply::No => true,
            _ => false,
        }
    }
}

impl From<StandardReply> for StandardButton<StandardReply> {
    fn from(reply: StandardReply) -> Self {
        (reply.localize().into(), reply)
    }
}

impl ToString for StandardReply {
    fn to_string(&self) -> String {
        self.localize()
    }
}

/// A standard dialog button with text and a reply
pub type StandardButton<Reply> = (WidgetText, Reply);

/// A standard dialog.
/// Use `Dialogs::info`, `Dialogs::warn`, ...
/// to directly show a standard dialog.
///
/// Or use `StandardDialog::info`, `StandardDialog::warn`, ...
/// to create a customizable standard dialog
pub struct StandardDialog<'i, Reply> {
    pub title: WidgetText,
    pub content: WidgetText,
    pub image: Option<ImageSource<'i>>,
    pub buttons: Vec<StandardButton<Reply>>,
    pub min_size: Vec2,
    pub max_size: Vec2,
}

/// Customize a standard dialog
impl<'i, Reply> StandardDialog<'i, Reply>
where
    Reply: Clone,
{
    pub fn new(title: impl Into<WidgetText>, content: impl Into<WidgetText>) -> Self {
        Self {
            title: title.into(),
            content: content.into(),
            image: None,
            buttons: vec![],
            min_size: Vec2::ZERO,
            max_size: Vec2::INFINITY,
        }
    }

    /// Set the dialog title
    #[inline]
    pub fn title(mut self, title: impl Into<WidgetText>) -> Self {
        self.title = title.into();
        self
    }

    /// Set the dialog content
    #[inline]
    pub fn content(mut self, content: impl Into<WidgetText>) -> Self {
        self.content = content.into();
        self
    }

    /// Set the dialog image
    #[inline]
    pub fn image(mut self, image: ImageSource<'i>) -> Self {
        self.image = Some(image);
        self
    }

    /// Set the dialog buttons
    #[inline]
    pub fn buttons(mut self, buttons: Vec<StandardButton<Reply>>) -> Self {
        self.buttons = buttons;
        self
    }

    /// Add a button to the dialog
    #[inline]
    pub fn push_button(mut self, button: StandardButton<Reply>) -> Self {
        self.buttons.push(button);
        self
    }

    /// Set the minimum size of the dialog
    #[inline]
    pub fn min_size(mut self, min_size: Vec2) -> Self {
        self.min_size = min_size;
        self
    }

    /// Set the maximum size of the dialog
    #[inline]
    pub fn max_size(mut self, max_size: Vec2) -> Self {
        self.max_size = max_size;
        self
    }
}

/// Build a standard dialog
impl<'i> StandardDialog<'i, StandardReply> {
    /// Create an info dialog
    pub fn info(title: impl Into<WidgetText>, content: impl Into<WidgetText>) -> Self {
        Self {
            title: title.into(),
            content: content.into(),
            image: Some(ICON_INFO),
            buttons: vec![StandardReply::Ok.into()],
            min_size: Vec2::ZERO,
            max_size: Vec2::INFINITY,
        }
    }

    /// Create a success dialog
    pub fn success(title: impl Into<WidgetText>, content: impl Into<WidgetText>) -> Self {
        Self {
            title: title.into(),
            content: content.into(),
            image: Some(ICON_SUCCESS),
            buttons: vec![StandardReply::Ok.into()],
            min_size: Vec2::ZERO,
            max_size: Vec2::INFINITY,
        }
    }

    /// Create a confirmation dialog
    pub fn confirm(title: impl Into<WidgetText>, content: impl Into<WidgetText>) -> Self {
        Self {
            title: title.into(),
            content: content.into(),
            image: Some(ICON_CONFIRM),
            buttons: vec![StandardReply::Yes.into(), StandardReply::No.into()],
            min_size: Vec2::ZERO,
            max_size: Vec2::INFINITY,
        }
    }

    /// Create a warning dialog
    pub fn warning(title: impl Into<WidgetText>, content: impl Into<WidgetText>) -> Self {
        Self {
            title: title.into(),
            content: content.into(),
            image: Some(ICON_WARNING),
            buttons: vec![StandardReply::Ok.into()],
            min_size: Vec2::ZERO,
            max_size: Vec2::INFINITY,
        }
    }

    /// Create an error dialog
    pub fn error(title: impl Into<WidgetText>, content: impl Into<WidgetText>) -> Self {
        Self {
            title: title.into(),
            content: content.into(),
            image: Some(ICON_ERROR),
            buttons: vec![StandardReply::Ok.into()],
            min_size: Vec2::ZERO,
            max_size: Vec2::INFINITY,
        }
    }
}

impl<'i, Reply> Dialog<Reply> for StandardDialog<'i, Reply>
where
    Reply: Clone,
{
    fn show(&mut self, ctx: &egui::Context, dctx: &DialogContext) -> Option<Reply> {
        let Self {
            title,
            content,
            image,
            buttons,
            min_size,
            max_size,
        } = self;

        let mut reply = None;
        let mut open = true;

        closable_dialog_window(ctx, dctx, title.clone(), &mut open)
            // explicitly center the dialog as our button layout depends on this
            .anchor(Align2::CENTER_CENTER, [0., 0.])
            .min_size(min_size.max(dctx.min_size.unwrap_or(Vec2::ZERO)))
            .max_size(max_size.min(dctx.max_size.unwrap_or(dctx.mask_rect.size())))
            .show(ctx, |ui| {
                ui.style_mut().override_font_id = Some(FontId::proportional(16.0));

                ui.horizontal_top(|ui| {
                    const IMAGE_WIDTH: f32 = 48.;
                    
                    if let Some(image) = image {
                        ui.add(
                            Image::new(image.clone())
                                .fit_to_exact_size(vec2(IMAGE_WIDTH, IMAGE_WIDTH)),
                        );
                    }

                    let text_height = ui
                        .style()
                        .text_styles
                        .get(&egui::TextStyle::Button)
                        .map(|f| f.size)
                        .unwrap_or(20.)
                        * 1.5;
                    let max_height = ui.max_rect().height()
                        - ui.spacing().item_spacing.y
                        - ui.spacing().button_padding.y * 2.
                        - text_height;
                    ScrollArea::vertical()
                        .auto_shrink([true, true])
                        .max_height(max_height)
                        .show(ui, |ui| {
                            ui.add(Label::new(content.clone()).wrap());
                        });
                });

                let layout = if ui.is_sizing_pass() {
                    Layout::left_to_right(Align::Min)
                } else {
                    // infer available width for the buttons from the dialog's position
                    ui.set_max_width(
                        (ctx.content_rect().center().x - ui.next_widget_position().x).abs() * 2.,
                    );
                    Layout::right_to_left(Align::Min)
                };
                ui.with_layout(layout, |ui| {
                    for (text, reply_value) in buttons.iter().rev() {
                        if ui.button(text.clone()).clicked() {
                            reply = Some(reply_value.clone());
                            break;
                        }
                    }
                });
            });

        if let Some(reply_value) = reply {
            Some(reply_value.clone())
        } else if !open {
            buttons.last().map(|(_, reply_value)| reply_value.clone())
        } else {
            None
        }
    }
}

/// Create a suggested dialog window
pub fn dialog_window<'open>(
    ctx: &egui::Context,
    dctx: &DialogContext,
    title: impl Into<WidgetText>,
) -> egui::Window<'open> {
    let frame = egui::Frame::window(&ctx.style()).inner_margin(16.);

    let mut window = egui::Window::new(title.into())
        .collapsible(false)
        .resizable(false)
        .anchor(Align2::CENTER_CENTER, [0., 0.])
        .frame(frame)
        .fade_in(dctx.animation.is_some())
        .fade_out(dctx.animation.is_some())
        .interactable(!dctx.already_closed);

    if let Some(min_size) = dctx.min_size {
        window = window.min_size(min_size);
    }

    if let Some(max_size) = dctx.max_size {
        window = window.max_size(max_size);
    }

    if let Some(id) = dctx.dialog_id {
        window = window.id(id);
    }

    window
}

/// Create a suggested dialog window with a close button
#[inline]
pub fn closable_dialog_window<'open>(
    ctx: &egui::Context,
    dctx: &DialogContext,
    title: impl Into<WidgetText>,
    open: &'open mut bool,
) -> egui::Window<'open> {
    if dctx.already_closed {
        *open = false;
    }

    dialog_window(ctx, dctx, title).open(open)
}