cacao/appkit/notifications/notifications.rs

29 lines
1.1 KiB
Rust
Raw Normal View History

2020-02-28 13:34:34 +11:00
//! Acts as a (currently dumb) wrapper for `UNMutableNotificationContent`, which is what you mostly
//! need to pass to the notification center for things to work.
use objc_id::Id;
use objc::runtime::Object;
use objc::{class, msg_send, sel, sel_impl};
use crate::foundation::{id, NSString};
2020-02-28 13:34:34 +11:00
/// A wrapper for `UNMutableNotificationContent`. Retains the pointer from the Objective C side,
/// and is ultimately dropped upon sending.
pub struct Notification(pub Id<Object>);
2020-02-28 13:34:34 +11:00
impl Notification {
/// Constructs a new `Notification`. This allocates `NSString`'s, as it has to do so for the
/// Objective C runtime - be aware if you're slaming this (you shouldn't be slamming this).
pub fn new(title: &str, body: &str) -> Self {
let title = NSString::new(title);
let body = NSString::new(body);
2020-02-28 13:34:34 +11:00
Notification(unsafe {
let content: id = msg_send![class!(UNMutableNotificationContent), new];
let _: () = msg_send![content, setTitle:title];
let _: () = msg_send![content, setBody:body];
Id::from_ptr(content)
})
2020-02-28 13:34:34 +11:00
}
}