123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- //
- // KMPDFInsertPreviewViewController.swift
- // PDF Master
- //
- // Created by wanjun on 2022/11/24.
- //
- import Cocoa
- @objc protocol KMPDFInsertPreviewViewControllerDelegate {
- @objc optional func insertPreviewViewController(_ viewController: KMPDFInsertPreviewViewController, pageChanged: Int)
- }
- class KMPDFInsertPreviewViewController: NSViewController {
- @IBOutlet weak var previewView: CPDFListView!
- @IBOutlet weak var leftTurnBox: KMBox!
- @IBOutlet weak var leftTurnImageView: NSImageView!
- @IBOutlet weak var currentPageNumberTextField: NSTextField!
- @IBOutlet weak var totalPageNumberLabel: NSTextField!
- @IBOutlet weak var rightTurnBox: KMBox!
- @IBOutlet weak var rightTurnImageView: NSImageView!
-
- var pdfDocument: CPDFDocument? = nil
-
- open weak var delete: KMPDFInsertPreviewViewControllerDelegate?
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
-
- previewView.document = pdfDocument
- previewView.autoScales = true
-
- initializeUI()
- initLocalization()
-
- leftTurnBox.downCallback = { [self] (downEntered: Bool, mouseBox: KMBox, event) -> Void in
- if downEntered {
- previousButtonAction(leftTurnBox)
- }
- }
- rightTurnBox.downCallback = { [self] (downEntered: Bool, mouseBox: KMBox, event) -> Void in
- if downEntered {
- nextButtonAction(rightTurnBox)
- }
- }
-
- NotificationCenter.default.addObserver(self, selector: #selector(PDFViewDocumentChangedNotification), name: NSNotification.Name.CPDFViewPageChanged, object: nil)
- }
-
- override func viewDidAppear() {
- super.viewDidAppear()
-
- reloadPDFData()
- }
-
- // MARK: Init
-
- func initializeUI() -> Void {
-
- }
-
- func initLocalization() -> Void {
- leftTurnBox.toolTip = NSLocalizedString("向前", comment: "")
- rightTurnBox.toolTip = NSLocalizedString("向后", comment: "")
- }
- // MARK: private
- func reloadPDFData() -> Void {
- previewView.documentView().documentView?.enclosingScrollView?.hasVerticalScroller = false
- previewView.documentView().documentView?.enclosingScrollView?.hasHorizontalScroller = false
-
- let pageCount = pdfDocument?.pageCount
- let currentPageIndex = pdfDocument?.index(for: previewView.currentPage())
- currentPageNumberTextField.stringValue = String(format: "%ld", (currentPageIndex!+1))
- totalPageNumberLabel.stringValue = String(format: "/ %ld", pageCount!)
-
- self.delete?.insertPreviewViewController?(self, pageChanged: Int(currentPageIndex!+1))
- }
-
- // MARK: Action
-
- @IBAction func nextButtonAction(_ sender: KMBox) -> Void {
- previewView.goToNextPage(nil)
- reloadPDFData()
- }
-
- @IBAction func previousButtonAction(_ sender: KMBox) -> Void {
- previewView.goToPreviousPage(nil)
- reloadPDFData()
- }
-
- // MARK: Notification
-
- @objc func PDFViewDocumentChangedNotification(_ notification: Notification) -> Void {
- let page = previewView.currentPage()
- let pageIndex = previewView.document.index(for: page)
- currentPageNumberTextField.stringValue = String(format: "%ld", (pageIndex + 1))
- }
- }
- extension KMPDFInsertPreviewViewController: NSTextFieldDelegate {
- func controlTextDidChange(_ obj: Notification) {
-
- }
-
- func controlTextDidEndEditing(_ obj: Notification) {
- let textField = obj.object as! NSTextField
- if textField.isEqual(currentPageNumberTextField) {
- let index = Int(currentPageNumberTextField.stringValue)
- let pageCount = pdfDocument?.pageCount
- let currentPageIndex = pdfDocument?.index(for: previewView.currentPage())
- if index! > 0 && index! <= pageCount! {
- let gotoPage = pdfDocument?.page(at: UInt(index! - 1))
- previewView.go(toPageIndex: Int((pdfDocument?.index(for: gotoPage))!), animated: true)
- reloadPDFData()
- } else {
- currentPageNumberTextField.stringValue = String(format: "%ld", (currentPageIndex!+1))
- }
- }
- }
- }
|