2017-06-25 18:56:24 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Cocoa
|
2017-06-25 22:29:30 +03:00
|
|
|
import SwiftNeoVim
|
2017-06-25 18:56:24 +03:00
|
|
|
import PureLayout
|
|
|
|
|
2017-06-25 22:29:30 +03:00
|
|
|
protocol ThemedView: class {
|
|
|
|
|
2017-06-27 07:41:16 +03:00
|
|
|
var theme: Theme { get }
|
2017-06-25 22:29:30 +03:00
|
|
|
}
|
|
|
|
|
2017-06-25 19:31:00 +03:00
|
|
|
class ThemedTableRow: NSTableRowView {
|
|
|
|
|
2017-06-25 22:29:30 +03:00
|
|
|
init(withIdentifier identifier: String, themedView: ThemedView) {
|
|
|
|
self.themedView = themedView
|
|
|
|
|
2017-06-25 19:31:00 +03:00
|
|
|
super.init(frame: .zero)
|
|
|
|
|
|
|
|
self.identifier = identifier
|
|
|
|
}
|
|
|
|
|
2017-06-25 22:29:30 +03:00
|
|
|
open override func drawBackground(in dirtyRect: NSRect) {
|
|
|
|
if let cell = self.view(atColumn: 0) as? ThemedTableCell {
|
2017-06-27 07:41:16 +03:00
|
|
|
cell.textField?.textColor = self.themedView?.theme.foreground ?? Theme.default.foreground
|
2017-06-25 22:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
self.themedView?.theme.background.set()
|
|
|
|
NSRectFill(dirtyRect)
|
|
|
|
}
|
|
|
|
|
2017-06-25 19:31:00 +03:00
|
|
|
override func drawSelection(in dirtyRect: NSRect) {
|
2017-06-25 22:29:30 +03:00
|
|
|
if let cell = self.view(atColumn: 0) as? ThemedTableCell {
|
2017-06-27 07:41:16 +03:00
|
|
|
cell.textField?.textColor = self.themedView?.theme.highlightForeground ?? Theme.default.highlightForeground
|
2017-06-25 22:29:30 +03:00
|
|
|
}
|
2017-06-25 19:31:00 +03:00
|
|
|
|
2017-06-27 07:41:16 +03:00
|
|
|
self.themedView?.theme.highlightBackground.set()
|
2017-06-25 19:31:00 +03:00
|
|
|
NSRectFill(dirtyRect)
|
|
|
|
}
|
|
|
|
|
2017-06-25 22:29:30 +03:00
|
|
|
fileprivate weak var themedView: ThemedView?
|
|
|
|
|
2017-06-25 19:31:00 +03:00
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-25 18:56:24 +03:00
|
|
|
class ThemedTableCell: NSTableCellView {
|
|
|
|
|
|
|
|
// MARK: - API
|
|
|
|
static let font = NSFont.systemFont(ofSize: 12)
|
|
|
|
static let widthWithoutText = CGFloat(2 + 16 + 4 + 2)
|
|
|
|
|
|
|
|
static func width(with text: String) -> CGFloat {
|
|
|
|
let attrStr = NSAttributedString(string: text, attributes: [NSFontAttributeName: ThemedTableCell.font])
|
|
|
|
|
|
|
|
return self.widthWithoutText + attrStr.size().width
|
|
|
|
}
|
|
|
|
|
|
|
|
override var intrinsicContentSize: CGSize {
|
|
|
|
return CGSize(width: ThemedTableCell.widthWithoutText + self._textField.intrinsicContentSize.width,
|
|
|
|
height: max(self._textField.intrinsicContentSize.height, 16))
|
|
|
|
}
|
|
|
|
|
|
|
|
var attributedText: NSAttributedString {
|
|
|
|
get {
|
|
|
|
return self.textField!.attributedStringValue
|
|
|
|
}
|
|
|
|
|
|
|
|
set {
|
|
|
|
self.textField?.attributedStringValue = newValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var text: String {
|
|
|
|
get {
|
|
|
|
return self.textField!.stringValue
|
|
|
|
}
|
|
|
|
|
|
|
|
set {
|
|
|
|
self.textField?.stringValue = newValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var image: NSImage? {
|
|
|
|
get {
|
|
|
|
return self.imageView?.image
|
|
|
|
}
|
|
|
|
|
|
|
|
set {
|
|
|
|
self.imageView?.image = newValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
init(withIdentifier identifier: String) {
|
|
|
|
super.init(frame: .zero)
|
|
|
|
|
|
|
|
self.identifier = identifier
|
|
|
|
|
|
|
|
self.textField = self._textField
|
|
|
|
self.imageView = self._imageView
|
|
|
|
|
|
|
|
let textField = self._textField
|
|
|
|
textField.font = ThemedTableCell.font
|
|
|
|
textField.isBordered = false
|
|
|
|
textField.isBezeled = false
|
|
|
|
textField.allowsEditingTextAttributes = false
|
|
|
|
textField.isEditable = false
|
|
|
|
textField.usesSingleLineMode = true
|
|
|
|
textField.drawsBackground = false
|
|
|
|
|
|
|
|
let imageView = self._imageView
|
|
|
|
|
|
|
|
self.addSubview(textField)
|
|
|
|
self.addSubview(imageView)
|
|
|
|
|
|
|
|
imageView.autoPinEdge(toSuperviewEdge: .top, withInset: 2)
|
|
|
|
imageView.autoPinEdge(toSuperviewEdge: .left, withInset: 2)
|
|
|
|
imageView.autoSetDimension(.width, toSize: 16)
|
|
|
|
imageView.autoSetDimension(.height, toSize: 16)
|
|
|
|
|
|
|
|
textField.autoPinEdge(toSuperviewEdge: .top, withInset: 2)
|
|
|
|
textField.autoPinEdge(toSuperviewEdge: .right, withInset: 2)
|
|
|
|
textField.autoPinEdge(toSuperviewEdge: .bottom, withInset: 2)
|
|
|
|
textField.autoPinEdge(.left, to: .right, of: imageView, withOffset: 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
func reset() -> ThemedTableCell {
|
|
|
|
self.text = ""
|
|
|
|
self.image = nil
|
|
|
|
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
|
|
|
fileprivate let _textField = NSTextField(forAutoLayout: ())
|
|
|
|
fileprivate let _imageView = NSImageView(forAutoLayout: ())
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
}
|