KMTableRowView.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // KMTableRowView.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2023/10/10.
  6. //
  7. import Cocoa
  8. class KMTableRowView: NSTableRowView {
  9. var _hasBottomLine: Bool = false
  10. var _bottomLineColor: NSColor?
  11. var selectionInset: NSEdgeInsets = NSEdgeInsets()
  12. @objc var selectionRadius: CGFloat = 0.0
  13. @objc var selectionBackgroundColorBlock: (() -> NSColor)?
  14. override init(frame frameRect: NSRect) {
  15. super.init(frame: frameRect)
  16. addTrackingArea()
  17. selectionInset = NSEdgeInsetsZero
  18. selectionRadius = 0.0
  19. }
  20. required init?(coder: NSCoder) {
  21. super.init(coder: coder)
  22. }
  23. var hasBottomLine: Bool {
  24. get {
  25. return _hasBottomLine
  26. }
  27. set {
  28. _hasBottomLine = newValue
  29. self.needsDisplay = true
  30. }
  31. }
  32. var bottomLineColor: NSColor? {
  33. get {
  34. return _bottomLineColor
  35. }
  36. set {
  37. _bottomLineColor = newValue
  38. self.needsDisplay = true
  39. }
  40. }
  41. //MARK: Private Methods
  42. func addTrackingArea() {
  43. let trackingArea = NSTrackingArea.init(rect: self.bounds, options: [.mouseEnteredAndExited, .inVisibleRect, .activeAlways, .mouseMoved], owner: self)
  44. self.addTrackingArea(trackingArea)
  45. }
  46. override func draw(_ dirtyRect: NSRect) {
  47. super.draw(dirtyRect)
  48. if self.hasBottomLine {
  49. var color = self.bottomLineColor
  50. if color == nil {
  51. color = NSColor.lightGray
  52. }
  53. color!.setStroke()
  54. color!.setFill()
  55. let lineRect = NSRect(x: 0, y: NSHeight(bounds) - 1, width: NSWidth(bounds), height: 1)
  56. let linePath = NSBezierPath(roundedRect: lineRect, xRadius: 0, yRadius: 0)
  57. linePath.fill()
  58. linePath.stroke()
  59. }
  60. }
  61. override func drawSelection(in dirtyRect: NSRect) {
  62. if selectionBackgroundColorBlock == nil {
  63. let selectionRect = NSRect(x: selectionInset.left, y: selectionInset.top, width: NSWidth(bounds) - selectionInset.left - selectionInset.right, height: NSHeight(bounds) - selectionInset.top - selectionInset.bottom)
  64. NSColor.lightGray.setFill()
  65. let selectionPath = NSBezierPath(roundedRect: selectionRect, xRadius: selectionRadius, yRadius: selectionRadius)
  66. selectionPath.fill()
  67. return
  68. }
  69. let selectionRect = NSRect(x: selectionInset.left, y: selectionInset.top, width: NSWidth(bounds) - selectionInset.left - selectionInset.right, height: NSHeight(bounds) - selectionInset.top - selectionInset.bottom)
  70. let color = selectionBackgroundColorBlock!()
  71. color.setStroke()
  72. color.setFill()
  73. let selectionPath = NSBezierPath(roundedRect: selectionRect, xRadius: selectionRadius, yRadius: selectionRadius)
  74. selectionPath.fill()
  75. selectionPath.stroke()
  76. }
  77. override func mouseEntered(with event: NSEvent) {
  78. super.mouseEntered(with: event)
  79. }
  80. }