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

168 lines
4.0 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 Account
import RSTree
2017-05-27 19:43:27 +02:00
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
}
}
}
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 = IconView()
private let unreadCountView = UnreadCountView(frame: NSZeroRect)
2020-09-02 20:38:02 +02:00
override var backgroundStyle: NSView.BackgroundStyle {
didSet {
updateFaviconImage()
}
}
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() {
if let cellAppearance = cellAppearance {
titleView.font = cellAppearance.textFieldFont
}
2017-05-27 19:43:27 +02:00
resizeSubviews(withOldSize: NSZeroSize)
}
override func resizeSubviews(withOldSize oldSize: NSSize) {
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) {
2020-01-09 06:40:21 +01:00
faviconImageView.setFrame(ifNotEqualTo: layout.faviconRect)
titleView.setFrame(ifNotEqualTo: layout.titleRect)
unreadCountView.setFrame(ifNotEqualTo: layout.unreadCountRect)
}
func updateFaviconImage() {
var updatedIconImage = iconImage
if let iconImage = iconImage, iconImage.isSymbol {
2020-09-02 20:38:02 +02:00
if backgroundStyle != .normal {
let image = iconImage.image.tinted(with: .white)
updatedIconImage = IconImage(image, isSymbol: iconImage.isSymbol, isBackgroundSupressed: iconImage.isBackgroundSupressed)
} else {
if let preferredColor = iconImage.preferredColor {
let image = iconImage.image.tinted(with: NSColor(cgColor: preferredColor)!)
updatedIconImage = IconImage(image, isSymbol: iconImage.isSymbol, isBackgroundSupressed: iconImage.isBackgroundSupressed)
} else {
let image = iconImage.image.tinted(with: .controlAccentColor)
updatedIconImage = IconImage(image, isSymbol: iconImage.isSymbol, isBackgroundSupressed: iconImage.isBackgroundSupressed)
}
}
}
if let image = updatedIconImage {
faviconImageView.iconImage = shouldShowImage ? image : nil
} else {
faviconImageView.iconImage = nil
}
}
}
2017-05-27 19:43:27 +02:00