KMBOTAOutlineRowView.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // KMBOTAOutlineRowView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/4/2.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. typealias KMBOTAOutlineRowViewHoverCallback = (_ mouseEntered: Bool, _ mouseBox: KMBox) -> Void
  10. typealias KMBOTAOutlineRowViewRightMouseCallback = (_ view: KMBOTAOutlineRowView, _ event: NSEvent) -> Void
  11. typealias KMBOTAOutlineRowViewMouseDownCallback = (_ view: KMBOTAOutlineRowView, _ event: NSEvent) -> Void
  12. class KMBOTAOutlineRowView: NSTableRowView {
  13. var box: KMBox?
  14. var contentBox: KMBox?
  15. var model: KMBOTAOutlineItem? {
  16. didSet {
  17. self.reloadData()
  18. }
  19. }
  20. var hoverCallback: KMBOTAOutlineRowViewHoverCallback?
  21. var rightMouseCallback: KMBOTAOutlineRowViewRightMouseCallback?
  22. var mouseDownCallback: KMBOTAOutlineRowViewMouseDownCallback?
  23. var itemSelect: Bool = false {
  24. didSet {
  25. self.needsDisplay = true
  26. }
  27. }
  28. override func draw(_ dirtyRect: NSRect) {
  29. super.draw(dirtyRect)
  30. self.addBox()
  31. self.drawView()
  32. }
  33. func addBox() {
  34. if self.box == nil {
  35. let rect = self.bounds
  36. self.box?.wantsLayer = true
  37. self.box = KMBox(frame: rect)
  38. self.box?.borderWidth = 1
  39. self.box?.borderColor = NSColor.km_init(hex: "#EDEEF0")
  40. self.box?.layer?.cornerRadius = 4
  41. self.box?.boxType = .custom
  42. self.box?.autoresizingMask = [.width, .height]
  43. self.box?.moveCallback = { [unowned self] (mouseEntered, mouseBox) in
  44. self.hoverCallback?(mouseEntered, mouseBox)
  45. self.reloadData()
  46. }
  47. self.addSubview(self.box!, positioned: NSWindow.OrderingMode.below, relativeTo: self)
  48. }
  49. var rect = self.bounds
  50. rect.origin.x = self.bounds.origin.x + 8
  51. rect.origin.y = self.bounds.origin.y
  52. rect.size.height = self.bounds.size.height
  53. rect.size.width = self.bounds.size.width - 16
  54. self.box?.frame = rect
  55. if self.contentBox == nil {
  56. let rect = self.bounds
  57. self.contentBox?.wantsLayer = true
  58. self.contentBox = KMBox(frame: rect)
  59. self.contentBox?.borderWidth = 0
  60. self.contentBox?.boxType = .custom
  61. self.contentBox?.autoresizingMask = [.width, .height]
  62. self.contentBox?.rightDownCallback = { [unowned self] (downEntered, mouseBox, event) in
  63. guard let callBack = rightMouseCallback else { return }
  64. callBack(self, event)
  65. }
  66. }
  67. self.addSubview(self.contentBox!, positioned: NSWindow.OrderingMode.above, relativeTo: self)
  68. }
  69. override func drawSelection(in dirtyRect: NSRect) {
  70. // self.model.select = true
  71. self.reloadData()
  72. }
  73. override func mouseDown(with event: NSEvent) {
  74. super.mouseDown(with: event)
  75. guard let callBack = mouseDownCallback else { return }
  76. callBack(self, event)
  77. }
  78. }
  79. extension KMBOTAOutlineRowView {
  80. func reloadData() {
  81. self.drawView()
  82. }
  83. func drawView(_ color: NSColor = NSColor.km_init(hex: "#EDEEF0")) {
  84. box?.fillColor = .clear
  85. box?.borderWidth = 0
  86. var cellView: KMBOTAOutlineCellView?
  87. if self.numberOfColumns > 0 {
  88. cellView = (self.view(atColumn: 0) as? KMBOTAOutlineCellView)
  89. }
  90. let box = cellView?.hoverBox
  91. if self.model?.select == true {
  92. let color = ComponentLibrary.shared.getComponentColorFromKey("colorPrimary/bg-opacity-dark")
  93. box?.backgroundColor(color)
  94. if let value = ComponentLibrary.shared.getComponentValueFromKey("radius/xs") {
  95. let currentValue = value as? CGFloat ?? 0
  96. box?.layer?.cornerRadius = currentValue
  97. } else {
  98. box?.layer?.cornerRadius = 4
  99. }
  100. box?.borderColor = color
  101. box?.borderWidth = 0
  102. } else if self.model?.hover == true {
  103. box?.backgroundColor(ComponentLibrary.shared.getComponentColorFromKey("colorFill/hov-opacity"))
  104. if let value = ComponentLibrary.shared.getComponentValueFromKey("radius/xs") {
  105. let currentValue = value as? CGFloat ?? 0
  106. box?.layer?.cornerRadius = currentValue
  107. } else {
  108. box?.layer?.cornerRadius = 4
  109. }
  110. box?.borderWidth = 0
  111. } else {
  112. // box?.backgroundColor(NSColor.km_init(hex: "#F7F8FA"))
  113. box?.backgroundColor(.clear)
  114. box?.borderWidth = 0
  115. }
  116. }
  117. }