From c223c12934ad78745fdde643b47a60dc254f4158 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Tue, 22 Sep 2020 12:45:50 -0700 Subject: [PATCH] Context menu support: starting with open in new tab --- App/Browser View/BrowserViewController.swift | 25 ++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/App/Browser View/BrowserViewController.swift b/App/Browser View/BrowserViewController.swift index eb53d38..5912ead 100644 --- a/App/Browser View/BrowserViewController.swift +++ b/App/Browser View/BrowserViewController.swift @@ -364,6 +364,31 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat return newTab.webView } + func webView(_ webView: WKWebView, contextMenuConfigurationForElement elementInfo: WKContextMenuElementInfo, completionHandler: @escaping (UIContextMenuConfiguration?) -> Void) { + let menuConfig = UIContextMenuConfiguration(identifier: nil, + previewProvider: nil) { (menuElements: [UIMenuElement]) -> UIMenu? in + + let openInNewTab = UIAction(title: "Open in New Tab", + image: UIImage(systemName: "plus.app"), + identifier: nil, + discoverabilityTitle: nil, + attributes: [], + state: .off) { [unowned self] _ in + let newTab = tabController.createNewTab(url: elementInfo.linkURL) + self.tab = newTab + } + + + return UIMenu(title: elementInfo.linkURL?.absoluteString ?? "Link", + image: nil, + identifier: nil, + options: .displayInline, + children: [ openInNewTab ] + menuElements) + } + + completionHandler(menuConfig) + } + // MARK: UITextField Delegate func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {