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

Handle URLs on macOS #3825

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,15 @@ pub trait ApplicationHandler {
fn memory_warning(&mut self, event_loop: &ActiveEventLoop) {
let _ = event_loop;
}

/// Emitted when the application has received a URL, through a
/// custom URL handler.
///
/// Only supported on macOS.
fn received_url(&mut self, event_loop: &ActiveEventLoop, url: String) {
let _ = event_loop;
let _ = url;
}
Comment on lines +329 to +336
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you create a macOS specific ApplicationHandler and add it there?

}

#[deny(clippy::missing_trait_methods)]
Expand Down
15 changes: 15 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ pub(crate) enum Event {

/// User requested a wake up.
UserWakeUp,

/// 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),
}

/// Describes the reason the event loop is resuming.
Expand Down
60 changes: 59 additions & 1 deletion src/platform_impl/apple/appkit/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use std::sync::Arc;
use std::time::Instant;

use objc2::rc::Retained;
use objc2::{declare_class, msg_send_id, mutability, ClassType, DeclaredClass};
use objc2::runtime::AnyObject;
use objc2::{
class, declare_class, msg_send, msg_send_id, mutability, sel, ClassType, DeclaredClass,
};
use objc2_app_kit::{NSApplication, NSApplicationActivationPolicy, NSApplicationDelegate};
use objc2_foundation::{MainThreadMarker, NSNotification, NSObject, NSObjectProtocol};

Expand All @@ -19,6 +22,14 @@ use crate::event::{StartCause, WindowEvent};
use crate::event_loop::{ActiveEventLoop as RootActiveEventLoop, ControlFlow};
use crate::window::WindowId as RootWindowId;

/// 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;

#[derive(Debug)]
pub(super) struct AppState {
activation_policy: NSApplicationActivationPolicy,
Expand Down Expand Up @@ -72,6 +83,33 @@ declare_class!(
fn app_will_terminate(&self, notification: &NSNotification) {
self.will_terminate(notification)
}

#[method(applicationWillFinishLaunching:)]
fn will_finish_launching(&self, _sender: Option<&AnyObject>) {
trace_scope!("applicationWillFinishLaunching");

unsafe {
let event_manager = class!(NSAppleEventManager);
let shared_manager: *mut AnyObject =
msg_send![event_manager, sharedAppleEventManager];

let () = msg_send![shared_manager,
setEventHandler: self
andSelector: sel!(handleEvent:withReplyEvent:)
forEventClass: kInternetEventClass
andEventID: kAEGetURL
];
}
}

#[method(handleEvent:withReplyEvent:)]
fn handle_url(&self, event: &AnyObject, _reply: u64) {
if let Some(string) = parse_url(event) {
self.with_handler(|app, event_loop| {
app.received_url(event_loop, string);
});
}
}
}
);

Expand Down Expand Up @@ -428,3 +466,23 @@ fn window_activation_hack(app: &NSApplication) {
}
})
}

/// Convert a URL received as a kInternetEventClass into a Rust `String`
fn parse_url(event: &AnyObject) -> 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: *const AnyObject =
msg_send![event, paramDescriptorForKeyword: keyDirectObject];
let nsstring: *const AnyObject = msg_send![subevent, stringValue];
let cstr: *const i8 = msg_send![nsstring, UTF8String];
if !cstr.is_null() {
Some(std::ffi::CStr::from_ptr(cstr).to_string_lossy().into_owned())
} else {
None
}
}
}
Loading