KMDottedLineView.swift 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // KMDottedLineView.swift
  3. // PDF Master
  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(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(hex: "#CED0D4").setStroke()
  36. path.stroke()
  37. }
  38. }
  39. override func updateTrackingAreas() {
  40. super.updateTrackingAreas()
  41. if (self.area != nil) {
  42. self.removeTrackingArea(self.area!)
  43. self.area = nil
  44. }
  45. self.area = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .mouseMoved, .activeAlways], owner: self)
  46. self.addTrackingArea(self.area!)
  47. }
  48. override func mouseEntered(with event: NSEvent) {
  49. if canHover {
  50. self.state = .Hover
  51. self.needsDisplay = true
  52. }
  53. }
  54. override func mouseExited(with event: NSEvent) {
  55. if canHover {
  56. self.state = .None
  57. self.needsDisplay = true
  58. }
  59. }
  60. }