macOS: KeyValueImageRow class

This commit is contained in:
Roopesh Chander 2019-03-16 19:28:54 +05:30
parent cdb189fab4
commit 62b03272ee
1 changed files with 44 additions and 4 deletions

View File

@ -24,6 +24,8 @@ class EditableKeyValueRow: NSView {
return valueLabel return valueLabel
}() }()
let valueImageView: NSImageView?
var key: String { var key: String {
get { return keyLabel.stringValue } get { return keyLabel.stringValue }
set(value) { keyLabel.stringValue = value } set(value) { keyLabel.stringValue = value }
@ -42,13 +44,24 @@ class EditableKeyValueRow: NSView {
} }
} }
} }
var valueImage: NSImage? {
get { return valueImageView?.image }
set(value) { valueImageView?.image = value }
}
var observationToken: AnyObject?
override var intrinsicContentSize: NSSize { override var intrinsicContentSize: NSSize {
let height = max(keyLabel.intrinsicContentSize.height, valueLabel.intrinsicContentSize.height) let height = max(keyLabel.intrinsicContentSize.height, valueLabel.intrinsicContentSize.height)
return NSSize(width: NSView.noIntrinsicMetric, height: height) return NSSize(width: NSView.noIntrinsicMetric, height: height)
} }
init() { convenience init() {
self.init(hasValueImage: false)
}
fileprivate init(hasValueImage: Bool) {
valueImageView = hasValueImage ? NSImageView() : nil
super.init(frame: CGRect.zero) super.init(frame: CGRect.zero)
addSubview(keyLabel) addSubview(keyLabel)
@ -60,10 +73,24 @@ class EditableKeyValueRow: NSView {
keyLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor), keyLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor),
keyLabel.firstBaselineAnchor.constraint(equalTo: valueLabel.firstBaselineAnchor), keyLabel.firstBaselineAnchor.constraint(equalTo: valueLabel.firstBaselineAnchor),
self.leadingAnchor.constraint(equalTo: keyLabel.leadingAnchor), self.leadingAnchor.constraint(equalTo: keyLabel.leadingAnchor),
keyLabel.trailingAnchor.constraint(equalTo: valueLabel.leadingAnchor, constant: -5),
valueLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor) valueLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor)
]) ])
let spacing: CGFloat = 5
if let valueImageView = valueImageView {
addSubview(valueImageView)
valueImageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
valueImageView.centerYAnchor.constraint(equalTo: self.centerYAnchor),
valueImageView.leadingAnchor.constraint(equalTo: keyLabel.trailingAnchor, constant: spacing),
valueLabel.leadingAnchor.constraint(equalTo: valueImageView.trailingAnchor)
])
} else {
NSLayoutConstraint.activate([
valueLabel.leadingAnchor.constraint(equalTo: keyLabel.trailingAnchor, constant: spacing)
])
}
keyLabel.setContentCompressionResistancePriority(.defaultHigh + 2, for: .horizontal) keyLabel.setContentCompressionResistancePriority(.defaultHigh + 2, for: .horizontal)
keyLabel.setContentHuggingPriority(.defaultHigh, for: .horizontal) keyLabel.setContentHuggingPriority(.defaultHigh, for: .horizontal)
valueLabel.setContentHuggingPriority(.defaultLow, for: .horizontal) valueLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)
@ -85,8 +112,21 @@ class EditableKeyValueRow: NSView {
} }
class KeyValueRow: EditableKeyValueRow { class KeyValueRow: EditableKeyValueRow {
override init() { init() {
super.init() super.init(hasValueImage: false)
valueLabel.isEditable = false
valueLabel.isBordered = false
valueLabel.backgroundColor = .clear
}
required init?(coder decoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class KeyValueImageRow: EditableKeyValueRow {
init() {
super.init(hasValueImage: true)
valueLabel.isEditable = false valueLabel.isEditable = false
valueLabel.isBordered = false valueLabel.isBordered = false
valueLabel.backgroundColor = .clear valueLabel.backgroundColor = .clear