NetNewsWire/iOS/Timeline/Cell/TimelineCellLayout.swift

115 lines
3.0 KiB
Swift
Raw Normal View History

//
2024-02-26 17:37:15 +01:00
// TimelineCellLayout.swift
2019-04-30 00:19:08 +02:00
// NetNewsWire-iOS
//
2019-04-30 00:19:08 +02:00
// Created by Maurice Parker on 4/29/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import UIKit
import Images
2024-02-26 17:37:15 +01:00
protocol TimelineCellLayout {
2019-04-29 21:40:14 +02:00
2019-04-30 00:19:08 +02:00
var height: CGFloat {get}
var unreadIndicatorRect: CGRect {get}
var starRect: CGRect {get}
var iconImageRect: CGRect {get}
2019-04-30 00:19:08 +02:00
var titleRect: CGRect {get}
var summaryRect: CGRect {get}
var feedNameRect: CGRect {get}
var dateRect: CGRect {get}
}
2024-02-26 17:37:15 +01:00
extension TimelineCellLayout {
2019-04-30 00:19:08 +02:00
2019-04-29 21:40:14 +02:00
static func rectForUnreadIndicator(_ point: CGPoint) -> CGRect {
var r = CGRect.zero
2024-02-26 17:37:15 +01:00
r.size = CGSize(width: TimelineDefaultCellLayout.unreadCircleDimension, height: TimelineDefaultCellLayout.unreadCircleDimension)
2019-04-29 21:40:14 +02:00
r.origin.x = point.x
r.origin.y = point.y + 5
2019-04-29 21:40:14 +02:00
return r
}
2019-04-30 00:19:08 +02:00
2019-04-29 21:40:14 +02:00
static func rectForStar(_ point: CGPoint) -> CGRect {
var r = CGRect.zero
2024-02-26 17:37:15 +01:00
r.size.width = TimelineDefaultCellLayout.starDimension
r.size.height = TimelineDefaultCellLayout.starDimension
r.origin.x = floor(point.x - ((TimelineDefaultCellLayout.starDimension - TimelineDefaultCellLayout.unreadCircleDimension) / 2.0))
r.origin.y = point.y + 3
2019-04-29 21:40:14 +02:00
return r
}
2019-04-30 00:19:08 +02:00
static func rectForIconView(_ point: CGPoint, iconSize: IconSize) -> CGRect {
2019-04-29 21:40:14 +02:00
var r = CGRect.zero
2019-11-09 00:16:09 +01:00
r.size = iconSize.size
r.origin.x = point.x
r.origin.y = point.y + 4
2019-04-29 21:40:14 +02:00
return r
}
2024-04-21 00:41:34 +02:00
@MainActor static func rectForTitle(_ cellData: TimelineCellData, _ point: CGPoint, _ textAreaWidth: CGFloat) -> (CGRect, Int) {
2019-04-29 21:40:14 +02:00
var r = CGRect.zero
if cellData.title.isEmpty {
return (r, 0)
}
2019-04-29 21:40:14 +02:00
r.origin = point
2024-02-26 17:37:15 +01:00
let sizeInfo = MultilineUILabelSizer.size(for: cellData.title, font: TimelineDefaultCellLayout.titleFont, numberOfLines: cellData.numberOfLines, width: Int(textAreaWidth))
2019-04-29 21:40:14 +02:00
r.size.width = textAreaWidth
r.size.height = sizeInfo.size.height
if sizeInfo.numberOfLinesUsed < 1 {
r.size.height = 0
}
2019-04-29 21:40:14 +02:00
return (r, sizeInfo.numberOfLinesUsed)
2019-04-29 21:40:14 +02:00
}
2019-04-30 00:19:08 +02:00
2024-04-21 00:41:34 +02:00
@MainActor static func rectForSummary(_ cellData: TimelineCellData, _ point: CGPoint, _ textAreaWidth: CGFloat, _ linesUsed: Int) -> CGRect {
2019-04-29 21:40:14 +02:00
let linesLeft = cellData.numberOfLines - linesUsed
2019-04-29 21:40:14 +02:00
var r = CGRect.zero
if cellData.summary.isEmpty || linesLeft < 1 {
return r
}
2019-04-29 21:40:14 +02:00
r.origin = point
2024-02-26 17:37:15 +01:00
let sizeInfo = MultilineUILabelSizer.size(for: cellData.summary, font: TimelineDefaultCellLayout.summaryFont, numberOfLines: linesLeft, width: Int(textAreaWidth))
2019-04-29 21:40:14 +02:00
r.size.width = textAreaWidth
r.size.height = sizeInfo.size.height
if sizeInfo.numberOfLinesUsed < 1 {
r.size.height = 0
}
2019-04-29 21:40:14 +02:00
return r
2019-04-29 21:40:14 +02:00
}
2024-05-04 07:49:27 +02:00
@MainActor static func rectForFeedName(_ cellData: TimelineCellData, _ point: CGPoint, _ textAreaWidth: CGFloat) -> CGRect {
2019-04-30 00:19:08 +02:00
var r = CGRect.zero
2019-04-29 21:40:14 +02:00
r.origin = point
2019-04-30 00:19:08 +02:00
let feedName = cellData.showFeedName == .feed ? cellData.feedName : cellData.byline
2024-02-26 17:37:15 +01:00
let size = SingleLineUILabelSizer.size(for: feedName, font: TimelineDefaultCellLayout.feedNameFont)
2019-04-29 21:40:14 +02:00
r.size = size
if r.size.width > textAreaWidth {
r.size.width = textAreaWidth
}
return r
2019-04-29 21:40:14 +02:00
}
2019-04-29 21:40:14 +02:00
}