Files
NetNewsWire/Mac/MainWindow/Timeline/Cell/ThumbnailView.swift
T
2026-10-27 11:26:03 -07:00

78 lines
1.4 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// ThumbnailView.swift
// NetNewsWire
//
// Created by Brent Simmons on 10/25/26.
//
import AppKit
/// An article’s thumbnail in a timeline cell.
///
/// The whole image shows, scaled to fit, on a fill that is also the placeholder until the image arrives.
final class ThumbnailView: NSView {
var image: NSImage? {
didSet {
if image !== oldValue {
needsDisplay = true
}
}
}
var fillColor = NSColor.systemFill {
didSet {
if fillColor != oldValue {
needsDisplay = true
}
}
}
var cornerRadius: CGFloat = 0 {
didSet {
layer?.cornerRadius = cornerRadius
}
}
nonisolated override var isFlipped: Bool {
true
}
override var wantsUpdateLayer: Bool {
true
}
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 updateLayer() {
guard let layer else {
return
}
layer.contents = image?.cgImage(forProposedRect: nil, context: nil, hints: nil)
// Resolved here so the fill follows light and dark mode.
layer.backgroundColor = fillColor.cgColor
}
}
private extension ThumbnailView {
func commonInit() {
wantsLayer = true
layerContentsRedrawPolicy = .onSetNeedsDisplay
layer?.contentsGravity = .resizeAspect
layer?.masksToBounds = true
}
}