Files
NetNewsWire/Mac/MainWindow/Sidebar/Cell/SidebarCell.swift
T

193 lines
4.6 KiB
Swift

//
// SidebarCell.swift
// NetNewsWire
//
// Created by Brent Simmons on 8/1/15.
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import RSCore
import Account
import RSTree
import Images
final class SidebarCell: NSTableCellView {
var iconImage: IconImage? {
didSet {
updateFaviconImage()
}
}
var shouldShowImage = false {
didSet {
if shouldShowImage != oldValue {
needsLayout = true
}
faviconImageView.iconImage = shouldShowImage ? iconImage : nil
}
}
var cellAppearance: SidebarCellAppearance? {
didSet {
if cellAppearance != oldValue {
needsLayout = true
}
}
}
var unreadCount: Int {
get {
return unreadCountView.unreadCount
}
set {
if unreadCountView.unreadCount != newValue {
unreadCountView.unreadCount = newValue
updateUnreadCountView()
}
}
}
var name: String {
get {
return titleView.stringValue
}
set {
if titleView.stringValue != newValue {
titleView.stringValue = newValue
needsDisplay = true
needsLayout = true
}
}
}
private let titleView: NSTextField = {
let textField = NSTextField(labelWithString: "")
textField.usesSingleLineMode = true
textField.maximumNumberOfLines = 1
textField.isEditable = false
textField.lineBreakMode = .byTruncatingTail
textField.allowsDefaultTighteningForTruncation = false
return textField
}()
private let faviconImageView = IconView()
private let unreadCountView = UnreadCountView(frame: NSRect.zero)
override var backgroundStyle: NSView.BackgroundStyle {
didSet {
updateFaviconImage()
unreadCountView.isSelected = (backgroundStyle != .normal)
}
}
nonisolated override var isFlipped: Bool {
return true
}
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
override func layout() {
if let cellAppearance = cellAppearance {
titleView.font = cellAppearance.textFieldFont
}
resizeSubviews(withOldSize: NSSize.zero)
}
override func resizeSubviews(withOldSize oldSize: NSSize) {
guard let cellAppearance = cellAppearance else {
return
}
let layout = SidebarCellLayout(appearance: cellAppearance, cellSize: bounds.size, trailingEdge: trailingEdge(), shouldShowImage: shouldShowImage, textField: titleView, unreadCountView: unreadCountView)
layoutWith(layout)
}
func updateUnreadCountView() {
unreadCountView.invalidateIntrinsicContentSize()
unreadCountView.needsDisplay = true
unreadCountView.isHidden = unreadCountView.unreadCountText == nil
needsLayout = true
}
override func accessibilityLabel() -> String? {
if unreadCount > 0 && AppDefaults.shared.unreadCountDisplay == .count {
let unreadLabel = NSLocalizedString("unread", comment: "Unread label for accessibility")
return "\(name) \(unreadCount) \(unreadLabel)"
} else {
return name
}
}
}
private extension SidebarCell {
func commonInit() {
addSubviewAtInit(unreadCountView)
addSubviewAtInit(faviconImageView)
addSubviewAtInit(titleView)
}
func addSubviewAtInit(_ view: NSView) {
addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false
}
// Ask the outline view for the cell frame instead of trusting
// the cell's own width, which can be stale while the outline view is tiling.
func trailingEdge() -> CGFloat {
var view = superview
while let currentView = view {
if let outlineView = currentView as? NSOutlineView {
let row = outlineView.row(for: self)
if row < 0 {
return bounds.maxX
}
let cellFrame = outlineView.frameOfCell(atColumn: 0, row: row)
return convert(NSPoint(x: cellFrame.maxX, y: 0), from: outlineView).x
}
view = currentView.superview
}
return bounds.maxX
}
func layoutWith(_ layout: SidebarCellLayout) {
faviconImageView.setFrameIfNotEqual(layout.faviconRect)
titleView.setFrameIfNotEqual(layout.titleRect)
unreadCountView.setFrameIfNotEqual(layout.unreadCountRect)
}
func updateFaviconImage() {
var updatedIconImage = iconImage
if let iconImage = iconImage, iconImage.isSymbol {
var tintColor: NSColor
if backgroundStyle != .normal {
tintColor = NSColor.white
} else {
if let preferredColor = iconImage.preferredColor {
tintColor = preferredColor
} else {
tintColor = NSColor.controlAccentColor
}
}
updatedIconImage = IconImage(iconImage.image, isSymbol: iconImage.isSymbol, isBackgroundSuppressed: iconImage.isBackgroundSuppressed, preferredColor: tintColor)
}
if let image = updatedIconImage {
faviconImageView.iconImage = shouldShowImage ? image : nil
} else {
faviconImageView.iconImage = nil
}
}
}