NetNewsWire/Evergreen/MainWindow/Sidebar/Cell/SidebarCell.swift

126 lines
2.5 KiB
Swift
Raw Normal View History

2017-05-27 19:43:27 +02:00
//
// SidebarCell.swift
// Evergreen
//
// Created by Brent Simmons on 8/1/15.
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import DB5
private var textSizeCache = [String: NSSize]()
class SidebarCell : NSTableCellView {
var image: NSImage? {
didSet {
if let image = image {
imageView?.image = shouldShowImage ? image : nil
imageView?.alphaValue = image.isTemplate ? 0.75 : 1.0
}
else {
imageView?.image = nil
imageView?.alphaValue = 1.0
}
}
}
var shouldShowImage = false {
didSet {
if shouldShowImage != oldValue {
needsLayout = true
}
imageView?.image = shouldShowImage ? image : nil
}
}
2017-05-27 19:43:27 +02:00
private let unreadCountView = UnreadCountView(frame: NSZeroRect)
var cellAppearance: SidebarCellAppearance? {
didSet {
if cellAppearance != oldValue {
needsLayout = true
}
}
}
2017-05-27 19:43:27 +02:00
var unreadCount: Int {
get {
return unreadCountView.unreadCount
}
set {
if unreadCountView.unreadCount != newValue {
unreadCountView.unreadCount = newValue
unreadCountView.isHidden = (newValue < 1)
needsLayout = true
2017-05-27 19:43:27 +02:00
}
}
}
var name: String {
get {
if let s = textField?.stringValue {
return s
}
return ""
}
set {
if textField?.stringValue != newValue {
textField?.stringValue = newValue
needsDisplay = true
needsLayout = true
}
}
}
override var isFlipped: Bool {
return true
2017-05-27 19:43:27 +02:00
}
private func commonInit() {
unreadCountView.translatesAutoresizingMaskIntoConstraints = false
imageView?.translatesAutoresizingMaskIntoConstraints = false
textField?.translatesAutoresizingMaskIntoConstraints = false
2017-05-27 19:43:27 +02:00
addSubview(unreadCountView)
}
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
override func layout() {
resizeSubviews(withOldSize: NSZeroSize)
}
override func resizeSubviews(withOldSize oldSize: NSSize) {
2017-05-27 19:43:27 +02:00
guard let textField = textField, let cellAppearance = cellAppearance else {
2017-05-27 19:43:27 +02:00
return
}
let layout = SidebarCellLayout(appearance: cellAppearance, cellSize: bounds.size, shouldShowImage: shouldShowImage, textField: textField, unreadCountView: unreadCountView)
layoutWith(layout)
2017-05-27 19:43:27 +02:00
}
}
private extension SidebarCell {
2017-05-27 19:43:27 +02:00
func layoutWith(_ layout: SidebarCellLayout) {
2017-05-27 19:43:27 +02:00
imageView?.rs_setFrameIfNotEqual(layout.faviconRect)
textField?.rs_setFrameIfNotEqual(layout.titleRect)
unreadCountView.rs_setFrameIfNotEqual(layout.unreadCountRect)
}
}
2017-05-27 19:43:27 +02:00