Files
NetNewsWire/Mac/MainWindow/Timeline/Cell/TimelineColumnCellViews.swift
T

177 lines
5.8 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.
//
// TimelineColumnCellViews.swift
// NetNewsWire
//
// Created by Brent Simmons on 9/19/26.
// Copyright © 2026 Ranchero Software. All rights reserved.
//
import AppKit
// Cell views for the timeline’s column layout. Selection state arrives through backgroundStyle,
// which the standard NSTableRowView sets, so no custom row view is needed.
private let columnCellHorizontalPadding: CGFloat = 8.0
private let feedIconDimension: CGFloat = 16.0
private let feedIconMarginRight: CGFloat = 5.0
final class TimelineUnreadColumnCellView: NSTableCellView {
private let unreadIndicatorView = UnreadIndicatorView()
var isUnread = false {
didSet {
unreadIndicatorView.isHidden = !isUnread
// The dot is the only cue, so VoiceOver gets the word instead. Read rows say nothing.
setAccessibilityElement(isUnread)
setAccessibilityLabel(isUnread ? NSLocalizedString("Unread", comment: "Unread") : nil)
}
}
override var backgroundStyle: NSView.BackgroundStyle {
didSet {
let isEmphasized = backgroundStyle == .emphasized
unreadIndicatorView.isSelected = isEmphasized
unreadIndicatorView.isEmphasized = isEmphasized
}
}
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
unreadIndicatorView.translatesAutoresizingMaskIntoConstraints = false
unreadIndicatorView.isHidden = true
unreadIndicatorView.setAccessibilityElement(false)
addSubview(unreadIndicatorView)
NSLayoutConstraint.activate([
unreadIndicatorView.widthAnchor.constraint(equalToConstant: UnreadIndicatorView.unreadCircleDimension),
unreadIndicatorView.heightAnchor.constraint(equalToConstant: UnreadIndicatorView.unreadCircleDimension),
unreadIndicatorView.centerXAnchor.constraint(equalTo: centerXAnchor),
unreadIndicatorView.centerYAnchor.constraint(equalTo: centerYAnchor)
])
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
}
final class TimelineStarColumnCellView: NSTableCellView {
private let starImageView = NSImageView(image: Assets.Images.timelineStar)
var isStarred = false {
didSet {
starImageView.isHidden = !isStarred
setAccessibilityElement(isStarred)
setAccessibilityLabel(isStarred ? NSLocalizedString("Starred", comment: "Starred") : nil)
}
}
override var backgroundStyle: NSView.BackgroundStyle {
didSet {
starImageView.contentTintColor = backgroundStyle == .emphasized ? .white : Assets.Colors.star
}
}
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
starImageView.translatesAutoresizingMaskIntoConstraints = false
starImageView.imageScaling = .scaleNone
starImageView.contentTintColor = Assets.Colors.star
starImageView.isHidden = true
starImageView.setAccessibilityElement(false)
addSubview(starImageView)
NSLayoutConstraint.activate([
starImageView.centerXAnchor.constraint(equalTo: centerXAnchor),
starImageView.centerYAnchor.constraint(equalTo: centerYAnchor)
])
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
}
/// Single line of truncating text — used for the Title and Date columns.
final class TimelineTextColumnCellView: NSTableCellView {
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
let label = Self.makeLabel()
addSubview(label)
textField = label
NSLayoutConstraint.activate([
label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: columnCellHorizontalPadding),
label.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -columnCellHorizontalPadding),
label.centerYAnchor.constraint(equalTo: centerYAnchor)
])
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
static func makeLabel() -> NSTextField {
let label = NSTextField(labelWithString: "")
label.translatesAutoresizingMaskIntoConstraints = false
label.lineBreakMode = .byTruncatingTail
label.maximumNumberOfLines = 1
label.cell?.truncatesLastVisibleLine = true
label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
return label
}
}
/// Feed icon plus feed name.
final class TimelineFeedColumnCellView: NSTableCellView {
/// When false, the icon is hidden and the name starts at the leading edge.
var showsIcon = true {
didSet {
guard showsIcon != oldValue else {
return
}
imageView?.isHidden = !showsIcon
labelLeadingAfterIconConstraint?.isActive = showsIcon
labelLeadingWithoutIconConstraint?.isActive = !showsIcon
}
}
private var labelLeadingAfterIconConstraint: NSLayoutConstraint?
private var labelLeadingWithoutIconConstraint: NSLayoutConstraint?
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
let iconView = NSImageView()
iconView.translatesAutoresizingMaskIntoConstraints = false
iconView.imageScaling = .scaleProportionallyDown
iconView.wantsLayer = true
iconView.layer?.cornerRadius = 2.0
iconView.layer?.masksToBounds = true
addSubview(iconView)
imageView = iconView
let label = TimelineTextColumnCellView.makeLabel()
addSubview(label)
textField = label
let labelLeadingAfterIconConstraint = label.leadingAnchor.constraint(equalTo: iconView.trailingAnchor, constant: feedIconMarginRight)
self.labelLeadingAfterIconConstraint = labelLeadingAfterIconConstraint
self.labelLeadingWithoutIconConstraint = label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: columnCellHorizontalPadding)
NSLayoutConstraint.activate([
iconView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: columnCellHorizontalPadding),
iconView.widthAnchor.constraint(equalToConstant: feedIconDimension),
iconView.heightAnchor.constraint(equalToConstant: feedIconDimension),
iconView.centerYAnchor.constraint(equalTo: centerYAnchor),
labelLeadingAfterIconConstraint,
label.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -columnCellHorizontalPadding),
label.centerYAnchor.constraint(equalTo: centerYAnchor)
])
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
}