Skip to content

Commit a52085d

Browse files
committed
feat: add OpenFile Event for macOS
1 parent 5e7c1ec commit a52085d

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

.changes/support-open-file.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tao": minor
3+
---
4+
5+
Support OpenFile on macOS.

src/event.rs

+8
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ pub enum Event<'a, T: 'static> {
100100
position: PhysicalPosition<f64>,
101101
},
102102

103+
/// Emitted when open an external file with the app
104+
/// ## Platform-specific
105+
/// - **Windows / Android / Linux:** Unsupported.
106+
OpenFile(PathBuf),
107+
103108
/// Emitted when a global shortcut is triggered.
104109
///
105110
/// ## Platform-specific
@@ -203,6 +208,7 @@ impl<T: Clone> Clone for Event<'static, T> {
203208
position: *position,
204209
},
205210
GlobalShortcutEvent(accelerator_id) => GlobalShortcutEvent(*accelerator_id),
211+
OpenFile(file_path) => OpenFile(file_path.clone()),
206212
}
207213
}
208214
}
@@ -240,6 +246,7 @@ impl<'a, T> Event<'a, T> {
240246
position,
241247
}),
242248
GlobalShortcutEvent(accelerator_id) => Ok(GlobalShortcutEvent(accelerator_id)),
249+
OpenFile(file_path) => Ok(OpenFile(file_path)),
243250
}
244251
}
245252

@@ -279,6 +286,7 @@ impl<'a, T> Event<'a, T> {
279286
position,
280287
}),
281288
GlobalShortcutEvent(accelerator_id) => Some(GlobalShortcutEvent(accelerator_id)),
289+
OpenFile(file_path) => Some(OpenFile(file_path)),
282290
}
283291
}
284292
}

src/platform_impl/macos/app_delegate.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33

44
use crate::{platform::macos::ActivationPolicy, platform_impl::platform::app_state::AppState};
55

6-
use cocoa::base::id;
6+
use cocoa::{
7+
base::{id, YES},
8+
foundation::NSString,
9+
};
710
use objc::{
811
declare::ClassDecl,
912
runtime::{Class, Object, Sel},
1013
};
1114
use std::{
1215
cell::{RefCell, RefMut},
16+
ffi::CStr,
1317
os::raw::c_void,
18+
path::PathBuf,
1419
};
1520

1621
static AUX_DELEGATE_STATE_NAME: &str = "auxState";
@@ -44,6 +49,10 @@ lazy_static! {
4449
sel!(applicationWillTerminate:),
4550
application_will_terminate as extern "C" fn(&Object, Sel, id),
4651
);
52+
decl.add_method(
53+
sel!(application:openFile:),
54+
application_open_file as extern "C" fn(&Object, Sel, id, id) -> i8,
55+
);
4756
decl.add_ivar::<*mut c_void>(AUX_DELEGATE_STATE_NAME);
4857

4958
AppDelegateClass(decl.register())
@@ -92,3 +101,17 @@ extern "C" fn application_will_terminate(_: &Object, _: Sel, _: id) {
92101
AppState::exit();
93102
trace!("Completed `applicationWillTerminate`");
94103
}
104+
105+
extern "C" fn application_open_file(_: &Object, _: Sel, _: id, file: id) -> i8 {
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;
117+
}

src/platform_impl/macos/app_state.rs

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::{
77
fmt::{self, Debug},
88
hint::unreachable_unchecked,
99
mem,
10+
path::PathBuf,
1011
rc::{Rc, Weak},
1112
sync::{
1213
atomic::{AtomicBool, Ordering},
@@ -297,6 +298,10 @@ impl AppState {
297298
HANDLER.set_in_callback(false);
298299
}
299300

301+
pub fn open_file(path: PathBuf) {
302+
HANDLER.handle_nonuser_event(EventWrapper::StaticEvent(Event::OpenFile(path)));
303+
}
304+
300305
pub fn wakeup(panic_info: Weak<PanicInfo>) {
301306
let panic_info = panic_info
302307
.upgrade()

0 commit comments

Comments
 (0)