1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-26 07:13:24 +03:00
vimr/VimR/ImageAndTextTableCell.swift

131 lines
3.4 KiB
Swift
Raw Normal View History

2016-09-04 00:13:52 +03:00
/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Cocoa
import PureLayout
2016-10-03 16:03:18 +03:00
class ImageAndTextTableCell: NSTableCellView {
fileprivate let _textField = NSTextField(forAutoLayout: ())
fileprivate let _imageView = NSImageView(forAutoLayout: ())
2016-09-06 00:29:32 +03:00
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - API
static let font = NSFont.systemFont(ofSize: 12)
static let widthWithoutText = CGFloat(2 + 16 + 4 + 2)
2016-11-25 17:25:55 +03:00
static func width(with text: String) -> CGFloat {
let attrStr = NSAttributedString(string: text, attributes: [NSFontAttributeName: ImageAndTextTableCell.font])
2016-11-25 17:25:55 +03:00
return self.widthWithoutText + attrStr.size().width
}
override var intrinsicContentSize: CGSize {
return CGSize(width: ImageAndTextTableCell.widthWithoutText + self._textField.intrinsicContentSize.width,
height: max(self._textField.intrinsicContentSize.height, 16))
}
override var backgroundStyle: NSBackgroundStyle {
didSet {
let attrStr = NSMutableAttributedString(attributedString: self.attributedText)
let wholeRange = NSRange(location: 0, length: attrStr.length)
var nameRange = NSRange(location: 0, length: 0)
let _ = attrStr.attributes(at: 0, longestEffectiveRange: &nameRange, in: wholeRange)
if nameRange.length == attrStr.length {
// If we only have one style, Cocoa automatically inverts the color of the text.
return
}
switch self.backgroundStyle {
case .light:
attrStr.addAttribute(NSForegroundColorAttributeName, value: NSColor.black, range: nameRange)
case .dark:
attrStr.addAttribute(NSForegroundColorAttributeName, value: NSColor.white, range: nameRange)
default:
return
}
self.attributedText = attrStr
}
}
2016-09-27 19:02:05 +03:00
var attributedText: NSAttributedString {
2016-09-06 00:29:32 +03:00
get {
2016-10-03 16:03:18 +03:00
return self.textField!.attributedStringValue
2016-09-06 00:29:32 +03:00
}
2016-11-25 17:25:55 +03:00
2016-09-06 00:29:32 +03:00
set {
2016-10-03 16:03:18 +03:00
self.textField?.attributedStringValue = newValue
2016-09-06 00:29:32 +03:00
}
}
2016-11-25 17:25:55 +03:00
2016-09-27 19:02:05 +03:00
var text: String {
get {
2016-10-03 16:03:18 +03:00
return self.textField!.stringValue
2016-09-27 19:02:05 +03:00
}
set {
2016-10-03 16:03:18 +03:00
self.textField?.stringValue = newValue
2016-09-27 19:02:05 +03:00
}
}
2016-09-06 00:29:32 +03:00
var image: NSImage? {
get {
2016-10-03 16:03:18 +03:00
return self.imageView?.image
2016-09-06 00:29:32 +03:00
}
set {
2016-10-03 16:03:18 +03:00
self.imageView?.image = newValue
2016-09-06 00:29:32 +03:00
}
}
2016-10-05 22:09:14 +03:00
2016-09-04 00:13:52 +03:00
init(withIdentifier identifier: String) {
super.init(frame: CGRect.zero)
2016-11-25 17:25:55 +03:00
2016-09-04 00:13:52 +03:00
self.identifier = identifier
2016-10-03 16:03:18 +03:00
self.textField = self._textField
self.imageView = self._imageView
let textField = self._textField
2016-10-05 22:09:14 +03:00
textField.font = ImageAndTextTableCell.font
2016-09-25 18:50:33 +03:00
textField.isBordered = false
2016-09-27 19:02:05 +03:00
textField.isBezeled = false
textField.allowsEditingTextAttributes = false
2016-09-25 18:50:33 +03:00
textField.isEditable = false
2016-09-27 19:02:05 +03:00
textField.usesSingleLineMode = true
2016-09-04 00:13:52 +03:00
textField.drawsBackground = false
2016-10-03 16:03:18 +03:00
let imageView = self._imageView
2016-09-04 00:13:52 +03:00
self.addSubview(textField)
self.addSubview(imageView)
2016-10-03 16:03:18 +03:00
2016-09-25 18:50:33 +03:00
imageView.autoPinEdge(toSuperviewEdge: .top, withInset: 2)
imageView.autoPinEdge(toSuperviewEdge: .left, withInset: 2)
imageView.autoSetDimension(.width, toSize: 16)
imageView.autoSetDimension(.height, toSize: 16)
2016-11-25 17:25:55 +03:00
2016-09-25 18:50:33 +03:00
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)
2016-09-04 00:13:52 +03:00
}
2016-11-25 17:25:55 +03:00
func reset() -> ImageAndTextTableCell {
self.text = ""
self.image = nil
return self
2016-09-04 00:13:52 +03:00
}
2016-09-25 18:50:33 +03:00
}