// // KMAotuFlowExtension.swift // PDF Reader Pro // // Created by lizhe on 2024/1/16. // import Cocoa private var autoFlowTimerKey: UInt8 = 0 private var fastFlowTimerKey: UInt8 = 0 extension CPDFView { var autoFlowTimer: Timer? { get { return objc_getAssociatedObject(self, &autoFlowTimerKey) as? Timer } set { objc_setAssociatedObject(self, &autoFlowTimerKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } } var fastFlowTimer: Timer? { get { return objc_getAssociatedObject(self, &fastFlowTimerKey) as? Timer } set { objc_setAssociatedObject(self, &fastFlowTimerKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } } func startAutoFlow() { let timeInterval = KMAutoFlowOptionsSheetController.timeInterval() let timer = Timer.scheduledTimer(timeInterval: TimeInterval(timeInterval), target: self, selector: #selector(updateAutoFlow), userInfo: nil, repeats: true) autoFlowTimer = timer CustomAlertView.alertView(message: NSLocalizedString("AutoScroll On", comment: ""), fromView: self, withStyle: .black) } @objc func updateAutoFlow() { self.setDisplay(.singlePage) let jumpSpace = KMAutoFlowOptionsSheetController.jumpSpace() if KMPreferenceManager.shared.viewPageDisplayType == .singlePage || KMPreferenceManager.shared.viewPageDisplayType == .twoUp { if canGoToNextPage() { goToNextPage(nil) } else { stopAutoFlow() } } else { if let scrollView = documentView() { let clipView = scrollView.contentView var newOrigin = clipView.bounds.origin newOrigin.y = newOrigin.y + CGFloat(jumpSpace) clipView.animator().setBoundsOrigin(newOrigin) if newOrigin.y <= 0 { stopAutoFlow() } } } } func stopAutoFlow() { autoFlowTimer?.invalidate() autoFlowTimer = nil CustomAlertView.alertView(message: NSLocalizedString("AutoScroll Off", comment: ""), fromView: self, withStyle: .black) } func isAutoFlow() -> Bool { return autoFlowTimer != nil && autoFlowTimer!.isValid } func autoFlow() { if isAutoFlow() { stopAutoFlow() } else { startAutoFlow() } } func startFastFlow() { let timeInterval: TimeInterval = 0.1 let timer = Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector: #selector(updateFastFlow), userInfo: nil, repeats: true) fastFlowTimer = timer } @objc func updateFastFlow() { let jumpSpace: CGFloat = 200 // if !isScrollUp { // // jumpSpace为负值 // let negativeJumpSpace = -jumpSpace // updateScrollView(with: negativeJumpSpace) // } else { updateScrollView(with: jumpSpace) // } } func updateScrollView(with jumpSpace: CGFloat) { if self.displayMode() == .singlePage || self.displayMode() == .twoUp { if canGoToNextPage() { goToNextPage(nil) } else { stopFastFlow() } } else { if let scrollView = documentView().enclosingScrollView { let clipView = scrollView.contentView var newOrigin = clipView.bounds.origin newOrigin.y -= jumpSpace clipView.animator().setBoundsOrigin(newOrigin) if newOrigin.y <= 0 /*&& isScrollUp*/ { stopFastFlow() } else if newOrigin.y >= documentView().frame.size.height /*&& !isScrollUp*/ { stopFastFlow() } } } } func stopFastFlow() { fastFlowTimer?.invalidate() fastFlowTimer = nil } func isFastFlow() -> Bool { return fastFlowTimer != nil && fastFlowTimer!.isValid } func fastFlow() { if isFastFlow() { stopFastFlow() } else { startFastFlow() } } } //extension CPDFView { // func displayViewMode() -> CPDFDisplayViewMode { // var displayViewMode: CPDFDisplayViewMode = .singlePage // if self.displayMode() == .singlePage && self.displayDirection == .vertical { // displayViewMode = .singlePageContinuous // } // // if self.displayMode() == .twoUp && self.displayDirection == .vertical { // displayViewMode = .twoUpContinuous // } // return displayViewMode // } // // func setDisplayViewMode(mode: CPDFDisplayViewMode) { // switch mode { // case .twoUpContinuous: // self.displayTwoUp = true // self.displaysAsBook = false // self.displayDirection = .vertical // break // case .twoUp: // self.displayTwoUp = true // self.displaysAsBook = false // self.displayDirection = .horizontal // break // case .singlePageContinuous: // self.displayTwoUp = false // self.displayDirection = .vertical // break // default: // self.displayTwoUp = false // self.displayDirection = .horizontal // break // } // } //}