|
14 | 14 | // You should have received a copy of the GNU Affero General Public License |
15 | 15 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
16 | 16 |
|
17 | | -// Based on https://github.com/FabianLars/tauri-plugin-deep-link |
18 | | - |
19 | | -use std::{ |
20 | | - io::{ErrorKind, Result}, |
21 | | - sync::Mutex, |
22 | | -}; |
23 | | - |
24 | | -use objc2::{ |
25 | | - class, define_class, |
26 | | - ffi::NSInteger, |
27 | | - msg_send, |
28 | | - rc::Retained, |
29 | | - runtime::{AnyObject, NSObject}, |
30 | | - sel, ClassType, |
31 | | -}; |
32 | | -use once_cell::sync::OnceCell; |
33 | 17 | use tauri::{menu::{Menu, MenuItem, PredefinedMenuItem, Submenu}, AppHandle}; |
34 | 18 |
|
35 | | -type THandler = OnceCell<Mutex<Box<dyn FnMut(Vec<String>) + Send + 'static>>>; |
36 | | - |
37 | | -// If the Mutex turns out to be a problem, or FnMut turns out to be useless, we can remove the Mutex and turn FnMut into Fn |
38 | | -static HANDLER: THandler = OnceCell::new(); |
39 | | - |
40 | | -// keyDirectObject |
41 | | -const KEY_DIRECT_OBJECT: u32 = 0x2d2d2d2d; |
42 | | - |
43 | | -// kInternetEventClass |
44 | | -const GURL_EVENT_CLASS: u32 = 0x4755524c; |
45 | | -// kAEGetURL |
46 | | -const EVENT_GET_URL: u32 = 0x4755524c; |
47 | | - |
48 | | -// kCoreEventClass |
49 | | -const CORE_EVENT_CLASS: u32 = 0x61657674; |
50 | | -// kAEOpenDocuments |
51 | | -const EVENT_OPEN_DOCUMENTS: u32 = 0x6F646F63; |
52 | | -// kAEReopenApplication |
53 | | -const EVENT_REOPEN_APP: u32 = 0x72617070; |
54 | | - |
55 | | -// Adapted from https://github.com/mrmekon/fruitbasket/blob/aad14e400d710d1d46317c0d8c55ff742bfeaadd/src/osx.rs#L848 |
56 | | -fn parse_event(event: *mut AnyObject) -> Vec<String> { |
57 | | - if event as u64 == 0u64 { |
58 | | - return vec![]; |
59 | | - } |
60 | | - unsafe { |
61 | | - let class: u32 = msg_send![event, eventClass]; |
62 | | - let id: u32 = msg_send![event, eventID]; |
63 | | - |
64 | | - match (class, id) { |
65 | | - (GURL_EVENT_CLASS, EVENT_GET_URL) => { |
66 | | - let url: *mut AnyObject = |
67 | | - msg_send![event, paramDescriptorForKeyword: KEY_DIRECT_OBJECT]; |
68 | | - let nsstring: *mut AnyObject = msg_send![url, stringValue]; |
69 | | - let cstr: *const i8 = msg_send![nsstring, UTF8String]; |
70 | | - |
71 | | - if !cstr.is_null() { |
72 | | - vec![std::ffi::CStr::from_ptr(cstr).to_string_lossy().to_string()] |
73 | | - } else { |
74 | | - vec![] |
75 | | - } |
76 | | - } |
77 | | - (CORE_EVENT_CLASS, EVENT_OPEN_DOCUMENTS) => { |
78 | | - let documents: *mut AnyObject = |
79 | | - msg_send![event, paramDescriptorForKeyword: KEY_DIRECT_OBJECT]; |
80 | | - let count: NSInteger = msg_send![documents, numberOfItems]; |
81 | | - |
82 | | - let mut paths = Vec::<String>::new(); |
83 | | - |
84 | | - for i in 1..count + 1 { |
85 | | - let path: *mut AnyObject = msg_send![documents, descriptorAtIndex: i]; |
86 | | - let nsstring: *mut AnyObject = msg_send![path, stringValue]; |
87 | | - let cstr: *const i8 = msg_send![nsstring, UTF8String]; |
88 | | - |
89 | | - if !cstr.is_null() { |
90 | | - let path_str = std::ffi::CStr::from_ptr(cstr).to_string_lossy().to_string(); |
91 | | - paths.push(path_str); |
92 | | - } |
93 | | - } |
94 | | - |
95 | | - paths |
96 | | - } |
97 | | - // reopen app event has no useful payload |
98 | | - (_, _) => { |
99 | | - vec![] |
100 | | - } |
101 | | - } |
102 | | - } |
103 | | -} |
104 | | - |
105 | | -define_class!( |
106 | | - #[unsafe(super(NSObject))] |
107 | | - #[name = "TauriPluginDeepLinkHandler"] |
108 | | - struct Handler; |
109 | | - |
110 | | - impl Handler { |
111 | | - #[unsafe(method(handleEvent:withReplyEvent:))] |
112 | | - fn handle_event(&self, event: *mut AnyObject, _replace: *const AnyObject) { |
113 | | - let s = parse_event(event); |
114 | | - let mut cb = HANDLER.get().unwrap().lock().unwrap(); |
115 | | - cb(s); |
116 | | - } |
117 | | - } |
118 | | -); |
119 | | - |
120 | | -impl Handler { |
121 | | - pub fn new() -> Retained<Self> { |
122 | | - let cls = Self::class(); |
123 | | - unsafe { msg_send![msg_send![cls, alloc], init] } |
124 | | - } |
125 | | -} |
126 | | - |
127 | | -// Call this once early in app main() or setup hook |
128 | | -pub fn set_handler<F: FnMut(Vec<String>) + Send + 'static>(handler: F) -> Result<()> { |
129 | | - if HANDLER.set(Mutex::new(Box::new(handler))).is_err() { |
130 | | - return Err(std::io::Error::new( |
131 | | - ErrorKind::AlreadyExists, |
132 | | - "Handler was already set", |
133 | | - )); |
134 | | - } |
135 | | - |
136 | | - Ok(()) |
137 | | -} |
138 | | - |
139 | | -fn listen_apple_event(event_class: u32, event_id: u32) { |
140 | | - unsafe { |
141 | | - let event_manager: Retained<AnyObject> = |
142 | | - msg_send![class!(NSAppleEventManager), sharedAppleEventManager]; |
143 | | - |
144 | | - let handler = Handler::new(); |
145 | | - let handler_boxed = Box::into_raw(Box::new(handler)); |
146 | | - |
147 | | - let _: () = msg_send![&event_manager, |
148 | | - setEventHandler: &**handler_boxed, |
149 | | - andSelector: sel!(handleEvent:withReplyEvent:), |
150 | | - forEventClass: event_class, |
151 | | - andEventID: event_id]; |
152 | | - } |
153 | | -} |
154 | | - |
155 | | -// Call this in app setup hook |
156 | | -pub fn listen_url() { |
157 | | - listen_apple_event(GURL_EVENT_CLASS, EVENT_GET_URL); |
158 | | -} |
159 | | - |
160 | | -// Call this after app is initialised |
161 | | -pub fn listen_open_documents() { |
162 | | - listen_apple_event(CORE_EVENT_CLASS, EVENT_OPEN_DOCUMENTS); |
163 | | -} |
164 | | - |
165 | | -// Call this after app is initialised |
166 | | -pub fn listen_reopen_app() { |
167 | | - listen_apple_event(CORE_EVENT_CLASS, EVENT_REOPEN_APP); |
168 | | -} |
169 | | - |
170 | 19 | pub fn make_menu<R>(app: &AppHandle<R>) -> tauri::Result<Menu<R>> |
171 | 20 | where |
172 | 21 | R: tauri::Runtime, |
|
0 commit comments