KMDottedLineView.swift 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // KMDottedLineView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2023/5/19.
  6. //
  7. import Cocoa
  8. @objc enum KMDrawBorderState : Int {
  9. case None = 0
  10. case Hover
  11. case Select
  12. }
  13. class KMDottedLineView: NSView {
  14. var state: KMDrawBorderState = .None
  15. var area: NSTrackingArea?
  16. var canHover : Bool! = true // 是否允许悬浮
  17. var canClick : Bool! = true // 是否允许点击
  18. deinit {
  19. if (self.area != nil) {
  20. self.removeTrackingArea(self.area!)
  21. self.area = nil
  22. }
  23. }
  24. override func draw(_ dirtyRect: NSRect) {
  25. super.draw(dirtyRect)
  26. if ((self.state == .Hover) || (self.state == .Select)) {
  27. let path = NSBezierPath(roundedRect: self.bounds, xRadius: 8.0, yRadius: 8.0)
  28. path.lineWidth = 1.0
  29. NSColor.km_init(hex: "#68ACF8").setStroke()
  30. path.stroke()
  31. } else {
  32. let path = NSBezierPath(roundedRect: self.bounds, xRadius: 8.0, yRadius: 8.0)
  33. path.setLineDash([3, 3, 3], count: 3, phase: 0)
  34. path.lineWidth = 1.0
  35. NSColor.km_init(hex: "#CED0D4").setStroke()
  36. path.stroke()
  37. }
  38. }
  39. override func updateTrackingAreas() {
  40. super.updateTrackingAreas()
  41. if let _area = self.area, _area.rect.isEmpty == false {
  42. if (_area.rect.equalTo(self.bounds)) {
  43. return
  44. }
  45. }
  46. if (self.area != nil) {
  47. self.removeTrackingArea(self.area!)
  48. self.area = nil
  49. }
  50. self.area = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .mouseMoved, .activeAlways], owner: self)
  51. self.addTrackingArea(self.area!)
  52. }
  53. override func mouseEntered(with event: NSEvent) {
  54. if canHover {
  55. self.state = .Hover
  56. self.needsDisplay = true
  57. }
  58. }
  59. override func mouseExited(with event: NSEvent) {
  60. if canHover {
  61. self.state = .None
  62. self.needsDisplay = true
  63. }
  64. }
  65. }