Skip to content

Commit

Permalink
Support an OpenURL event on macOS
Browse files Browse the repository at this point in the history
This hookst log into the application:openUrls delegate callback on macOS to deliver
a corresponding `Event::OpenURL` to applications.

Although this event is currently only supported on macOS; potentially in the
future this could also be supported on Linux by handling the
org.freedesktop.Application.Open method and potentially Android Intent's with a
URL could be mapped to OpenURL events via some Activity::onNewIntent
integration.
  • Loading branch information
rib committed Jan 30, 2024
1 parent dbeeaef commit 9ab5bd6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ features = [
"Foundation_NSProcessInfo",
"Foundation_NSThread",
"Foundation_NSNumber",
"Foundation_NSURL",
]

[target.'cfg(target_os = "ios")'.dependencies.icrate]
Expand Down
8 changes: 8 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ pub enum Event<T: 'static> {
/// [`ControlFlow::WaitUntil`](crate::event_loop::ControlFlow::WaitUntil) has elapsed.
NewEvents(StartCause),

/// Emitted if a user has requested to open an application specific URL that is registered with the OS
///
/// # Portability
///
/// This event is only ever delivered on MacOS currently
OpenURL { url: String },

/// Emitted when the OS sends an event to a winit window.
WindowEvent {
window_id: WindowId,
Expand Down Expand Up @@ -259,6 +266,7 @@ impl<T> Event<T> {
use self::Event::*;
match self {
UserEvent(_) => Err(self),
OpenURL { url } => Ok(OpenURL { url }),
WindowEvent { window_id, event } => Ok(WindowEvent { window_id, event }),
DeviceEvent { device_id, event } => Ok(DeviceEvent { device_id, event }),
NewEvents(cause) => Ok(NewEvents(cause)),
Expand Down
15 changes: 14 additions & 1 deletion src/platform_impl/macos/app_delegate.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::ptr::NonNull;

use icrate::Foundation::NSObject;
use icrate::Foundation::{NSArray, NSObject, NSURL};
use objc2::declare::{IvarBool, IvarEncode};
use objc2::rc::Id;
use objc2::runtime::AnyObject;
use objc2::{declare_class, msg_send, msg_send_id, mutability, ClassType};

use crate::event::Event;

use super::app_state::AppState;
use super::appkit::NSApplicationActivationPolicy;

Expand Down Expand Up @@ -58,6 +60,17 @@ declare_class!(
// TODO: Notify every window that it will be destroyed, like done in iOS?
AppState::internal_exit();
}

#[method(application:openURLs:)]
fn application_open_urls(&self, _application: &NSObject, urls: &NSArray<NSURL>) {
for url in urls {
if let Some(url) = unsafe { url.absoluteString() } {
AppState::queue_event(Event::OpenURL {
url: url.to_string(),
});
}
}
}
}
);

Expand Down

0 comments on commit 9ab5bd6

Please sign in to comment.