Add context menu previews

This commit is contained in:
Shadowfacts 2020-12-20 14:39:26 -05:00
parent 444c4c053d
commit a0a12f3865
1 changed files with 24 additions and 0 deletions

View File

@ -75,6 +75,7 @@ class BrowserWebViewController: UIViewController {
webView.backgroundColor = .systemBackground
webView.isOpaque = false
webView.navigationDelegate = self
webView.uiDelegate = self
// it is safe to set the delegate of the web view's internal scroll view becuase WebKit takes care of forwarding between its internal delegate and our own
webView.scrollView.delegate = scrollViewDelegate
// this doesn't default to .default :S
@ -247,3 +248,26 @@ extension BrowserWebViewController: WKNavigationDelegate {
}
}
}
extension BrowserWebViewController: WKUIDelegate {
func webView(_ webView: WKWebView, contextMenuConfigurationForElement elementInfo: WKContextMenuElementInfo, completionHandler: @escaping (UIContextMenuConfiguration?) -> Void) {
guard let url = elementInfo.linkURL,
url.scheme == "gemini" else {
completionHandler(nil)
return
}
let config = UIContextMenuConfiguration(identifier: nil) {
return BrowserWebViewController(navigator: self.navigator, url: url)
} actionProvider: { (_) in
return nil
}
completionHandler(config)
}
func webView(_ webView: WKWebView, contextMenuForElement elementInfo: WKContextMenuElementInfo, willCommitWithAnimator animator: UIContextMenuInteractionCommitAnimating) {
animator.preferredCommitStyle = .pop
animator.addCompletion {
self.navigator.changeURL(elementInfo.linkURL!)
}
}
}