Context menu support: starting with open in new tab

This commit is contained in:
James Magahern
2020-09-22 12:45:50 -07:00
parent 2cdee87e8a
commit c223c12934

View File

@@ -364,6 +364,31 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
return newTab.webView 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 // MARK: UITextField Delegate
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {