-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make command-click open links in default browser
Fixes #20
- Loading branch information
1 parent
26e193a
commit fa73f16
Showing
4 changed files
with
38 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import WebKit | ||
|
||
/// A WKNavigationDelegate that opens links in the default browser | ||
/// when the command key is pressed. | ||
class DefaultBrowserLinkOpener: NSObject, WKNavigationDelegate { | ||
func webView( | ||
_ webView: WKWebView, | ||
decidePolicyFor navigationAction: WKNavigationAction, | ||
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void | ||
) { | ||
if navigationAction.navigationType == .linkActivated { | ||
// Check if the command key is pressed | ||
if navigationAction.modifierFlags.contains(.command) { | ||
if let url = navigationAction.request.url { | ||
// Open the URL in the default browser | ||
NSWorkspace.shared.open(url) | ||
// Cancel the navigation in the WKWebView | ||
decisionHandler(.cancel) | ||
return | ||
} | ||
} | ||
} | ||
// Allow the navigation | ||
decisionHandler(.allow) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters