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
use crate::{
    toastify::{OnActivated, OnDismissed, OnFailed},
    ActivationType, Crop, Duration, HashMap, Result, Scenario, ToastHandler, Toastify,
};
pub struct Toast<'a> {
    id: &'a str,
    title: Option<&'a str>,
    description: Option<&'a str>,
    icon: Option<(&'a str, Crop)>,
    image: Option<&'a str>,
    hero: Option<&'a str>,
    audio: Option<(&'a str, bool)>,
    buttons: Vec<(&'a str, &'a str, ActivationType, &'a str)>,
    context_buttons: Vec<(&'a str, &'a str)>,
    input: Vec<(&'a str, &'a str)>,
    textbox: Option<(&'a str, &'a str, &'a str, &'a str, &'a str)>,
    progress: Option<(&'a str, &'a str, &'a str, &'a str)>,
    selection: Vec<(Vec<&'a str>, &'a str)>,
    data: Option<HashMap<String, String>>,
    duration: Option<Duration>,
    scenario: Option<Scenario>,
    tag: Option<&'a str>,
    group: Option<&'a str>,
    remote_id: Option<&'a str>,
    on_click: Option<&'a str>,
    on_activated: Option<OnActivated>,
    on_dismissed: Option<OnDismissed>,
    on_failed: Option<OnFailed>,
}
impl<'a> Toast<'a> {
    /// Constructor for the toast builder.
    pub fn new(id: &'a str) -> Self {
        Self {
            id,
            title: None,
            description: None,
            icon: None,
            image: None,
            hero: None,
            progress: None,
            audio: None,
            buttons: vec![],
            context_buttons: vec![],
            input: vec![],
            textbox: None,
            selection: vec![],
            data: None,
            duration: None,
            on_click: None,
            scenario: None,
            tag: None,
            group: None,
            remote_id: None,
            on_activated: None,
            on_dismissed: None,
            on_failed: None,
        }
    }
    /// Sets the title of the toast
    pub fn set_title(mut self, title: &'a str) -> Self {
        self.title = Some(title);
        self
    }
    /// Sets the first line of text below title
    pub fn set_description(mut self, description: &'a str) -> Self {
        self.description = Some(description);
        self
    }
    /// Set the icon shown in the upper left of the toast
    ///
    /// The default is determined by your app id.
    /// If you are using the MSEdge workaround, it will be the MSEdge icon
    pub fn set_icon(mut self, icon: &'a str, crop: Crop) -> Self {
        self.icon = Some((icon, crop));
        self
    }
    /// Set image for the toast
    pub fn set_image(mut self, image: &'a str) -> Self {
        self.image = Some(image);
        self
    }
    /// Set hero for the toast
    pub fn set_hero(mut self, hero: &'a str) -> Self {
        self.hero = Some(hero);
        self
    }
    /// Set the sound for the toast or silence it
    pub fn set_audio(mut self, audio: &'a str, silent: bool) -> Self {
        self.audio = Some((audio, silent));
        self
    }
    /// Set button for the toast
    pub fn add_button(
        mut self,
        title: &'a str,
        arg: &'a str,
        activation_type: ActivationType,
        image_uri: Option<&'a str>,
    ) -> Self {
        let image_uri = match image_uri {
            Some(uri) => uri,
            None => "",
        };

        self.buttons.push((title, arg, activation_type, image_uri));
        self
    }
    /// Set context button for the toast
    pub fn add_context_button(mut self, title: &'a str, arg: &'a str) -> Self {
        self.context_buttons.push((title, arg));
        self
    }
    /// Set input for the toast
    pub fn add_input(mut self, input: &'a str, id: &'a str) -> Self {
        self.input.push((input, id));
        self
    }
    /// Set textbox for the toast
    pub fn set_textbox(
        mut self,
        input: &'a str,
        id: &'a str,
        title_box: &'a str,
        arg_box: &'a str,
        image_uri_box: Option<&'a str>,
    ) -> Self {
        let image_uri_box = match image_uri_box {
            Some(uri) => uri,
            None => "",
        };

        self.textbox = Some((input, id, title_box, arg_box, image_uri_box));
        self
    }
    /// Set progress for the toast
    pub fn set_progress(
        mut self,
        title: &'a str,
        status: &'a str,
        value: &'a str,
        value_string: &'a str,
    ) -> Self {
        self.progress = Some((title, status, value, value_string));
        self
    }
    /// Set selection for the toast
    pub fn add_selection(mut self, selection: Vec<&'a str>, id: &'a str) -> Self {
        self.selection.push((selection, id));
        self
    }
    /// Set data with custom keys and values
    pub fn set_data(mut self, data: HashMap<String, String>) -> Self {
        self.data = Some(data);
        self
    }
    /// Set duration for the toast
    pub fn set_duration(mut self, duration: Duration) -> Self {
        self.duration = Some(duration);
        self
    }
    /// Set scenario for the toast
    pub fn set_scenario(mut self, scenario: Scenario) -> Self {
        self.scenario = Some(scenario);
        self
    }
    /// Sets the click action of the toast
    pub fn on_click(mut self, action: &'a str) -> Self {
        self.on_click = Some(action);
        self
    }
    pub fn on_activated(mut self, action: OnActivated) -> Self {
        self.on_activated = Some(action);
        self
    }
    pub fn on_dismissed(mut self, action: OnDismissed) -> Self {
        self.on_dismissed = Some(action);
        self
    }
    pub fn on_failed(mut self, action: OnFailed) -> Self {
        self.on_failed = Some(action);
        self
    }
    /// Sets the tag of the toast
    pub fn set_tag(mut self, tag: &'a str) -> Self {
        self.tag = Some(tag);
        self
    }
    /// Sets the group of the toast
    pub fn set_group(mut self, group: &'a str) -> Self {
        self.group = Some(group);
        self
    }
    /// Sets the remote id of the toast
    pub fn set_remote_id(mut self, remote_id: &'a str) -> Self {
        self.remote_id = Some(remote_id);
        self
    }
    pub fn show(self) -> Result<ToastHandler<'a>> {
        let mut toast = Toastify::create()?;

        if let Some(title) = self.title {
            toast.add_text(title)?;
        }
        if let Some(description) = self.description {
            toast.add_text(description)?;
        }
        if let Some((icon, crop)) = self.icon {
            toast.add_icon(icon, crop)?;
        }
        if let Some(image) = self.image {
            toast.add_image(image)?;
        }
        if let Some(hero) = self.hero {
            toast.add_hero(hero)?;
        }
        if let Some((audio, silent)) = self.audio {
            toast.add_audio(audio, silent)?;
        }
        if let Some((title, status, value, value_string)) = self.progress {
            toast.add_progress(title, status, value, value_string)?;
        }
        for (selection, id) in self.selection {
            toast.add_selection(selection, id)?;
        }
        for (input, id) in self.input {
            toast.add_input(input, id)?;
        }
        for (title, arg, activation_type, image_uri) in self.buttons {
            toast.add_button(title, arg, activation_type, image_uri)?;
        }
        if let Some((input, id, title_box, arg_box, image_uri_box)) = self.textbox {
            toast.add_textbox(input, id, title_box, arg_box, image_uri_box)?;
        }
        for (title, arg) in self.context_buttons {
            toast.add_context_button(title, arg)?;
        }
        if let Some(data) = self.data {
            toast.add_data(data)?;
        }
        if let Some(duration) = self.duration {
            toast.add_duration(duration)?;
        }
        if let Some(scenario) = self.scenario {
            toast.add_scenario(scenario)?;
        }
        if let Some(on_click) = self.on_click {
            toast.add_launch(on_click)?;
        }

        let (toast, manager) = toast.show(
            self.id,
            self.tag,
            self.group,
            self.remote_id,
            self.on_activated,
            self.on_dismissed,
            self.on_failed,
        )?;

        let toast_handler = ToastHandler::new(self.id, toast, manager);
        Ok(toast_handler)
    }

    pub fn show_xml(self, xml: &str) -> Result<ToastHandler<'a>> {
        let mut toast = Toastify::create()?;

        if let Some(data) = self.data {
            toast.add_data(data)?;
        }

        let (toast, manager) = toast.show_xml(self.id, xml)?;

        let toast_handler = ToastHandler::new(self.id, toast, manager);
        Ok(toast_handler)
    }
}