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 command-click and option-click on links #1803

Merged
merged 5 commits into from
Aug 31, 2024
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
12 changes: 12 additions & 0 deletions Vienna/Sources/Main window/Browser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,20 @@ protocol Browser {
/// - load: whether the page to which the URL points is supposed to be loaded immediately
/// (otherwise it is opened when opening the tab)
/// - Returns: the new tab
@discardableResult
func createNewTab(_ url: URL?, inBackground: Bool, load: Bool) -> any Tab

/// Add a new tab to the open tabs of the browser, after the currently selected one
///
/// - Parameters:
/// - url: optional URL for the new tab
/// - inBackground: if the tab shall stay unselected
/// - load: whether the page to which the URL points is supposed to be loaded immediately
/// (otherwise it is opened when opening the tab)
/// - Returns: the new tab
@discardableResult
func createNewTabAfterSelected(_ url: URL?, inBackground: Bool, load: Bool) -> any Tab
barijaona marked this conversation as resolved.
Show resolved Hide resolved

/// Saves all tabs persistently.
/// Next time when instanciating the browser, these tabs will be re-instanciated as well.
func saveOpenTabs()
Expand Down
18 changes: 18 additions & 0 deletions Vienna/Sources/Main window/BrowserTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,24 @@ extension BrowserTab: Tab {

extension BrowserTab: WKNavigationDelegate {

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .linkActivated {
let commandKey = navigationAction.modifierFlags.contains(.command)
let optionKey = navigationAction.modifierFlags.contains(.option)
if commandKey {
decisionHandler(.cancel)
NSApp.appController.browser.createNewTabAfterSelected(navigationAction.request.url, inBackground: true, load: true)
} else if optionKey {
decisionHandler(.cancel)
NSApp.appController.open(navigationAction.request.url, inPreferredBrowser: false)
} else {
decisionHandler(.allow)
}
} else {
decisionHandler(.allow)
}
}

func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
if navigationResponse.canShowMIMEType {
decisionHandler(.allow)
Expand Down
23 changes: 8 additions & 15 deletions Vienna/Sources/Main window/TabbedBrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,17 @@ class TabbedBrowserViewController: NSViewController, RSSSource {
}

extension TabbedBrowserViewController: Browser {
@discardableResult
func createNewTab(_ url: URL?, inBackground: Bool, load: Bool) -> any Tab {
createNewTab(url, inBackground: inBackground, load: load, insertAt: nil)
}

func createNewTab(_ request: URLRequest, config: WKWebViewConfiguration, inBackground: Bool, insertAt index: Int? = nil) -> any Tab {
let newTab = BrowserTab(request, config: config)
return initNewTab(newTab, request.url, false, inBackground, insertAt: index)
@discardableResult
func createNewTabAfterSelected(_ url: URL?, inBackground: Bool, load: Bool) -> any Tab {
barijaona marked this conversation as resolved.
Show resolved Hide resolved
createNewTab(url, inBackground: inBackground, load: load, insertAt: getIndexAfterSelected())
}

@discardableResult
func createNewTab(_ url: URL? = nil, inBackground: Bool = false, load: Bool = false, insertAt index: Int? = nil) -> any Tab {
let newTab = BrowserTab()
return initNewTab(newTab, url, load, inBackground, insertAt: index)
Expand Down Expand Up @@ -297,7 +299,7 @@ extension TabbedBrowserViewController: MMTabBarViewDelegate {
}

func addNewTab(to aTabView: NSTabView) {
_ = self.createNewTab()
self.createNewTab()
}

func tabView(_ tabView: NSTabView, willSelect tabViewItem: NSTabViewItem?) {
Expand All @@ -323,15 +325,6 @@ extension TabbedBrowserViewController: CustomWKUIDelegate {

private static var contextMenuCustomizer: any BrowserContextMenuDelegate = WebKitContextMenuCustomizer()

func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
let newTab = self.createNewTab(navigationAction.request, config: configuration, inBackground: false, insertAt: getIndexAfterSelected())
if let webView = webView as? CustomWKWebView {
// The listeners are removed from the old webview userContentController on creating the new one, restore them
webView.resetScriptListeners()
}
return (newTab as? BrowserTab)?.webView
}

barijaona marked this conversation as resolved.
Show resolved Hide resolved
func contextMenuItemsFor(purpose: WKWebViewContextMenuContext, existingMenuItems: [NSMenuItem]) -> [NSMenuItem] {
// specific customization of menuItems may be added here
// using the following commented out construct
Expand All @@ -348,9 +341,9 @@ extension TabbedBrowserViewController: CustomWKUIDelegate {
}
switch menuItem.identifier {
case NSUserInterfaceItemIdentifier.WKMenuItemOpenLinkInBackground:
_ = self.createNewTab(url, inBackground: true, load: true, insertAt: getIndexAfterSelected())
self.createNewTabAfterSelected(url, inBackground: true, load: true)
case NSUserInterfaceItemIdentifier.WKMenuItemOpenLinkInNewWindow, NSUserInterfaceItemIdentifier.WKMenuItemOpenImageInNewWindow, NSUserInterfaceItemIdentifier.WKMenuItemOpenMediaInNewWindow:
_ = self.createNewTab(url, inBackground: false, load: true, insertAt: getIndexAfterSelected())
self.createNewTabAfterSelected(url, inBackground: false, load: true)
case NSUserInterfaceItemIdentifier.WKMenuItemOpenLinkInSystemBrowser:
NSApp.appController.openURL(inDefaultBrowser: url)
case NSUserInterfaceItemIdentifier.WKMenuItemDownloadImage, NSUserInterfaceItemIdentifier.WKMenuItemDownloadMedia, NSUserInterfaceItemIdentifier.WKMenuItemDownloadLinkedFile:
Expand Down
2 changes: 1 addition & 1 deletion Vienna/Sources/Main window/WebKitArticleTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class WebKitArticleTab: BrowserTab, ArticleContentView {

// MARK: Navigation delegate

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
override func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
// TODO: how do forms work in the article view?
// i.e. navigationAction.navigationType == .formSubmitted or .formResubmitted
// TODO: in the future, we might want to allow limited browsing in the primary tab
Expand Down
4 changes: 2 additions & 2 deletions Vienna/Sources/Main window/WebKitArticleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ class WebKitArticleView: CustomWKWebView, ArticleContentView, WKNavigationDelega
}
switch menuItem.identifier {
case NSUserInterfaceItemIdentifier.WKMenuItemOpenLinkInBackground:
_ = NSApp.appController.browser.createNewTab(url, inBackground: true, load: true)
NSApp.appController.browser.createNewTab(url, inBackground: true, load: true)
case NSUserInterfaceItemIdentifier.WKMenuItemOpenLinkInNewWindow, NSUserInterfaceItemIdentifier.WKMenuItemOpenImageInNewWindow, NSUserInterfaceItemIdentifier.WKMenuItemOpenMediaInNewWindow:
_ = NSApp.appController.browser.createNewTab(url, inBackground: false, load: true)
NSApp.appController.browser.createNewTab(url, inBackground: false, load: true)
case NSUserInterfaceItemIdentifier.WKMenuItemOpenLinkInSystemBrowser:
NSApp.appController.openURL(inDefaultBrowser: url)
case NSUserInterfaceItemIdentifier.WKMenuItemDownloadImage, NSUserInterfaceItemIdentifier.WKMenuItemDownloadMedia, NSUserInterfaceItemIdentifier.WKMenuItemDownloadLinkedFile:
Expand Down