-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathalert.rs
61 lines (55 loc) · 1.89 KB
/
alert.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! A wrapper for `NSAlert`.
//!
//! This is housed inside `appkit` as it's a useful tool for a few cases, but it doesn't match the
//! iOS API, so we make no guarantees about it being a universal control. In general this also
//! doesn't produce an amazing user experience, and you may want to shy away from using it.
//!
//! If you want to show a complex view in an alert-esque fashion, you may consider looking at
//! `Sheet`.
//!
//! ```rust,no_run
//! use cacao::appkit::{App, AppDelegate, Alert};
//!
//! #[derive(Default)]
//! struct ExampleApp;
//!
//! impl AppDelegate for ExampleApp {
//! fn did_finish_launching(&self) {
//!
//! }
//! }
//!
//! fn main() {
//! App::new("com.alert.example", ExampleApp::default()).run()
//! }
//! ```
use objc::rc::{Id, Owned};
use objc::runtime::Object;
use objc::{class, msg_send, msg_send_id, sel};
use crate::foundation::{id, NSString};
/// Represents an `NSAlert`. Has no information other than the retained pointer to the Objective C
/// side, so... don't bother inspecting this.
#[derive(Debug)]
pub struct Alert(Id<Object, Owned>);
impl Alert {
/// Creates a basic `NSAlert`, storing a pointer to it in the Objective C runtime.
/// You can show this alert by calling `show()`.
pub fn new(title: &str, message: &str) -> Self {
let title = NSString::new(title);
let message = NSString::new(message);
let ok = NSString::new("OK");
Alert(unsafe {
let mut alert = msg_send_id![class!(NSAlert), new].unwrap();
let _: () = msg_send![&mut alert, setMessageText: &*title];
let _: () = msg_send![&mut alert, setInformativeText: &*message];
let _: () = msg_send![&mut alert, addButtonWithTitle: &*ok];
alert
})
}
/// Shows this alert as a modal.
pub fn show(&self) {
unsafe {
let _: () = msg_send![&*self.0, runModal];
}
}
}