Files
NetNewsWire/Mac/MainWindow/Sidebar/SidebarProgressView.swift
T

147 lines
3.7 KiB
Swift
Raw Normal View History

2017-12-16 16:36:35 -08:00
//
// SidebarProgressView.swift
2018-08-28 22:18:24 -07:00
// NetNewsWire
2017-12-16 16:36:35 -08:00
//
// Created by Brent Simmons on 9/17/16.
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
//
import AppKit
2017-12-16 16:36:35 -08:00
import RSCore
import Articles
2017-12-16 16:36:35 -08:00
import RSWeb
import Account
final class SidebarProgressView: NSView {
2017-12-16 16:36:35 -08:00
@IBOutlet var progressIndicator: NSProgressIndicator!
@IBOutlet var progressLabel: NSTextField!
@IBOutlet var bottomConstraint: NSLayoutConstraint!
2019-02-01 20:26:40 -08:00
@IBOutlet var heightConstraint: NSLayoutConstraint!
/// When true, the view stays hidden and shows no progress.
2026-10-13 15:53:53 -07:00
/// When collapsed, this view sits just below the sidebar, unclipped — where the main window status bar is.
/// Its separator would double the status bar’s top line, so it’s hidden while suppressed.
var isSuppressed = false {
didSet {
if isSuppressed != oldValue {
2026-10-13 15:53:53 -07:00
isHidden = isSuppressed
updateUI()
}
}
}
private var isAnimatingProgress = false
nonisolated override var isFlipped: Bool {
return true
2017-12-16 16:36:35 -08:00
}
2017-12-16 16:36:35 -08:00
override func awakeFromNib() {
2025-11-18 13:25:47 -08:00
MainActor.assumeIsolated {
progressIndicator.isHidden = true
progressLabel.isHidden = true
2017-12-16 16:36:35 -08:00
2025-11-18 13:25:47 -08:00
let progressLabelFontSize = progressLabel.font?.pointSize ?? 13.0
progressLabel.font = NSFont.monospacedDigitSystemFont(ofSize: progressLabelFontSize, weight: NSFont.Weight.regular)
progressLabel.stringValue = ""
2017-12-16 16:36:35 -08:00
NotificationCenter.default.addObserver(self, selector: #selector(progressInfoDidChange(_:)), name: .progressInfoDidChange, object: CombinedRefreshProgress.shared)
2025-11-18 13:25:47 -08:00
}
2017-12-16 16:36:35 -08:00
}
@objc func updateUI() {
2017-12-16 16:36:35 -08:00
updateProgressIndicator()
updateProgressLabel()
2017-12-16 16:36:35 -08:00
}
// MARK: Notifications
2017-12-16 16:36:35 -08:00
@objc func progressInfoDidChange(_ notification: Notification) {
CoalescingQueue.standard.add(self, #selector(updateUI))
}
2017-12-16 16:36:35 -08:00
}
private extension SidebarProgressView {
2017-12-16 16:36:35 -08:00
// MARK: Progress
static let animationDuration = 0.2
2017-12-16 16:36:35 -08:00
func stopProgressIfNeeded() {
if !isAnimatingProgress {
return
}
isAnimatingProgress = false
progressIndicator.stopAnimation(self)
progressIndicator.isHidden = true
progressLabel.isHidden = true
2019-02-01 20:26:40 -08:00
superview?.layoutSubtreeIfNeeded()
NSAnimationContext.runAnimationGroup { context in
context.duration = SidebarProgressView.animationDuration
2019-02-01 20:26:40 -08:00
context.allowsImplicitAnimation = true
bottomConstraint.constant = -(heightConstraint.constant)
2019-02-01 20:26:40 -08:00
superview?.layoutSubtreeIfNeeded()
}
2017-12-16 16:36:35 -08:00
}
func startProgressIfNeeded() {
if isAnimatingProgress {
return
}
isAnimatingProgress = true
progressIndicator.isHidden = false
progressLabel.isHidden = false
2017-12-16 16:36:35 -08:00
progressIndicator.startAnimation(self)
superview?.layoutSubtreeIfNeeded()
NSAnimationContext.runAnimationGroup { context in
context.duration = SidebarProgressView.animationDuration
context.allowsImplicitAnimation = true
bottomConstraint.constant = 0
superview?.layoutSubtreeIfNeeded()
}
2017-12-16 16:36:35 -08:00
}
func updateProgressIndicator() {
let progressInfo = CombinedRefreshProgress.shared.progressInfo
2017-12-16 16:36:35 -08:00
if progressInfo.isComplete || isSuppressed {
2017-12-16 16:36:35 -08:00
stopProgressIfNeeded()
return
}
startProgressIfNeeded()
let maxValue = Double(progressInfo.numberOfTasks)
2017-12-16 16:36:35 -08:00
if progressIndicator.maxValue != maxValue {
progressIndicator.maxValue = maxValue
}
let doubleValue = Double(progressInfo.numberCompleted)
2017-12-16 16:36:35 -08:00
if progressIndicator.doubleValue != doubleValue {
progressIndicator.doubleValue = doubleValue
}
}
func updateProgressLabel() {
let progressInfo = CombinedRefreshProgress.shared.progressInfo
2017-12-16 16:36:35 -08:00
if progressInfo.isComplete {
2017-12-16 16:36:35 -08:00
progressLabel.stringValue = ""
return
}
let formatString = NSLocalizedString("%@ of %@", comment: "Status bar progress")
let s = String(format: formatString, NSNumber(value: progressInfo.numberCompleted), NSNumber(value: progressInfo.numberOfTasks))
2017-12-16 16:36:35 -08:00
progressLabel.stringValue = s
2017-12-16 16:36:35 -08:00
}
}