KMPDFInsertPreviewViewController.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // KMPDFInsertPreviewViewController.swift
  3. // Cisdem PDFMaster
  4. //
  5. // Created by wanjun on 2022/11/24.
  6. //
  7. import Cocoa
  8. @objc protocol KMPDFInsertPreviewViewControllerDelegate {
  9. @objc optional func insertPreviewViewController(_ viewController: KMPDFInsertPreviewViewController, pageChanged: Int)
  10. }
  11. class KMPDFInsertPreviewViewController: NSViewController {
  12. @IBOutlet weak var previewView: CPDFListView!
  13. @IBOutlet weak var leftTurnBox: KMBox!
  14. @IBOutlet weak var leftTurnImageView: NSImageView!
  15. @IBOutlet weak var currentPageNumberTextField: NSTextField!
  16. @IBOutlet weak var totalPageNumberLabel: NSTextField!
  17. @IBOutlet weak var rightTurnBox: KMBox!
  18. @IBOutlet weak var rightTurnImageView: NSImageView!
  19. var pdfDocument: CPDFDocument? = nil
  20. open weak var delete: KMPDFInsertPreviewViewControllerDelegate?
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23. // Do view setup here.
  24. previewView.document = pdfDocument
  25. previewView.autoScales = true
  26. initializeUI()
  27. initLocalization()
  28. leftTurnBox.downCallback = { [self] (downEntered: Bool, mouseBox: KMBox, event) -> Void in
  29. if downEntered {
  30. previousButtonAction(leftTurnBox)
  31. }
  32. }
  33. rightTurnBox.downCallback = { [self] (downEntered: Bool, mouseBox: KMBox, event) -> Void in
  34. if downEntered {
  35. nextButtonAction(rightTurnBox)
  36. }
  37. }
  38. NotificationCenter.default.addObserver(self, selector: #selector(PDFViewDocumentChangedNotification), name: NSNotification.Name.CPDFViewPageChanged, object: nil)
  39. }
  40. override func viewDidAppear() {
  41. super.viewDidAppear()
  42. reloadPDFData()
  43. }
  44. // MARK: Init
  45. func initializeUI() -> Void {
  46. }
  47. func initLocalization() -> Void {
  48. leftTurnBox.toolTip = NSLocalizedString("向前", comment: "")
  49. rightTurnBox.toolTip = NSLocalizedString("向后", comment: "")
  50. }
  51. // MARK: private
  52. func reloadPDFData() -> Void {
  53. previewView.documentView().documentView?.enclosingScrollView?.hasVerticalScroller = false
  54. previewView.documentView().documentView?.enclosingScrollView?.hasHorizontalScroller = false
  55. let pageCount = pdfDocument?.pageCount
  56. let currentPageIndex = pdfDocument?.index(for: previewView.currentPage())
  57. currentPageNumberTextField.stringValue = String(format: "%ld", (currentPageIndex!+1))
  58. totalPageNumberLabel.stringValue = String(format: "/ %ld", pageCount!)
  59. self.delete?.insertPreviewViewController?(self, pageChanged: Int(currentPageIndex!+1))
  60. }
  61. // MARK: Action
  62. @IBAction func nextButtonAction(_ sender: KMBox) -> Void {
  63. previewView.goToNextPage(nil)
  64. reloadPDFData()
  65. }
  66. @IBAction func previousButtonAction(_ sender: KMBox) -> Void {
  67. previewView.goToPreviousPage(nil)
  68. reloadPDFData()
  69. }
  70. // MARK: Notification
  71. @objc func PDFViewDocumentChangedNotification(_ notification: Notification) -> Void {
  72. let page = previewView.currentPage()
  73. let pageIndex = previewView.document.index(for: page)
  74. currentPageNumberTextField.stringValue = String(format: "%ld", (pageIndex + 1))
  75. }
  76. }
  77. extension KMPDFInsertPreviewViewController: NSTextFieldDelegate {
  78. func controlTextDidChange(_ obj: Notification) {
  79. }
  80. func controlTextDidEndEditing(_ obj: Notification) {
  81. let textField = obj.object as! NSTextField
  82. if textField.isEqual(currentPageNumberTextField) {
  83. let index = Int(currentPageNumberTextField.stringValue)
  84. let pageCount = pdfDocument?.pageCount
  85. let currentPageIndex = pdfDocument?.index(for: previewView.currentPage())
  86. if index! > 0 && index! <= pageCount! {
  87. let gotoPage = pdfDocument?.page(at: UInt(index! - 1))
  88. previewView.go(toPageIndex: Int((pdfDocument?.index(for: gotoPage))!), animated: true)
  89. reloadPDFData()
  90. } else {
  91. currentPageNumberTextField.stringValue = String(format: "%ld", (currentPageIndex!+1))
  92. }
  93. }
  94. }
  95. }