rust_macios/user_notifications/
un_notification_attachment.rs

1use objc::{msg_send, sel, sel_impl};
2use rust_macios_objective_c_runtime_proc_macros::interface_impl;
3
4use crate::{
5    foundation::{NSDictionary, NSError, NSString, NSURL},
6    object,
7    objective_c_runtime::{
8        id,
9        traits::{FromId, PNSObject},
10    },
11};
12
13object! {
14    /// A media file associated with a notification.
15    unsafe pub struct UNNotificationAttachment;
16}
17
18extern "C" {
19    /// A hint about an attachment’s file type.
20    pub static UNNotificationAttachmentOptionsTypeHintKey: NSString;
21
22    /// A Boolean value indicating whether the system hides the attachment’s thumbnail.
23    pub static UNNotificationAttachmentOptionsThumbnailHiddenKey: NSString;
24
25    /// The clipping rectangle for a thumbnail image.
26    pub static UNNotificationAttachmentOptionsThumbnailClippingRectKey: NSString;
27
28    /// The frame number of an animation to use as a thumbnail image.
29    pub static UNNotificationAttachmentOptionsThumbnailTimeKey: NSString;
30}
31
32#[interface_impl(NSObject)]
33impl UNNotificationAttachment {
34    /* Creating an Attachment
35     */
36
37    /// Creates an attachment object from the specified file and options.
38    #[method]
39    pub fn attachment_with_identifier_url_options(
40        identifier: NSString,
41        url: NSURL,
42        options: NSDictionary<id, id>,
43    ) -> Result<Self, NSError>
44    where
45        Self: Sized + FromId,
46    {
47        unsafe {
48            let mut error = NSError::m_alloc();
49            let ptr = Self::from_id(msg_send![
50                Self::m_class(),
51                attachmentWithIdentifier: identifier
52                URL: url
53                options: options
54                error: &mut error
55            ]);
56
57            if error.m_self().is_null() {
58                Ok(ptr)
59            } else {
60                Err(error)
61            }
62        }
63    }
64
65    /* Getting the Attachment Contents
66     */
67
68    /// The unique identifier for the attachment.
69    #[property]
70    pub fn identifier(&self) -> NSString {
71        unsafe { NSString::from_id(msg_send![self.m_self(), identifier]) }
72    }
73
74    /// The URL of the file for this attachment.
75    #[property]
76    pub fn url(&self) -> NSURL {
77        unsafe { NSURL::from_id(msg_send![self.m_self(), URL]) }
78    }
79
80    /// The UTI type of the attachment.
81    #[property]
82    pub fn _type(&self) -> NSString {
83        unsafe { NSString::from_id(msg_send![self.m_self(), type]) }
84    }
85}