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
use crate::dialog::{Dialog, DialogImpl, MessageAlert, MessageConfirm};
use crate::Result;

/// Represents the type of the message. Usually determines the icon in the dialog.
#[derive(Copy, Clone)]
pub enum MessageType {
    Info,
    Warning,
    Error,
}

/// Builds and shows message dialogs.
pub struct MessageDialog<'a> {
    pub(crate) title: &'a str,
    pub(crate) text: &'a str,
    pub(crate) typ: MessageType,
}

impl<'a> MessageDialog<'a> {
    pub fn new() -> Self {
        MessageDialog {
            title: "",
            text: "",
            typ: MessageType::Info,
        }
    }

    pub fn set_title(mut self, title: &'a str) -> Self {
        self.title = title;
        self
    }

    pub fn set_text(mut self, text: &'a str) -> Self {
        self.text = text;
        self
    }

    pub fn set_type(mut self, typ: MessageType) -> Self {
        self.typ = typ;
        self
    }

    pub fn show_alert(self) -> Result<<MessageAlert<'a> as Dialog>::Output> {
        let mut dialog = MessageAlert {
            title: self.title,
            text: self.text,
            typ: self.typ,
        };
        dialog.show()
    }

    pub fn show_confirm(self) -> Result<<MessageConfirm<'a> as Dialog>::Output> {
        let mut dialog = MessageConfirm {
            title: self.title,
            text: self.text,
            typ: self.typ,
        };
        dialog.show()
    }
}

impl Default for MessageDialog<'_> {
    fn default() -> Self {
        Self::new()
    }
}