Files
NetNewsWire/Mac/MainWindow/IconView.swift
T

133 lines
3.2 KiB
Swift
Raw Normal View History

//
// TimelineIconView.swift
// NetNewsWire
//
// Created by Brent Simmons on 9/15/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import AppKit
2026-05-29 10:46:00 -07:00
import Images
final class IconView: NSView {
2026-01-01 15:59:20 -08:00
var iconImage: IconImage? {
didSet {
if iconImage !== oldValue {
imageView.image = iconImage?.image
2022-01-21 23:19:28 +01:00
if let tintColor = iconImage?.preferredColor {
imageView.contentTintColor = tintColor
2022-01-21 23:19:28 +01:00
}
if iconImage?.isBackgroundSuppressed ?? false {
isDiscernable = true
} else if NSApplication.shared.effectiveAppearance.isDarkMode {
if iconImage?.isDark ?? false {
isDiscernable = false
} else {
isDiscernable = true
}
} else {
if iconImage?.isBright ?? false {
isDiscernable = false
} else {
isDiscernable = true
}
}
needsDisplay = true
needsLayout = true
}
}
}
2021-03-06 16:25:10 -06:00
private var isDiscernable = true
2025-11-05 11:22:10 -08:00
nonisolated override var isFlipped: Bool {
return true
}
private let imageView: NSImageView = {
let imageView = NSImageView(frame: NSRect.zero)
imageView.animates = false
imageView.imageAlignment = .alignCenter
imageView.imageScaling = .scaleProportionallyUpOrDown
return imageView
}()
private var hasExposedVerticalBackground: Bool {
return imageView.frame.size.height < bounds.size.height
}
2025-11-19 10:38:18 -08:00
private static var lightBackgroundColor = Assets.Colors.iconLightBackground
private static var darkBackgroundColor = Assets.Colors.iconDarkBackground
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
convenience init() {
self.init(frame: NSRect.zero)
}
override func viewDidMoveToSuperview() {
needsLayout = true
needsDisplay = true
}
override func layout() {
2026-01-02 17:50:35 -08:00
resizeSubviews(withOldSize: NSSize.zero)
}
override func resizeSubviews(withOldSize oldSize: NSSize) {
imageView.setFrameIfNotEqual(rectForImageView())
}
override func draw(_ dirtyRect: NSRect) {
2025-04-28 21:45:23 -07:00
guard !(iconImage?.isBackgroundSuppressed ?? false) else { return }
2021-03-06 16:25:10 -06:00
guard hasExposedVerticalBackground || !isDiscernable else { return }
let color = NSApplication.shared.effectiveAppearance.isDarkMode ? IconView.darkBackgroundColor : IconView.lightBackgroundColor
color.set()
dirtyRect.fill()
}
}
private extension IconView {
func commonInit() {
addSubview(imageView)
wantsLayer = true
layer?.cornerRadius = 4.0
}
func rectForImageView() -> NSRect {
guard !(iconImage?.isSymbol ?? false) else {
2026-01-02 17:50:35 -08:00
return NSRect(x: 0.0, y: 0.0, width: bounds.size.width, height: bounds.size.height)
}
2025-11-05 11:22:10 -08:00
guard let image = iconImage?.image else {
return NSRect.zero
}
let imageSize = image.size
let viewSize = bounds.size
guard imageSize.width > 0.0, imageSize.height > 0.0 else {
return NSRect.zero
}
// Aspect-fit, but never scale up — small icons render at natural size, centered.
let factor = min(viewSize.width / imageSize.width, viewSize.height / imageSize.height, 1.0)
let width = imageSize.width * factor
let height = imageSize.height * factor
let originX = floor((viewSize.width - width) / 2.0)
let originY = floor((viewSize.height - height) / 2.0)
return NSRect(x: originX, y: originY, width: width, height: height)
}
}