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

286 lines
12 KiB
Swift
Raw Normal View History

2017-05-27 10:43:27 -07:00
//
// TimelineCellLayout.swift
2018-08-28 22:18:24 -07:00
// NetNewsWire
2017-05-27 10:43:27 -07:00
//
// Created by Brent Simmons on 2/6/16.
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
//
import AppKit
2017-05-27 10:43:27 -07:00
import RSCore
2025-11-13 17:11:34 -08:00
@MainActor struct TimelineCellLayout {
2025-11-05 11:22:10 -08:00
2017-05-27 10:43:27 -07:00
let width: CGFloat
let height: CGFloat
let feedNameRect: NSRect
let dateRect: NSRect
let titleRect: NSRect
let numberOfLinesForTitle: Int
let summaryRect: NSRect
let textRect: NSRect
2017-05-27 10:43:27 -07:00
let unreadIndicatorRect: NSRect
let starRect: NSRect
let iconImageRect: NSRect
2026-10-25 14:55:23 -07:00
let thumbnailRect: NSRect
let separatorRect: NSRect
2017-05-27 10:43:27 -07:00
let paddingBottom: CGFloat
2025-11-05 11:22:10 -08:00
2026-10-25 14:55:23 -07:00
init(width: CGFloat, height: CGFloat, feedNameRect: NSRect, dateRect: NSRect, titleRect: NSRect, numberOfLinesForTitle: Int, summaryRect: NSRect, textRect: NSRect, unreadIndicatorRect: NSRect, starRect: NSRect, iconImageRect: NSRect, thumbnailRect: NSRect, separatorRect: NSRect, paddingBottom: CGFloat) {
2025-11-05 11:22:10 -08:00
2017-05-27 10:43:27 -07:00
self.width = width
self.feedNameRect = feedNameRect
self.dateRect = dateRect
self.titleRect = titleRect
self.numberOfLinesForTitle = numberOfLinesForTitle
self.summaryRect = summaryRect
self.textRect = textRect
2017-05-27 10:43:27 -07:00
self.unreadIndicatorRect = unreadIndicatorRect
self.starRect = starRect
self.iconImageRect = iconImageRect
2026-10-25 14:55:23 -07:00
self.thumbnailRect = thumbnailRect
self.separatorRect = separatorRect
2017-05-27 10:43:27 -07:00
self.paddingBottom = paddingBottom
if height > 0.1 {
self.height = height
2026-01-02 11:57:55 -08:00
} else {
2026-10-25 14:55:23 -07:00
self.height = [feedNameRect, dateRect, titleRect, summaryRect, textRect, unreadIndicatorRect, iconImageRect, thumbnailRect].maxY() + paddingBottom
}
}
2026-10-25 14:55:23 -07:00
init(width: CGFloat, height: CGFloat, cellData: TimelineCellData, appearance: TimelineCellAppearance) {
// If height == 0.0, then height is calculated.
let showIcon = cellData.showIcon
2026-10-25 14:55:23 -07:00
let thumbnailDimension = TimelineCellLayout.thumbnailDimension(cellData, appearance, height)
var textBoxRect = TimelineCellLayout.rectForTextBox(appearance, cellData, showIcon, thumbnailDimension, width)
let (titleRect, numberOfLinesForTitle) = TimelineCellLayout.rectForTitle(textBoxRect, appearance, cellData)
let summaryRect = numberOfLinesForTitle > 0 ? TimelineCellLayout.rectForSummary(textBoxRect, titleRect, numberOfLinesForTitle, appearance, cellData) : NSRect.zero
let textRect = numberOfLinesForTitle > 0 ? NSRect.zero : TimelineCellLayout.rectForText(textBoxRect, appearance, cellData)
var lastTextRect = titleRect
if numberOfLinesForTitle == 0 {
lastTextRect = textRect
2026-01-02 11:57:55 -08:00
} else if numberOfLinesForTitle < appearance.titleNumberOfLines {
if summaryRect.height > 0.1 {
lastTextRect = summaryRect
}
}
let dateRect = TimelineCellLayout.rectForDate(textBoxRect, lastTextRect, appearance, cellData)
let feedNameRect = TimelineCellLayout.rectForFeedName(textBoxRect, dateRect, appearance, cellData)
textBoxRect.size.height = ceil([titleRect, summaryRect, textRect, dateRect, feedNameRect].maxY() - textBoxRect.origin.y)
let iconImageRect = TimelineCellLayout.rectForIcon(cellData, appearance, showIcon, textBoxRect, width, height)
2026-10-25 15:02:32 -07:00
let thumbnailRect = appearance.showsLargeThumbnails ? TimelineCellLayout.rectForLargeThumbnail(cellData, appearance, textBoxRect, iconImageRect, width, height) : TimelineCellLayout.rectForThumbnail(appearance, thumbnailDimension, width)
let unreadIndicatorRect = TimelineCellLayout.rectForUnreadIndicator(appearance, textBoxRect)
let starRect = TimelineCellLayout.rectForStar(appearance, unreadIndicatorRect)
let separatorRect = TimelineCellLayout.rectForSeparator(cellData, appearance, showIcon ? iconImageRect : titleRect, width, height)
let paddingBottom = appearance.cellPadding.bottom
2026-10-25 14:55:23 -07:00
self.init(width: width, height: height, feedNameRect: feedNameRect, dateRect: dateRect, titleRect: titleRect, numberOfLinesForTitle: numberOfLinesForTitle, summaryRect: summaryRect, textRect: textRect, unreadIndicatorRect: unreadIndicatorRect, starRect: starRect, iconImageRect: iconImageRect, thumbnailRect: thumbnailRect, separatorRect: separatorRect, paddingBottom: paddingBottom)
}
static func height(for width: CGFloat, cellData: TimelineCellData, appearance: TimelineCellAppearance) -> CGFloat {
2026-10-25 14:55:23 -07:00
let layout = TimelineCellLayout(width: width, height: 0.0, cellData: cellData, appearance: appearance)
return layout.height
2017-05-27 10:43:27 -07:00
}
}
// MARK: - Calculate Rects
2017-05-27 10:43:27 -07:00
2025-11-13 17:11:34 -08:00
@MainActor private extension TimelineCellLayout {
2017-11-28 21:39:09 -08:00
2026-10-25 15:02:32 -07:00
/// The small right-side thumbnail’s width and height. Zero when there’s no small thumbnail.
/// Fits the thumbnail within the row’s padding, since the row height doesn’t grow for it.
/// A calculated height (zero) never includes a small thumbnail.
2026-10-25 14:55:23 -07:00
static func thumbnailDimension(_ cellData: TimelineCellData, _ appearance: TimelineCellAppearance, _ height: CGFloat) -> CGFloat {
2026-10-25 15:02:32 -07:00
guard cellData.showThumbnail, !appearance.showsLargeThumbnails, height > 0.1 else {
2026-10-25 14:55:23 -07:00
return 0.0
}
let availableHeight = floor(height - appearance.cellPadding.top - appearance.cellPadding.bottom)
return max(0.0, min(appearance.thumbnailMaximumDimension, availableHeight))
}
static func rectForTextBox(_ appearance: TimelineCellAppearance, _ cellData: TimelineCellData, _ showIcon: Bool, _ thumbnailDimension: CGFloat, _ width: CGFloat) -> NSRect {
2017-05-27 10:43:27 -07:00
// Returned height is a placeholder. Not needed when this is calculated.
let iconSpace = showIcon ? appearance.iconSize.width + appearance.iconMarginRight : 0.0
2026-10-25 14:55:23 -07:00
let thumbnailSpace = thumbnailDimension > 0.0 ? thumbnailDimension + appearance.thumbnailMarginLeft : 0.0
let textBoxOriginX = appearance.cellPadding.left + appearance.unreadCircleDimension + appearance.unreadCircleMarginRight + iconSpace
2026-10-25 14:55:23 -07:00
let textBoxMaxX = floor(width - appearance.cellPadding.right - thumbnailSpace)
let textBoxWidth = floor(textBoxMaxX - textBoxOriginX)
let textBoxRect = NSRect(x: textBoxOriginX, y: appearance.cellPadding.top, width: textBoxWidth, height: 1000000)
return textBoxRect
2017-05-27 10:43:27 -07:00
}
static func rectForTitle(_ textBoxRect: NSRect, _ appearance: TimelineCellAppearance, _ cellData: TimelineCellData) -> (NSRect, Int) {
2017-05-27 10:43:27 -07:00
var r = textBoxRect
2017-05-27 10:43:27 -07:00
if cellData.title.isEmpty {
r.size.height = 0
return (r, 0)
}
2025-11-05 11:22:10 -08:00
let attributedTitle = cellData.attributedTitle.applyingBaseFont(appearance.titleFont)
let sizeInfo = MultilineTextFieldSizer.size(for: attributedTitle, numberOfLines: appearance.titleNumberOfLines, width: Int(textBoxRect.width))
r.size.height = sizeInfo.size.height
if sizeInfo.numberOfLinesUsed < 1 {
r.size.height = 0
}
return (r, sizeInfo.numberOfLinesUsed)
}
2026-01-02 17:50:35 -08:00
static func rectForSummary(_ textBoxRect: NSRect, _ titleRect: NSRect, _ titleNumberOfLines: Int, _ appearance: TimelineCellAppearance, _ cellData: TimelineCellData) -> NSRect {
if titleNumberOfLines >= appearance.titleNumberOfLines || cellData.text.isEmpty {
return NSRect.zero
}
var r = textBoxRect
2026-01-02 17:50:35 -08:00
r.origin.y = titleRect.maxY
let summaryNumberOfLines = appearance.titleNumberOfLines - titleNumberOfLines
2025-11-05 11:22:10 -08:00
let sizeInfo = MultilineTextFieldSizer.size(for: cellData.text, font: appearance.textOnlyFont, numberOfLines: summaryNumberOfLines, width: Int(textBoxRect.width))
r.size.height = sizeInfo.size.height
if sizeInfo.numberOfLinesUsed < 1 {
r.size.height = 0
}
return r
}
static func rectForText(_ textBoxRect: NSRect, _ appearance: TimelineCellAppearance, _ cellData: TimelineCellData) -> NSRect {
var r = textBoxRect
if cellData.text.isEmpty {
r.size.height = 0
return r
}
let sizeInfo = MultilineTextFieldSizer.size(for: cellData.text, font: appearance.textOnlyFont, numberOfLines: appearance.titleNumberOfLines, width: Int(textBoxRect.width))
r.size.height = sizeInfo.size.height
if sizeInfo.numberOfLinesUsed < 1 {
r.size.height = 0
}
return r
}
2017-05-27 10:43:27 -07:00
static func rectForDate(_ textBoxRect: NSRect, _ rectAbove: NSRect, _ appearance: TimelineCellAppearance, _ cellData: TimelineCellData) -> NSRect {
let textFieldSize = SingleLineTextFieldSizer.size(for: cellData.dateString, font: appearance.dateFont)
2025-11-05 11:22:10 -08:00
2026-01-02 17:50:35 -08:00
var r = NSRect.zero
r.size = textFieldSize
2026-01-02 17:50:35 -08:00
r.origin.y = rectAbove.maxY + appearance.titleBottomMargin
r.size.width = textFieldSize.width
2017-05-27 10:43:27 -07:00
r.origin.x = textBoxRect.maxX - textFieldSize.width
return r
}
static func rectForFeedName(_ textBoxRect: NSRect, _ dateRect: NSRect, _ appearance: TimelineCellAppearance, _ cellData: TimelineCellData) -> NSRect {
if cellData.showFeedName == .none {
2026-01-02 17:50:35 -08:00
return NSRect.zero
}
let textFieldSize = SingleLineTextFieldSizer.size(for: cellData.feedName, font: appearance.feedNameFont)
2026-01-02 17:50:35 -08:00
var r = NSRect.zero
r.size = textFieldSize
r.origin.y = dateRect.minY
r.origin.x = textBoxRect.origin.x
r.size.width = (textBoxRect.maxX - (dateRect.size.width + appearance.dateMarginLeft)) - textBoxRect.origin.x
2025-11-05 11:22:10 -08:00
return r
}
2017-11-28 21:39:09 -08:00
static func rectForUnreadIndicator(_ appearance: TimelineCellAppearance, _ titleRect: NSRect) -> NSRect {
2026-01-02 17:50:35 -08:00
var r = NSRect.zero
r.size = NSSize(width: appearance.unreadCircleDimension, height: appearance.unreadCircleDimension)
r.origin.x = appearance.cellPadding.left
r.origin.y = titleRect.minY + 6
// r = RSRectCenteredVerticallyInRect(r, titleRect)
// r.origin.y += 1
return r
}
static func rectForStar(_ appearance: TimelineCellAppearance, _ unreadIndicatorRect: NSRect) -> NSRect {
var r = NSRect.zero
r.size.width = appearance.starDimension
r.size.height = appearance.starDimension
r.origin.x = floor(unreadIndicatorRect.origin.x - ((appearance.starDimension - appearance.unreadCircleDimension) / 2.0))
r.origin.y = unreadIndicatorRect.origin.y - 4.0
return r
}
static func rectForIcon(_ cellData: TimelineCellData, _ appearance: TimelineCellAppearance, _ showIcon: Bool, _ textBoxRect: NSRect, _ width: CGFloat, _ height: CGFloat) -> NSRect {
var r = NSRect.zero
if !showIcon {
return r
}
r.size = appearance.iconSize
2019-05-12 20:42:52 -07:00
r.origin.x = appearance.cellPadding.left + appearance.unreadCircleDimension + appearance.unreadCircleMarginRight
r.origin.y = textBoxRect.origin.y + appearance.iconAdjustmentTop
return r
}
2025-11-05 11:22:10 -08:00
2026-10-25 14:55:23 -07:00
static func rectForThumbnail(_ appearance: TimelineCellAppearance, _ thumbnailDimension: CGFloat, _ width: CGFloat) -> NSRect {
if thumbnailDimension <= 0.0 {
return NSRect.zero
}
let originX = floor(width - appearance.cellPadding.right - thumbnailDimension)
return NSRect(x: originX, y: appearance.cellPadding.top, width: thumbnailDimension, height: thumbnailDimension)
}
2026-10-25 16:23:15 -07:00
/// Below the text and icon, `largeThumbnailHeight` tall, from the icon’s left edge
/// (or the text’s, when there’s no icon) to the right padding.
2026-10-25 15:02:32 -07:00
/// In a cell, it’s bottom-aligned, since rows share one height and titles vary in length,
/// and it shrinks rather than overlap the text if the row is shorter than expected.
static func rectForLargeThumbnail(_ cellData: TimelineCellData, _ appearance: TimelineCellAppearance, _ textBoxRect: NSRect, _ iconImageRect: NSRect, _ width: CGFloat, _ height: CGFloat) -> NSRect {
guard cellData.showThumbnail else {
return NSRect.zero
}
2026-10-25 16:23:15 -07:00
let originX = cellData.showIcon ? iconImageRect.minX : textBoxRect.minX
let thumbnailWidth = floor(width - appearance.cellPadding.right - originX)
var thumbnailHeight = appearance.largeThumbnailHeight
2026-10-25 15:02:32 -07:00
let minimumOriginY = ceil(max(textBoxRect.maxY, iconImageRect.maxY) + appearance.largeThumbnailMarginTop)
var originY = minimumOriginY
if height > 0.1 {
let maximumY = floor(height - appearance.cellPadding.bottom)
thumbnailHeight = min(thumbnailHeight, maximumY - minimumOriginY)
originY = maximumY - thumbnailHeight
}
guard thumbnailWidth > 0.0, thumbnailHeight > 0.0 else {
return NSRect.zero
}
2026-10-25 16:23:15 -07:00
return NSRect(x: originX, y: originY, width: thumbnailWidth, height: thumbnailHeight)
2026-10-25 15:02:32 -07:00
}
static func rectForSeparator(_ cellData: TimelineCellData, _ appearance: TimelineCellAppearance, _ alignmentRect: NSRect, _ width: CGFloat, _ height: CGFloat) -> NSRect {
return NSRect(x: alignmentRect.minX, y: height - 1, width: width - alignmentRect.minX, height: 1)
}
2017-11-27 13:16:08 -08:00
}
private extension Array where Element == NSRect {
func maxY() -> CGFloat {
var y: CGFloat = 0.0
for r in self {
y = Swift.max(y, r.maxY)
}
return y
}
2017-05-27 10:43:27 -07:00
}