KMAotuFlowExtension.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // KMAotuFlowExtension.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2024/1/16.
  6. //
  7. import Cocoa
  8. private var autoFlowTimerKey: UInt8 = 0
  9. private var fastFlowTimerKey: UInt8 = 0
  10. extension CPDFView {
  11. var autoFlowTimer: Timer? {
  12. get {
  13. return objc_getAssociatedObject(self, &autoFlowTimerKey) as? Timer
  14. }
  15. set {
  16. objc_setAssociatedObject(self, &autoFlowTimerKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  17. }
  18. }
  19. var fastFlowTimer: Timer? {
  20. get {
  21. return objc_getAssociatedObject(self, &fastFlowTimerKey) as? Timer
  22. }
  23. set {
  24. objc_setAssociatedObject(self, &fastFlowTimerKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  25. }
  26. }
  27. func startAutoFlow() {
  28. let timeInterval = KMAutoFlowOptionsSheetController.timeInterval()
  29. let timer = Timer.scheduledTimer(timeInterval: TimeInterval(timeInterval), target: self, selector: #selector(updateAutoFlow), userInfo: nil, repeats: true)
  30. autoFlowTimer = timer
  31. CustomAlertView.alertView(message: NSLocalizedString("AutoScroll On", comment: ""), fromView: self, withStyle: .black)
  32. }
  33. @objc func updateAutoFlow() {
  34. self.setDisplay(.singlePage)
  35. let jumpSpace = KMAutoFlowOptionsSheetController.jumpSpace()
  36. if self.displayMode() == .singlePage ||
  37. self.displayMode() == .twoUp {
  38. if canGoToNextPage() {
  39. goToNextPage(nil)
  40. } else {
  41. stopAutoFlow()
  42. }
  43. } else {
  44. if let scrollView = documentView() {
  45. let clipView = scrollView.contentView
  46. var newOrigin = clipView.bounds.origin
  47. newOrigin.y = newOrigin.y + CGFloat(jumpSpace)
  48. clipView.animator().setBoundsOrigin(newOrigin)
  49. if newOrigin.y <= 0 {
  50. stopAutoFlow()
  51. }
  52. }
  53. }
  54. }
  55. func stopAutoFlow() {
  56. autoFlowTimer?.invalidate()
  57. autoFlowTimer = nil
  58. CustomAlertView.alertView(message: NSLocalizedString("AutoScroll Off", comment: ""), fromView: self, withStyle: .black)
  59. }
  60. func isAutoFlow() -> Bool {
  61. return autoFlowTimer != nil && autoFlowTimer!.isValid
  62. }
  63. func autoFlow() {
  64. if isAutoFlow() {
  65. stopAutoFlow()
  66. } else {
  67. startAutoFlow()
  68. }
  69. }
  70. func startFastFlow() {
  71. let timeInterval: TimeInterval = 0.1
  72. let timer = Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector: #selector(updateFastFlow), userInfo: nil, repeats: true)
  73. fastFlowTimer = timer
  74. }
  75. @objc func updateFastFlow() {
  76. let jumpSpace: CGFloat = 200
  77. updateScrollView(with: jumpSpace)
  78. }
  79. func updateScrollView(with jumpSpace: CGFloat) {
  80. if self.displayMode() == .singlePage || self.displayMode() == .twoUp {
  81. if canGoToNextPage() {
  82. goToNextPage(nil)
  83. } else {
  84. stopFastFlow()
  85. }
  86. } else {
  87. if let scrollView = documentView().enclosingScrollView {
  88. let clipView = scrollView.contentView
  89. var newOrigin = clipView.bounds.origin
  90. newOrigin.y -= jumpSpace
  91. clipView.animator().setBoundsOrigin(newOrigin)
  92. if newOrigin.y <= 0 /*&& isScrollUp*/ {
  93. stopFastFlow()
  94. } else if newOrigin.y >= documentView().frame.size.height /*&& !isScrollUp*/ {
  95. stopFastFlow()
  96. }
  97. }
  98. }
  99. }
  100. func stopFastFlow() {
  101. fastFlowTimer?.invalidate()
  102. fastFlowTimer = nil
  103. }
  104. func isFastFlow() -> Bool {
  105. return fastFlowTimer != nil && fastFlowTimer!.isValid
  106. }
  107. func fastFlow() {
  108. if isFastFlow() {
  109. stopFastFlow()
  110. } else {
  111. startFastFlow()
  112. }
  113. }
  114. }