KMAotuFlowExtension.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 KMPreferenceManager.shared.viewPageDisplayType == .singlePage ||
  37. KMPreferenceManager.shared.viewPageDisplayType == .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. // if !isScrollUp {
  78. // // jumpSpace为负值
  79. // let negativeJumpSpace = -jumpSpace
  80. // updateScrollView(with: negativeJumpSpace)
  81. // } else {
  82. updateScrollView(with: jumpSpace)
  83. // }
  84. }
  85. func updateScrollView(with jumpSpace: CGFloat) {
  86. if self.displayMode() == .singlePage || self.displayMode() == .twoUp {
  87. if canGoToNextPage() {
  88. goToNextPage(nil)
  89. } else {
  90. stopFastFlow()
  91. }
  92. } else {
  93. if let scrollView = documentView().enclosingScrollView {
  94. let clipView = scrollView.contentView
  95. var newOrigin = clipView.bounds.origin
  96. newOrigin.y -= jumpSpace
  97. clipView.animator().setBoundsOrigin(newOrigin)
  98. if newOrigin.y <= 0 /*&& isScrollUp*/ {
  99. stopFastFlow()
  100. } else if newOrigin.y >= documentView().frame.size.height /*&& !isScrollUp*/ {
  101. stopFastFlow()
  102. }
  103. }
  104. }
  105. }
  106. func stopFastFlow() {
  107. fastFlowTimer?.invalidate()
  108. fastFlowTimer = nil
  109. }
  110. func isFastFlow() -> Bool {
  111. return fastFlowTimer != nil && fastFlowTimer!.isValid
  112. }
  113. func fastFlow() {
  114. if isFastFlow() {
  115. stopFastFlow()
  116. } else {
  117. startFastFlow()
  118. }
  119. }
  120. }
  121. //extension CPDFView {
  122. // func displayViewMode() -> CPDFDisplayViewMode {
  123. // var displayViewMode: CPDFDisplayViewMode = .singlePage
  124. // if self.displayMode() == .singlePage && self.displayDirection == .vertical {
  125. // displayViewMode = .singlePageContinuous
  126. // }
  127. //
  128. // if self.displayMode() == .twoUp && self.displayDirection == .vertical {
  129. // displayViewMode = .twoUpContinuous
  130. // }
  131. // return displayViewMode
  132. // }
  133. //
  134. // func setDisplayViewMode(mode: CPDFDisplayViewMode) {
  135. // switch mode {
  136. // case .twoUpContinuous:
  137. // self.displayTwoUp = true
  138. // self.displaysAsBook = false
  139. // self.displayDirection = .vertical
  140. // break
  141. // case .twoUp:
  142. // self.displayTwoUp = true
  143. // self.displaysAsBook = false
  144. // self.displayDirection = .horizontal
  145. // break
  146. // case .singlePageContinuous:
  147. // self.displayTwoUp = false
  148. // self.displayDirection = .vertical
  149. // break
  150. // default:
  151. // self.displayTwoUp = false
  152. // self.displayDirection = .horizontal
  153. // break
  154. // }
  155. // }
  156. //}