KMPageIndicator.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // KMPageIndicator.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/10/10.
  6. //
  7. import Cocoa
  8. @objc(TCPageIndicatorDelegate)
  9. protocol KMPageIndicatorDelegate: AnyObject {
  10. func pageIndicator(_ pageIndicator: KMPageIndicator, didSelectPageAt index: UInt)
  11. }
  12. @objc (TCPageIndicator)
  13. @objcMembers class KMPageIndicator: NSView {
  14. weak var delegate: KMPageIndicatorDelegate?
  15. var selectedColor: NSColor?
  16. var normalColor: NSColor?
  17. var indicatorMargin: CGFloat = 8.0 {
  18. didSet {
  19. self.needsDisplay = true
  20. }
  21. }
  22. var currentPage: UInt = 0 {
  23. didSet {
  24. self.needsDisplay = true
  25. }
  26. }
  27. var numberOfPages: UInt = 0 {
  28. didSet {
  29. self.needsDisplay = true
  30. }
  31. }
  32. var pageIndicatorSize: NSSize = CGSizeMake(6, 6) {
  33. didSet {
  34. self.needsDisplay = true
  35. }
  36. }
  37. // 是否响应事件 [默认为 YES ]
  38. var enabled = true
  39. private var _indicatorRects: [NSValue]?
  40. override init(frame frameRect: NSRect) {
  41. super.init(frame: frameRect)
  42. self.initDefaultValue()
  43. }
  44. required init?(coder: NSCoder) {
  45. super.init(coder: coder)
  46. self.initDefaultValue()
  47. }
  48. func initDefaultValue() {
  49. self.selectedColor = NSColor.gray
  50. self.normalColor = NSColor.gray.withAlphaComponent(0.5)
  51. self._indicatorRects = [NSValue](repeating: NSValue(rect: NSZeroRect), count: 10)
  52. }
  53. override func draw(_ dirtyRect: NSRect) {
  54. super.draw(dirtyRect)
  55. var indicatorAreaWidth = 0.0
  56. if (self.numberOfPages > 0) {
  57. indicatorAreaWidth = self.pageIndicatorSize.width * self.numberOfPages.cgFloat + self.indicatorMargin * (self.numberOfPages-1).cgFloat
  58. }
  59. var leftPosition = (self.bounds.size.width - indicatorAreaWidth) * 0.5
  60. let topPadding = (self.bounds.size.height - self.pageIndicatorSize.height) * 0.5
  61. self._indicatorRects?.removeAll()
  62. for i in 0 ..< self.numberOfPages {
  63. let position = NSMakePoint(leftPosition, topPadding)
  64. let rect = CGRectMake(position.x, position.y, self.pageIndicatorSize.width, self.pageIndicatorSize.height)
  65. self._indicatorRects?.append(NSValue(rect: rect))
  66. let path = NSBezierPath(ovalIn: rect)
  67. if (self.currentPage == i) {
  68. self.selectedColor?.setFill()
  69. } else {
  70. self.normalColor?.setFill()
  71. }
  72. path.fill()
  73. leftPosition += (self.pageIndicatorSize.width + self.indicatorMargin)
  74. }
  75. }
  76. override func mouseDown(with event: NSEvent) {
  77. super.mouseDown(with: event)
  78. if (self.enabled == false) {
  79. return
  80. }
  81. let eventLocation = event.locationInWindow
  82. /// 转换成视图的本地坐标
  83. let pointInView = self.convert(eventLocation, from: nil)
  84. for i in 0 ..< self.numberOfPages {
  85. if let datas = self._indicatorRects {
  86. let rect = CGRectInset(datas[Int(i)].rectValue, -2, -2)
  87. if (NSPointInRect(pointInView, rect)) {
  88. self.currentPage = i
  89. self.delegate?.pageIndicator(self, didSelectPageAt: i)
  90. }
  91. }
  92. }
  93. }
  94. }