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 {
|
|
|
|
|
2017-11-25 06:57:28 +01:00
|
|
|
var image: NSImage? {
|
|
|
|
didSet {
|
2017-12-17 19:52:31 +01:00
|
|
|
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
|
|
|
|
}
|
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
|
|
|
|
}
|
2017-11-25 06:57:28 +01:00
|
|
|
imageView?.image = shouldShowImage ? image : nil
|
2017-11-25 06:39:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
private let unreadCountView = UnreadCountView(frame: NSZeroRect)
|
|
|
|
|
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 {
|
|
|
|
if let s = textField?.stringValue {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
if textField?.stringValue != newValue {
|
|
|
|
textField?.stringValue = newValue
|
|
|
|
needsDisplay = true
|
|
|
|
needsLayout = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override var isFlipped: Bool {
|
2018-02-14 22:14:25 +01:00
|
|
|
return true
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private func commonInit() {
|
|
|
|
|
|
|
|
unreadCountView.translatesAutoresizingMaskIntoConstraints = false
|
2017-12-29 01:37:30 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-11-25 06:39:59 +01:00
|
|
|
override func resizeSubviews(withOldSize oldSize: NSSize) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2017-12-28 03:00:17 +01:00
|
|
|
guard let textField = textField, let cellAppearance = cellAppearance else {
|
2017-05-27 19:43:27 +02:00
|
|
|
return
|
|
|
|
}
|
2017-11-25 06:39:59 +01:00
|
|
|
let layout = SidebarCellLayout(appearance: cellAppearance, cellSize: bounds.size, shouldShowImage: shouldShowImage, textField: textField, unreadCountView: unreadCountView)
|
|
|
|
layoutWith(layout)
|
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
|
|
|
|
2017-11-25 06:39:59 +01:00
|
|
|
func layoutWith(_ layout: SidebarCellLayout) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2017-11-25 06:39:59 +01:00
|
|
|
imageView?.rs_setFrameIfNotEqual(layout.faviconRect)
|
|
|
|
textField?.rs_setFrameIfNotEqual(layout.titleRect)
|
|
|
|
unreadCountView.rs_setFrameIfNotEqual(layout.unreadCountRect)
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 19:43:27 +02:00
|
|
|
|