NetNewsWire/Evergreen/MainWindow/StatusBar/StatusBarView.swift

189 lines
4.5 KiB
Swift
Raw Normal View History

2017-05-27 19:43:27 +02:00
//
// StatusBarView.swift
// Evergreen
//
// Created by Brent Simmons on 9/17/16.
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
2017-05-27 19:43:27 +02:00
//
import Cocoa
import RSCore
import Data
2017-05-27 19:43:27 +02:00
import RSWeb
import Account
2017-05-27 19:43:27 +02:00
final class StatusBarView: NSView {
@IBOutlet var progressIndicator: NSProgressIndicator!
@IBOutlet var progressLabel: NSTextField!
@IBOutlet var urlLabel: NSTextField!
fileprivate var isAnimatingProgress = false
fileprivate var article: Article? {
didSet {
updateURLLabel()
}
}
override var isFlipped: Bool {
get {
return true
}
}
override func awakeFromNib() {
let progressLabelFontSize = progressLabel.font?.pointSize ?? 13.0
2017-09-17 21:54:08 +02:00
progressLabel.font = NSFont.monospacedDigitSystemFont(ofSize: progressLabelFontSize, weight: NSFont.Weight.regular)
2017-05-27 19:43:27 +02:00
progressLabel.stringValue = ""
NotificationCenter.default.addObserver(self, selector: #selector(progressDidChange(_:)), name: .AccountRefreshProgressDidChange, object: nil)
2017-05-27 19:43:27 +02:00
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)
2017-05-27 19:43:27 +02:00
}
// MARK: Notifications
2017-09-17 21:34:10 +02:00
@objc dynamic func progressDidChange(_ notification: Notification) {
2017-05-27 19:43:27 +02:00
let progress = AccountManager.shared.combinedRefreshProgress
updateProgressIndicator(progress)
updateProgressLabel(progress)
2017-05-27 19:43:27 +02:00
}
@objc dynamic func mouseDidEnterLink(_ notification: Notification) {
guard let appInfo = AppInfo.pullFromUserInfo(notification.userInfo) else {
return
}
guard let window = window, let notificationWindow = appInfo.view?.window, window === notificationWindow else {
return
}
guard let link = appInfo.url else {
return
}
print(link)
}
@objc dynamic func mouseDidExitLink(_ notification: Notification) {
guard let appInfo = AppInfo.pullFromUserInfo(notification.userInfo) else {
return
}
guard let window = window, let notificationWindow = appInfo.view?.window, window === notificationWindow else {
return
}
guard let link = appInfo.url else {
return
}
print(link)
}
2017-05-27 19:43:27 +02:00
// MARK: Notifications
2017-09-17 21:34:10 +02:00
@objc dynamic func timelineSelectionDidChange(_ note: Notification) {
2017-09-24 21:24:44 +02:00
let timelineView = note.appInfo?.view
if timelineView?.window === self.window {
article = note.appInfo?.article
2017-05-27 19:43:27 +02:00
}
}
// MARK: Drawing
private let lineColor = NSColor(calibratedWhite: 0.57, alpha: 1.0)
override func draw(_ dirtyRect: NSRect) {
let path = NSBezierPath()
path.lineWidth = 1.0
path.move(to: NSPoint(x: NSMinX(bounds), y: NSMinY(bounds) + 0.5))
path.line(to: NSPoint(x: NSMaxX(bounds), y: NSMinY(bounds) + 0.5))
lineColor.set()
path.stroke()
}
}
private extension StatusBarView {
// MARK: URL Label
func updateURLLabel() {
needsLayout = true
guard let article = article else {
urlLabel.stringValue = ""
return
}
2017-09-18 01:30:45 +02:00
if let s = article.preferredLink {
2017-05-27 19:43:27 +02:00
urlLabel.stringValue = (s as NSString).rs_stringByStrippingHTTPOrHTTPSScheme()
}
else {
urlLabel.stringValue = ""
}
}
// MARK: Progress
func stopProgressIfNeeded() {
if !isAnimatingProgress {
return
}
progressIndicator.stopAnimation(self)
isAnimatingProgress = false
progressIndicator.needsDisplay = true
}
func startProgressIfNeeded() {
if isAnimatingProgress {
return
}
isAnimatingProgress = true
progressIndicator.startAnimation(self)
}
func updateProgressIndicator(_ progress: CombinedRefreshProgress) {
2017-05-27 19:43:27 +02:00
if progress.isComplete {
stopProgressIfNeeded()
return
}
startProgressIfNeeded()
let maxValue = Double(progress.numberOfTasks)
if progressIndicator.maxValue != maxValue {
progressIndicator.maxValue = maxValue
}
let doubleValue = Double(progress.numberCompleted)
if progressIndicator.doubleValue != doubleValue {
progressIndicator.doubleValue = doubleValue
}
}
func updateProgressLabel(_ progress: CombinedRefreshProgress) {
2017-05-27 19:43:27 +02:00
if progress.isComplete {
progressLabel.stringValue = ""
return
}
let numberOfTasks = progress.numberOfTasks
let numberCompleted = progress.numberCompleted
let formatString = NSLocalizedString("%@ of %@", comment: "Status bar progress")
let s = NSString(format: formatString as NSString, NSNumber(value: numberCompleted), NSNumber(value: numberOfTasks))
progressLabel.stringValue = s as String
}
}