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

Conversation

nicoburns
Copy link

This allows macOS apps to access the URL when they are opened "as a browser / url handler". This is adapted from Iced's fork of Winit and I believe it is the main things preventing Iced from using upstream Winit.

Fixes #2190

  • Tested on all platforms changed
  • Added an entry to the changelog module if knowledge of this change could be valuable to users
  • Updated documentation to reflect any user-facing changes, including notes of platform-specific behavior
  • Created or updated an example program if it would help users understand this functionality
  • Updated feature matrix, if new features were added or implemented

@nicoburns nicoburns requested a review from madsmtm as a code owner July 26, 2024 05:22
Copy link
Member

@kchibisov kchibisov left a comment

Choose a reason for hiding this comment

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

I think @madsmtm has a different idea on how to approach all of that, but I don't mind having it under macOS specific application handler.

Also, it should be added without a specific event, just rely on the newly added callback.

Comment on lines +329 to +336
/// 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;
}
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?

Copy link
Member

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

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

I would very much like to get Iced back to using mainline Winit!

I just merged #3758, could you check if that allows you to do what Iced needs? See in particular the new docs under winit::platform::macos.

@nicoburns
Copy link
Author

@madsmtm Super excited to see #3758 merged. That looks like an absolutely fantastic solution for modular Winit on apple platforms. I'll have to implement it to be sure, but I reckon it will be possible for this to be implemented outside of Winit on top of #3758. And I reckon I can use some of the objc2 crates' functionality to really clean it up too. The one question I have is when/where I ought to run the code that registers the listener for the event that returns the url:

I could use my own AppDelegate and register it in applicationWillFinishLaunching like the current PR does. But I would really like to be able to wrap this up in a library, and my feeling is that the approach you've used in #3758 ought to enable that. But that perhaps the create_observer function ought to become public so it can be used in 3rd party libs as well as winit.

Regarding event registration ordering:

  • I'm guessing I ought to put my code in Winit's "resume" event?
  • Do you think I could register for the kAEGetURL event directly in "resume"?
  • Or do you think I would need to register for applicationWillFinishLaunching in "resume" and then register for kAEGetURL?

@madsmtm
Copy link
Member

madsmtm commented Aug 12, 2024

perhaps the create_observer function ought to become public so it can be used in 3rd party libs

It is quite a simple function, and will become even simpler in the future once block2 supports references in arguments. If you really feel you need it, just copy the definition.

Regarding event registration ordering:

  • I'm guessing I ought to put my code in Winit's "resume" event?
  • Do you think I could register for the kAEGetURL event directly in "resume"?
  • Or do you think I would need to register for applicationWillFinishLaunching in "resume" and then register for kAEGetURL?

I don't know anything about the kAEGetURL event, and I'm unfamiliar with these kinds of lower-level events, but it seems probable that the registration needs to happen before the application has launched. You may be able to do it at the top of main (or possibly right after EventLoop::new), but otherwise, doing it in applicationWillFinishLaunching: could be done with something like:

let block = RcBlock::new(|notification: NonNull<NSNotification>| {
    let notification = unsafe { notification.as_ref() };
    // ... Register in here
});
let observer = unsafe {
    center.addObserverForName_object_queue_usingBlock(
        // `applicationWillFinishLaunching:`
        Some(unsafe { NSApplicationWillFinishLaunchingNotification }),
        None,
        None,
        &block,
    )
}
// Keep `observer` around until the end of `main` (or leak it).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Feature request: Custom URI handling
3 participants