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
#[cfg(target_os = "macos")]
extern crate mac_notification_sys;

#[cfg(target_os = "linux")]
extern crate notify_rust;

#[cfg(target_os = "windows")]
extern crate winrt;

trait Platform {
    fn setup() -> Self;
    fn notify(msg_title: &str, msg_body: &str);
    fn teardown(self);
}

#[cfg(target_os = "windows")]
struct Windows(winrt::RuntimeContext);

#[cfg(target_os = "windows")]
impl Platform for Windows {
    fn setup() -> Self {
        Windows(winrt::RuntimeContext::init())
    }

    fn notify(msg_title: &str, msg_body: &str) {
        use winrt::windows::data::xml::dom::*;
        use winrt::windows::ui::notifications::*;
        use winrt::*;
        let toast_xml =
            ToastNotificationManager::get_template_content(ToastTemplateType::ToastText02)
                .unwrap().unwrap();
        let toast_text_elements = toast_xml
            .get_elements_by_tag_name(&FastHString::new("text"))
            .unwrap().unwrap();

        toast_text_elements
            .item(0)
            .unwrap()
            .unwrap()
            .append_child(
                &*toast_xml
                    .create_text_node(&FastHString::from(msg_title))
                    .unwrap()
                    .unwrap()
                    .query_interface::<IXmlNode>()
                    .unwrap(),
            )
            .unwrap();
        toast_text_elements
            .item(1)
            .unwrap()
            .unwrap()
            .append_child(
                &*toast_xml
                    .create_text_node(&FastHString::from(msg_body))
                    .unwrap()
                    .unwrap()
                    .query_interface::<IXmlNode>()
                    .unwrap(),
            )
            .unwrap();

        let toast = ToastNotification::create_toast_notification(&*toast_xml).unwrap();
        ToastNotificationManager::create_toast_notifier_with_id(&FastHString::new(
            "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\\WindowsPowerShell\\v1.0\\powershell.exe",
        ))
        .unwrap()
        .unwrap()
        .show(&*toast)
        .unwrap();
    }

    fn teardown(self) {
        self.0.uninit();
    }
}

#[cfg(target_os = "macos")]
struct MacOs;

#[cfg(target_os = "macos")]
impl Platform for MacOs {
    fn setup() -> Self {
        MacOs
    }

    fn notify(msg_title: &str, msg_body: &str) {
        let bundle = mac_notification_sys::get_bundle_identifier("Script Editor").unwrap();
        mac_notification_sys::set_application(&bundle).unwrap();
        mac_notification_sys::send_notification(msg_title, &None, msg_body, &None).unwrap();
    }

    fn teardown(self) {}
}

#[cfg(target_os = "linux")]
struct Linux;

#[cfg(target_os = "linux")]
impl Platform for Linux {
    fn setup() -> Self {
        Linux
    }

    fn notify(msg_title: &str, msg_body: &str) {
        notify_rust::Notification::new()
            .summary(msg_title)
            .body(msg_body)
            .show()
            .unwrap();
    }

    fn teardown(self) {}
}

#[cfg(target_os = "windows")]
type CurrPlatform = Windows;
#[cfg(target_os = "macos")]
type CurrPlatform = MacOs;
#[cfg(target_os = "linux")]
type CurrPlatform = Linux;

pub fn notify(msg_title: &str, msg_body: &str) {
    let p = CurrPlatform::setup();
    CurrPlatform::notify(msg_title, msg_body);
    p.teardown();
}