KMGuideTargetView.swift 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // KMGuideTargetView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2024/6/25.
  6. //
  7. import Cocoa
  8. class KMGuideTargetView: NSView {
  9. var lineWidth: CGFloat = 2 {
  10. didSet {
  11. self.needsDisplay = true
  12. }
  13. }
  14. var strokeColor: NSColor = .black {
  15. didSet {
  16. self.needsDisplay = true
  17. }
  18. }
  19. var insets: NSEdgeInsets = .init(top: 0, left: 0, bottom: 0, right: 0) {
  20. didSet {
  21. self.needsDisplay = true
  22. }
  23. }
  24. override func draw(_ dirtyRect: NSRect) {
  25. super.draw(dirtyRect)
  26. let rect = self.bounds
  27. let left = self.insets.left
  28. let top = self.insets.top
  29. let drawW = max(NSWidth(rect)-left-self.insets.right, 0)
  30. let drawH = max(NSHeight(rect)-top-self.insets.bottom, 0)
  31. let drawRect = NSMakeRect(left, top, drawW, drawH)
  32. let context = NSGraphicsContext.current?.cgContext
  33. context?.saveGState()
  34. context?.setLineWidth(self.lineWidth)
  35. context?.setStrokeColor(self.strokeColor.cgColor)
  36. let dxy = self.lineWidth * 0.5
  37. let outRect = drawRect.insetBy(dx: dxy, dy: dxy)
  38. context?.addEllipse(in: outRect)
  39. let offset: CGFloat = 4
  40. let inRect = outRect.insetBy(dx: dxy+offset, dy: dxy+offset)
  41. context?.addEllipse(in: inRect)
  42. context?.strokePath()
  43. context?.restoreGState()
  44. }
  45. }