KMTableRowView.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // KMTableRowView.swift
  3. // PDF Reader Pro
  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. KMAppearance.Status.selColor().setFill()
  66. let selectionPath = NSBezierPath(roundedRect: selectionRect, xRadius: selectionRadius, yRadius: selectionRadius)
  67. selectionPath.fill()
  68. return
  69. }
  70. let selectionRect = NSRect(x: selectionInset.left, y: selectionInset.top, width: NSWidth(bounds) - selectionInset.left - selectionInset.right, height: NSHeight(bounds) - selectionInset.top - selectionInset.bottom)
  71. let color = selectionBackgroundColorBlock!()
  72. color.setStroke()
  73. color.setFill()
  74. let selectionPath = NSBezierPath(roundedRect: selectionRect, xRadius: selectionRadius, yRadius: selectionRadius)
  75. selectionPath.fill()
  76. selectionPath.stroke()
  77. }
  78. override func mouseEntered(with event: NSEvent) {
  79. super.mouseEntered(with: event)
  80. }
  81. }