Skip to content

Commit d628f30

Browse files
committed
feat: use openURLs instead of openFile
1 parent f1f82d7 commit d628f30

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

.changes/support-open-file.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"tao": minor
33
---
44

5-
Support OpenFile on macOS.
5+
Support OpenURLs on macOS.

src/event.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub enum Event<'a, T: 'static> {
105105
/// ## Platform-specific
106106
///
107107
/// - **Windows / Android / Linux:** Unsupported.
108-
OpenFile(PathBuf),
108+
OpenURLs(Vec<String>),
109109

110110
/// Emitted when a global shortcut is triggered.
111111
///
@@ -210,7 +210,7 @@ impl<T: Clone> Clone for Event<'static, T> {
210210
position: *position,
211211
},
212212
GlobalShortcutEvent(accelerator_id) => GlobalShortcutEvent(*accelerator_id),
213-
OpenFile(file_path) => OpenFile(file_path.clone()),
213+
OpenURLs(urls) => OpenURLs(urls.clone()),
214214
}
215215
}
216216
}
@@ -248,7 +248,7 @@ impl<'a, T> Event<'a, T> {
248248
position,
249249
}),
250250
GlobalShortcutEvent(accelerator_id) => Ok(GlobalShortcutEvent(accelerator_id)),
251-
OpenFile(file_path) => Ok(OpenFile(file_path)),
251+
OpenURLs(urls) => Ok(OpenURLs(urls)),
252252
}
253253
}
254254

@@ -288,7 +288,7 @@ impl<'a, T> Event<'a, T> {
288288
position,
289289
}),
290290
GlobalShortcutEvent(accelerator_id) => Some(GlobalShortcutEvent(accelerator_id)),
291-
OpenFile(file_path) => Some(OpenFile(file_path)),
291+
OpenURLs(urls) => Some(OpenURLs(urls)),
292292
}
293293
}
294294
}

src/platform_impl/macos/app_delegate.rs

+19-17
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
use crate::{platform::macos::ActivationPolicy, platform_impl::platform::app_state::AppState};
55

66
use cocoa::{
7-
base::{id, YES, BOOL},
8-
foundation::NSString,
7+
base::id,
8+
foundation::{NSArray, NSString, NSURL},
99
};
1010
use objc::{
1111
declare::ClassDecl,
@@ -15,7 +15,6 @@ use std::{
1515
cell::{RefCell, RefMut},
1616
ffi::CStr,
1717
os::raw::c_void,
18-
path::PathBuf,
1918
};
2019

2120
static AUX_DELEGATE_STATE_NAME: &str = "auxState";
@@ -50,8 +49,8 @@ lazy_static! {
5049
application_will_terminate as extern "C" fn(&Object, Sel, id),
5150
);
5251
decl.add_method(
53-
sel!(application:openFile:),
54-
application_open_file as extern "C" fn(&Object, Sel, id, id) -> BOOL,
52+
sel!(application:openURLs:),
53+
application_open_urls as extern "C" fn(&Object, Sel, id, id),
5554
);
5655
decl.add_ivar::<*mut c_void>(AUX_DELEGATE_STATE_NAME);
5756

@@ -102,16 +101,19 @@ extern "C" fn application_will_terminate(_: &Object, _: Sel, _: id) {
102101
trace!("Completed `applicationWillTerminate`");
103102
}
104103

105-
extern "C" fn application_open_file(_: &Object, _: Sel, _: id, file: id) -> BOOL {
106-
let path_string = unsafe { CStr::from_ptr(file.UTF8String()).to_string_lossy() };
107-
108-
let path = PathBuf::from(path_string.as_ref());
109-
trace!("Trigger `application:openFile:` with path: {}", path_string);
110-
AppState::open_file(path);
111-
trace!(
112-
"Completed `application:openFile:` with path: {}",
113-
&path_string
114-
);
115-
116-
return YES;
104+
extern "C" fn application_open_urls(_: &Object, _: Sel, _: id, urls: id) -> () {
105+
trace!("Trigger `application:openURLs:`");
106+
107+
let url_strings = unsafe {
108+
(0..urls.count())
109+
.map(|i| {
110+
CStr::from_ptr(urls.objectAtIndex(i).absoluteString().UTF8String())
111+
.to_string_lossy()
112+
.into_owned()
113+
})
114+
.collect::<Vec<_>>()
115+
};
116+
trace!("Get `application:openURLs:` URLs: {:?}", url_strings);
117+
AppState::open_urls(url_strings);
118+
trace!("Completed `application:openURLs:`");
117119
}

src/platform_impl/macos/app_state.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::{
77
fmt::{self, Debug},
88
hint::unreachable_unchecked,
99
mem,
10-
path::PathBuf,
1110
rc::{Rc, Weak},
1211
sync::{
1312
atomic::{AtomicBool, Ordering},
@@ -298,8 +297,8 @@ impl AppState {
298297
HANDLER.set_in_callback(false);
299298
}
300299

301-
pub fn open_file(path: PathBuf) {
302-
HANDLER.handle_nonuser_event(EventWrapper::StaticEvent(Event::OpenFile(path)));
300+
pub fn open_urls(urls: Vec<String>) {
301+
HANDLER.handle_nonuser_event(EventWrapper::StaticEvent(Event::OpenURLs(urls)));
303302
}
304303

305304
pub fn wakeup(panic_info: Weak<PanicInfo>) {

0 commit comments

Comments
 (0)