Simplify communication with DetailStatusBarView.

This commit is contained in:
Brent Simmons 2019-02-10 21:39:11 -08:00
parent a74e0762e7
commit 1498823c20
4 changed files with 14 additions and 66 deletions

View File

@ -16,10 +16,6 @@ extension Notification.Name {
static let UserDidAddFeed = Notification.Name("UserDidAddFeedNotification")
static let UserDidRequestSidebarSelection = Notification.Name("UserDidRequestSidebarSelectionNotification")
// Sent by DetailViewController when mouse hovers over link in web view.
static let MouseDidEnterLink = Notification.Name("MouseDidEnterLinkNotification")
static let MouseDidExitLink = Notification.Name("MouseDidExitLinkNotification")
}
typealias UserInfoDictionary = [AnyHashable: Any]

View File

@ -521,7 +521,7 @@
<rect key="frame" x="0.0" y="0.0" width="450" height="300"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<customView hidden="YES" translatesAutoresizingMaskIntoConstraints="NO" id="xI5-lx-RD8" customClass="DetailStatusBarView" customModule="NetNewsWire" customModuleProvider="target">
<customView hidden="YES" alphaValue="0.90000000000000002" translatesAutoresizingMaskIntoConstraints="NO" id="xI5-lx-RD8" customClass="DetailStatusBarView" customModule="NetNewsWire" customModuleProvider="target">
<rect key="frame" x="6" y="2" width="12" height="20"/>
<subviews>
<textField horizontalHuggingPriority="850" verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" translatesAutoresizingMaskIntoConstraints="NO" id="Dim-ed-Dcz" userLabel="URL Label">
@ -555,6 +555,7 @@
</customView>
<connections>
<outlet property="containerView" destination="cJ9-6s-66u" id="gXc-Pz-9sQ"/>
<outlet property="statusBarView" destination="xI5-lx-RD8" id="meP-4g-U63"/>
</connections>
</viewController>
<customObject id="vzM-Vn-mEn" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>

View File

@ -14,8 +14,7 @@ final class DetailStatusBarView: NSView {
@IBOutlet var urlLabel: NSTextField!
private var didConfigureLayerRadius = false
private var mouseoverLink: String? {
var mouseoverLink: String? {
didSet {
updateLinkForDisplay()
}
@ -35,6 +34,8 @@ final class DetailStatusBarView: NSView {
}
}
private var didConfigureLayerRadius = false
override var isOpaque: Bool {
return false
}
@ -48,7 +49,6 @@ final class DetailStatusBarView: NSView {
}
override func updateLayer() {
guard let layer = layer else {
return
}
@ -60,53 +60,13 @@ final class DetailStatusBarView: NSView {
let color = self.effectiveAppearance.isDarkMode ? NSColor.textBackgroundColor : appDelegate.currentTheme.color(forKey: "MainWindow.Detail.statusBar.backgroundColor")
layer.backgroundColor = color.cgColor
}
override func awakeFromNib() {
NotificationCenter.default.addObserver(self, selector: #selector(timelineSelectionDidChange(_:)), name: .TimelineSelectionDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(mouseDidEnterLink(_:)), name: .MouseDidEnterLink, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(mouseDidExitLink(_:)), name: .MouseDidExitLink, object: nil)
alphaValue = 0.9
}
// MARK: - Notifications
@objc func mouseDidEnterLink(_ notification: Notification) {
guard let userInfo = notification.userInfo, let view = userInfo[UserInfoKey.view] as? NSView, window === view.window else {
return
}
guard let link = userInfo[UserInfoKey.url] as? String else {
return
}
mouseoverLink = link
}
@objc func mouseDidExitLink(_ notification: Notification) {
guard let view = notification.userInfo?[UserInfoKey.view] as? NSView, window === view.window else {
return
}
mouseoverLink = nil
}
@objc func timelineSelectionDidChange(_ notification: Notification) {
guard let view = notification.userInfo?[UserInfoKey.view] as? NSView, window === view.window else {
return
}
mouseoverLink = nil
}
}
// MARK: - Private
private extension DetailStatusBarView {
// MARK: URL Label
func updateLinkForDisplay() {
if let mouseoverLink = mouseoverLink, !mouseoverLink.isEmpty {
linkForDisplay = (mouseoverLink as NSString).rs_stringByStrippingHTTPOrHTTPSScheme()
}

View File

@ -15,6 +15,7 @@ import RSWeb
final class DetailViewController: NSViewController, WKUIDelegate {
@IBOutlet var containerView: DetailContainerView!
@IBOutlet var statusBarView: DetailStatusBarView!
var webview: DetailWebView!
@ -24,6 +25,7 @@ final class DetailViewController: NSViewController, WKUIDelegate {
article = articles.first!
return
}
statusBarView.mouseoverLink = nil
article = nil
reloadHTML()
}
@ -32,6 +34,7 @@ final class DetailViewController: NSViewController, WKUIDelegate {
private var article: Article? {
didSet {
if article != nil, article != oldValue {
statusBarView.mouseoverLink = nil
reloadHTML()
}
}
@ -76,7 +79,6 @@ final class DetailViewController: NSViewController, WKUIDelegate {
reloadHTML()
containerView.contentView = webview
containerView.viewController = self
}
// MARK: - Scrolling
@ -162,25 +164,14 @@ extension DetailViewController: WKScriptMessageHandler {
}
private func mouseDidEnter(_ link: String) {
if link.isEmpty {
guard !link.isEmpty else {
return
}
var userInfo = UserInfoDictionary()
userInfo[UserInfoKey.view] = view
userInfo[UserInfoKey.url] = link
NotificationCenter.default.post(name: .MouseDidEnterLink, object: self, userInfo: userInfo)
statusBarView.mouseoverLink = link
}
private func mouseDidExit(_ link: String) {
var userInfo = UserInfoDictionary()
userInfo[UserInfoKey.view] = view
userInfo[UserInfoKey.url] = link
NotificationCenter.default.post(name: .MouseDidExitLink, object: self, userInfo: userInfo)
statusBarView.mouseoverLink = nil
}
}