KMBox.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // KMBox.swift
  3. // PDF Master
  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: mouseDownCallback?
  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(hex: "#68ACF8")
  29. var area: NSTrackingArea?
  30. deinit {
  31. if (self.area != nil) {
  32. self.removeTrackingArea(self.area!)
  33. self.area = nil
  34. }
  35. }
  36. override func draw(_ dirtyRect: NSRect) {
  37. super.draw(dirtyRect)
  38. if isDottedLine {
  39. let path = NSBezierPath(roundedRect: self.bounds, xRadius: dottedLineRadius, yRadius: dottedLineRadius)
  40. path.setLineDash(lineDash, count: 3, phase: 0)
  41. path.lineWidth = dottedLineWidth
  42. dottedLineColor.setStroke()
  43. path.stroke()
  44. }
  45. }
  46. override func updateTrackingAreas() {
  47. super.updateTrackingAreas()
  48. if let _area = self.area, _area.rect.isEmpty == false {
  49. if (_area.rect.equalTo(self.bounds)) {
  50. return
  51. }
  52. }
  53. if (self.area != nil) {
  54. self.removeTrackingArea(self.area!)
  55. self.area = nil
  56. }
  57. self.area = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .mouseMoved, .activeAlways], owner: self)
  58. self.addTrackingArea(self.area!)
  59. // self.addTrackingRect(self.bounds, owner: self, userData: nil, assumeInside: false)
  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 (event.clickCount == 1) {
  88. if let callback = downCallback {
  89. callback(true, self, event)
  90. }
  91. } else if (event.clickCount > 1) {
  92. if let callback = doubleCallback {
  93. callback(true, self)
  94. }
  95. }
  96. }
  97. }
  98. override func mouseUp(with event: NSEvent) {
  99. super.mouseUp(with: event)
  100. if canClick {
  101. if clickColor != nil {
  102. fillColor = defaultColor!
  103. }
  104. if let callback = downCallback {
  105. callback(false, self, event)
  106. }
  107. }
  108. }
  109. override func rightMouseDown(with event: NSEvent) {
  110. super.rightMouseDown(with: event)
  111. if canRightClick {
  112. if let callback = rightDownCallback {
  113. callback(true, self, event)
  114. }
  115. }
  116. }
  117. }