2017-05-27 19:43:27 +02:00
|
|
|
//
|
|
|
|
// TimelineTableCellView.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
2017-05-27 19:43:27 +02:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 8/31/15.
|
|
|
|
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2020-11-19 05:02:54 +01:00
|
|
|
import AppKit
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
class TimelineTableCellView: NSTableCellView {
|
|
|
|
|
2018-02-20 06:26:15 +01:00
|
|
|
private let titleView = TimelineTableCellView.multiLineTextField()
|
2020-03-24 03:43:54 +01:00
|
|
|
private let summaryView = TimelineTableCellView.multiLineTextField()
|
2018-02-24 07:15:35 +01:00
|
|
|
private let textView = TimelineTableCellView.multiLineTextField()
|
2018-02-18 07:37:33 +01:00
|
|
|
private let unreadIndicatorView = UnreadIndicatorView(frame: NSZeroRect)
|
2018-02-20 00:56:15 +01:00
|
|
|
private let dateView = TimelineTableCellView.singleLineTextField()
|
|
|
|
private let feedNameView = TimelineTableCellView.singleLineTextField()
|
2018-02-24 08:31:59 +01:00
|
|
|
|
2019-11-12 16:22:23 +01:00
|
|
|
private lazy var iconView = IconView()
|
2018-02-24 08:31:59 +01:00
|
|
|
|
2020-08-20 03:00:34 +02:00
|
|
|
private var starView = TimelineTableCellView.imageView(with: AppAssets.timelineStarUnselected, scaling: .scaleNone)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2018-02-20 00:56:15 +01:00
|
|
|
private lazy var textFields = {
|
2018-02-24 07:15:35 +01:00
|
|
|
return [self.dateView, self.feedNameView, self.titleView, self.summaryView, self.textView]
|
2018-02-18 07:23:36 +01:00
|
|
|
}()
|
2017-12-30 21:45:10 +01:00
|
|
|
|
2017-12-31 21:10:30 +01:00
|
|
|
var cellAppearance: TimelineCellAppearance! {
|
|
|
|
didSet {
|
2019-02-08 07:01:31 +01:00
|
|
|
if cellAppearance != oldValue {
|
|
|
|
updateTextFieldFonts()
|
2019-11-06 01:05:57 +01:00
|
|
|
iconView.layer?.cornerRadius = cellAppearance.iconCornerRadius
|
2019-02-08 07:01:31 +01:00
|
|
|
needsLayout = true
|
|
|
|
}
|
2017-12-31 21:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
var cellData: TimelineCellData! {
|
|
|
|
didSet {
|
|
|
|
updateSubviews()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-21 23:08:16 +02:00
|
|
|
var isEmphasized: Bool = false {
|
|
|
|
didSet {
|
|
|
|
unreadIndicatorView.isEmphasized = isEmphasized
|
2020-08-20 03:00:34 +02:00
|
|
|
updateStarView()
|
2019-05-21 23:08:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var isSelected: Bool = false {
|
|
|
|
didSet {
|
|
|
|
unreadIndicatorView.isSelected = isSelected
|
2020-08-20 03:00:34 +02:00
|
|
|
updateStarView()
|
2019-05-21 23:08:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
override var isFlipped: Bool {
|
|
|
|
return true
|
|
|
|
}
|
2017-11-12 06:53:43 +01:00
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
override init(frame frameRect: NSRect) {
|
|
|
|
super.init(frame: frameRect)
|
|
|
|
commonInit()
|
|
|
|
}
|
|
|
|
|
2019-02-14 06:33:50 +01:00
|
|
|
required init?(coder: NSCoder) {
|
2017-05-27 19:43:27 +02:00
|
|
|
super.init(coder: coder)
|
|
|
|
commonInit()
|
|
|
|
}
|
2019-02-14 06:33:50 +01:00
|
|
|
|
|
|
|
convenience init() {
|
|
|
|
self.init(frame: NSRect.zero)
|
|
|
|
}
|
2019-09-02 18:13:21 +02:00
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
override func setFrameSize(_ newSize: NSSize) {
|
|
|
|
|
|
|
|
if newSize == self.frame.size {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
super.setFrameSize(newSize)
|
|
|
|
needsLayout = true
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidMoveToSuperview() {
|
|
|
|
|
|
|
|
updateSubviews()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func layout() {
|
|
|
|
|
|
|
|
resizeSubviews(withOldSize: NSZeroSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func resizeSubviews(withOldSize oldSize: NSSize) {
|
|
|
|
|
|
|
|
let layoutRects = updatedLayoutRects()
|
2018-02-24 07:15:35 +01:00
|
|
|
|
|
|
|
setFrame(for: titleView, rect: layoutRects.titleRect)
|
|
|
|
setFrame(for: summaryView, rect: layoutRects.summaryRect)
|
|
|
|
setFrame(for: textView, rect: layoutRects.textRect)
|
|
|
|
|
2020-01-09 06:40:21 +01:00
|
|
|
dateView.setFrame(ifNotEqualTo: layoutRects.dateRect)
|
|
|
|
unreadIndicatorView.setFrame(ifNotEqualTo: layoutRects.unreadIndicatorRect)
|
|
|
|
feedNameView.setFrame(ifNotEqualTo: layoutRects.feedNameRect)
|
|
|
|
iconView.setFrame(ifNotEqualTo: layoutRects.iconImageRect)
|
|
|
|
starView.setFrame(ifNotEqualTo: layoutRects.starRect)
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
2018-02-18 07:37:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
|
|
private extension TimelineTableCellView {
|
|
|
|
|
2018-02-20 00:56:15 +01:00
|
|
|
static func singleLineTextField() -> NSTextField {
|
|
|
|
|
|
|
|
let textField = NSTextField(labelWithString: "")
|
|
|
|
textField.usesSingleLineMode = true
|
|
|
|
textField.maximumNumberOfLines = 1
|
|
|
|
textField.isEditable = false
|
|
|
|
textField.lineBreakMode = .byTruncatingTail
|
2018-02-21 07:32:14 +01:00
|
|
|
textField.allowsDefaultTighteningForTruncation = false
|
2018-02-20 00:56:15 +01:00
|
|
|
return textField
|
|
|
|
}
|
|
|
|
|
2018-02-20 06:26:15 +01:00
|
|
|
static func multiLineTextField() -> NSTextField {
|
|
|
|
|
|
|
|
let textField = NSTextField(wrappingLabelWithString: "")
|
|
|
|
textField.usesSingleLineMode = false
|
2020-03-24 03:43:54 +01:00
|
|
|
textField.maximumNumberOfLines = 0
|
2018-02-20 06:26:15 +01:00
|
|
|
textField.isEditable = false
|
|
|
|
textField.cell?.truncatesLastVisibleLine = true
|
2018-02-21 07:32:14 +01:00
|
|
|
textField.allowsDefaultTighteningForTruncation = false
|
2018-02-20 06:26:15 +01:00
|
|
|
return textField
|
|
|
|
}
|
|
|
|
|
2018-02-20 00:56:15 +01:00
|
|
|
static func imageView(with image: NSImage?, scaling: NSImageScaling) -> NSImageView {
|
|
|
|
|
|
|
|
let imageView = image != nil ? NSImageView(image: image!) : NSImageView(frame: NSRect.zero)
|
|
|
|
imageView.animates = false
|
|
|
|
imageView.imageAlignment = .alignCenter
|
|
|
|
imageView.imageScaling = scaling
|
|
|
|
return imageView
|
|
|
|
}
|
2019-09-02 18:13:21 +02:00
|
|
|
|
2018-02-24 07:15:35 +01:00
|
|
|
func setFrame(for textField: NSTextField, rect: NSRect) {
|
|
|
|
|
|
|
|
if Int(floor(rect.height)) == 0 || Int(floor(rect.width)) == 0 {
|
|
|
|
hideView(textField)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
showView(textField)
|
2020-01-09 06:40:21 +01:00
|
|
|
textField.setFrame(ifNotEqualTo: rect)
|
2018-02-24 07:15:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 05:53:19 +02:00
|
|
|
func makeTextFieldColorsNormal() {
|
2018-07-18 22:34:44 +02:00
|
|
|
titleView.textColor = NSColor.labelColor
|
|
|
|
feedNameView.textColor = NSColor.secondaryLabelColor
|
|
|
|
dateView.textColor = NSColor.secondaryLabelColor
|
2020-08-19 18:51:44 +02:00
|
|
|
summaryView.textColor = NSColor.secondaryLabelColor
|
2018-07-18 22:34:44 +02:00
|
|
|
textView.textColor = NSColor.labelColor
|
2018-02-20 00:56:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateTextFieldFonts() {
|
|
|
|
|
|
|
|
feedNameView.font = cellAppearance.feedNameFont
|
|
|
|
dateView.font = cellAppearance.dateFont
|
2018-02-22 07:48:34 +01:00
|
|
|
titleView.font = cellAppearance.titleFont
|
2018-02-24 07:15:35 +01:00
|
|
|
summaryView.font = cellAppearance.textFont
|
|
|
|
textView.font = cellAppearance.textOnlyFont
|
2018-02-20 00:56:15 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 07:37:33 +01:00
|
|
|
func addSubviewAtInit(_ view: NSView, hidden: Bool) {
|
2017-11-12 06:53:43 +01:00
|
|
|
|
2018-02-18 07:37:33 +01:00
|
|
|
addSubview(view)
|
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.isHidden = hidden
|
|
|
|
}
|
|
|
|
|
|
|
|
func commonInit() {
|
|
|
|
addSubviewAtInit(titleView, hidden: false)
|
2018-02-24 07:15:35 +01:00
|
|
|
addSubviewAtInit(summaryView, hidden: true)
|
|
|
|
addSubviewAtInit(textView, hidden: true)
|
2018-02-18 07:37:33 +01:00
|
|
|
addSubviewAtInit(unreadIndicatorView, hidden: true)
|
|
|
|
addSubviewAtInit(dateView, hidden: false)
|
|
|
|
addSubviewAtInit(feedNameView, hidden: true)
|
2019-11-06 01:05:57 +01:00
|
|
|
addSubviewAtInit(iconView, hidden: true)
|
2018-02-24 07:15:35 +01:00
|
|
|
addSubviewAtInit(starView, hidden: true)
|
2019-02-08 07:01:31 +01:00
|
|
|
|
|
|
|
makeTextFieldColorsNormal()
|
2018-02-18 07:37:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func updatedLayoutRects() -> TimelineCellLayout {
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
return TimelineCellLayout(width: bounds.width, height: bounds.height, cellData: cellData, appearance: cellAppearance, hasIcon: iconView.iconImage != nil)
|
2018-02-18 07:37:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateTitleView() {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2020-04-25 03:24:24 +02:00
|
|
|
updateTextFieldText(titleView, cellData?.title)
|
2020-04-30 09:36:32 +02:00
|
|
|
updateTextFieldAttributedText(titleView, cellData?.attributedTitle)
|
2018-02-24 07:15:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateSummaryView() {
|
|
|
|
|
|
|
|
updateTextFieldText(summaryView, cellData?.text)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateTextView() {
|
|
|
|
|
|
|
|
updateTextFieldText(textView, cellData?.text)
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
2018-02-18 07:37:33 +01:00
|
|
|
|
|
|
|
func updateDateView() {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2018-02-24 07:15:35 +01:00
|
|
|
updateTextFieldText(dateView, cellData.dateString)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateTextFieldText(_ textField: NSTextField, _ text: String?) {
|
|
|
|
let s = text ?? ""
|
|
|
|
if textField.stringValue != s {
|
|
|
|
textField.stringValue = s
|
|
|
|
needsLayout = true
|
|
|
|
}
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 09:36:32 +02:00
|
|
|
func updateTextFieldAttributedText(_ textField: NSTextField, _ text: NSAttributedString?) {
|
|
|
|
var s = text ?? NSAttributedString(string: "")
|
|
|
|
|
2020-05-01 08:44:23 +02:00
|
|
|
if let fieldFont = textField.font {
|
|
|
|
s = s.adding(font: fieldFont)
|
2020-04-30 09:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if textField.attributedStringValue != s {
|
|
|
|
textField.attributedStringValue = s
|
|
|
|
needsLayout = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 07:37:33 +01:00
|
|
|
func updateFeedNameView() {
|
2020-04-18 14:53:56 +02:00
|
|
|
switch cellData.showFeedName {
|
|
|
|
case .byline:
|
|
|
|
showView(feedNameView)
|
|
|
|
updateTextFieldText(feedNameView, cellData.byline)
|
|
|
|
case .feed:
|
2018-02-24 07:15:35 +01:00
|
|
|
showView(feedNameView)
|
|
|
|
updateTextFieldText(feedNameView, cellData.feedName)
|
2020-04-18 14:53:56 +02:00
|
|
|
case .none:
|
2018-02-24 07:15:35 +01:00
|
|
|
hideView(feedNameView)
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 07:37:33 +01:00
|
|
|
func updateUnreadIndicator() {
|
2018-02-24 07:15:35 +01:00
|
|
|
showOrHideView(unreadIndicatorView, cellData.read || cellData.starred)
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
2018-02-18 07:37:33 +01:00
|
|
|
func updateStarView() {
|
2020-08-20 03:00:34 +02:00
|
|
|
if isSelected && isEmphasized {
|
|
|
|
starView.image = AppAssets.timelineStarSelected
|
|
|
|
} else {
|
|
|
|
starView.image = AppAssets.timelineStarUnselected
|
|
|
|
}
|
2018-02-24 07:15:35 +01:00
|
|
|
showOrHideView(starView, !cellData.starred)
|
2018-02-18 07:23:36 +01:00
|
|
|
}
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
func updateIcon() {
|
|
|
|
guard let iconImage = cellData.iconImage, cellData.showIcon else {
|
|
|
|
makeIconEmpty()
|
2018-01-05 06:20:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
showView(iconView)
|
|
|
|
if iconView.iconImage !== iconImage {
|
|
|
|
iconView.iconImage = iconImage
|
2018-02-24 08:31:59 +01:00
|
|
|
needsLayout = true
|
2017-12-31 21:10:30 +01:00
|
|
|
}
|
2018-02-24 08:31:59 +01:00
|
|
|
}
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
func makeIconEmpty() {
|
|
|
|
if iconView.iconImage != nil {
|
|
|
|
iconView.iconImage = nil
|
2018-02-24 08:31:59 +01:00
|
|
|
needsLayout = true
|
2017-12-31 21:10:30 +01:00
|
|
|
}
|
2019-11-06 01:05:57 +01:00
|
|
|
hideView(iconView)
|
2017-11-29 06:39:09 +01:00
|
|
|
}
|
|
|
|
|
2018-02-24 07:15:35 +01:00
|
|
|
func hideView(_ view: NSView) {
|
|
|
|
if !view.isHidden {
|
|
|
|
view.isHidden = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func showView(_ view: NSView) {
|
|
|
|
if view.isHidden {
|
|
|
|
view.isHidden = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func showOrHideView(_ view: NSView, _ shouldHide: Bool) {
|
|
|
|
shouldHide ? hideView(view) : showView(view)
|
|
|
|
}
|
|
|
|
|
2018-02-18 07:37:33 +01:00
|
|
|
func updateSubviews() {
|
2017-05-27 19:43:27 +02:00
|
|
|
updateTitleView()
|
2018-02-24 07:15:35 +01:00
|
|
|
updateSummaryView()
|
|
|
|
updateTextView()
|
2017-05-27 19:43:27 +02:00
|
|
|
updateDateView()
|
|
|
|
updateFeedNameView()
|
|
|
|
updateUnreadIndicator()
|
2018-02-18 07:23:36 +01:00
|
|
|
updateStarView()
|
2019-11-06 01:05:57 +01:00
|
|
|
updateIcon()
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
}
|