Files

68 lines
1.3 KiB
Swift
Raw Permalink 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. The cell sizes the view to the image, so the corner radius rounds the image itself.
/// A placeholder color shows until the image arrives.
final class ThumbnailView: NSView {
var image: NSImage? {
didSet {
if image !== 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() {
layer?.contents = image?.cgImage(forProposedRect: nil, context: nil, hints: nil)
// Resolved here so the placeholder follows light and dark mode.
layer?.backgroundColor = image == nil ? Assets.Colors.thumbnailPlaceholder.cgColor : nil
}
}
private extension ThumbnailView {
func commonInit() {
wantsLayer = true
layerContentsRedrawPolicy = .onSetNeedsDisplay
layer?.contentsGravity = .resizeAspect
layer?.masksToBounds = true
}
}