KMBookMarkTableRowView.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // KMBookMarkTableRowView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/3/14.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. typealias KMBookMarkTableRowViewHoverCallback = (_ mouseEntered: Bool, _ mouseBox: KMBox) -> Void
  10. typealias KMBookMarkTableRowViewMouseDown = (_ view: KMBookMarkTableRowView, _ event: NSEvent) -> Void
  11. typealias KMBookMarkTableRowViewRightMouseDown = (_ view: KMBookMarkTableRowView, _ event: NSEvent) -> Void
  12. class KMBookMarkTableRowView: NSTableRowView {
  13. var box: KMBox?
  14. var contentBox: KMBox?
  15. var model: KMBookMarkItem! {
  16. didSet {
  17. self.reloadData()
  18. }
  19. }
  20. var hoverCallback: KMBookMarkTableRowViewHoverCallback?
  21. var mouseDownAction: KMBookMarkTableRowViewMouseDown?
  22. var rightMouseDownAction: KMBookMarkTableRowViewRightMouseDown?
  23. var contentBackgroundColor: NSColor?
  24. var menuClickedAction: ((_ clickPoint: NSPoint) -> NSMenu)?
  25. override func draw(_ dirtyRect: NSRect) {
  26. super.draw(dirtyRect)
  27. self.addBox()
  28. self.drawView()
  29. }
  30. func addBox() {
  31. if self.box == nil {
  32. var rect = self.bounds
  33. rect.origin.x = self.bounds.origin.x + 8.0
  34. rect.size.width = self.bounds.size.width - 16.0
  35. self.box?.wantsLayer = true
  36. self.box?.layer?.masksToBounds = true
  37. self.box = KMBox(frame: rect)
  38. self.box?.borderWidth = 0
  39. self.box?.boxType = .custom
  40. self.addSubview(self.box!, positioned: NSWindow.OrderingMode.below, relativeTo: self)
  41. self.box?.autoresizingMask = [.width, .height]
  42. self.box?.moveCallback = { [unowned self] (mouseEntered, mouseBox) in
  43. self.hoverCallback?(mouseEntered, mouseBox)
  44. self.reloadData()
  45. }
  46. self.box?.downCallback = { [unowned self] (mouseEntered, mouseBox, event) in
  47. self.mouseDownAction?(self, event)
  48. }
  49. self.box?.rightDownCallback = { [unowned self] (mouseEntered, mouseBox, event) in
  50. self.rightMouseDownAction?(self, event)
  51. _ = menuClickedAction?(event.locationInWindow)
  52. }
  53. self.contentBox?.wantsLayer = true
  54. self.contentBox?.layer?.masksToBounds = true
  55. self.contentBox = KMBox(frame: self.bounds)
  56. self.contentBox?.borderWidth = 0
  57. self.contentBox?.layer?.cornerRadius = 8
  58. self.contentBox?.boxType = .custom
  59. self.contentBox?.autoresizingMask = [.width, .height]
  60. self.contentBox?.rightDownCallback = { [unowned self] (downEntered, mouseBox, event) in
  61. guard let callBack = rightMouseDownAction else { return }
  62. callBack(self, event)
  63. _ = menuClickedAction?(event.locationInWindow)
  64. }
  65. }
  66. self.addSubview(self.contentBox!, positioned: NSWindow.OrderingMode.above, relativeTo: self)
  67. }
  68. override func drawSelection(in dirtyRect: NSRect) {
  69. // super.drawSelection(in: dirtyRect)
  70. self.reloadData()
  71. }
  72. override func menu(for event: NSEvent) -> NSMenu? {
  73. let point = event.locationInWindow
  74. if menuClickedAction != nil {
  75. let menu = menuClickedAction!(point)
  76. return menu
  77. }
  78. return super.menu(for: event)
  79. }
  80. }
  81. extension KMBookMarkTableRowView {
  82. func reloadData() {
  83. self.drawView()
  84. }
  85. func drawView(_ color: NSColor = NSColor.km_init(hex: "#EDEEF0")) {
  86. if self.model!.select {
  87. self.box?.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorPrimary/bg-opacity-dark")
  88. } else if self.model.hover {
  89. self.box?.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorFill/hov-opacity")
  90. } else {
  91. self.box?.fillColor = .clear
  92. }
  93. }
  94. }