NetNewsWire/Mac/MainWindow/LegacyArticleExtractorButto...

112 lines
3.1 KiB
Swift
Raw Normal View History

2019-09-19 01:15:55 +02:00
//
// ArticleExtractorButton.swift
// NetNewsWire
//
// Created by Maurice Parker on 9/18/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import Foundation
2024-03-25 05:24:31 +01:00
final class LegacyArticleExtractorButton: NSButton {
2019-09-19 01:15:55 +02:00
var isError = false {
didSet {
2019-09-20 00:30:54 +02:00
if isError != oldValue {
needsDisplay = true
}
2019-09-19 01:15:55 +02:00
}
}
var isInProgress = false {
didSet {
2019-09-20 00:30:54 +02:00
if isInProgress != oldValue {
needsDisplay = true
}
2019-09-19 01:15:55 +02:00
}
}
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
wantsLayer = true
}
required init?(coder: NSCoder) {
super.init(coder: coder)
wantsLayer = true
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
guard let hostedLayer = self.layer else {
return
}
if let imageLayer = hostedLayer.sublayers?[0] {
if needsToDraw(imageLayer.bounds) {
imageLayer.removeFromSuperlayer()
} else {
return
}
}
let opacity: Float = isEnabled ? 1.0 : 0.5
switch true {
case isError:
2020-10-19 00:04:30 +02:00
addImageSublayer(to: hostedLayer, image: AppAssets.legacyArticleExtractorError, opacity: opacity)
2019-09-19 01:15:55 +02:00
case isInProgress:
2019-09-24 23:34:11 +02:00
addAnimatedSublayer(to: hostedLayer)
2019-09-19 01:15:55 +02:00
default:
if NSApplication.shared.isActive {
addImageSublayer(to: hostedLayer, image: AppAssets.legacyArticleExtractor, opacity: opacity)
} else {
if NSApplication.shared.effectiveAppearance.isDarkMode {
addImageSublayer(to: hostedLayer, image: AppAssets.legacyArticleExtractorInactiveDark, opacity: opacity)
} else {
addImageSublayer(to: hostedLayer, image: AppAssets.legacyArticleExtractorInactiveLight, opacity: opacity)
}
}
2019-09-19 01:15:55 +02:00
}
}
private func makeLayerForImage(_ image: NSImage) -> CALayer {
let imageLayer = CALayer()
imageLayer.bounds = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
2020-10-18 23:37:00 +02:00
imageLayer.position = CGPoint(x: bounds.midX, y: floor(bounds.midY))
2019-09-19 01:15:55 +02:00
return imageLayer
}
private func addImageSublayer(to hostedLayer: CALayer, image: NSImage, opacity: Float = 1.0) {
2019-09-19 01:15:55 +02:00
let imageLayer = makeLayerForImage(image)
imageLayer.contents = image
imageLayer.opacity = opacity
hostedLayer.addSublayer(imageLayer)
}
2019-09-24 23:34:11 +02:00
private func addAnimatedSublayer(to hostedLayer: CALayer) {
let imageProgress1 = AppAssets.legacyArticleExtractorProgress1
let imageProgress2 = AppAssets.legacyArticleExtractorProgress2
let imageProgress3 = AppAssets.legacyArticleExtractorProgress3
let imageProgress4 = AppAssets.legacyArticleExtractorProgress4
2020-10-18 23:37:00 +02:00
let images = [imageProgress1, imageProgress2, imageProgress3, imageProgress4, imageProgress3, imageProgress2, imageProgress1]
2019-09-19 01:15:55 +02:00
let imageLayer = CALayer()
imageLayer.bounds = CGRect(x: 0, y: 0, width: imageProgress1?.size.width ?? 0, height: imageProgress1?.size.height ?? 0)
2020-10-18 23:37:00 +02:00
imageLayer.position = CGPoint(x: bounds.midX, y: floor(bounds.midY))
2019-09-19 01:15:55 +02:00
hostedLayer.addSublayer(imageLayer)
let animation = CAKeyframeAnimation(keyPath: "contents")
animation.calculationMode = CAAnimationCalculationMode.linear
2020-10-18 23:37:00 +02:00
animation.keyTimes = [0, 0.16, 0.32, 0.50, 0.66, 0.82, 1]
2019-09-19 01:15:55 +02:00
animation.duration = 2
animation.values = images as [Any]
animation.repeatCount = HUGE
imageLayer.add(animation, forKey: "contents")
}
}