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

152 lines
3.4 KiB
Swift
Raw Normal View History

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
import RSCore
import Account
import RSTree
2017-05-27 19:43:27 +02:00
class SidebarCell : NSTableCellView {
var image: NSImage? {
didSet {
if let image = image {
faviconImageView.image = shouldShowImage ? image : nil
faviconImageView.alphaValue = image.isTemplate ? 0.75 : 1.0
}
else {
faviconImageView.image = nil
faviconImageView.alphaValue = 1.0
}
}
}
var shouldShowImage = false {
didSet {
if shouldShowImage != oldValue {
needsLayout = true
}
faviconImageView.image = shouldShowImage ? image : nil
}
}
2017-05-27 19:43:27 +02:00
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 {
return titleView.stringValue
2017-05-27 19:43:27 +02:00
}
set {
if titleView.stringValue != newValue {
titleView.stringValue = newValue
2017-05-27 19:43:27 +02:00
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: NSImageView = {
let image = AppAssets.genericFeedImage
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
}()
private let unreadCountView = UnreadCountView(frame: NSZeroRect)
override var isFlipped: Bool {
return true
}
2017-05-27 19:43:27 +02:00
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
commonInit()
}
required init?(coder: NSCoder) {
2017-05-27 19:43:27 +02:00
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 cellAppearance = cellAppearance else {
2017-05-27 19:43:27 +02:00
return
}
let layout = SidebarCellLayout(appearance: cellAppearance, cellSize: bounds.size, shouldShowImage: shouldShowImage, textField: titleView, unreadCountView: unreadCountView)
layoutWith(layout)
2017-05-27 19:43:27 +02: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
}
private extension SidebarCell {
2017-05-27 19:43:27 +02:00
func commonInit() {
addSubviewAtInit(unreadCountView)
addSubviewAtInit(faviconImageView)
addSubviewAtInit(titleView)
}
func addSubviewAtInit(_ view: NSView) {
addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false
}
func layoutWith(_ layout: SidebarCellLayout) {
faviconImageView.rs_setFrameIfNotEqual(layout.faviconRect)
titleView.rs_setFrameIfNotEqual(layout.titleRect)
unreadCountView.rs_setFrameIfNotEqual(layout.unreadCountRect)
}
}
2017-05-27 19:43:27 +02:00