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 = SettingsManager.sharedInstance.autoScrollTimeInterval
  29. let timer = Timer.scheduledTimer(timeInterval: TimeInterval(timeInterval), target: self, selector: #selector(updateAutoFlow), userInfo: nil, repeats: true)
  30. autoFlowTimer = timer
  31. let _ = CustomAlertView.alertView(message: NSLocalizedString("AutoScroll On", comment: ""), fromView: self, withStyle: .black)
  32. }
  33. @objc func updateAutoFlow() {
  34. let jumpSpace = SettingsManager.sharedInstance.autoScrollJumpSpace
  35. if self.displayMode() == .singlePage ||
  36. self.displayMode() == .twoUp {
  37. if canGoToNextPage() {
  38. goToNextPage(nil)
  39. } else {
  40. stopAutoFlow()
  41. }
  42. } else {
  43. if let scrollView = documentView() {
  44. let clipView = scrollView.contentView
  45. var newOrigin = clipView.bounds.origin
  46. newOrigin.y = newOrigin.y + CGFloat(jumpSpace)
  47. clipView.animator().setBoundsOrigin(newOrigin)
  48. if newOrigin.y <= 0 {
  49. stopAutoFlow()
  50. }
  51. }
  52. }
  53. }
  54. func stopAutoFlow() {
  55. autoFlowTimer?.invalidate()
  56. autoFlowTimer = nil
  57. let _ = CustomAlertView.alertView(message: NSLocalizedString("AutoScroll Off", comment: ""), fromView: self, withStyle: .black)
  58. }
  59. func isAutoFlow() -> Bool {
  60. return autoFlowTimer != nil && autoFlowTimer!.isValid
  61. }
  62. func autoFlow() {
  63. if isAutoFlow() {
  64. stopAutoFlow()
  65. } else {
  66. startAutoFlow()
  67. }
  68. }
  69. func startFastFlow() {
  70. let timeInterval: TimeInterval = 0.1
  71. let timer = Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector: #selector(updateFastFlow), userInfo: nil, repeats: true)
  72. fastFlowTimer = timer
  73. }
  74. @objc func updateFastFlow() {
  75. let jumpSpace: CGFloat = 200
  76. updateScrollView(with: jumpSpace)
  77. }
  78. func updateScrollView(with jumpSpace: CGFloat) {
  79. if self.displayMode() == .singlePage || self.displayMode() == .twoUp {
  80. if canGoToNextPage() {
  81. goToNextPage(nil)
  82. } else {
  83. stopFastFlow()
  84. }
  85. } else {
  86. if let scrollView = documentView().enclosingScrollView {
  87. let clipView = scrollView.contentView
  88. var newOrigin = clipView.bounds.origin
  89. newOrigin.y -= jumpSpace
  90. clipView.animator().setBoundsOrigin(newOrigin)
  91. if newOrigin.y <= 0 /*&& isScrollUp*/ {
  92. stopFastFlow()
  93. } else if newOrigin.y >= documentView().frame.size.height /*&& !isScrollUp*/ {
  94. stopFastFlow()
  95. }
  96. }
  97. }
  98. }
  99. func stopFastFlow() {
  100. fastFlowTimer?.invalidate()
  101. fastFlowTimer = nil
  102. }
  103. func isFastFlow() -> Bool {
  104. return fastFlowTimer != nil && fastFlowTimer!.isValid
  105. }
  106. func fastFlow() {
  107. if isFastFlow() {
  108. stopFastFlow()
  109. } else {
  110. startFastFlow()
  111. }
  112. }
  113. }