2017-05-27 19:43:27 +02:00
|
|
|
//
|
|
|
|
// DetailViewController.swift
|
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 7/26/15.
|
|
|
|
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import WebKit
|
|
|
|
import RSCore
|
2017-09-17 21:22:15 +02:00
|
|
|
import Data
|
2017-12-03 21:13:44 +01:00
|
|
|
import RSWeb
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
class DetailViewController: NSViewController, WKNavigationDelegate, WKUIDelegate {
|
2017-11-06 05:31:50 +01:00
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
var webview: WKWebView!
|
2017-12-03 21:38:44 +01:00
|
|
|
var visualEffectView: NSVisualEffectView!
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
var article: Article? {
|
|
|
|
didSet {
|
|
|
|
reloadHTML()
|
2017-12-03 21:38:44 +01:00
|
|
|
showOrHideWebView()
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
}
|
2017-11-06 05:31:50 +01:00
|
|
|
|
|
|
|
private struct MessageName {
|
|
|
|
static let mouseDidEnter = "mouseDidEnter"
|
|
|
|
static let mouseDidExit = "mouseDidExit"
|
|
|
|
}
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(timelineSelectionDidChange(_:)), name: .TimelineSelectionDidChange, object: nil)
|
|
|
|
|
|
|
|
let preferences = WKPreferences()
|
|
|
|
preferences.minimumFontSize = 12.0
|
|
|
|
preferences.javaScriptCanOpenWindowsAutomatically = false
|
|
|
|
preferences.javaEnabled = false
|
|
|
|
preferences.javaScriptEnabled = true
|
|
|
|
preferences.plugInsEnabled = false
|
|
|
|
|
|
|
|
let configuration = WKWebViewConfiguration()
|
|
|
|
configuration.preferences = preferences
|
|
|
|
|
2017-11-06 05:31:50 +01:00
|
|
|
let userContentController = WKUserContentController()
|
|
|
|
userContentController.add(self, name: MessageName.mouseDidEnter)
|
|
|
|
userContentController.add(self, name: MessageName.mouseDidExit)
|
|
|
|
configuration.userContentController = userContentController
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
webview = WKWebView(frame: self.view.bounds, configuration: configuration)
|
|
|
|
webview.uiDelegate = self
|
|
|
|
webview.navigationDelegate = self
|
|
|
|
webview.translatesAutoresizingMaskIntoConstraints = false
|
2017-12-03 21:13:44 +01:00
|
|
|
if let userAgent = UserAgent.fromInfoPlist() {
|
|
|
|
webview.customUserAgent = userAgent
|
|
|
|
}
|
|
|
|
|
2017-12-03 21:38:44 +01:00
|
|
|
visualEffectView = NSVisualEffectView(frame: self.view.bounds)
|
|
|
|
visualEffectView.material = .appearanceBased
|
|
|
|
visualEffectView.blendingMode = .behindWindow
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
let boxView = self.view as! DetailBox
|
|
|
|
boxView.viewController = self
|
2017-12-03 21:38:44 +01:00
|
|
|
|
|
|
|
showOrHideWebView()
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Notifications
|
|
|
|
|
2017-09-17 21:54:08 +02:00
|
|
|
@objc func timelineSelectionDidChange(_ note: Notification) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2017-09-24 21:24:44 +02:00
|
|
|
let timelineView = note.appInfo?.view
|
|
|
|
if timelineView?.window === self.view.window {
|
|
|
|
article = note.appInfo?.article
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func viewWillStartLiveResize() {
|
|
|
|
|
|
|
|
webview.evaluateJavaScript("document.body.style.overflow = 'hidden';", completionHandler: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func viewDidEndLiveResize() {
|
|
|
|
|
|
|
|
webview.evaluateJavaScript("document.body.style.overflow = 'visible';", completionHandler: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private func reloadHTML() {
|
|
|
|
|
|
|
|
if let article = article {
|
2017-09-23 22:59:19 +02:00
|
|
|
let articleRenderer = ArticleRenderer(article: article, style: ArticleStylesManager.shared.currentStyle)
|
2017-05-27 19:43:27 +02:00
|
|
|
webview.loadHTMLString(articleRenderer.html, baseURL: articleRenderer.baseURL)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
webview.loadHTMLString("", baseURL: nil)
|
|
|
|
}
|
|
|
|
}
|
2017-12-03 21:38:44 +01:00
|
|
|
|
|
|
|
private func showOrHideWebView() {
|
|
|
|
|
|
|
|
if let _ = article {
|
|
|
|
switchToView(webview)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
switchToView(visualEffectView)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func switchToView(_ view: NSView) {
|
|
|
|
|
|
|
|
let boxView = self.view as! DetailBox
|
|
|
|
if boxView.contentView == view {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
boxView.contentView = view
|
|
|
|
boxView.rs_addFullSizeConstraints(forSubview: view)
|
|
|
|
}
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
// MARK: WKNavigationDelegate
|
|
|
|
|
|
|
|
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
|
|
|
|
|
|
|
|
if navigationAction.navigationType == .linkActivated {
|
|
|
|
|
|
|
|
if let url = navigationAction.request.url {
|
2017-10-06 03:12:58 +02:00
|
|
|
Browser.open(url.absoluteString)
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
decisionHandler(.cancel)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decisionHandler(.allow)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-06 05:31:50 +01:00
|
|
|
extension DetailViewController: WKScriptMessageHandler {
|
|
|
|
|
|
|
|
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
|
|
|
|
|
|
|
if message.name == MessageName.mouseDidEnter, let link = message.body as? String {
|
|
|
|
mouseDidEnter(link)
|
|
|
|
}
|
|
|
|
else if message.name == MessageName.mouseDidExit, let link = message.body as? String{
|
|
|
|
mouseDidExit(link)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func mouseDidEnter(_ link: String) {
|
|
|
|
|
|
|
|
if link.isEmpty {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let appInfo = AppInfo()
|
|
|
|
appInfo.view = self.view
|
|
|
|
appInfo.url = link
|
|
|
|
|
|
|
|
NotificationCenter.default.post(name: .MouseDidEnterLink, object: self, userInfo: appInfo.userInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func mouseDidExit(_ link: String) {
|
|
|
|
|
|
|
|
let appInfo = AppInfo()
|
|
|
|
appInfo.view = self.view
|
|
|
|
appInfo.url = link
|
|
|
|
|
|
|
|
NotificationCenter.default.post(name: .MouseDidExitLink, object: self, userInfo: appInfo.userInfo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
class DetailBox: NSBox {
|
|
|
|
|
|
|
|
weak var viewController: DetailViewController?
|
|
|
|
|
|
|
|
override func viewWillStartLiveResize() {
|
|
|
|
|
|
|
|
viewController?.viewWillStartLiveResize()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidEndLiveResize() {
|
|
|
|
|
|
|
|
viewController?.viewDidEndLiveResize()
|
|
|
|
}
|
|
|
|
}
|