1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-26 23:36:08 +03:00
This commit is contained in:
Tae Won Ha 2020-01-21 22:27:21 +01:00
parent 2fdd4dc36b
commit 870b5b30a6
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44

View File

@ -11,23 +11,26 @@ class ImageAndTextTableCell: NSTableCellView {
private let _textField = NSTextField(forAutoLayout: ())
private let _imageView = NSImageView(forAutoLayout: ())
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
// MARK: - API
static let font = NSFont.systemFont(ofSize: 12)
static let widthWithoutText = (2 + 16 + 4 + 2).cgf
static func width(with text: String) -> CGFloat {
let attrStr = NSAttributedString(string: text, attributes: [NSAttributedString.Key.font: ImageAndTextTableCell.font])
let attrStr = NSAttributedString(
string: text,
attributes: [NSAttributedString.Key.font: ImageAndTextTableCell.font]
)
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))
return CGSize(
width: ImageAndTextTableCell.widthWithoutText + self._textField.intrinsicContentSize.width,
height: max(self._textField.intrinsicContentSize.height, 16)
)
}
override var backgroundStyle: NSView.BackgroundStyle {
@ -38,17 +41,19 @@ class ImageAndTextTableCell: NSTableCellView {
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
}
// If we only have one style, Cocoa automatically inverts the color of the text.
if nameRange.length == attrStr.length { return }
switch self.backgroundStyle {
case .light:
attrStr.addAttribute(NSAttributedString.Key.foregroundColor, value: NSColor.black, range: nameRange)
attrStr.addAttribute(
NSAttributedString.Key.foregroundColor, value: NSColor.black, range: nameRange
)
case .dark:
attrStr.addAttribute(NSAttributedString.Key.foregroundColor, value: NSColor.white, range: nameRange)
attrStr.addAttribute(
NSAttributedString.Key.foregroundColor, value: NSColor.white, range: nameRange
)
default:
return
@ -59,33 +64,18 @@ class ImageAndTextTableCell: NSTableCellView {
}
var attributedText: NSAttributedString {
get {
return self.textField!.attributedStringValue
}
set {
self.textField?.attributedStringValue = newValue
}
get { self.textField!.attributedStringValue }
set { self.textField?.attributedStringValue = newValue }
}
var text: String {
get {
return self.textField!.stringValue
}
set {
self.textField?.stringValue = newValue
}
get { self.textField!.stringValue }
set { self.textField?.stringValue = newValue }
}
var image: NSImage? {
get {
return self.imageView?.image
}
set {
self.imageView?.image = newValue
}
get { self.imageView?.image }
set { self.imageView?.image = newValue }
}
init(withIdentifier identifier: String) {