rust_macios/user_notifications/
un_notification_sound.rs

1use libc::c_float;
2use objc::{msg_send, sel, sel_impl};
3use rust_macios_objective_c_runtime_proc_macros::interface_impl;
4
5use crate::{
6    object,
7    objective_c_runtime::traits::{FromId, PNSObject},
8};
9
10use super::UNNotificationSoundName;
11
12object! {
13    /// The sound played upon delivery of a notification.
14    unsafe pub struct UNNotificationSound;
15}
16
17#[interface_impl(NSObject)]
18impl UNNotificationSound {
19    /* Creating Notification Sounds
20     */
21
22    /// Returns an object representing the default sound for notifications.
23    #[property]
24    pub fn default_sound() -> UNNotificationSound {
25        unsafe { UNNotificationSound::from_id(msg_send![Self::m_class(), defaultSound]) }
26    }
27
28    /// Creates a sound object that represents a custom sound file.
29    #[method]
30    pub fn sound_named(name: UNNotificationSoundName) -> Self
31    where
32        Self: Sized + FromId,
33    {
34        unsafe { Self::from_id(msg_send![Self::m_class(), soundNamed: name]) }
35    }
36
37    /* Getting Critical Sounds
38     */
39
40    /// The default sound used for critical alerts.
41    #[property]
42    pub fn default_critical_sound() -> UNNotificationSound {
43        unsafe { UNNotificationSound::from_id(msg_send![Self::m_class(), defaultCriticalSound]) }
44    }
45
46    /// Creates a sound object that plays the default critical alert sound at the volume you specify.
47    #[method]
48    pub fn default_critical_sound_with_audio_volume(volume: c_float) -> Self
49    where
50        Self: Sized + FromId,
51    {
52        unsafe {
53            Self::from_id(msg_send![
54                Self::m_class(),
55                defaultCriticalSoundWithAudioVolume: volume
56            ])
57        }
58    }
59
60    /// Creates a custom sound object for critical alerts.
61    #[method]
62    pub fn critical_sound_named(name: UNNotificationSoundName) -> Self
63    where
64        Self: Sized + FromId,
65    {
66        unsafe { Self::from_id(msg_send![Self::m_class(), criticalSoundNamed: name]) }
67    }
68
69    /// Creates a custom sound object for critical alerts with the volume you specify.
70    #[method]
71    pub fn critical_sound_named_with_audio_volume(
72        name: UNNotificationSoundName,
73        volume: c_float,
74    ) -> Self
75    where
76        Self: Sized + FromId,
77    {
78        unsafe {
79            Self::from_id(
80                msg_send![Self::m_class(), criticalSoundNamed: name withAudioVolume: volume],
81            )
82        }
83    }
84
85    /* Type Properties
86     */
87
88    #[property]
89    pub fn default_ringtone_sound() -> UNNotificationSound {
90        unsafe { UNNotificationSound::from_id(msg_send![Self::m_class(), defaultRingtoneSound]) }
91    }
92
93    /* Type Methods
94     */
95
96    #[method]
97    pub fn ringtone_sound_named(name: UNNotificationSoundName) -> Self
98    where
99        Self: Sized + FromId,
100    {
101        unsafe { Self::from_id(msg_send![Self::m_class(), ringtoneSoundNamed: name]) }
102    }
103}