-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: allow playing default notification sound #50
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ use objc_foundation::{INSDictionary, INSString, NSDictionary, NSString}; | |
use objc_id::Id; | ||
use std::default::Default; | ||
use std::ops::Deref; | ||
use std::path::PathBuf; | ||
|
||
use crate::error::{NotificationError, NotificationResult}; | ||
use crate::{ensure, ensure_application_set, sys}; | ||
|
@@ -43,6 +42,24 @@ pub enum MainButton<'a> { | |
Response(&'a str), | ||
} | ||
|
||
/// Helper to determine whether you want to play the default sound or custom one | ||
#[derive(Clone)] | ||
pub enum Sound { | ||
/// notification plays the sound [`NSUserNotificationDefaultSoundName`](https://developer.apple.com/documentation/foundation/nsusernotification/nsusernotificationdefaultsoundname) | ||
Default, | ||
/// notification plays your custom sound | ||
Custom(String), | ||
} | ||
|
||
impl<I> From<I> for Sound | ||
where | ||
I: ToString, | ||
{ | ||
fn from(value: I) -> Self { | ||
Sound::Custom(value.to_string()) | ||
} | ||
} | ||
|
||
/// Options to further customize the notification | ||
#[derive(Clone, Default)] | ||
pub struct Notification<'a> { | ||
|
@@ -54,7 +71,7 @@ pub struct Notification<'a> { | |
pub(crate) app_icon: Option<&'a str>, | ||
pub(crate) content_image: Option<&'a str>, | ||
pub(crate) delivery_date: Option<f64>, | ||
pub(crate) sound: Option<&'a str>, | ||
pub(crate) sound: Option<Sound>, | ||
pub(crate) asynchronous: Option<bool>, | ||
} | ||
|
||
|
@@ -156,29 +173,46 @@ impl<'a> Notification<'a> { | |
self | ||
} | ||
|
||
/// Play a system sound when the notification is delivered | ||
/// Play the default sound `"NSUserNotificationDefaultSoundName"` system sound when the notification is delivered. | ||
/// # Example: | ||
/// | ||
/// ```no_run | ||
/// # use mac_notification_sys::*; | ||
/// let _ = Notification::new().sound("Blow"); | ||
/// ``` | ||
pub fn default_sound(&mut self) -> &mut Self { | ||
self.sound = Some(Sound::Default); | ||
self | ||
} | ||
|
||
/// Play a system sound when the notification is delivered. Use [`Sound::Default`] to play the default sound. | ||
/// # Example: | ||
/// | ||
/// ```no_run | ||
/// # use mac_notification_sys::*; | ||
/// let _ = Notification::new().sound("Blow"); | ||
/// ``` | ||
pub fn sound(&mut self, sound: &'a str) -> &mut Self { | ||
self.sound = Some(sound); | ||
pub fn sound<S>(&mut self, sound: S) -> &mut Self | ||
where | ||
S: Into<Sound>, | ||
{ | ||
self.sound = Some(sound.into()); | ||
self | ||
} | ||
|
||
/// Play a system sound when the notification is delivered | ||
/// Play a system sound when the notification is delivered. Use [`Sound::Default`] to play the default sound. | ||
/// | ||
/// # Example: | ||
/// | ||
/// ```no_run | ||
/// # use mac_notification_sys::*; | ||
/// let _ = Notification::new().sound("Blow"); | ||
/// ``` | ||
pub fn maybe_sound(&mut self, sound: Option<&'a str>) -> &mut Self { | ||
self.sound = sound; | ||
pub fn maybe_sound<S>(&mut self, sound: Option<S>) -> &mut Self | ||
where | ||
S: Into<Sound>, | ||
{ | ||
self.sound = sound.map(Into::into); | ||
self | ||
} | ||
|
||
|
@@ -223,6 +257,12 @@ impl<'a> Notification<'a> { | |
None => ("", &[], false), | ||
}; | ||
|
||
let sound = match self.sound { | ||
Some(Sound::Custom(ref name)) => name.as_str(), | ||
Some(Sound::Default) => "NSUserNotificationDefaultSoundName", | ||
None => "", | ||
}; | ||
|
||
let vals = vec![ | ||
NSString::from_str(main_button_label), | ||
// TODO: Find a way to support NSArray as a NSDictionary Value rather than JUST NSString so I don't have to convert array to string and back | ||
|
@@ -241,10 +281,7 @@ impl<'a> Notification<'a> { | |
Some(true) => "yes", | ||
_ => "no", | ||
}), | ||
NSString::from_str(match self.sound { | ||
Some(sound) if check_sound(sound) => sound, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is better to let the OS decided whether this is a valid sound or not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah it's hard to determine if a sound is valid, the OS pretty much checks the entire file system 😂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the verdict here? leave it in? |
||
_ => "_mute", | ||
}), | ||
NSString::from_str(sound), | ||
]; | ||
NSDictionary::from_keys_and_objects(keys, vals) | ||
} | ||
|
@@ -335,20 +372,3 @@ impl NotificationResponse { | |
} | ||
} | ||
} | ||
|
||
pub(crate) fn check_sound(sound_name: &str) -> bool { | ||
dirs_next::home_dir() | ||
.map(|path| path.join("/Library/Sounds/")) | ||
.into_iter() | ||
.chain( | ||
[ | ||
"/Library/Sounds/", | ||
"/Network/Library/Sounds/", | ||
"/System/Library/Sounds/", | ||
] | ||
.iter() | ||
.map(PathBuf::from), | ||
) | ||
.map(|sound_path| sound_path.join(format!("{}.aiff", sound_name))) | ||
.any(|some_path| some_path.exists()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
example is wrong here but ok :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh no