2017-05-27 19:43:27 +02:00
|
|
|
//
|
|
|
|
// TimelineTableCellView.swift
|
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 8/31/15.
|
|
|
|
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSTextDrawing
|
|
|
|
|
|
|
|
class TimelineTableCellView: NSTableCellView {
|
|
|
|
|
2018-02-18 07:37:33 +01:00
|
|
|
private let titleView = RSMultiLineView(frame: NSZeroRect)
|
|
|
|
private let unreadIndicatorView = UnreadIndicatorView(frame: NSZeroRect)
|
|
|
|
private let dateView = RSSingleLineView(frame: NSZeroRect)
|
|
|
|
private let feedNameView = RSSingleLineView(frame: NSZeroRect)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2018-02-18 07:37:33 +01:00
|
|
|
private let avatarImageView: NSImageView = {
|
2017-11-27 22:10:57 +01:00
|
|
|
let imageView = NSImageView(frame: NSRect.zero)
|
2017-11-29 22:10:01 +01:00
|
|
|
imageView.imageScaling = .scaleProportionallyDown
|
2017-11-27 22:10:57 +01:00
|
|
|
imageView.animates = false
|
2017-12-31 21:10:30 +01:00
|
|
|
imageView.imageAlignment = .alignCenter
|
2018-02-18 03:19:25 +01:00
|
|
|
imageView.image = AppImages.genericFeedImage
|
2017-11-27 22:10:57 +01:00
|
|
|
return imageView
|
|
|
|
}()
|
|
|
|
|
2018-02-18 07:37:33 +01:00
|
|
|
private let starView: NSImageView = {
|
2018-02-18 07:23:36 +01:00
|
|
|
let imageView = NSImageView(frame: NSRect.zero)
|
|
|
|
imageView.imageScaling = .scaleNone
|
|
|
|
imageView.animates = false
|
|
|
|
imageView.imageAlignment = .alignCenter
|
|
|
|
imageView.image = AppImages.timelineStar
|
|
|
|
return imageView
|
|
|
|
}()
|
2017-12-30 21:45:10 +01:00
|
|
|
|
2017-12-31 21:10:30 +01:00
|
|
|
var cellAppearance: TimelineCellAppearance! {
|
|
|
|
didSet {
|
2018-01-05 06:20:09 +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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override var isFlipped: Bool {
|
|
|
|
return true
|
|
|
|
}
|
2017-11-12 06:53:43 +01:00
|
|
|
|
|
|
|
override var isOpaque: Bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-11-18 20:39:25 +01:00
|
|
|
override var wantsUpdateLayer: Bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
var isEmphasized = false {
|
|
|
|
didSet {
|
|
|
|
dateView.emphasized = isEmphasized
|
|
|
|
feedNameView.emphasized = isEmphasized
|
2018-01-06 23:33:16 +01:00
|
|
|
titleView.emphasized = isEmphasized
|
|
|
|
unreadIndicatorView.isEmphasized = isEmphasized
|
2017-11-12 21:16:27 +01:00
|
|
|
needsDisplay = true
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var isSelected = false {
|
|
|
|
didSet {
|
|
|
|
dateView.selected = isSelected
|
|
|
|
feedNameView.selected = isSelected
|
|
|
|
titleView.selected = isSelected
|
2018-01-06 23:33:16 +01:00
|
|
|
unreadIndicatorView.isSelected = isSelected
|
2017-11-12 06:53:43 +01:00
|
|
|
needsDisplay = true
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
}
|
2017-11-27 22:10:57 +01:00
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
override init(frame frameRect: NSRect) {
|
|
|
|
|
|
|
|
super.init(frame: frameRect)
|
|
|
|
commonInit()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
|
|
|
|
super.init(coder: coder)
|
|
|
|
commonInit()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func setFrameSize(_ newSize: NSSize) {
|
|
|
|
|
|
|
|
if newSize == self.frame.size {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
super.setFrameSize(newSize)
|
|
|
|
needsLayout = true
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidMoveToSuperview() {
|
|
|
|
|
|
|
|
updateSubviews()
|
|
|
|
updateAppearance()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func layout() {
|
|
|
|
|
|
|
|
resizeSubviews(withOldSize: NSZeroSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func resizeSubviews(withOldSize oldSize: NSSize) {
|
|
|
|
|
|
|
|
let layoutRects = updatedLayoutRects()
|
|
|
|
titleView.rs_setFrameIfNotEqual(layoutRects.titleRect)
|
|
|
|
unreadIndicatorView.rs_setFrameIfNotEqual(layoutRects.unreadIndicatorRect)
|
|
|
|
dateView.rs_setFrameIfNotEqual(layoutRects.dateRect)
|
|
|
|
feedNameView.rs_setFrameIfNotEqual(layoutRects.feedNameRect)
|
2017-11-29 06:39:09 +01:00
|
|
|
avatarImageView.rs_setFrameIfNotEqual(layoutRects.avatarImageRect)
|
2018-02-18 07:23:36 +01:00
|
|
|
starView.rs_setFrameIfNotEqual(layoutRects.starRect)
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
2017-11-12 06:53:43 +01:00
|
|
|
|
2017-11-18 20:39:25 +01:00
|
|
|
override func updateLayer() {
|
2017-11-12 06:53:43 +01:00
|
|
|
|
2017-11-12 21:16:27 +01:00
|
|
|
let color: NSColor
|
|
|
|
if isSelected {
|
|
|
|
color = isEmphasized ? NSColor.alternateSelectedControlColor : NSColor.secondarySelectedControlColor
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
color = NSColor.white
|
|
|
|
}
|
2017-11-18 20:39:25 +01:00
|
|
|
|
|
|
|
if layer?.backgroundColor != color.cgColor {
|
|
|
|
layer?.backgroundColor = color.cgColor
|
|
|
|
}
|
2017-11-12 06:53:43 +01:00
|
|
|
}
|
2018-02-18 07:37:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
|
|
private extension TimelineTableCellView {
|
|
|
|
|
|
|
|
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)
|
|
|
|
addSubviewAtInit(unreadIndicatorView, hidden: true)
|
|
|
|
addSubviewAtInit(dateView, hidden: false)
|
|
|
|
addSubviewAtInit(feedNameView, hidden: true)
|
|
|
|
addSubviewAtInit(avatarImageView, hidden: false)
|
|
|
|
addSubviewAtInit(starView, hidden: false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updatedLayoutRects() -> TimelineCellLayout {
|
|
|
|
|
2018-02-19 00:13:47 +01:00
|
|
|
return TimelineCellLayout(width: bounds.width, cellData: cellData, appearance: cellAppearance)
|
2018-02-18 07:37:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateAppearance() {
|
|
|
|
|
|
|
|
if let rowView = superview as? NSTableRowView {
|
|
|
|
isEmphasized = rowView.isEmphasized
|
|
|
|
isSelected = rowView.isSelected
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
isEmphasized = false
|
|
|
|
isSelected = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateTitleView() {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
titleView.attributedStringValue = cellData.attributedTitle
|
|
|
|
needsLayout = true
|
|
|
|
}
|
2018-02-18 07:37:33 +01:00
|
|
|
|
|
|
|
func updateDateView() {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
dateView.attributedStringValue = cellData.attributedDateString
|
|
|
|
needsLayout = true
|
|
|
|
}
|
|
|
|
|
2018-02-18 07:37:33 +01:00
|
|
|
func updateFeedNameView() {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
if cellData.showFeedName {
|
|
|
|
if feedNameView.isHidden {
|
|
|
|
feedNameView.isHidden = false
|
|
|
|
}
|
|
|
|
feedNameView.attributedStringValue = cellData.attributedFeedName
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if !feedNameView.isHidden {
|
|
|
|
feedNameView.isHidden = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 07:37:33 +01:00
|
|
|
func updateUnreadIndicator() {
|
2018-02-18 07:23:36 +01:00
|
|
|
|
|
|
|
let shouldHide = cellData.read || cellData.starred
|
|
|
|
if unreadIndicatorView.isHidden != shouldHide {
|
|
|
|
unreadIndicatorView.isHidden = shouldHide
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 07:37:33 +01:00
|
|
|
func updateStarView() {
|
2018-02-18 07:23:36 +01:00
|
|
|
|
|
|
|
starView.isHidden = !cellData.starred
|
|
|
|
}
|
|
|
|
|
2018-02-18 07:37:33 +01:00
|
|
|
func updateAvatar() {
|
2017-11-29 06:39:09 +01:00
|
|
|
|
2018-01-05 06:20:09 +01:00
|
|
|
if !cellData.showAvatar {
|
|
|
|
avatarImageView.image = nil
|
|
|
|
avatarImageView.isHidden = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
avatarImageView.isHidden = false
|
2017-12-31 21:10:30 +01:00
|
|
|
|
|
|
|
if let image = cellData.avatar {
|
|
|
|
if avatarImageView.image !== image {
|
|
|
|
avatarImageView.image = image
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2018-01-05 06:20:09 +01:00
|
|
|
avatarImageView.image = nil
|
2017-12-31 21:10:30 +01:00
|
|
|
}
|
2018-01-05 06:20:09 +01:00
|
|
|
|
|
|
|
avatarImageView.wantsLayer = true
|
|
|
|
avatarImageView.layer?.cornerRadius = cellAppearance.avatarCornerRadius
|
2017-11-29 06:39:09 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 07:37:33 +01:00
|
|
|
func updateSubviews() {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
updateTitleView()
|
|
|
|
updateDateView()
|
|
|
|
updateFeedNameView()
|
|
|
|
updateUnreadIndicator()
|
2018-02-18 07:23:36 +01:00
|
|
|
updateStarView()
|
2017-11-29 06:39:09 +01:00
|
|
|
updateAvatar()
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
}
|