toast: re-structure os-dependent bits
This reduces redundancy and makes things a bit more uniform. refs: #489
This commit is contained in:
1 parent
d886de8300
commit
e331ff3626
5 files changed
+65
-46
No files matched your search
@@ -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"
|
||||
|
||||
@@ -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<dyn std::error::Error>> {
|
||||
let connection = zbus::Connection::new_session()?;
|
||||
|
||||
let proxy = NotificationsProxy::new(&connection)?;
|
||||
|
||||
@@ -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<dyn std::error::Error>> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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<dyn std::error::Error>> {
|
||||
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(())
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#![cfg(windows)]
|
||||
|
||||
pub fn show_notif(
|
||||
title: &str,
|
||||
message: &str,
|
||||
url: Option<&str>,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
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(())
|
||||
}
|
||||
Reference in new issue
Block a user