2019-02-12 07:36:31 +01:00
|
|
|
//
|
|
|
|
// DetailWebViewController.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 2/11/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import AppKit
|
|
|
|
import WebKit
|
2020-03-17 23:18:24 +01:00
|
|
|
import RSCore
|
2019-02-12 07:36:31 +01:00
|
|
|
import RSWeb
|
|
|
|
import Articles
|
|
|
|
|
2020-03-20 14:13:39 +01:00
|
|
|
extension Notification.Name {
|
|
|
|
static let appleColorPreferencesChangedNotification = Notification.Name("AppleColorPreferencesChangedNotification")
|
|
|
|
static let appleInterfaceThemeChangedNotification = Notification.Name("AppleInterfaceThemeChangedNotification")
|
|
|
|
}
|
|
|
|
|
2019-02-13 03:12:12 +01:00
|
|
|
protocol DetailWebViewControllerDelegate: class {
|
2019-02-18 06:43:51 +01:00
|
|
|
func mouseDidEnter(_: DetailWebViewController, link: String)
|
|
|
|
func mouseDidExit(_: DetailWebViewController, link: String)
|
2019-02-13 03:12:12 +01:00
|
|
|
}
|
|
|
|
|
2019-02-12 07:36:31 +01:00
|
|
|
final class DetailWebViewController: NSViewController, WKUIDelegate {
|
|
|
|
|
2019-02-13 03:12:12 +01:00
|
|
|
weak var delegate: DetailWebViewControllerDelegate?
|
2019-09-01 07:10:20 +02:00
|
|
|
var webView: DetailWebView!
|
2019-02-13 07:57:19 +01:00
|
|
|
var state: DetailState = .noSelection {
|
2019-02-12 07:36:31 +01:00
|
|
|
didSet {
|
|
|
|
if state != oldValue {
|
|
|
|
reloadHTML()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-28 00:27:13 +01:00
|
|
|
|
|
|
|
var article: Article? {
|
|
|
|
switch state {
|
|
|
|
case .article(let article):
|
|
|
|
return article
|
|
|
|
case .extracted(let article, _):
|
|
|
|
return article
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-09-17 04:06:35 +02:00
|
|
|
|
2019-09-19 17:38:17 +02:00
|
|
|
#if !MAC_APP_STORE
|
|
|
|
private var webInspectorEnabled: Bool {
|
|
|
|
get {
|
|
|
|
return webView.configuration.preferences._developerExtrasEnabled
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
webView.configuration.preferences._developerExtrasEnabled = newValue
|
|
|
|
}
|
2019-09-17 04:06:35 +02:00
|
|
|
}
|
2019-09-19 17:38:17 +02:00
|
|
|
#endif
|
2019-03-01 23:56:40 +01:00
|
|
|
|
2020-01-27 20:58:32 +01:00
|
|
|
private let detailIconSchemeHandler = DetailIconSchemeHandler()
|
2019-09-01 07:10:20 +02:00
|
|
|
private var waitingForFirstReload = false
|
2019-03-01 23:56:40 +01:00
|
|
|
private let keyboardDelegate = DetailKeyboardDelegate()
|
|
|
|
|
2019-02-13 07:57:19 +01:00
|
|
|
private struct MessageName {
|
|
|
|
static let mouseDidEnter = "mouseDidEnter"
|
|
|
|
static let mouseDidExit = "mouseDidExit"
|
|
|
|
}
|
|
|
|
|
2019-02-12 07:36:31 +01:00
|
|
|
override func loadView() {
|
2019-09-01 07:10:20 +02:00
|
|
|
// Wrap the webview in a box configured with the same background color that the web view uses
|
|
|
|
let box = NSBox(frame: .zero)
|
|
|
|
box.boxType = .custom
|
|
|
|
box.borderType = .noBorder
|
|
|
|
box.titlePosition = .noTitle
|
|
|
|
box.contentViewMargins = .zero
|
|
|
|
box.fillColor = NSColor(named: "webviewBackgroundColor")!
|
|
|
|
|
|
|
|
view = box
|
|
|
|
|
2019-02-12 07:36:31 +01:00
|
|
|
let preferences = WKPreferences()
|
|
|
|
preferences.minimumFontSize = 12.0
|
|
|
|
preferences.javaScriptCanOpenWindowsAutomatically = false
|
|
|
|
preferences.javaScriptEnabled = true
|
|
|
|
|
|
|
|
let configuration = WKWebViewConfiguration()
|
|
|
|
configuration.preferences = preferences
|
2020-01-27 20:58:32 +01:00
|
|
|
configuration.setURLSchemeHandler(detailIconSchemeHandler, forURLScheme: ArticleRenderer.imageIconScheme)
|
2019-02-12 07:36:31 +01:00
|
|
|
|
|
|
|
let userContentController = WKUserContentController()
|
|
|
|
userContentController.add(self, name: MessageName.mouseDidEnter)
|
|
|
|
userContentController.add(self, name: MessageName.mouseDidExit)
|
|
|
|
configuration.userContentController = userContentController
|
|
|
|
|
2019-09-01 07:10:20 +02:00
|
|
|
webView = DetailWebView(frame: NSRect.zero, configuration: configuration)
|
|
|
|
webView.uiDelegate = self
|
|
|
|
webView.navigationDelegate = self
|
|
|
|
webView.keyboardDelegate = keyboardDelegate
|
|
|
|
webView.translatesAutoresizingMaskIntoConstraints = false
|
2019-02-12 07:36:31 +01:00
|
|
|
if let userAgent = UserAgent.fromInfoPlist() {
|
2019-09-01 07:10:20 +02:00
|
|
|
webView.customUserAgent = userAgent
|
2019-02-12 07:36:31 +01:00
|
|
|
}
|
2019-02-13 07:57:19 +01:00
|
|
|
|
2019-09-01 07:10:20 +02:00
|
|
|
box.addSubview(webView)
|
|
|
|
|
|
|
|
let constraints = [
|
|
|
|
webView.topAnchor.constraint(equalTo: view.topAnchor),
|
|
|
|
webView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
|
|
webView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
webView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
|
|
]
|
|
|
|
|
|
|
|
NSLayoutConstraint.activate(constraints)
|
|
|
|
|
|
|
|
// Hide the web view until the first reload (navigation) is complete (plus some delay) to avoid the awful white flash that happens on the initial display in dark mode.
|
|
|
|
// See bug #901.
|
|
|
|
webView.isHidden = true
|
|
|
|
waitingForFirstReload = true
|
|
|
|
|
2019-09-19 17:38:17 +02:00
|
|
|
#if !MAC_APP_STORE
|
2020-07-02 05:17:38 +02:00
|
|
|
webInspectorEnabled = AppDefaults.shared.webInspectorEnabled
|
2019-09-19 17:38:17 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(webInspectorEnabledDidChange(_:)), name: .WebInspectorEnabledDidChange, object: nil)
|
|
|
|
#endif
|
2019-09-17 04:06:35 +02:00
|
|
|
|
2019-11-19 02:44:31 +01:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(webFeedIconDidBecomeAvailable(_:)), name: .WebFeedIconDidBecomeAvailable, object: nil)
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(avatarDidBecomeAvailable(_:)), name: .AvatarDidBecomeAvailable, object: nil)
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(faviconDidBecomeAvailable(_:)), name: .FaviconDidBecomeAvailable, object: nil)
|
2020-03-20 14:13:39 +01:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(faviconDidBecomeAvailable(_:)), name: .FaviconDidBecomeAvailable, object: nil)
|
|
|
|
|
|
|
|
DistributedNotificationCenter.default().addObserver(self, selector: #selector(appleColorPreferencesChanged(_:)), name: .appleColorPreferencesChangedNotification, object: nil)
|
|
|
|
DistributedNotificationCenter.default().addObserver(self, selector: #selector(appleInterfaceThemeChanged(_:)), name: .appleInterfaceThemeChangedNotification, object: nil)
|
2019-11-19 02:44:31 +01:00
|
|
|
|
2020-03-17 23:18:24 +01:00
|
|
|
webView.loadFileURL(ArticleRenderer.blank.url, allowingReadAccessTo: ArticleRenderer.blank.baseURL)
|
2019-02-12 07:36:31 +01:00
|
|
|
}
|
|
|
|
|
2019-11-19 02:44:31 +01:00
|
|
|
// MARK: Notifications
|
|
|
|
|
|
|
|
@objc func webFeedIconDidBecomeAvailable(_ note: Notification) {
|
|
|
|
reloadArticleImage()
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func avatarDidBecomeAvailable(_ note: Notification) {
|
|
|
|
reloadArticleImage()
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func faviconDidBecomeAvailable(_ note: Notification) {
|
|
|
|
reloadArticleImage()
|
|
|
|
}
|
|
|
|
|
2020-03-20 14:13:39 +01:00
|
|
|
@objc func appleColorPreferencesChanged(_ note: Notification) {
|
|
|
|
reloadHTML()
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func appleInterfaceThemeChanged(_ note: Notification) {
|
|
|
|
reloadHTML()
|
|
|
|
}
|
|
|
|
|
2020-02-27 21:15:41 +01:00
|
|
|
// MARK: Media Functions
|
|
|
|
|
|
|
|
func stopMediaPlayback() {
|
|
|
|
webView.evaluateJavaScript("stopMediaPlayback();")
|
|
|
|
}
|
|
|
|
|
2019-02-13 07:57:19 +01:00
|
|
|
// MARK: Scrolling
|
|
|
|
|
2019-12-15 02:01:34 +01:00
|
|
|
func canScrollDown(_ completion: @escaping (Bool) -> Void) {
|
2019-02-13 07:57:19 +01:00
|
|
|
fetchScrollInfo { (scrollInfo) in
|
2019-12-15 02:01:34 +01:00
|
|
|
completion(scrollInfo?.canScrollDown ?? false)
|
2019-02-13 07:57:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 20:33:25 +02:00
|
|
|
func canScrollUp(_ completion: @escaping (Bool) -> Void) {
|
|
|
|
fetchScrollInfo { (scrollInfo) in
|
|
|
|
completion(scrollInfo?.canScrollUp ?? false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 07:57:19 +01:00
|
|
|
override func scrollPageDown(_ sender: Any?) {
|
2019-09-01 07:10:20 +02:00
|
|
|
webView.scrollPageDown(sender)
|
2019-02-12 07:36:31 +01:00
|
|
|
}
|
2020-07-10 20:33:25 +02:00
|
|
|
|
|
|
|
override func scrollPageUp(_ sender: Any?) {
|
|
|
|
webView.scrollPageUp(sender)
|
|
|
|
}
|
2019-02-12 07:36:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - WKScriptMessageHandler
|
|
|
|
|
|
|
|
extension DetailWebViewController: WKScriptMessageHandler {
|
|
|
|
|
|
|
|
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
|
|
|
if message.name == MessageName.mouseDidEnter, let link = message.body as? String {
|
2019-02-18 06:43:51 +01:00
|
|
|
delegate?.mouseDidEnter(self, link: link)
|
2019-02-12 07:36:31 +01:00
|
|
|
}
|
|
|
|
else if message.name == MessageName.mouseDidExit, let link = message.body as? String{
|
2019-02-18 06:43:51 +01:00
|
|
|
delegate?.mouseDidExit(self, link: link)
|
2019-02-12 07:36:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - WKNavigationDelegate
|
|
|
|
|
|
|
|
extension DetailWebViewController: WKNavigationDelegate {
|
|
|
|
|
|
|
|
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
|
|
|
|
if navigationAction.navigationType == .linkActivated {
|
|
|
|
if let url = navigationAction.request.url {
|
2020-05-21 03:08:13 +02:00
|
|
|
let flags = navigationAction.modifierFlags
|
|
|
|
let invert = flags.contains(.shift) || flags.contains(.command)
|
|
|
|
Browser.open(url.absoluteString, invertPreference: invert)
|
2019-02-12 07:36:31 +01:00
|
|
|
}
|
|
|
|
decisionHandler(.cancel)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decisionHandler(.allow)
|
|
|
|
}
|
2019-09-01 07:10:20 +02:00
|
|
|
|
|
|
|
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
|
|
|
// See note in viewDidLoad()
|
|
|
|
if waitingForFirstReload {
|
|
|
|
assert(webView.isHidden)
|
|
|
|
waitingForFirstReload = false
|
2019-09-21 02:28:13 +02:00
|
|
|
reloadHTML()
|
2019-09-01 07:10:20 +02:00
|
|
|
|
|
|
|
// Waiting for the first navigation to complete isn't long enough to avoid the flash of white.
|
|
|
|
// A hard coded value is awful, but 5/100th of a second seems to be enough.
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
|
|
|
|
webView.isHidden = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-12 07:36:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
|
|
private extension DetailWebViewController {
|
|
|
|
|
2019-11-19 02:44:31 +01:00
|
|
|
func reloadArticleImage() {
|
2020-01-28 00:27:13 +01:00
|
|
|
guard let article = article else { return }
|
2020-02-09 22:08:11 +01:00
|
|
|
|
|
|
|
var components = URLComponents()
|
|
|
|
components.scheme = ArticleRenderer.imageIconScheme
|
|
|
|
components.path = article.articleID
|
|
|
|
|
|
|
|
if let imageSrc = components.string {
|
|
|
|
webView?.evaluateJavaScript("reloadArticleImage(\"\(imageSrc)\")")
|
|
|
|
}
|
2019-11-19 02:44:31 +01:00
|
|
|
}
|
|
|
|
|
2019-02-12 07:36:31 +01:00
|
|
|
func reloadHTML() {
|
|
|
|
let style = ArticleStylesManager.shared.currentStyle
|
2019-09-21 19:36:35 +02:00
|
|
|
let rendering: ArticleRenderer.Rendering
|
2019-02-12 07:36:31 +01:00
|
|
|
|
|
|
|
switch state {
|
|
|
|
case .noSelection:
|
2019-09-21 02:28:13 +02:00
|
|
|
rendering = ArticleRenderer.noSelectionHTML(style: style)
|
2019-02-12 07:36:31 +01:00
|
|
|
case .multipleSelection:
|
2019-09-21 02:28:13 +02:00
|
|
|
rendering = ArticleRenderer.multipleSelectionHTML(style: style)
|
2019-09-21 22:03:42 +02:00
|
|
|
case .loading:
|
|
|
|
rendering = ArticleRenderer.loadingHTML(style: style)
|
2019-02-12 07:36:31 +01:00
|
|
|
case .article(let article):
|
2020-01-27 20:58:32 +01:00
|
|
|
detailIconSchemeHandler.currentArticle = article
|
2019-09-21 02:28:13 +02:00
|
|
|
rendering = ArticleRenderer.articleHTML(article: article, style: style)
|
2019-09-19 01:15:55 +02:00
|
|
|
case .extracted(let article, let extractedArticle):
|
2020-01-27 20:58:32 +01:00
|
|
|
detailIconSchemeHandler.currentArticle = article
|
2019-09-21 02:28:13 +02:00
|
|
|
rendering = ArticleRenderer.articleHTML(article: article, extractedArticle: extractedArticle, style: style)
|
2019-02-12 07:36:31 +01:00
|
|
|
}
|
2019-09-19 07:03:23 +02:00
|
|
|
|
2020-03-17 23:18:24 +01:00
|
|
|
let substitutions = [
|
|
|
|
"title": rendering.title,
|
|
|
|
"baseURL": rendering.baseURL,
|
|
|
|
"style": rendering.style,
|
|
|
|
"body": rendering.html
|
|
|
|
]
|
2019-09-19 07:03:23 +02:00
|
|
|
|
2020-03-17 23:18:24 +01:00
|
|
|
let html = try! MacroProcessor.renderedText(withTemplate: ArticleRenderer.page.html, substitutions: substitutions)
|
|
|
|
webView.loadHTMLString(html, baseURL: ArticleRenderer.page.baseURL)
|
2019-02-12 07:36:31 +01:00
|
|
|
}
|
2019-02-13 07:57:19 +01:00
|
|
|
|
2019-12-15 02:01:34 +01:00
|
|
|
func fetchScrollInfo(_ completion: @escaping (ScrollInfo?) -> Void) {
|
2019-10-19 02:37:56 +02:00
|
|
|
var javascriptString = "var x = {contentHeight: document.body.scrollHeight, offsetY: document.body.scrollTop}; x"
|
|
|
|
if #available(macOS 10.15, *) {
|
|
|
|
javascriptString = "var x = {contentHeight: document.body.scrollHeight, offsetY: window.pageYOffset}; x"
|
|
|
|
}
|
2019-02-13 07:57:19 +01:00
|
|
|
|
2019-09-01 07:10:20 +02:00
|
|
|
webView.evaluateJavaScript(javascriptString) { (info, error) in
|
2019-02-13 07:57:19 +01:00
|
|
|
guard let info = info as? [String: Any] else {
|
2019-12-15 02:01:34 +01:00
|
|
|
completion(nil)
|
2019-02-13 07:57:19 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let contentHeight = info["contentHeight"] as? CGFloat, let offsetY = info["offsetY"] as? CGFloat else {
|
2019-12-15 02:01:34 +01:00
|
|
|
completion(nil)
|
2019-02-13 07:57:19 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-01 07:10:20 +02:00
|
|
|
let scrollInfo = ScrollInfo(contentHeight: contentHeight, viewHeight: self.webView.frame.height, offsetY: offsetY)
|
2019-12-15 02:01:34 +01:00
|
|
|
completion(scrollInfo)
|
2019-02-13 07:57:19 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-17 11:41:57 +02:00
|
|
|
|
2019-09-19 17:38:17 +02:00
|
|
|
#if !MAC_APP_STORE
|
|
|
|
@objc func webInspectorEnabledDidChange(_ notification: Notification) {
|
|
|
|
self.webInspectorEnabled = notification.object! as! Bool
|
|
|
|
}
|
|
|
|
#endif
|
2019-02-12 07:43:04 +01:00
|
|
|
}
|
|
|
|
|
2019-02-13 07:57:19 +01:00
|
|
|
// MARK: - ScrollInfo
|
|
|
|
|
|
|
|
private struct ScrollInfo {
|
|
|
|
|
|
|
|
let contentHeight: CGFloat
|
|
|
|
let viewHeight: CGFloat
|
|
|
|
let offsetY: CGFloat
|
|
|
|
let canScrollDown: Bool
|
|
|
|
let canScrollUp: Bool
|
|
|
|
|
|
|
|
init(contentHeight: CGFloat, viewHeight: CGFloat, offsetY: CGFloat) {
|
|
|
|
self.contentHeight = contentHeight
|
|
|
|
self.viewHeight = viewHeight
|
|
|
|
self.offsetY = offsetY
|
|
|
|
|
|
|
|
self.canScrollDown = viewHeight + offsetY < contentHeight
|
|
|
|
self.canScrollUp = offsetY > 0.1
|
|
|
|
}
|
|
|
|
}
|