// // KMTableRowView.swift // PDF Reader Pro // // Created by wanjun on 2023/10/10. // import Cocoa class KMTableRowView: NSTableRowView { var _hasBottomLine: Bool = false var _bottomLineColor: NSColor? var selectionInset: NSEdgeInsets = NSEdgeInsets() @objc var selectionRadius: CGFloat = 0.0 @objc var selectionBackgroundColorBlock: (() -> NSColor)? override init(frame frameRect: NSRect) { super.init(frame: frameRect) addTrackingArea() selectionInset = NSEdgeInsetsZero selectionRadius = 0.0 } required init?(coder: NSCoder) { super.init(coder: coder) } var hasBottomLine: Bool { get { return _hasBottomLine } set { _hasBottomLine = newValue self.needsDisplay = true } } var bottomLineColor: NSColor? { get { return _bottomLineColor } set { _bottomLineColor = newValue self.needsDisplay = true } } //MARK: Private Methods func addTrackingArea() { let trackingArea = NSTrackingArea.init(rect: self.bounds, options: [.mouseEnteredAndExited, .inVisibleRect, .activeAlways, .mouseMoved], owner: self) self.addTrackingArea(trackingArea) } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) if self.hasBottomLine { var color = self.bottomLineColor if color == nil { color = NSColor.lightGray } color!.setStroke() color!.setFill() let lineRect = NSRect(x: 0, y: NSHeight(bounds) - 1, width: NSWidth(bounds), height: 1) let linePath = NSBezierPath(roundedRect: lineRect, xRadius: 0, yRadius: 0) linePath.fill() linePath.stroke() } } override func drawSelection(in dirtyRect: NSRect) { if selectionBackgroundColorBlock == nil { let selectionRect = NSRect(x: selectionInset.left, y: selectionInset.top, width: NSWidth(bounds) - selectionInset.left - selectionInset.right, height: NSHeight(bounds) - selectionInset.top - selectionInset.bottom) // NSColor.lightGray.setFill() KMAppearance.Status.selColor().setFill() let selectionPath = NSBezierPath(roundedRect: selectionRect, xRadius: selectionRadius, yRadius: selectionRadius) selectionPath.fill() return } let selectionRect = NSRect(x: selectionInset.left, y: selectionInset.top, width: NSWidth(bounds) - selectionInset.left - selectionInset.right, height: NSHeight(bounds) - selectionInset.top - selectionInset.bottom) let color = selectionBackgroundColorBlock!() color.setStroke() color.setFill() let selectionPath = NSBezierPath(roundedRect: selectionRect, xRadius: selectionRadius, yRadius: selectionRadius) selectionPath.fill() selectionPath.stroke() } override func mouseEntered(with event: NSEvent) { super.mouseEntered(with: event) } }