KMCustomTableRowView.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // KMCustomTableRowView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lxy on 2022/11/7.
  6. //
  7. import Cocoa
  8. class KMCustomTableRowView: NSTableRowView {
  9. var highlightColor : NSColor!
  10. var selectionColor : NSColor!
  11. var isHaveRadius = false
  12. var color : NSColor!
  13. // MARK - Draw
  14. override func drawBackground(in dirtyRect: NSRect) {
  15. if(self.color != nil) {
  16. self.color.setFill()
  17. if self.isHaveRadius == true {
  18. let selectionPath = NSBezierPath.init(roundedRect: dirtyRect, xRadius: 6.0, yRadius: 6.0)
  19. selectionPath.fill()
  20. } else {
  21. __NSRectFill(dirtyRect)
  22. }
  23. } else {
  24. super.drawBackground(in: dirtyRect)
  25. }
  26. }
  27. override func drawSelection(in dirtyRect: NSRect) {
  28. if self.selectionHighlightStyle == NSTableView.SelectionHighlightStyle.none {
  29. super.drawSelection(in: dirtyRect)
  30. } else {
  31. if(self.selectionColor != nil) {
  32. self.selectionColor.setFill()
  33. if self.isHaveRadius == true {
  34. let selectionPath = NSBezierPath.init(roundedRect: dirtyRect, xRadius: 6.0, yRadius: 6.0)
  35. selectionPath.fill()
  36. } else {
  37. __NSRectFill(dirtyRect)
  38. }
  39. } else {
  40. super.drawSelection(in: dirtyRect)
  41. }
  42. }
  43. }
  44. // MARK - Event
  45. private func addTrackingArea() {
  46. let opt = (NSTrackingArea.Options.mouseEnteredAndExited.rawValue | NSTrackingArea.Options.inVisibleRect.rawValue | NSTrackingArea.Options.activeInKeyWindow.rawValue)
  47. let trackingArea = NSTrackingArea.init(rect: self.bounds,options:NSTrackingArea.Options(rawValue: opt), owner: self, userInfo:nil)
  48. self.addTrackingArea(trackingArea)
  49. }
  50. override func mouseEntered(with event: NSEvent) {
  51. super.mouseEntered(with: event)
  52. self.color = self.highlightColor
  53. self.needsDisplay = false
  54. }
  55. override func mouseExited(with event: NSEvent) {
  56. super.mouseExited(with: event)
  57. self.color = NSColor()
  58. }
  59. }