2017-05-27 10:43:27 -07:00
|
|
|
//
|
|
|
|
// SidebarCell.swift
|
2018-08-28 22:18:24 -07:00
|
|
|
// NetNewsWire
|
2017-05-27 10:43:27 -07:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 8/1/15.
|
|
|
|
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2018-11-22 13:58:24 -08:00
|
|
|
import RSCore
|
2018-09-09 15:09:03 -05:00
|
|
|
import Account
|
|
|
|
import RSTree
|
2017-05-27 10:43:27 -07:00
|
|
|
|
|
|
|
class SidebarCell : NSTableCellView {
|
2018-11-22 13:58:24 -08:00
|
|
|
|
2017-11-24 21:57:28 -08:00
|
|
|
var image: NSImage? {
|
|
|
|
didSet {
|
2017-12-17 10:52:31 -08:00
|
|
|
if let image = image {
|
2019-02-09 18:54:16 -08:00
|
|
|
faviconImageView.image = shouldShowImage ? image : nil
|
|
|
|
faviconImageView.alphaValue = image.isTemplate ? 0.75 : 1.0
|
2017-12-17 10:52:31 -08:00
|
|
|
}
|
|
|
|
else {
|
2019-02-09 18:54:16 -08:00
|
|
|
faviconImageView.image = nil
|
|
|
|
faviconImageView.alphaValue = 1.0
|
2017-12-17 10:52:31 -08:00
|
|
|
}
|
2017-11-24 21:57:28 -08:00
|
|
|
}
|
|
|
|
}
|
2017-11-24 21:39:59 -08:00
|
|
|
|
|
|
|
var shouldShowImage = false {
|
|
|
|
didSet {
|
|
|
|
if shouldShowImage != oldValue {
|
|
|
|
needsLayout = true
|
|
|
|
}
|
2019-02-09 18:54:16 -08:00
|
|
|
faviconImageView.image = shouldShowImage ? image : nil
|
2017-11-24 21:39:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
|
2017-12-27 18:00:17 -08:00
|
|
|
var cellAppearance: SidebarCellAppearance? {
|
2017-11-24 21:39:59 -08:00
|
|
|
didSet {
|
|
|
|
if cellAppearance != oldValue {
|
|
|
|
needsLayout = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
var unreadCount: Int {
|
|
|
|
get {
|
|
|
|
return unreadCountView.unreadCount
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
if unreadCountView.unreadCount != newValue {
|
|
|
|
unreadCountView.unreadCount = newValue
|
2017-11-24 21:39:59 -08:00
|
|
|
unreadCountView.isHidden = (newValue < 1)
|
|
|
|
needsLayout = true
|
2017-05-27 10:43:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var name: String {
|
|
|
|
get {
|
2019-02-09 18:54:16 -08:00
|
|
|
return titleView.stringValue
|
2017-05-27 10:43:27 -07:00
|
|
|
}
|
|
|
|
set {
|
2019-02-09 18:54:16 -08:00
|
|
|
if titleView.stringValue != newValue {
|
|
|
|
titleView.stringValue = newValue
|
2017-05-27 10:43:27 -07:00
|
|
|
needsDisplay = true
|
|
|
|
needsLayout = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 18:54:16 -08:00
|
|
|
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: NSImageView = {
|
2019-11-12 15:36:03 -06:00
|
|
|
let imageView = NSImageView(frame: NSRect.zero)
|
2019-02-09 18:54:16 -08:00
|
|
|
imageView.animates = false
|
|
|
|
imageView.imageAlignment = .alignCenter
|
|
|
|
imageView.imageScaling = .scaleProportionallyDown
|
|
|
|
imageView.wantsLayer = true
|
|
|
|
return imageView
|
|
|
|
}()
|
2018-12-05 21:48:31 -08:00
|
|
|
|
2019-02-09 18:54:16 -08:00
|
|
|
private let unreadCountView = UnreadCountView(frame: NSZeroRect)
|
2018-12-05 21:48:31 -08:00
|
|
|
|
2019-02-09 18:54:16 -08:00
|
|
|
override var isFlipped: Bool {
|
|
|
|
return true
|
2018-12-05 21:48:31 -08:00
|
|
|
}
|
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
override init(frame frameRect: NSRect) {
|
|
|
|
super.init(frame: frameRect)
|
|
|
|
commonInit()
|
|
|
|
}
|
|
|
|
|
2019-02-09 18:54:16 -08:00
|
|
|
required init?(coder: NSCoder) {
|
2017-05-27 10:43:27 -07:00
|
|
|
super.init(coder: coder)
|
|
|
|
commonInit()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func layout() {
|
|
|
|
resizeSubviews(withOldSize: NSZeroSize)
|
|
|
|
}
|
|
|
|
|
2017-11-24 21:39:59 -08:00
|
|
|
override func resizeSubviews(withOldSize oldSize: NSSize) {
|
2017-05-27 10:43:27 -07:00
|
|
|
|
2019-02-09 18:54:16 -08:00
|
|
|
guard let cellAppearance = cellAppearance else {
|
2017-05-27 10:43:27 -07:00
|
|
|
return
|
|
|
|
}
|
2019-02-09 18:54:16 -08:00
|
|
|
let layout = SidebarCellLayout(appearance: cellAppearance, cellSize: bounds.size, shouldShowImage: shouldShowImage, textField: titleView, unreadCountView: unreadCountView)
|
2017-11-24 21:39:59 -08:00
|
|
|
layoutWith(layout)
|
2017-05-27 10:43:27 -07:00
|
|
|
}
|
2019-02-06 14:37:58 -06:00
|
|
|
|
|
|
|
override func accessibilityLabel() -> String? {
|
|
|
|
if unreadCount > 0 {
|
|
|
|
let unreadLabel = NSLocalizedString("unread", comment: "Unread label for accessiblity")
|
|
|
|
return "\(name) \(unreadCount) \(unreadLabel)"
|
|
|
|
} else {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 10:43:27 -07:00
|
|
|
}
|
|
|
|
|
2017-11-24 21:39:59 -08:00
|
|
|
private extension SidebarCell {
|
2017-05-27 10:43:27 -07:00
|
|
|
|
2019-02-09 18:54:16 -08:00
|
|
|
func commonInit() {
|
|
|
|
addSubviewAtInit(unreadCountView)
|
|
|
|
addSubviewAtInit(faviconImageView)
|
|
|
|
addSubviewAtInit(titleView)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addSubviewAtInit(_ view: NSView) {
|
|
|
|
addSubview(view)
|
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
}
|
|
|
|
|
2017-11-24 21:39:59 -08:00
|
|
|
func layoutWith(_ layout: SidebarCellLayout) {
|
2020-01-08 23:40:21 -06:00
|
|
|
faviconImageView.setFrame(ifNotEqualTo: layout.faviconRect)
|
|
|
|
titleView.setFrame(ifNotEqualTo: layout.titleRect)
|
|
|
|
unreadCountView.setFrame(ifNotEqualTo: layout.unreadCountRect)
|
2017-11-24 21:39:59 -08:00
|
|
|
}
|
|
|
|
}
|
2017-05-27 10:43:27 -07:00
|
|
|
|