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
use winrt_notification::Toast;

pub use crate::{
    error::*,
    notification::Notification,
    timeout::Timeout,
};


use std::{path::Path, str::FromStr};

pub(crate) fn show_notification(notification: &Notification) -> Result<()> {
    let sound = match &notification.sound_name {
        Some(chosen_sound_name) => winrt_notification::Sound::from_str(&chosen_sound_name).ok(),
        None => None,
    };

    let duration = match notification.timeout {
        Timeout::Default => winrt_notification::Duration::Short,
        Timeout::Never => winrt_notification::Duration::Long,
        Timeout::Milliseconds(t) => {
            if t >= 25000 {
                winrt_notification::Duration::Long
            } else {
                winrt_notification::Duration::Short
            }
        }
    };

    let powershell_app_id = &Toast::POWERSHELL_APP_ID.to_string();
    let app_id = &notification.app_id.as_ref().unwrap_or(powershell_app_id);
    let mut toast = Toast::new(app_id)
            .title(&notification.summary)
            .text1(&notification.subtitle.as_ref().map(AsRef::as_ref).unwrap_or("")) // subtitle
            .text2(&notification.body)
            .sound(sound)
            .duration(duration);
    if let Some(image_path) = &notification.path_to_image {
        toast = toast.image(&Path::new(&image_path), "");
    }

    toast
        .show()
        .map_err(|e| Error::from(ErrorKind::Msg(format!("{:?}", e))))
}