KMBotaTableRowView.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // KMBotaTableRowView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/28.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. typealias KMAnnOutlineRowViewRightMouseCallback = (_ view: KMBotaTableRowView, _ event: NSEvent) -> Void
  10. class KMBotaTableRowView: NSTableRowView {
  11. var selectCallback: ((KMBotaTableRowView)->Void)?
  12. var rightMouseCallback: KMAnnOutlineRowViewRightMouseCallback?
  13. var contentBox: KMBox?
  14. convenience init() {
  15. self.init(frame: .zero)
  16. self.addTrackingArea()
  17. }
  18. override func draw(_ dirtyRect: NSRect) {
  19. super.draw(dirtyRect)
  20. addBox()
  21. // Drawing code here.
  22. }
  23. func addTrackingArea() {
  24. let trackingArea = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .inVisibleRect, .activeAlways, .mouseMoved], owner: self, userInfo: nil)
  25. self.addTrackingArea(trackingArea)
  26. }
  27. func addBox() {
  28. if self.contentBox == nil {
  29. let rect = self.bounds
  30. self.contentBox?.wantsLayer = true
  31. self.contentBox = KMBox(frame: rect)
  32. self.contentBox?.borderWidth = 0
  33. self.contentBox?.boxType = .custom
  34. self.contentBox?.autoresizingMask = [.width, .height]
  35. self.contentBox?.rightDownCallback = { [unowned self] (downEntered, mouseBox, event) in
  36. guard let callBack = rightMouseCallback else { return }
  37. callBack(self, event)
  38. }
  39. }
  40. self.addSubview(contentBox!)
  41. }
  42. override func drawSelection(in dirtyRect: NSRect) {
  43. let selectionRect = self.bounds
  44. let color = ComponentLibrary.shared.getComponentColorFromKey("colorPrimary/bg-opacity-dark")
  45. color.setFill()
  46. let selectionPath = NSBezierPath(roundedRect: selectionRect, xRadius: 0, yRadius: 0)
  47. selectionRect.fill()
  48. }
  49. override var isSelected: Bool {
  50. get {
  51. return super.isSelected
  52. }
  53. set {
  54. super.isSelected = newValue
  55. self.selectCallback?(self)
  56. }
  57. }
  58. }