From e331ff36264fc752e9833b6e20bb64127a2561a0 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 17 Feb 2021 22:06:12 -0800 Subject: [PATCH] toast: re-structure os-dependent bits This reduces redundancy and makes things a bit more uniform. refs: #489 --- wezterm-toast-notification/Cargo.toml | 2 +- wezterm-toast-notification/src/dbus.rs | 6 ++- wezterm-toast-notification/src/lib.rs | 65 ++++++++--------------- wezterm-toast-notification/src/macos.rs | 8 ++- wezterm-toast-notification/src/windows.rs | 30 +++++++++++ 5 files changed, 65 insertions(+), 46 deletions(-) create mode 100644 wezterm-toast-notification/src/windows.rs diff --git a/wezterm-toast-notification/Cargo.toml b/wezterm-toast-notification/Cargo.toml index e541314b5..65aae7d69 100644 --- a/wezterm-toast-notification/Cargo.toml +++ b/wezterm-toast-notification/Cargo.toml @@ -9,9 +9,9 @@ build = "build.rs" [dependencies] open = "1.4" +log = "0.4" [target.'cfg(all(not(windows), not(target_os="macos"), not(target_os="freebsd")))'.dependencies] -log = "0.4" serde = {version="1.0", features = ["derive"]} zbus = "1.8" zvariant = "2.0" diff --git a/wezterm-toast-notification/src/dbus.rs b/wezterm-toast-notification/src/dbus.rs index e81548589..bb0cbed1d 100644 --- a/wezterm-toast-notification/src/dbus.rs +++ b/wezterm-toast-notification/src/dbus.rs @@ -85,7 +85,11 @@ impl Reason { } } -pub fn show_notif(title: &str, message: &str, url: Option<&str>) -> Result<(), zbus::Error> { +pub fn show_notif( + title: &str, + message: &str, + url: Option<&str>, +) -> Result<(), Box> { let connection = zbus::Connection::new_session()?; let proxy = NotificationsProxy::new(&connection)?; diff --git a/wezterm-toast-notification/src/lib.rs b/wezterm-toast-notification/src/lib.rs index 689f34ec6..1586868c3 100644 --- a/wezterm-toast-notification/src/lib.rs +++ b/wezterm-toast-notification/src/lib.rs @@ -1,53 +1,32 @@ mod dbus; mod macos; +mod windows; + +#[cfg(all(not(target_os = "macos"), not(windows), not(target_os = "freebsd")))] +use dbus as backend; +#[cfg(target_os = "macos")] +use macos as backend; +#[cfg(windows)] +use windows as backend; + +mod nop { + #[allow(dead_code)] + pub fn show_notif(_: &str, _: &str, _: Option<&str>) -> Result<(), Box> { + Ok(()) + } +} + +#[cfg(target_os = "freebsd")] +use nop as backend; -#[allow(unused_variables)] pub fn persistent_toast_notification_with_click_to_open_url(title: &str, message: &str, url: &str) { - #[cfg(target_os = "macos")] - { - macos::show_notif(title, message, Some(url)); + if let Err(err) = backend::show_notif(title, message, Some(url)) { + log::error!("Failed to show notification: {}", err); } - - #[cfg(all(not(target_os = "macos"), not(windows), not(target_os = "freebsd")))] - { - if let Err(err) = dbus::show_notif(title, message, Some(url)) { - log::error!("Failed to show notification: {}", err); - } - } - - // No impl for the other OS's at this time } pub fn persistent_toast_notification(title: &str, message: &str) { - #[cfg(target_os = "macos")] - { - macos::show_notif(title, message, None); - } - - #[cfg(all(not(target_os = "macos"), not(windows), not(target_os = "freebsd")))] - { - if let Err(err) = dbus::show_notif(title, message, None) { - log::error!("Failed to show notification: {}", err); - } - } - - #[cfg(windows)] - { - let title = title.to_owned(); - let message = message.to_owned(); - - // We need to be in a different thread from the caller - // in case we get called in the guts of a windows message - // loop dispatch and are unable to pump messages - std::thread::spawn(move || { - use winrt_notification::Toast; - - Toast::new(Toast::POWERSHELL_APP_ID) - .title(&title) - .text1(&message) - .duration(winrt_notification::Duration::Long) - .show() - .ok(); - }); + if let Err(err) = backend::show_notif(title, message, None) { + log::error!("Failed to show notification: {}", err); } } diff --git a/wezterm-toast-notification/src/macos.rs b/wezterm-toast-notification/src/macos.rs index 6b8be9c46..fe1168204 100644 --- a/wezterm-toast-notification/src/macos.rs +++ b/wezterm-toast-notification/src/macos.rs @@ -82,7 +82,11 @@ fn nsstring(s: &str) -> StrongPtr { unsafe { StrongPtr::new(NSString::alloc(nil).init_str(s)) } } -pub fn show_notif(title: &str, message: &str, url: Option<&str>) { +pub fn show_notif( + title: &str, + message: &str, + url: Option<&str>, +) -> Result<(), Box> { unsafe { let center: id = msg_send![ class!(NSUserNotificationCenter), @@ -104,4 +108,6 @@ pub fn show_notif(title: &str, message: &str, url: Option<&str>) { let () = msg_send![center, setDelegate: delegate]; let () = msg_send![center, deliverNotification: notif]; } + + Ok(()) } diff --git a/wezterm-toast-notification/src/windows.rs b/wezterm-toast-notification/src/windows.rs new file mode 100644 index 000000000..876026f0a --- /dev/null +++ b/wezterm-toast-notification/src/windows.rs @@ -0,0 +1,30 @@ +#![cfg(windows)] + +pub fn show_notif( + title: &str, + message: &str, + url: Option<&str>, +) -> Result<(), Box> { + if url.is_some() { + return Ok(()); + } + + let title = title.to_owned(); + let message = message.to_owned(); + + // We need to be in a different thread from the caller + // in case we get called in the guts of a windows message + // loop dispatch and are unable to pump messages + std::thread::spawn(move || { + use winrt_notification::Toast; + + Toast::new(Toast::POWERSHELL_APP_ID) + .title(&title) + .text1(&message) + .duration(winrt_notification::Duration::Long) + .show() + .ok(); + }); + + Ok(()) +}