2019-04-15 22:03:05 +02:00
|
|
|
//
|
|
|
|
// MasterTableViewCellLayout.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 11/24/17.
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import RSCore
|
|
|
|
|
|
|
|
struct MasterTableViewCellLayout {
|
|
|
|
|
2019-04-20 17:58:16 +02:00
|
|
|
private static let indent = CGFloat(integerLiteral: 20)
|
|
|
|
private static let editingControlIndent = CGFloat(integerLiteral: 40)
|
2019-04-15 22:03:05 +02:00
|
|
|
private static let imageSize = CGSize(width: 16, height: 16)
|
2019-04-18 21:20:47 +02:00
|
|
|
private static let marginLeft = CGFloat(integerLiteral: 8)
|
2019-04-15 22:03:05 +02:00
|
|
|
private static let imageMarginRight = CGFloat(integerLiteral: 8)
|
|
|
|
private static let unreadCountMarginLeft = CGFloat(integerLiteral: 8)
|
2019-04-18 12:41:27 +02:00
|
|
|
private static let unreadCountMarginRight = CGFloat(integerLiteral: 0)
|
|
|
|
private static let disclosureButtonSize = CGSize(width: 44, height: 44)
|
2019-04-17 17:15:44 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
let faviconRect: CGRect
|
|
|
|
let titleRect: CGRect
|
|
|
|
let unreadCountRect: CGRect
|
2019-04-18 12:41:27 +02:00
|
|
|
let disclosureButtonRect: CGRect
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-04-18 12:41:27 +02:00
|
|
|
init(cellSize: CGSize, shouldShowImage: Bool, label: UILabel, unreadCountView: MasterUnreadCountView, showingEditingControl: Bool, indent: Bool, shouldShowDisclosure: Bool) {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-04-20 17:58:16 +02:00
|
|
|
var initialIndent = MasterTableViewCellLayout.marginLeft
|
|
|
|
if indent {
|
|
|
|
initialIndent += MasterTableViewCellLayout.indent
|
|
|
|
}
|
|
|
|
if showingEditingControl {
|
|
|
|
initialIndent += MasterTableViewCellLayout.editingControlIndent
|
|
|
|
}
|
|
|
|
|
|
|
|
let bounds = CGRect(x: initialIndent, y: 0.0, width: floor(cellSize.width - initialIndent), height: floor(cellSize.height))
|
|
|
|
|
2019-04-18 12:41:27 +02:00
|
|
|
// Favicon
|
2019-04-15 22:03:05 +02:00
|
|
|
var rFavicon = CGRect.zero
|
|
|
|
if shouldShowImage {
|
2019-04-20 17:58:16 +02:00
|
|
|
rFavicon = CGRect(x: bounds.origin.x, y: 0.0, width: MasterTableViewCellLayout.imageSize.width, height: MasterTableViewCellLayout.imageSize.height)
|
2019-04-15 22:03:05 +02:00
|
|
|
rFavicon = MasterTableViewCellLayout.centerVertically(rFavicon, bounds)
|
|
|
|
}
|
|
|
|
self.faviconRect = rFavicon
|
|
|
|
|
2019-04-18 12:41:27 +02:00
|
|
|
// Title
|
2019-04-15 22:03:05 +02:00
|
|
|
let labelSize = SingleLineUILabelSizer.size(for: label.text ?? "", font: label.font!)
|
|
|
|
|
|
|
|
var rLabel = CGRect(x: 0.0, y: 0.0, width: labelSize.width, height: labelSize.height)
|
|
|
|
if shouldShowImage {
|
|
|
|
rLabel.origin.x = rFavicon.maxX + MasterTableViewCellLayout.imageMarginRight
|
2019-04-18 14:24:55 +02:00
|
|
|
} else {
|
2019-04-20 17:58:16 +02:00
|
|
|
rLabel.origin.x = bounds.minX
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-04-18 14:24:55 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
rLabel = MasterTableViewCellLayout.centerVertically(rLabel, bounds)
|
|
|
|
|
2019-04-18 12:41:27 +02:00
|
|
|
// Unread Count
|
2019-04-15 22:03:05 +02:00
|
|
|
let unreadCountSize = unreadCountView.intrinsicContentSize
|
|
|
|
let unreadCountIsHidden = unreadCountView.unreadCount < 1
|
|
|
|
|
|
|
|
var rUnread = CGRect.zero
|
|
|
|
if !unreadCountIsHidden {
|
2019-04-18 12:41:27 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
rUnread.size = unreadCountSize
|
2019-04-18 12:41:27 +02:00
|
|
|
rUnread.origin.x = bounds.maxX -
|
|
|
|
(unreadCountSize.width + MasterTableViewCellLayout.unreadCountMarginRight + MasterTableViewCellLayout.disclosureButtonSize.width)
|
2019-04-15 22:03:05 +02:00
|
|
|
rUnread = MasterTableViewCellLayout.centerVertically(rUnread, bounds)
|
2019-04-18 12:41:27 +02:00
|
|
|
|
|
|
|
// Cap the Title width based on the unread indicator button
|
2019-04-15 22:03:05 +02:00
|
|
|
let labelMaxX = rUnread.minX - MasterTableViewCellLayout.unreadCountMarginLeft
|
|
|
|
if rLabel.maxX > labelMaxX {
|
|
|
|
rLabel.size.width = labelMaxX - rLabel.minX
|
|
|
|
}
|
2019-04-18 12:41:27 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
self.unreadCountRect = rUnread
|
2019-04-18 12:41:27 +02:00
|
|
|
|
|
|
|
// Disclosure Button
|
|
|
|
var rDisclosure = CGRect.zero
|
|
|
|
if shouldShowDisclosure {
|
|
|
|
|
|
|
|
rDisclosure.size = MasterTableViewCellLayout.disclosureButtonSize
|
|
|
|
rDisclosure.origin.x = bounds.maxX - MasterTableViewCellLayout.disclosureButtonSize.width
|
|
|
|
rDisclosure = MasterTableViewCellLayout.centerVertically(rDisclosure, bounds)
|
|
|
|
|
|
|
|
// Cap the Title width based on the disclosure button
|
|
|
|
let labelMaxX = rDisclosure.minX
|
|
|
|
if rLabel.maxX > labelMaxX {
|
|
|
|
rLabel.size.width = labelMaxX - rLabel.minX
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
self.disclosureButtonRect = rDisclosure
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-04-18 12:41:27 +02:00
|
|
|
|
|
|
|
// Cap the Title width based on total width
|
2019-04-15 22:03:05 +02:00
|
|
|
if rLabel.maxX > bounds.maxX {
|
2019-04-17 14:31:08 +02:00
|
|
|
rLabel.size.width = bounds.maxX - rLabel.minX
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self.titleRect = rLabel
|
2019-04-18 12:41:27 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ideally this will be implemented in RSCore (see RSGeometry)
|
|
|
|
static func centerVertically(_ originalRect: CGRect, _ containerRect: CGRect) -> CGRect {
|
|
|
|
var result = originalRect
|
|
|
|
result.origin.y = containerRect.midY - (result.height / 2.0)
|
|
|
|
result = result.integral
|
|
|
|
result.size = originalRect.size
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|