KMBox.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // KMBox.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2022/10/13.
  6. //
  7. import Cocoa
  8. typealias mouseMoveCallback = (_ mouseEntered: Bool, _ mouseBox: KMBox) -> Void
  9. typealias mouseDownCallback = (_ downEntered: Bool, _ mouseBox: KMBox, _ event: NSEvent) -> Void
  10. typealias doubleClickCallback = (_ downEntered: Bool, _ mouseBox: KMBox) -> Void
  11. typealias mouseRightDownCallback = (_ downEntered: Bool, _ mouseBox: KMBox, _ event: NSEvent) -> Void
  12. @objcMembers class KMBox: NSBox {
  13. var moveCallback: mouseMoveCallback?
  14. var downCallback: ((_ downEntered: Bool, _ mouseBox: KMBox, _ event: NSEvent) -> Void)?
  15. var doubleCallback: doubleClickCallback?
  16. var rightDownCallback: mouseRightDownCallback?
  17. var hoverColor : NSColor? // 悬浮颜色
  18. var clickColor : NSColor? // 点击颜色
  19. var defaultColor : NSColor? // 默认颜色
  20. var canHover : Bool! = true // 是否允许悬浮
  21. var canClick : Bool! = true // 是否允许点击
  22. var canRightClick : Bool! = true // 是否允许右击
  23. var canAutoHidden : Bool = false //是否需要自动隐藏
  24. var isDottedLine: Bool = false
  25. var lineDash: [CGFloat] = [3, 3, 3]
  26. var dottedLineRadius = 8.0
  27. var dottedLineWidth = 1.0
  28. var dottedLineColor: NSColor = NSColor.km_init(hex: "#68ACF8")
  29. var canDoubleClick : Bool = false //是否运行多次点击
  30. var area: NSTrackingArea?
  31. deinit {
  32. if (self.area != nil) {
  33. self.removeTrackingArea(self.area!)
  34. self.area = nil
  35. }
  36. }
  37. override func draw(_ dirtyRect: NSRect) {
  38. super.draw(dirtyRect)
  39. if isDottedLine {
  40. let path = NSBezierPath(roundedRect: self.bounds, xRadius: dottedLineRadius, yRadius: dottedLineRadius)
  41. path.setLineDash(lineDash, count: 3, phase: 0)
  42. path.lineWidth = dottedLineWidth
  43. dottedLineColor.setStroke()
  44. path.stroke()
  45. }
  46. }
  47. override func updateTrackingAreas() {
  48. super.updateTrackingAreas()
  49. if let _area = self.area, _area.rect.isEmpty == false {
  50. if (_area.rect.equalTo(self.bounds)) {
  51. return
  52. }
  53. }
  54. if (self.area != nil) {
  55. self.removeTrackingArea(self.area!)
  56. self.area = nil
  57. }
  58. self.area = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .mouseMoved, .activeAlways], owner: self)
  59. self.addTrackingArea(self.area!)
  60. // self.addTrackingRect(self.bounds, owner: self, userData: nil, assumeInside: false)
  61. }
  62. override func mouseEntered(with event: NSEvent) {
  63. if canHover {
  64. if hoverColor != nil {
  65. fillColor = hoverColor!
  66. }
  67. if let callback = moveCallback {
  68. callback(true, self)
  69. }
  70. }
  71. }
  72. override func mouseExited(with event: NSEvent) {
  73. if canHover {
  74. if defaultColor != nil {
  75. fillColor = defaultColor!
  76. }
  77. if let callback = moveCallback {
  78. callback(false, self)
  79. }
  80. }
  81. }
  82. override func mouseDown(with event: NSEvent) {
  83. super.mouseDown(with: event)
  84. if canClick {
  85. if clickColor != nil {
  86. fillColor = clickColor!
  87. }
  88. if self.canDoubleClick {
  89. if (event.clickCount == 1) {
  90. if let callback = downCallback {
  91. callback(true, self, event)
  92. }
  93. } else if (event.clickCount > 1) {
  94. if let callback = doubleCallback {
  95. callback(true, self)
  96. }
  97. }
  98. } else {
  99. if let callback = downCallback {
  100. callback(true, self, event)
  101. }
  102. }
  103. }
  104. }
  105. override func mouseUp(with event: NSEvent) {
  106. super.mouseUp(with: event)
  107. if canClick {
  108. if clickColor != nil {
  109. fillColor = defaultColor!
  110. }
  111. if let callback = downCallback {
  112. callback(false, self, event)
  113. }
  114. }
  115. }
  116. override func rightMouseDown(with event: NSEvent) {
  117. super.rightMouseDown(with: event)
  118. if canRightClick {
  119. if let callback = rightDownCallback {
  120. callback(true, self, event)
  121. }
  122. }
  123. }
  124. }