Skip to content

Commit

Permalink
Make command-click open links in default browser
Browse files Browse the repository at this point in the history
Fixes #20
  • Loading branch information
mr-pennyworth committed Aug 6, 2024
1 parent 26e193a commit fa73f16
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
8 changes: 6 additions & 2 deletions AlfredExtraPane.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
D011BDA02C62557400EBF4A0 /* DefaultBrowserLinkOpener.swift in Sources */ = {isa = PBXBuildFile; fileRef = D011BD9F2C62557400EBF4A0 /* DefaultBrowserLinkOpener.swift */; };
D01646BA255AF27A00646F0C /* utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = D01646B9255AF27A00646F0C /* utils.swift */; };
D03EA8D325551E0400D3656E /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = D03EA8D225551E0400D3656E /* main.swift */; };
D03EA8D725551E0700D3656E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D03EA8D625551E0700D3656E /* Assets.xcassets */; };
Expand Down Expand Up @@ -55,6 +56,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
D011BD9F2C62557400EBF4A0 /* DefaultBrowserLinkOpener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultBrowserLinkOpener.swift; sourceTree = "<group>"; };
D01646B9255AF27A00646F0C /* utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = utils.swift; sourceTree = "<group>"; };
D03EA8CF25551E0400D3656E /* AlfredExtraPane.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AlfredExtraPane.app; sourceTree = BUILT_PRODUCTS_DIR; };
D03EA8D225551E0400D3656E /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -125,6 +127,7 @@
D08B6EE6266F9B520099EB36 /* PanePositionCodable.swift */,
D0FCE9402C40049200F5006C /* Menu.swift */,
D0FCE9422C400A7600F5006C /* WebViewInjection.swift */,
D011BD9F2C62557400EBF4A0 /* DefaultBrowserLinkOpener.swift */,
);
path = AlfredExtraPane;
sourceTree = "<group>";
Expand Down Expand Up @@ -258,6 +261,7 @@
D03EA8D325551E0400D3656E /* main.swift in Sources */,
D08B6EE5266F31790099EB36 /* Pane.swift in Sources */,
D04F70D5255893B3008E17A4 /* NSColorExtension.swift in Sources */,
D011BDA02C62557400EBF4A0 /* DefaultBrowserLinkOpener.swift in Sources */,
D08B6EE7266F9B520099EB36 /* PanePositionCodable.swift in Sources */,
D0FCE9432C400A7600F5006C /* WebViewInjection.swift in Sources */,
D07349462C541D6300F004C8 /* PanePosition.swift in Sources */,
Expand Down Expand Up @@ -413,7 +417,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 11.0;
MARKETING_VERSION = 0.2.6;
MARKETING_VERSION = 0.2.7;
ONLY_ACTIVE_ARCH = NO;
OTHER_CODE_SIGN_FLAGS = "--deep";
PRODUCT_BUNDLE_IDENTIFIER = mr.pennyworth.AlfredExtraPane;
Expand All @@ -434,7 +438,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 11.0;
MARKETING_VERSION = 0.2.6;
MARKETING_VERSION = 0.2.7;
ONLY_ACTIVE_ARCH = NO;
OTHER_CODE_SIGN_FLAGS = "--deep";
PRODUCT_BUNDLE_IDENTIFIER = mr.pennyworth.AlfredExtraPane;
Expand Down
26 changes: 26 additions & 0 deletions AlfredExtraPane/DefaultBrowserLinkOpener.swift
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)
}
}
2 changes: 2 additions & 0 deletions AlfredExtraPane/Pane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Pane {
let window: NSWindow = makeWindow()
let margin: CGFloat = 5

private var webViewNavigationDelegate = DefaultBrowserLinkOpener()
private lazy var webView: WKWebView = {
makeWebView(
WorkflowPaneConfig(paneConfig: config, workflowUID: workflowUID)
Expand All @@ -54,6 +55,7 @@ class Pane {
self.config = workflowPaneConfig.paneConfig
self.workflowUID = workflowPaneConfig.workflowUID
window.contentView!.addSubview(webView)
webView.navigationDelegate = webViewNavigationDelegate

if let staticConf = self.config.staticPaneConfig {
webView.load(URLRequest(url: staticConf.initURL))
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ curl -sL https://raw.githubusercontent.com/mr-pennyworth/alfred-extra-pane/main/
- The warning dialog is different this time. It now allows to open the app.
Click `Open`: ![](media/install-attempt-2-result.png)

## Features
- To open a link displayed in the pane in the default browser, hold down
the command key (``) and click on the link.

## Configuration
The global pane(s) can be configured by editing
`{/path/to}/Alfred.alfredpreferences/preferences/mr.pennyworth.AlfredExtraPane/config.json`.
Expand Down

0 comments on commit fa73f16

Please sign in to comment.