Skip to content

Commit

Permalink
feat: add OpenFile Event for macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
meowtec committed Jun 23, 2022
1 parent 5e7c1ec commit a52085d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/support-open-file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": minor
---

Support OpenFile on macOS.
8 changes: 8 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ pub enum Event<'a, T: 'static> {
position: PhysicalPosition<f64>,
},

/// Emitted when open an external file with the app
/// ## Platform-specific
/// - **Windows / Android / Linux:** Unsupported.
OpenFile(PathBuf),

/// Emitted when a global shortcut is triggered.
///
/// ## Platform-specific
Expand Down Expand Up @@ -203,6 +208,7 @@ impl<T: Clone> Clone for Event<'static, T> {
position: *position,
},
GlobalShortcutEvent(accelerator_id) => GlobalShortcutEvent(*accelerator_id),
OpenFile(file_path) => OpenFile(file_path.clone()),
}
}
}
Expand Down Expand Up @@ -240,6 +246,7 @@ impl<'a, T> Event<'a, T> {
position,
}),
GlobalShortcutEvent(accelerator_id) => Ok(GlobalShortcutEvent(accelerator_id)),
OpenFile(file_path) => Ok(OpenFile(file_path)),
}
}

Expand Down Expand Up @@ -279,6 +286,7 @@ impl<'a, T> Event<'a, T> {
position,
}),
GlobalShortcutEvent(accelerator_id) => Some(GlobalShortcutEvent(accelerator_id)),
OpenFile(file_path) => Some(OpenFile(file_path)),
}
}
}
Expand Down
25 changes: 24 additions & 1 deletion src/platform_impl/macos/app_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@

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

use cocoa::base::id;
use cocoa::{
base::{id, YES},
foundation::NSString,
};
use objc::{
declare::ClassDecl,
runtime::{Class, Object, Sel},
};
use std::{
cell::{RefCell, RefMut},
ffi::CStr,
os::raw::c_void,
path::PathBuf,
};

static AUX_DELEGATE_STATE_NAME: &str = "auxState";
Expand Down Expand Up @@ -44,6 +49,10 @@ lazy_static! {
sel!(applicationWillTerminate:),
application_will_terminate as extern "C" fn(&Object, Sel, id),
);
decl.add_method(
sel!(application:openFile:),
application_open_file as extern "C" fn(&Object, Sel, id, id) -> i8,
);
decl.add_ivar::<*mut c_void>(AUX_DELEGATE_STATE_NAME);

AppDelegateClass(decl.register())
Expand Down Expand Up @@ -92,3 +101,17 @@ extern "C" fn application_will_terminate(_: &Object, _: Sel, _: id) {
AppState::exit();
trace!("Completed `applicationWillTerminate`");
}

extern "C" fn application_open_file(_: &Object, _: Sel, _: id, file: id) -> i8 {
let path_string = unsafe { CStr::from_ptr(file.UTF8String()).to_string_lossy() };

let path = PathBuf::from(path_string.as_ref());
trace!("Trigger `application:openFile:` with path: {}", path_string);
AppState::open_file(path);
trace!(
"Completed `application:openFile:` with path: {}",
&path_string
);

return YES;
}
5 changes: 5 additions & 0 deletions src/platform_impl/macos/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
fmt::{self, Debug},
hint::unreachable_unchecked,
mem,
path::PathBuf,
rc::{Rc, Weak},
sync::{
atomic::{AtomicBool, Ordering},
Expand Down Expand Up @@ -297,6 +298,10 @@ impl AppState {
HANDLER.set_in_callback(false);
}

pub fn open_file(path: PathBuf) {
HANDLER.handle_nonuser_event(EventWrapper::StaticEvent(Event::OpenFile(path)));
}

pub fn wakeup(panic_info: Weak<PanicInfo>) {
let panic_info = panic_info
.upgrade()
Expand Down

0 comments on commit a52085d

Please sign in to comment.