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 {
|
2026-10-22 13:46:32 -07:00
|
|
|
|
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 {
|
2026-10-22 13:46:32 -07:00
|
|
|
|
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:
|
2026-09-19 12:05:54 -07:00
|
|
|
|
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:
|
2026-09-19 12:05:54 -07:00
|
|
|
|
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:
|
2026-09-19 12:05:54 -07:00
|
|
|
|
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:
|
2026-09-19 12:05:54 -07:00
|
|
|
|
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:
|
2026-09-19 12:05:54 -07:00
|
|
|
|
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.
|
2026-09-19 11:41:48 -07:00
|
|
|
|
// 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
|
2026-10-09 21:31:53 -07:00
|
|
|
|
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
|
2026-09-19 11:45:48 -07:00
|
|
|
|
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() {
|
2026-09-19 11:41:48 -07:00
|
|
|
|
isConfiguringTableColumns = true
|
2026-09-19 10:26:29 -07:00
|
|
|
|
removeAllTableColumns()
|
|
|
|
|
|
for column in TimelineColumn.allCases {
|
|
|
|
|
|
tableView.addTableColumn(column.makeTableColumn())
|
|
|
|
|
|
}
|
|
|
|
|
|
tableView.headerView = NSTableHeaderView()
|
2026-09-19 11:45:48 -07:00
|
|
|
|
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
|
2026-09-19 11:41:48 -07:00
|
|
|
|
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
|
|
|
|
|
|
}
|
2026-09-19 12:03:21 -07:00
|
|
|
|
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)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-19 12:05:54 -07:00
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|