2017-05-27 19:43:27 +02:00
|
|
|
//
|
|
|
|
// TimelineCellLayout.swift
|
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
let unreadIndicatorRect: NSRect
|
2018-02-18 06:46:19 +01:00
|
|
|
let starRect: NSRect
|
2017-11-27 22:16:08 +01:00
|
|
|
let avatarImageRect: NSRect
|
2017-05-27 19:43:27 +02:00
|
|
|
let paddingBottom: CGFloat
|
|
|
|
|
2018-02-18 06:46:19 +01:00
|
|
|
init(width: CGFloat, feedNameRect: NSRect, dateRect: NSRect, titleRect: NSRect, unreadIndicatorRect: NSRect, starRect: NSRect, avatarImageRect: NSRect, paddingBottom: CGFloat) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
self.width = width
|
|
|
|
self.feedNameRect = feedNameRect
|
|
|
|
self.dateRect = dateRect
|
|
|
|
self.titleRect = titleRect
|
|
|
|
self.unreadIndicatorRect = unreadIndicatorRect
|
2018-02-18 06:46:19 +01:00
|
|
|
self.starRect = starRect
|
2017-11-27 22:16:08 +01:00
|
|
|
self.avatarImageRect = avatarImageRect
|
2017-05-27 19:43:27 +02:00
|
|
|
self.paddingBottom = paddingBottom
|
2018-02-19 00:13:47 +01:00
|
|
|
|
|
|
|
self.height = [feedNameRect, dateRect, titleRect, unreadIndicatorRect, avatarImageRect].maxY() + paddingBottom
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
2018-02-19 00:13:47 +01:00
|
|
|
init(width: CGFloat, cellData: TimelineCellData, appearance: TimelineCellAppearance) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2018-02-19 00:13:47 +01:00
|
|
|
var textBoxRect = TimelineCellLayout.rectForTextBox(appearance, cellData, width)
|
2017-11-29 06:39:09 +01:00
|
|
|
|
2018-02-20 05:28:00 +01:00
|
|
|
let titleRect = TimelineCellLayout.rectForTitle(textBoxRect, cellData)
|
2018-02-19 00:13:47 +01:00
|
|
|
let dateRect = TimelineCellLayout.rectForDate(textBoxRect, titleRect, appearance, cellData)
|
|
|
|
let feedNameRect = TimelineCellLayout.rectForFeedName(textBoxRect, dateRect, appearance, cellData)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2018-02-19 00:13:47 +01:00
|
|
|
textBoxRect.size.height = ceil([titleRect, dateRect, feedNameRect].maxY() - textBoxRect.origin.y)
|
|
|
|
let avatarImageRect = TimelineCellLayout.rectForAvatar(cellData, appearance, textBoxRect, width)
|
2018-02-20 05:28:00 +01:00
|
|
|
let unreadIndicatorRect = TimelineCellLayout.rectForUnreadIndicator(appearance, titleRect)
|
|
|
|
let starRect = TimelineCellLayout.rectForStar(appearance, unreadIndicatorRect)
|
2018-02-19 00:13:47 +01:00
|
|
|
|
|
|
|
let paddingBottom = appearance.cellPadding.bottom
|
|
|
|
|
|
|
|
self.init(width: width, feedNameRect: feedNameRect, dateRect: dateRect, titleRect: titleRect, unreadIndicatorRect: unreadIndicatorRect, starRect: starRect, avatarImageRect: avatarImageRect, 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 {
|
|
|
|
|
|
|
|
let layout = TimelineCellLayout(width: width, cellData: cellData, appearance: appearance)
|
|
|
|
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
|
|
|
|
2018-02-19 00:13:47 +01:00
|
|
|
static func rectForTextBox(_ appearance: TimelineCellAppearance, _ cellData: TimelineCellData, _ 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
|
|
|
|
2018-02-19 00:13:47 +01:00
|
|
|
let textBoxOriginX = appearance.cellPadding.left + appearance.unreadCircleDimension + appearance.unreadCircleMarginRight
|
|
|
|
let textBoxMaxX = floor((width - appearance.cellPadding.right) - (cellData.showAvatar ? appearance.avatarSize.width + appearance.avatarMarginLeft : 0.0))
|
|
|
|
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-20 05:28:00 +01:00
|
|
|
static func rectForTitle(_ textBoxRect: NSRect, _ cellData: TimelineCellData) -> NSRect {
|
2018-02-19 00:13:47 +01:00
|
|
|
|
|
|
|
var r = textBoxRect
|
2018-02-20 05:28:00 +01:00
|
|
|
let height = MultilineTextFieldSizer.size(for: cellData.attributedTitle, numberOfLines: 2, width: Int(textBoxRect.width))
|
|
|
|
r.size.height = CGFloat(height)
|
2018-02-19 00:13:47 +01:00
|
|
|
|
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-19 00:13:47 +01:00
|
|
|
static func rectForDate(_ textBoxRect: NSRect, _ titleRect: NSRect, _ appearance: TimelineCellAppearance, _ cellData: TimelineCellData) -> NSRect {
|
|
|
|
|
|
|
|
return rectOfLineBelow(textBoxRect, titleRect, appearance.titleBottomMargin, cellData.attributedDateString)
|
|
|
|
}
|
|
|
|
|
|
|
|
static func rectForFeedName(_ textBoxRect: NSRect, _ dateRect: NSRect, _ appearance: TimelineCellAppearance, _ cellData: TimelineCellData) -> NSRect {
|
|
|
|
|
|
|
|
if !cellData.showFeedName {
|
|
|
|
return NSZeroRect
|
|
|
|
}
|
|
|
|
|
|
|
|
return rectOfLineBelow(textBoxRect, dateRect, appearance.titleBottomMargin, cellData.attributedFeedName)
|
|
|
|
}
|
|
|
|
|
|
|
|
static func rectOfLineBelow(_ textBoxRect: NSRect, _ rectAbove: NSRect, _ topMargin: CGFloat, _ attributedString: NSAttributedString) -> NSRect {
|
|
|
|
|
2018-02-20 00:56:15 +01:00
|
|
|
let font = attributedString.attribute(NSAttributedStringKey.font, at: 0, effectiveRange: nil) as! NSFont
|
|
|
|
let textFieldSize = SingleLineTextFieldSizer.size(for: attributedString.string, font: font)
|
2018-02-19 00:13:47 +01:00
|
|
|
var r = NSZeroRect
|
2018-02-20 00:56:15 +01:00
|
|
|
r.size = textFieldSize
|
2018-02-19 00:13:47 +01:00
|
|
|
r.origin.y = NSMaxY(rectAbove) + topMargin
|
|
|
|
r.origin.x = textBoxRect.origin.x
|
|
|
|
|
2018-02-20 00:56:15 +01:00
|
|
|
var width = textFieldSize.width
|
2018-02-19 00:13:47 +01:00
|
|
|
width = min(width, textBoxRect.size.width)
|
|
|
|
width = max(width, 0.0)
|
|
|
|
r.size.width = width
|
2017-11-29 06:39:09 +01:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
static func rectForAvatar(_ cellData: TimelineCellData, _ appearance: TimelineCellAppearance, _ textBoxRect: NSRect, _ width: CGFloat) -> NSRect {
|
|
|
|
|
|
|
|
var r = NSRect.zero
|
|
|
|
if !cellData.showAvatar {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
r.size = appearance.avatarSize
|
|
|
|
r.origin.x = (width - appearance.cellPadding.right) - r.size.width
|
|
|
|
r = RSRectCenteredVerticallyInRect(r, textBoxRect)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
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
|
|
|
|