我愿意
为了指示选择,macOS 使用圆角矩形填充 NSColor.selectedContentBackgroundColor。我会模仿它,但使用自定义的红色。当控件不是按键或应用程序位于后台时,变量
isEmphasized
会变为 false,因此我将使用它来触发刷新。
import AppKit
final class CustomTableRowView: NSTableRowView {
override func layout() {
super.layout()
// the default .none won’t even call drawSelection(in:)
selectionHighlightStyle = .regular
}
override func drawSelection(in dirtyRect: NSRect) {
guard selectionHighlightStyle != .none else {
return
}
let selectionRect = NSInsetRect(bounds, 10, 0)
let fillColor = isEmphasized ? NSColor.red : NSColor.unemphasizedSelectedContentBackgroundColor
fillColor.setFill()
let selectionPath = NSBezierPath.init(roundedRect: selectionRect, xRadius: 5, yRadius: 5)
selectionPath.fill()
}
override var isEmphasized: Bool {
didSet {
needsDisplay = true
}
}
}
这里我告诉 NSViewController 将默认的 NSTableRowView 替换为上面的自定义类:
// MARK: - NSOutlineViewDelegate
func outlineView(_ outlineView: NSOutlineView, rowViewForItem item: Any) -> NSTableRowView? {
CustomTableRowView()
}