NetNewsWire/Evergreen/MainWindow/StatusBar/StatusBarView.swift

159 lines
3.6 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
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)
}
// MARK: Notifications
2017-09-17 21:34:10 +02:00
@objc dynamic func progressDidChange(_ notification: Notification) {
2017-05-27 19:43:27 +02:00
// guard let progress = notification.userInfo?[progressKey] as? DownloadProgress else {
// return
// }
// updateProgressIndicator(progress)
// updateProgressLabel(progress)
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: DownloadProgress) {
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: DownloadProgress) {
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
}
}