From c5b512748fdf860e2b0bf77d50400a2efa79330c Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 24 Feb 2021 09:23:17 -0800 Subject: [PATCH] toast: avoid blocking the main thread if dbus/notification services are not running closes: #498 --- wezterm-toast-notification/src/dbus.rs | 45 +++++++++++++++++--------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/wezterm-toast-notification/src/dbus.rs b/wezterm-toast-notification/src/dbus.rs index bb0cbed1d..129c0da5b 100644 --- a/wezterm-toast-notification/src/dbus.rs +++ b/wezterm-toast-notification/src/dbus.rs @@ -85,10 +85,10 @@ impl Reason { } } -pub fn show_notif( - title: &str, - message: &str, - url: Option<&str>, +fn show_notif_impl( + title: String, + message: String, + url: Option, ) -> Result<(), Box> { let connection = zbus::Connection::new_session()?; @@ -108,8 +108,8 @@ pub fn show_notif( "wezterm", 0, "org.wezfurlong.wezterm", - title, - message, + &title, + &message, if url.is_some() { &["show", "Show"] } else { @@ -158,17 +158,32 @@ pub fn show_notif( } })?; - std::thread::spawn(move || { - while !state.lock().unwrap().done { - match proxy.next_signal() { - Err(err) => { - log::error!("next_signal: {:#}", err); - break; - } - Ok(_) => {} + while !state.lock().unwrap().done { + match proxy.next_signal() { + Err(err) => { + log::error!("next_signal: {:#}", err); + break; } + Ok(_) => {} } + } + Ok(()) +} + +pub fn show_notif( + title: &str, + message: &str, + url: Option<&str>, +) -> Result<(), Box> { + let title = title.to_string(); + let message = message.to_string(); + let url = url.map(|s| s.to_string()); + // Run this in a separate thread as we don't know if dbus or the notification + // service on the other end are up, and we'd otherwise block for some time. + std::thread::spawn(move || { + if let Err(err) = show_notif_impl(title, message, url) { + log::error!("while showing notification: {:#}", err); + } }); - Ok(()) }