Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MacOS] Handle receiving URL as platform-specific event #1

Merged
merged 2 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ pub enum Event<'a, T: 'static> {
/// This is irreversable - if this event is emitted, it is guaranteed to be the last event that
/// gets emitted. You generally want to treat this as an "do on quit" event.
LoopDestroyed,

/// Emitted when the event loop receives an event that only occurs on some specific platform.
PlatformSpecific(PlatformSpecific),
}

/// Describes an event from some specific platform.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PlatformSpecific {
MacOS(MacOS),
}

/// Describes an event that only happens in `MacOS`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MacOS {
ReceivedUrl(String),
}

impl<T: Clone> Clone for Event<'static, T> {
Expand All @@ -138,6 +153,7 @@ impl<T: Clone> Clone for Event<'static, T> {
LoopDestroyed => LoopDestroyed,
Suspended => Suspended,
Resumed => Resumed,
PlatformSpecific(event) => PlatformSpecific(event.clone()),
}
}
}
Expand All @@ -156,6 +172,7 @@ impl<'a, T> Event<'a, T> {
LoopDestroyed => Ok(LoopDestroyed),
Suspended => Ok(Suspended),
Resumed => Ok(Resumed),
PlatformSpecific(event) => Ok(PlatformSpecific(event)),
}
}

Expand All @@ -176,6 +193,7 @@ impl<'a, T> Event<'a, T> {
LoopDestroyed => Some(LoopDestroyed),
Suspended => Some(Suspended),
Resumed => Some(Resumed),
PlatformSpecific(event) => Some(PlatformSpecific(event)),
}
}
}
Expand Down
78 changes: 76 additions & 2 deletions src/platform_impl/macos/app_delegate.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{platform::macos::ActivationPolicy, platform_impl::platform::app_state::AppState};

use crate::event::{Event, MacOS, PlatformSpecific};
use crate::platform::macos::ActivationPolicy;
use crate::platform_impl::platform::{app_state::AppState, event::EventWrapper};
use cocoa::base::id;
use objc::{
declare::ClassDecl,
Expand All @@ -21,6 +22,14 @@ pub struct AuxDelegateState {
pub create_default_menu: bool,
}

/// Apple constants
#[allow(non_upper_case_globals)]
pub const kInternetEventClass: u32 = 0x4755524c;
#[allow(non_upper_case_globals)]
pub const kAEGetURL: u32 = 0x4755524c;
#[allow(non_upper_case_globals)]
pub const keyDirectObject: u32 = 0x2d2d2d2d;

pub struct AppDelegateClass(pub *const Class);
unsafe impl Send for AppDelegateClass {}
unsafe impl Sync for AppDelegateClass {}
Expand All @@ -33,11 +42,25 @@ lazy_static! {
decl.add_class_method(sel!(new), new as extern "C" fn(&Class, Sel) -> id);
decl.add_method(sel!(dealloc), dealloc as extern "C" fn(&Object, Sel));

decl.add_method(
sel!(applicationWillFinishLaunching:),
will_finish_launching as extern "C" fn(&Object, Sel, id),
);
decl.add_method(
sel!(applicationDidFinishLaunching:),
did_finish_launching as extern "C" fn(&Object, Sel, id),
);
decl.add_ivar::<*mut c_void>(AUX_DELEGATE_STATE_NAME);
decl.add_method(
sel!(handleEvent:withReplyEvent:),
handle_url
as extern "C" fn(
&objc::runtime::Object,
_cmd: objc::runtime::Sel,
event: *mut Object,
_reply: u64,
),
);

AppDelegateClass(decl.register())
};
Expand Down Expand Up @@ -74,6 +97,57 @@ extern "C" fn dealloc(this: &Object, _: Sel) {
}
}

fn parse_url(event: *mut Object) -> Option<String> {
unsafe {
let class: u32 = msg_send![event, eventClass];
let id: u32 = msg_send![event, eventID];
if class != kInternetEventClass || id != kAEGetURL {
return None;
}
let subevent: *mut Object = msg_send![event, paramDescriptorForKeyword: keyDirectObject];
let nsstring: *mut Object = msg_send![subevent, stringValue];

let cstr: *const i8 = msg_send![nsstring, UTF8String];
if cstr != std::ptr::null() {
Some(
std::ffi::CStr::from_ptr(cstr)
.to_string_lossy()
.into_owned(),
)
} else {
None
}
}
}

extern "C" fn handle_url(
_this: &objc::runtime::Object,
_cmd: objc::runtime::Sel,
event: *mut Object,
_reply: u64,
) {
if let Some(string) = parse_url(event) {
AppState::queue_event(EventWrapper::StaticEvent(Event::PlatformSpecific(
PlatformSpecific::MacOS(MacOS::ReceivedUrl(string)),
)));
}
}

extern "C" fn will_finish_launching(this: &Object, _: Sel, _: id) {
trace!("Triggered `applicationWillFinishLaunching`");
unsafe {
let event_manager = class!(NSAppleEventManager);
let shared_manager: *mut Object = msg_send![event_manager, sharedAppleEventManager];
let () = msg_send![shared_manager,
setEventHandler: this
andSelector: sel!(handleEvent:withReplyEvent:)
forEventClass: kInternetEventClass
andEventID: kAEGetURL
];
}
trace!("Completed `applicationWillFinishLaunching`");
}

extern "C" fn did_finish_launching(this: &Object, _: Sel, _: id) {
trace!("Triggered `applicationDidFinishLaunching`");
AppState::launched(this);
Expand Down