Files
NetNewsWire/Mac/MainWindow/Timeline/TimelineViewController+ColumnLayout.swift
T

178 lines
6.2 KiB
Swift
Raw Normal View History

2026-09-19 10:26:29 -07:00
//
// TimelineViewController+ColumnLayout.swift
// NetNewsWire
//
// Created by Brent Simmons on 9/19/26.
// Copyright © 2026 Ranchero Software. All rights reserved.
//
import AppKit
import Articles
// The table view is shared by both layouts. These reconfigure it for the current one
// and supply the per-column cells used in column layout.
private let columnRowVerticalPadding: CGFloat = 3.0
private let columnAutosaveName = "TimelineColumns"
extension TimelineViewController {
func configureTableView(for layout: TimelineLayout) {
switch layout {
case .standard:
configureTableViewForStandardLayout()
case .column:
configureTableViewForColumnLayout()
}
}
var columnFont: NSFont {
NSFont.systemFont(ofSize: AppDefaults.actualFontSize(for: AppDefaults.timelineFontSize))
2026-09-19 10:26:29 -07:00
}
2026-09-19 11:31:27 -07:00
// Unread rows are bold, like Mail.
var boldColumnFont: NSFont {
NSFont.boldSystemFont(ofSize: AppDefaults.actualFontSize(for: AppDefaults.timelineFontSize))
2026-09-19 11:31:27 -07:00
}
2026-09-19 10:26:29 -07:00
func columnRowHeight() -> CGFloat {
let font = columnFont
let lineHeight = ceil(font.ascender - font.descender + font.leading)
return lineHeight + (columnRowVerticalPadding * 2.0)
}
func columnCellView(for tableColumn: NSTableColumn?, row: Int) -> NSView? {
guard let tableColumn, let column = TimelineColumn(identifier: tableColumn.identifier) else {
return nil
}
let article = articles.articleAtRow(row)
let identifier = tableColumn.identifier
2026-09-19 11:31:27 -07:00
let isUnread = article.map { !$0.status.read } ?? false
let textFont = isUnread ? boldColumnFont : columnFont
2026-09-19 10:26:29 -07:00
switch column {
case .unread:
let cell = dequeueCell(TimelineUnreadColumnCellView.self, identifier: identifier)
2026-09-19 11:31:27 -07:00
cell.isUnread = isUnread
2026-09-19 10:26:29 -07:00
return cell
case .starred:
let cell = dequeueCell(TimelineStarColumnCellView.self, identifier: identifier)
2026-09-19 10:26:29 -07:00
cell.isStarred = article?.status.starred ?? false
return cell
case .title:
let cell = dequeueCell(TimelineTextColumnCellView.self, identifier: identifier)
2026-09-19 11:31:27 -07:00
cell.textField?.font = textFont
2026-09-19 10:26:29 -07:00
cell.textField?.stringValue = article.map { columnTitleText(for: $0) } ?? ""
return cell
case .feed:
let cell = dequeueCell(TimelineFeedColumnCellView.self, identifier: identifier)
2026-09-19 11:31:27 -07:00
cell.textField?.font = textFont
2026-09-19 10:26:29 -07:00
cell.textField?.stringValue = article?.feed?.nameForDisplay ?? ""
cell.imageView?.image = article?.feed.flatMap { IconImageCache.shared.imageForFeed($0) }?.image
return cell
case .date:
let cell = dequeueCell(TimelineTextColumnCellView.self, identifier: identifier)
2026-09-19 11:31:27 -07:00
cell.textField?.font = textFont
2026-09-19 10:26:29 -07:00
cell.textField?.stringValue = article.map { ArticleStringFormatter.shared.dateString($0.logicalDatePublished) } ?? ""
return cell
}
}
/// Keeps the header’s sort indicator in step with this window’s sort. Column layout only.
func applySortDescriptorsToTableView() {
guard layout == .column else {
return
}
let descriptors = [NSSortDescriptor(key: sortParameters.key.rawValue, ascending: sortParameters.direction == .orderedAscending)]
if tableView.sortDescriptors != descriptors {
tableView.sortDescriptors = descriptors
}
}
// MARK: - NSTableViewDataSource
func tableView(_ tableView: NSTableView, sortDescriptorsDidChange oldDescriptors: [NSSortDescriptor]) {
// A header click. Don’t sort here — ask the container, which owns the window’s sort.
// Setting sortDescriptors programmatically lands here too, and the equality check ends that loop.
// The table also autosaves sort descriptors under its autosaveName and restores them when it’s configured.
// Those belong to whatever window last saved, so they’re ignored — the window’s own sort is applied right after.
guard !isConfiguringTableColumns, let descriptor = tableView.sortDescriptors.first, let rawKey = descriptor.key, let key = ArticleSortKey(rawValue: rawKey) else {
2026-09-19 10:26:29 -07:00
return
}
let direction: ComparisonResult = descriptor.ascending ? .orderedAscending : .orderedDescending
let requestedParameters = ArticleSortParameters(key: key, direction: direction)
2026-09-19 10:26:29 -07:00
if requestedParameters == sortParameters {
return
}
delegate?.timelineRequestedSortChange(self, parameters: requestedParameters)
}
}
// MARK: - Private
private extension TimelineViewController {
func configureTableViewForStandardLayout() {
tableView.autosaveTableColumns = false
tableView.autosaveName = nil
tableView.sortDescriptors = []
removeAllTableColumns()
if let standardColumn {
tableView.addTableColumn(standardColumn)
}
tableView.headerView = nil
tableView.usesAlternatingRowBackgroundColors = false
2026-09-19 10:26:29 -07:00
tableView.columnAutoresizingStyle = .lastColumnOnlyAutoresizingStyle
tableView.allowsColumnReordering = false
tableView.allowsColumnResizing = false
2026-09-19 12:07:07 -07:00
updateTableViewRowHeight()
2026-09-19 10:26:29 -07:00
}
func configureTableViewForColumnLayout() {
isConfiguringTableColumns = true
2026-09-19 10:26:29 -07:00
removeAllTableColumns()
for column in TimelineColumn.allCases {
tableView.addTableColumn(column.makeTableColumn())
}
tableView.headerView = NSTableHeaderView()
tableView.usesAlternatingRowBackgroundColors = true
2026-09-19 10:26:29 -07:00
tableView.columnAutoresizingStyle = .uniformColumnAutoresizingStyle
tableView.allowsColumnReordering = true
tableView.allowsColumnResizing = true
2026-09-19 12:07:07 -07:00
updateTableViewRowHeight()
2026-09-19 10:26:29 -07:00
// Set the name before turning autosave on so saved widths and order are read back.
tableView.autosaveName = columnAutosaveName
tableView.autosaveTableColumns = true
isConfiguringTableColumns = false
2026-09-19 21:35:22 -07:00
// After the flag is cleared, so the header shows this window’s sort rather than whatever autosave restored.
2026-09-19 10:26:29 -07:00
applySortDescriptorsToTableView()
}
/// An untitled article shows the start of its body instead, as the standard timeline does.
func columnTitleText(for article: Article) -> String {
let title = ArticleStringFormatter.shared.truncatedTitle(article)
if !title.isEmpty {
return title
}
return TimelineCellData.summaryText(for: article, title: title).collapsingWhitespace
2026-09-19 10:26:29 -07:00
}
func removeAllTableColumns() {
for column in tableView.tableColumns {
tableView.removeTableColumn(column)
}
}
func dequeueCell<T: NSTableCellView>(_ type: T.Type, identifier: NSUserInterfaceItemIdentifier) -> T {
if let cell = tableView.makeView(withIdentifier: identifier, owner: nil) as? T {
return cell
}
let cell = T()
2026-09-19 10:26:29 -07:00
cell.identifier = identifier
return cell
}
}