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: Use system API to open links #5256

Open
mitchellh opened this issue Jan 20, 2025 · 7 comments
Open

macOS: Use system API to open links #5256

mitchellh opened this issue Jan 20, 2025 · 7 comments
Labels

Comments

@mitchellh
Copy link
Contributor

Discussed in #5200

Originally posted by sindresorhus January 18, 2025
Currently, it uses the open command. The problem with using that is that it makes it impossible for browser pickers apps to detect that the link is opened from Ghostty, because by the time the app checks for the source app, the open command has exited. It should also be slightly faster to use the system API: https://developer.apple.com/documentation/appkit/nsworkspace/open(_:)?language=objc

I'm referring to: https://github.com/ghostty-org/ghostty/blob/72d085525b22d66468c5969a4d507a0fa68d4a04/src/os/open.zig#L32C9-L41C11


Specifically, Velja uses the source app to let users create advanced rules for which browser to use depending on the source app.

@shaps80
Copy link
Contributor

shaps80 commented Feb 4, 2025

Given this is a native app we could actually use SwiftUI's openURL from the Environment. This also ensures future compatibility with iOS too. This also simplifies URL overriding (if required) in SwiftUI.

Specifically I'm referring to:
https://developer.apple.com/documentation/swiftui/environmentvalues/openurl

@matthebrown
Copy link
Contributor

I was interested in taking a crack at this feature but was looking for some more clarity on the required changes. Would the approach here involve invoking the NSWorkspace.open method from the existing open.zig file like below? Or are we talking about handling links in the native SwiftUI?

const NSWorkspace = objc.getClass("NSWorkspace");
const shared = NSWorkspace.msgSend(objc.Object, objc.sel("shared"));
 _ = shared.msgSend(bool, objc.sel("open:"), .{ url });

@shaps80
Copy link
Contributor

shaps80 commented Feb 17, 2025

@matthebrown personally my preference would be the safer high-level APIs provided by SwiftUI.

Was there any reason specifically you wanted implement it using older Obj-C msgSend APIs?

@matthebrown
Copy link
Contributor

That makes sense, I'm just a little unsure what to do with this existing logic in open.zig. I guess we need to keep it as-is since it's also used to open text.

@jcollie
Copy link
Collaborator

jcollie commented Feb 17, 2025

Perhaps we need to convert opening URLs into an apprt action. That way it would be easier for each apprt to implement native experiences, plus make it easier for macOS to use native Swift code to implement the action.

@mitchellh
Copy link
Contributor Author

mitchellh commented Feb 17, 2025

I think converting this to an apprt action makes sense. The open.zig logic predates apprt actions and the previous callback system was relatively limited, so I think given the current state of things an action makes sense.

@shaps80
Copy link
Contributor

shaps80 commented Feb 19, 2025

FWIW I agree, I like the idea that it allows each platform to handle things more appropriately as well 👍

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

No branches or pull requests

4 participants