KMBox.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. }
  61. override func mouseEntered(with event: NSEvent) {
  62. if canHover {
  63. if hoverColor != nil {
  64. fillColor = hoverColor!
  65. }
  66. if let callback = moveCallback {
  67. callback(true, self)
  68. }
  69. }
  70. }
  71. override func mouseExited(with event: NSEvent) {
  72. if canHover {
  73. if defaultColor != nil {
  74. fillColor = defaultColor!
  75. }
  76. if let callback = moveCallback {
  77. callback(false, self)
  78. }
  79. }
  80. }
  81. override func mouseDown(with event: NSEvent) {
  82. super.mouseDown(with: event)
  83. if canClick {
  84. if clickColor != nil {
  85. fillColor = clickColor!
  86. }
  87. if self.canDoubleClick {
  88. if (event.clickCount == 1) {
  89. if let callback = downCallback {
  90. callback(true, self, event)
  91. }
  92. } else if (event.clickCount > 1) {
  93. if let callback = doubleCallback {
  94. callback(true, self)
  95. }
  96. }
  97. } else {
  98. if let callback = downCallback {
  99. callback(true, self, event)
  100. }
  101. }
  102. }
  103. }
  104. override func mouseUp(with event: NSEvent) {
  105. super.mouseUp(with: event)
  106. if canClick {
  107. if clickColor != nil {
  108. fillColor = defaultColor!
  109. }
  110. if let callback = downCallback {
  111. callback(false, self, event)
  112. }
  113. }
  114. }
  115. override func rightMouseDown(with event: NSEvent) {
  116. super.rightMouseDown(with: event)
  117. if canRightClick {
  118. if let callback = rightDownCallback {
  119. callback(true, self, event)
  120. }
  121. }
  122. }
  123. }