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

Support an OpenURL event on macOS #1

Merged
merged 1 commit into from
Nov 28, 2023
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
10 changes: 10 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,17 @@
///
/// This event type is useful as a place to put code that should be done before you start
/// processing events, such as updating frame timing information for benchmarking or checking
/// the [`StartCause`][crate::event::StartCause] to see if a timer set by

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, x11)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, x11)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, wayland,wayland-d...

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, wayland,wayland-dl...

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-apple-ios, macos-latest)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-pc-windows-gnu, windows-latest, -x86_64-pc-windows-gnu)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, i686-pc-windows-gnu, windows-latest, -i686-pc-windows-gnu)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, aarch64-apple-ios, macos-latest)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-pc-windows-msvc, windows-latest)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, i686-unknown-linux-gnu, ubuntu-latest)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, i686-unknown-linux-gnu, ubuntu-latest)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-apple-darwin, macos-latest)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, i686-pc-windows-gnu, windows-latest, -i686-pc-windows-gnu)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-pc-windows-msvc, windows-latest)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, aarch64-apple-ios, macos-latest)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, i686-pc-windows-msvc, windows-latest)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-apple-ios, macos-latest)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-pc-windows-gnu, windows-latest, -x86_64-pc-windows-gnu)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, i686-pc-windows-msvc, windows-latest)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-apple-darwin, macos-latest)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, wasm32-unknown-unknown, windows-latest)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, wasm32-unknown-unknown, windows-latest)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, aarch64-linux-android, ubuntu-latest, -p winit, apk --, android-native-activity)

redundant explicit link target

Check failure on line 57 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, aarch64-linux-android, ubuntu-latest, -p winit, apk --, android-native-activity)

redundant explicit link target
/// [`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 @@ -184,7 +191,7 @@
/// state-changing events have been handled and you want to do stuff (updating state, performing
/// calculations, etc) that happens as the "main body" of your event loop. If your program only draws
/// graphics when something changes, it's usually better to do it in response to
/// [`Event::RedrawRequested`](crate::event::Event::RedrawRequested), which gets emitted

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, x11)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, x11)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, wayland,wayland-d...

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, wayland,wayland-dl...

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-apple-ios, macos-latest)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-pc-windows-gnu, windows-latest, -x86_64-pc-windows-gnu)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, i686-pc-windows-gnu, windows-latest, -i686-pc-windows-gnu)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, aarch64-apple-ios, macos-latest)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-pc-windows-msvc, windows-latest)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, i686-unknown-linux-gnu, ubuntu-latest)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, i686-unknown-linux-gnu, ubuntu-latest)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-apple-darwin, macos-latest)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, i686-pc-windows-gnu, windows-latest, -i686-pc-windows-gnu)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-pc-windows-msvc, windows-latest)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, aarch64-apple-ios, macos-latest)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, i686-pc-windows-msvc, windows-latest)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-apple-ios, macos-latest)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-pc-windows-gnu, windows-latest, -x86_64-pc-windows-gnu)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, i686-pc-windows-msvc, windows-latest)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-apple-darwin, macos-latest)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, wasm32-unknown-unknown, windows-latest)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, wasm32-unknown-unknown, windows-latest)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, aarch64-linux-android, ubuntu-latest, -p winit, apk --, android-native-activity)

redundant explicit link target

Check failure on line 194 in src/event.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, aarch64-linux-android, ubuntu-latest, -p winit, apk --, android-native-activity)

redundant explicit link target
/// immediately after this event. Programs that draw graphics continuously, like most games,
/// can render here unconditionally for simplicity.
MainEventsCleared,
Expand Down Expand Up @@ -235,6 +242,7 @@
fn clone(&self) -> Self {
use self::Event::*;
match self {
OpenURL { url } => OpenURL { url: url.clone() },
WindowEvent { window_id, event } => WindowEvent {
window_id: *window_id,
event: event.clone(),
Expand All @@ -260,6 +268,7 @@
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 All @@ -277,6 +286,7 @@
pub fn to_static(self) -> Option<Event<'static, T>> {
use self::Event::*;
match self {
OpenURL { url } => Some(OpenURL { url }),
WindowEvent { window_id, event } => event
.to_static()
.map(|event| WindowEvent { window_id, event }),
Expand Down
37 changes: 35 additions & 2 deletions src/platform_impl/macos/app_delegate.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
use objc2::foundation::NSObject;
use objc2::foundation::{NSArray, NSObject, NSString};
use objc2::rc::{Id, Shared};
use objc2::runtime::Object;
use objc2::{declare_class, msg_send, msg_send_id, ClassType};
use objc2::{declare_class, extern_class, extern_methods, msg_send, msg_send_id, ClassType};

use crate::event::Event;

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

extern_class!(
#[derive(Debug)]
struct NSURL;
unsafe impl ClassType for NSURL {
type Super = NSObject;
}
);

unsafe impl Send for NSURL {}
unsafe impl Sync for NSURL {}

extern_methods!(
unsafe impl NSURL {
pub fn absolute_string(&self) -> Option<Id<NSString, Shared>> {
unsafe { msg_send_id![self, absoluteString] }
}
}
);

declare_class!(
#[derive(Debug)]
Expand Down Expand Up @@ -52,6 +74,17 @@ declare_class!(
// TODO: Notify every window that it will be destroyed, like done in iOS?
AppState::exit();
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/macos/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::{boxed::Box, os::raw::*, ptr, str, sync::Mutex};

use objc2::declare::{Ivar, IvarDrop};
use objc2::foundation::{
NSArray, NSAttributedString, NSAttributedStringKey, NSCopying,
NSMutableAttributedString, NSObject, NSPoint, NSRange, NSRect, NSSize, NSString, NSUInteger,
NSArray, NSAttributedString, NSAttributedStringKey, NSCopying, NSMutableAttributedString,
NSObject, NSPoint, NSRange, NSRect, NSSize, NSString, NSUInteger,
};
use objc2::rc::{Id, Owned, Shared, WeakId};
use objc2::runtime::{Object, Sel};
Expand Down
Loading