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