2017-05-27 19:43:27 +02:00
|
|
|
//
|
|
|
|
// TimelineCellLayout.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
2017-05-27 19:43:27 +02:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 2/6/16.
|
|
|
|
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2018-02-03 07:51:32 +01:00
|
|
|
import AppKit
|
2017-05-27 19:43:27 +02:00
|
|
|
import RSCore
|
|
|
|
|
|
|
|
struct TimelineCellLayout {
|
|
|
|
|
|
|
|
let width: CGFloat
|
|
|
|
let height: CGFloat
|
|
|
|
let feedNameRect: NSRect
|
|
|
|
let dateRect: NSRect
|
|
|
|
let titleRect: NSRect
|
2018-02-22 07:48:34 +01:00
|
|
|
let numberOfLinesForTitle: Int
|
|
|
|
let summaryRect: NSRect
|
|
|
|
let textRect: NSRect
|
2017-05-27 19:43:27 +02:00
|
|
|
let unreadIndicatorRect: NSRect
|
2018-02-18 06:46:19 +01:00
|
|
|
let starRect: NSRect
|
2019-11-06 01:05:57 +01:00
|
|
|
let iconImageRect: NSRect
|
2019-09-02 18:13:21 +02:00
|
|
|
let separatorRect: NSRect
|
2017-05-27 19:43:27 +02:00
|
|
|
let paddingBottom: CGFloat
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
init(width: CGFloat, height: CGFloat, feedNameRect: NSRect, dateRect: NSRect, titleRect: NSRect, numberOfLinesForTitle: Int, summaryRect: NSRect, textRect: NSRect, unreadIndicatorRect: NSRect, starRect: NSRect, iconImageRect: NSRect, separatorRect: NSRect, paddingBottom: CGFloat) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
self.width = width
|
|
|
|
self.feedNameRect = feedNameRect
|
|
|
|
self.dateRect = dateRect
|
|
|
|
self.titleRect = titleRect
|
2018-02-22 07:48:34 +01:00
|
|
|
self.numberOfLinesForTitle = numberOfLinesForTitle
|
|
|
|
self.summaryRect = summaryRect
|
|
|
|
self.textRect = textRect
|
2017-05-27 19:43:27 +02:00
|
|
|
self.unreadIndicatorRect = unreadIndicatorRect
|
2018-02-18 06:46:19 +01:00
|
|
|
self.starRect = starRect
|
2019-11-06 01:05:57 +01:00
|
|
|
self.iconImageRect = iconImageRect
|
2019-09-02 18:13:21 +02:00
|
|
|
self.separatorRect = separatorRect
|
2017-05-27 19:43:27 +02:00
|
|
|
self.paddingBottom = paddingBottom
|
2018-02-19 00:13:47 +01:00
|
|
|
|
2018-02-24 20:41:02 +01:00
|
|
|
if height > 0.1 {
|
|
|
|
self.height = height
|
|
|
|
}
|
|
|
|
else {
|
2019-11-06 01:05:57 +01:00
|
|
|
self.height = [feedNameRect, dateRect, titleRect, summaryRect, textRect, unreadIndicatorRect, iconImageRect].maxY() + paddingBottom
|
2018-02-24 20:41:02 +01:00
|
|
|
}
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
init(width: CGFloat, height: CGFloat, cellData: TimelineCellData, appearance: TimelineCellAppearance, hasIcon: Bool) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2018-02-24 20:41:02 +01:00
|
|
|
// If height == 0.0, then height is calculated.
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
let showIcon = cellData.showIcon
|
|
|
|
var textBoxRect = TimelineCellLayout.rectForTextBox(appearance, cellData, showIcon, width)
|
2017-11-29 06:39:09 +01:00
|
|
|
|
2018-02-22 07:48:34 +01:00
|
|
|
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)
|
2018-02-24 07:15:35 +01:00
|
|
|
|
|
|
|
var lastTextRect = titleRect
|
|
|
|
if numberOfLinesForTitle == 0 {
|
|
|
|
lastTextRect = textRect
|
|
|
|
}
|
2020-03-24 03:43:54 +01:00
|
|
|
else if numberOfLinesForTitle < appearance.titleNumberOfLines {
|
2018-02-24 07:20:59 +01:00
|
|
|
if summaryRect.height > 0.1 {
|
|
|
|
lastTextRect = summaryRect
|
|
|
|
}
|
2018-02-24 07:15:35 +01:00
|
|
|
}
|
|
|
|
let dateRect = TimelineCellLayout.rectForDate(textBoxRect, lastTextRect, appearance, cellData)
|
2018-02-19 00:13:47 +01:00
|
|
|
let feedNameRect = TimelineCellLayout.rectForFeedName(textBoxRect, dateRect, appearance, cellData)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2018-02-22 07:48:34 +01:00
|
|
|
textBoxRect.size.height = ceil([titleRect, summaryRect, textRect, dateRect, feedNameRect].maxY() - textBoxRect.origin.y)
|
2019-11-06 01:05:57 +01:00
|
|
|
let iconImageRect = TimelineCellLayout.rectForIcon(cellData, appearance, showIcon, textBoxRect, width, height)
|
2018-02-22 07:48:34 +01:00
|
|
|
let unreadIndicatorRect = TimelineCellLayout.rectForUnreadIndicator(appearance, textBoxRect)
|
2018-02-20 05:28:00 +01:00
|
|
|
let starRect = TimelineCellLayout.rectForStar(appearance, unreadIndicatorRect)
|
2019-11-06 01:05:57 +01:00
|
|
|
let separatorRect = TimelineCellLayout.rectForSeparator(cellData, appearance, showIcon ? iconImageRect : titleRect, width, height)
|
2018-02-19 00:13:47 +01:00
|
|
|
|
|
|
|
let paddingBottom = appearance.cellPadding.bottom
|
|
|
|
|
2019-11-06 01:05:57 +01: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, separatorRect: separatorRect, paddingBottom: paddingBottom)
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
2018-02-19 00:13:47 +01:00
|
|
|
static func height(for width: CGFloat, cellData: TimelineCellData, appearance: TimelineCellAppearance) -> CGFloat {
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
let layout = TimelineCellLayout(width: width, height: 0.0, cellData: cellData, appearance: appearance, hasIcon: true)
|
2018-02-19 00:13:47 +01:00
|
|
|
return layout.height
|
|
|
|
}
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
2018-02-19 00:13:47 +01:00
|
|
|
// MARK: - Calculate Rects
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2018-02-19 00:13:47 +01:00
|
|
|
private extension TimelineCellLayout {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
static func rectForTextBox(_ appearance: TimelineCellAppearance, _ cellData: TimelineCellData, _ showIcon: Bool, _ width: CGFloat) -> NSRect {
|
2017-11-29 06:39:09 +01:00
|
|
|
|
2018-02-19 00:13:47 +01:00
|
|
|
// Returned height is a placeholder. Not needed when this is calculated.
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
let iconSpace = showIcon ? appearance.iconSize.width + appearance.iconMarginRight : 0.0
|
|
|
|
let textBoxOriginX = appearance.cellPadding.left + appearance.unreadCircleDimension + appearance.unreadCircleMarginRight + iconSpace
|
2019-05-13 05:42:52 +02:00
|
|
|
let textBoxMaxX = floor(width - appearance.cellPadding.right)
|
2018-02-19 00:13:47 +01:00
|
|
|
let textBoxWidth = floor(textBoxMaxX - textBoxOriginX)
|
|
|
|
let textBoxRect = NSRect(x: textBoxOriginX, y: appearance.cellPadding.top, width: textBoxWidth, height: 1000000)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2018-02-19 00:13:47 +01:00
|
|
|
return textBoxRect
|
|
|
|
}
|
2018-02-18 06:46:19 +01:00
|
|
|
|
2018-02-22 07:48:34 +01:00
|
|
|
static func rectForTitle(_ textBoxRect: NSRect, _ appearance: TimelineCellAppearance, _ cellData: TimelineCellData) -> (NSRect, Int) {
|
2018-02-19 00:13:47 +01:00
|
|
|
|
|
|
|
var r = textBoxRect
|
|
|
|
|
2018-02-22 07:48:34 +01:00
|
|
|
if cellData.title.isEmpty {
|
|
|
|
r.size.height = 0
|
|
|
|
return (r, 0)
|
|
|
|
}
|
|
|
|
|
2020-04-07 23:05:13 +02:00
|
|
|
let attributedTitle = cellData.attributedTitle.adding(font: appearance.titleFont)
|
|
|
|
let sizeInfo = MultilineTextFieldSizer.size(for: attributedTitle, numberOfLines: appearance.titleNumberOfLines, width: Int(textBoxRect.width))
|
2018-02-22 07:48:34 +01:00
|
|
|
r.size.height = sizeInfo.size.height
|
|
|
|
if sizeInfo.numberOfLinesUsed < 1 {
|
|
|
|
r.size.height = 0
|
|
|
|
}
|
|
|
|
return (r, sizeInfo.numberOfLinesUsed)
|
|
|
|
}
|
|
|
|
|
|
|
|
static func rectForSummary(_ textBoxRect: NSRect, _ titleRect: NSRect, _ titleNumberOfLines: Int, _ appearance: TimelineCellAppearance, _ cellData: TimelineCellData) -> NSRect {
|
|
|
|
if titleNumberOfLines >= appearance.titleNumberOfLines || cellData.text.isEmpty {
|
|
|
|
return NSRect.zero
|
|
|
|
}
|
|
|
|
|
2020-03-24 03:43:54 +01:00
|
|
|
var r = textBoxRect
|
|
|
|
r.origin.y = NSMaxY(titleRect)
|
|
|
|
let summaryNumberOfLines = appearance.titleNumberOfLines - titleNumberOfLines
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-02-22 07:48:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2018-02-20 05:28:00 +01:00
|
|
|
return r
|
2018-02-19 00:13:47 +01:00
|
|
|
}
|
2018-02-18 06:46:19 +01:00
|
|
|
|
2018-02-22 07:48:34 +01:00
|
|
|
static func rectForDate(_ textBoxRect: NSRect, _ rectAbove: NSRect, _ appearance: TimelineCellAppearance, _ cellData: TimelineCellData) -> NSRect {
|
2020-03-24 03:43:54 +01:00
|
|
|
let textFieldSize = SingleLineTextFieldSizer.size(for: cellData.dateString, font: appearance.dateFont)
|
|
|
|
|
|
|
|
var r = NSZeroRect
|
|
|
|
r.size = textFieldSize
|
|
|
|
r.origin.y = NSMaxY(rectAbove) + appearance.titleBottomMargin
|
|
|
|
r.size.width = textFieldSize.width
|
|
|
|
|
|
|
|
r.origin.x = textBoxRect.maxX - textFieldSize.width
|
2018-02-19 00:13:47 +01:00
|
|
|
|
2020-03-24 03:43:54 +01:00
|
|
|
return r
|
2018-02-19 00:13:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static func rectForFeedName(_ textBoxRect: NSRect, _ dateRect: NSRect, _ appearance: TimelineCellAppearance, _ cellData: TimelineCellData) -> NSRect {
|
2020-04-18 14:53:56 +02:00
|
|
|
if cellData.showFeedName == .none {
|
2018-02-19 00:13:47 +01:00
|
|
|
return NSZeroRect
|
|
|
|
}
|
|
|
|
|
2020-03-24 03:43:54 +01:00
|
|
|
let textFieldSize = SingleLineTextFieldSizer.size(for: cellData.feedName, font: appearance.feedNameFont)
|
2018-02-19 00:13:47 +01:00
|
|
|
var r = NSZeroRect
|
2018-02-20 00:56:15 +01:00
|
|
|
r.size = textFieldSize
|
2020-03-24 03:43:54 +01:00
|
|
|
r.origin.y = dateRect.minY
|
2018-02-19 00:13:47 +01:00
|
|
|
r.origin.x = textBoxRect.origin.x
|
2020-03-24 03:43:54 +01:00
|
|
|
r.size.width = (textBoxRect.maxX - (dateRect.size.width + appearance.dateMarginLeft)) - textBoxRect.origin.x
|
|
|
|
|
2018-01-05 06:20:09 +01:00
|
|
|
return r
|
|
|
|
}
|
2017-11-27 22:16:08 +01:00
|
|
|
|
2018-02-20 05:28:00 +01:00
|
|
|
static func rectForUnreadIndicator(_ appearance: TimelineCellAppearance, _ titleRect: NSRect) -> NSRect {
|
2017-11-27 22:16:08 +01:00
|
|
|
|
2018-02-19 00:13:47 +01:00
|
|
|
var r = NSZeroRect
|
|
|
|
r.size = NSSize(width: appearance.unreadCircleDimension, height: appearance.unreadCircleDimension)
|
|
|
|
r.origin.x = appearance.cellPadding.left
|
2018-02-20 05:28:00 +01:00
|
|
|
r.origin.y = titleRect.minY + 6
|
|
|
|
// r = RSRectCenteredVerticallyInRect(r, titleRect)
|
|
|
|
// r.origin.y += 1
|
2018-02-19 00:13:47 +01:00
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
2018-01-05 06:20:09 +01:00
|
|
|
|
2018-02-19 00:13:47 +01:00
|
|
|
static func rectForStar(_ appearance: TimelineCellAppearance, _ unreadIndicatorRect: NSRect) -> NSRect {
|
2017-11-27 22:16:08 +01:00
|
|
|
|
2018-02-19 00:13:47 +01:00
|
|
|
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))
|
2018-02-20 05:28:00 +01:00
|
|
|
r.origin.y = unreadIndicatorRect.origin.y - 4.0
|
2018-02-19 00:13:47 +01:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
static func rectForIcon(_ cellData: TimelineCellData, _ appearance: TimelineCellAppearance, _ showIcon: Bool, _ textBoxRect: NSRect, _ width: CGFloat, _ height: CGFloat) -> NSRect {
|
2018-02-19 00:13:47 +01:00
|
|
|
|
|
|
|
var r = NSRect.zero
|
2019-11-06 01:05:57 +01:00
|
|
|
if !showIcon {
|
2018-02-19 00:13:47 +01:00
|
|
|
return r
|
|
|
|
}
|
2019-11-06 01:05:57 +01:00
|
|
|
r.size = appearance.iconSize
|
2019-05-13 05:42:52 +02:00
|
|
|
r.origin.x = appearance.cellPadding.left + appearance.unreadCircleDimension + appearance.unreadCircleMarginRight
|
2019-11-06 01:05:57 +01:00
|
|
|
r.origin.y = textBoxRect.origin.y + appearance.iconAdjustmentTop
|
2018-02-19 00:13:47 +01:00
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
2019-09-02 18:13:21 +02: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-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
2018-02-19 00:13:47 +01:00
|
|
|
private extension Array where Element == NSRect {
|
|
|
|
|
|
|
|
func maxY() -> CGFloat {
|
|
|
|
|
|
|
|
var y: CGFloat = 0.0
|
|
|
|
self.forEach { y = Swift.max(y, $0.maxY) }
|
|
|
|
return y
|
|
|
|
}
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
2018-02-19 00:13:47 +01:00
|
|
|
|