rust_macios/foundation/
ns_notification.rs

1use objc::{msg_send, sel, sel_impl};
2
3use crate::{
4    object,
5    objective_c_runtime::{
6        id,
7        macros::interface_impl,
8        traits::{FromId, PNSObject},
9    },
10};
11
12use super::{NSCoder, NSDictionary, NSNotificationName};
13
14object! {
15    /// A container for information broadcast through a notification center to all registered observers.
16    unsafe pub struct NSNotification;
17}
18
19#[interface_impl(NSObject)]
20impl NSNotification {
21    /* Creating Notifications
22     */
23
24    /// Initializes an empty notification.
25    #[method]
26    pub fn init(&mut self) -> Self
27    where
28        Self: Sized + FromId,
29    {
30        unsafe {
31            let ptr = msg_send![self.m_self(), init];
32            FromId::from_id(ptr)
33        }
34    }
35
36    /// Initializes a notification with the data from an unarchiver.
37    #[method]
38    pub fn init_with_coder(&mut self, coder: &NSCoder) -> Self
39    where
40        Self: Sized + FromId,
41    {
42        unsafe { FromId::from_id(msg_send![self.m_self(), initWithCoder: coder.m_self()]) }
43    }
44
45    /// Returns a new notification object with a specified name and object.
46    #[method]
47    pub fn notification_with_name_object(name: NSNotificationName, object: id) -> Self
48    where
49        Self: Sized + FromId,
50    {
51        unsafe {
52            FromId::from_id(msg_send![Self::m_class(), notificationWithName: name object: object])
53        }
54    }
55
56    /// Returns a notification object with a specified name, object, and user information.
57    #[method]
58    pub fn notification_with_name_object_user_info(
59        name: NSNotificationName,
60        object: id,
61        user_info: NSDictionary<id, id>,
62    ) -> Self
63    where
64        Self: Sized + FromId,
65    {
66        unsafe {
67            FromId::from_id(
68                msg_send![Self::m_class(), notificationWithName: name object: object userInfo: user_info],
69            )
70        }
71    }
72
73    /// Initializes a notification with a specified name, object, and user information.
74    #[method]
75    pub fn init_with_name_object_user_info(
76        &mut self,
77        name: &NSNotificationName,
78        object: id,
79        user_info: &NSDictionary<id, id>,
80    ) -> Self
81    where
82        Self: Sized + FromId,
83    {
84        unsafe {
85            FromId::from_id(
86                msg_send![self.m_self(), initWithName: name.m_self() object: object userInfo: user_info.m_self()],
87            )
88        }
89    }
90
91    /* Getting Notification Information
92     */
93
94    /// The name of the notification.
95    #[method]
96    pub fn name(&self) -> NSNotificationName {
97        unsafe { NSNotificationName::from_id(msg_send![self.m_self(), name]) }
98    }
99
100    /// The object associated with the notification.
101    #[method]
102    pub fn object(&self) -> id {
103        unsafe { msg_send![self.m_self(), object] }
104    }
105
106    /// The user information dictionary associated with the notification.
107    #[method]
108    pub fn user_info(&self) -> NSDictionary<id, id> {
109        unsafe { NSDictionary::from_id(msg_send![self.m_self(), userInfo]) }
110    }
111}