// // KMHomeInsertActionViewController.swift // PDF Master // // Created by wanjun on 2022/11/24. // import Cocoa enum KMInsertScopeType : Int { case firstPage = 0 case lastPage case customizePage } enum KMPageScopeType : Int { case previous = 0 case next } protocol KMHomeInsertActionViewControllerDelegate: NSObjectProtocol { func insertActionViewController(_ viewController: KMHomeInsertActionViewController, fromFilePath: URL) func insertActionViewController(_ viewController: KMHomeInsertActionViewController, insertScopeType: KMInsertScopeType, currentPage: Int, pageScope: KMPageScopeType) } class KMHomeInsertActionViewController: NSViewController { @IBOutlet weak var insertFileLabel: NSTextField! @IBOutlet weak var selectFileBox: NSBox! @IBOutlet weak var selectFileTextField: NSTextField! @IBOutlet weak var selectBox: KMBox! @IBOutlet weak var selectLabel: NSTextField! @IBOutlet weak var positionInsertLabel: NSTextField! @IBOutlet weak var firstPageBox: KMBox! @IBOutlet weak var firstPageImageView: NSImageView! @IBOutlet weak var firstPageLabel: NSTextField! @IBOutlet weak var lastPageBox: KMBox! @IBOutlet weak var lastPageImageView: NSImageView! @IBOutlet weak var lastPageLabel: NSTextField! @IBOutlet weak var customizePageBox: KMBox! @IBOutlet weak var customizePageImageView: NSImageView! @IBOutlet weak var customizePageLabel: NSTextField! @IBOutlet weak var currentPageNumberBox: NSBox! @IBOutlet weak var currentPageNumberTextField: NSTextField! @IBOutlet weak var totalPageNumberLabel: NSTextField! @IBOutlet weak var pageScopeBox: NSBox! @IBOutlet weak var pageScopePopUpButton: NSPopUpButton! var insertScopeType: KMInsertScopeType! = .firstPage var pageScopeType: KMPageScopeType! = .previous var fromFilePath: URL! var currentPageCount: Int! = 1 var pdfDocument: CPDFDocument? = nil open weak var delete: KMHomeInsertActionViewControllerDelegate? override func viewDidLoad() { super.viewDidLoad() // Do view setup here. initializeUI() initLocalization() selectBox.downCallback = { [weak self] (downEntered: Bool, mouseBox: KMBox, event) -> Void in if downEntered { if self != nil { self!.selectButtonAction(self!.selectBox) } } } firstPageBox.downCallback = { [weak self] (downEntered: Bool, mouseBox: KMBox, event) -> Void in if downEntered { if self != nil { self!.insertScopeType = .firstPage self!.reloadData() } } } lastPageBox.downCallback = { [weak self] (downEntered: Bool, mouseBox: KMBox, event) -> Void in if downEntered { if self != nil { self!.insertScopeType = .lastPage self!.reloadData() } } } customizePageBox.downCallback = { [weak self] (downEntered: Bool, mouseBox: KMBox, event) -> Void in if downEntered { if self != nil { self!.insertScopeType = .customizePage self!.reloadData() } } } } override func viewDidAppear() { super.viewDidAppear() reloadData() } // MARK: Init func initializeUI() -> Void { pageScopePopUpButton.menu = pageScopeMenu let menus = pageScopeMenu.items var opIndex = 0 for item: NSMenuItem in menus { item.target = self item.tag = opIndex item.action = #selector(self.pageScopePopUpButtonAction(_:)) opIndex += 1 } selectFileTextField.textColor = .black selectFileTextField.isEnabled = false } func initLocalization() -> Void { insertFileLabel.stringValue = NSLocalizedString("插入文件", comment: "") selectFileTextField.placeholderString = String(format: " " + NSLocalizedString("选择文件", comment: "")) selectBox.toolTip = NSLocalizedString("选择", comment: "") selectLabel.stringValue = NSLocalizedString("选择", comment: "") positionInsertLabel.stringValue = NSLocalizedString("插入位置", comment: "") firstPageBox.toolTip = NSLocalizedString("第一页", comment: "") firstPageLabel.stringValue = NSLocalizedString("第一页", comment: "") lastPageBox.toolTip = NSLocalizedString("最后一页", comment: "") lastPageLabel.stringValue = NSLocalizedString("最后一页", comment: "") customizePageBox.toolTip = NSLocalizedString("页面", comment: "") customizePageLabel.stringValue = NSLocalizedString("页面", comment: "") } // MARK: private func reloadData() -> Void { firstPageImageView.image = NSImage(named: "KMImageNameUnSelect") lastPageImageView.image = NSImage(named: "KMImageNameUnSelect") customizePageImageView.image = NSImage(named: "KMImageNameUnSelect") pageScopePopUpButton.isEnabled = false pageScopeBox.borderColor = .gray switch insertScopeType { case .firstPage: firstPageImageView.image = NSImage(named: "KMImageNameSelect") case .lastPage: lastPageImageView.image = NSImage(named: "KMImageNameSelect") case .customizePage: customizePageImageView.image = NSImage(named: "KMImageNameSelect") pageScopePopUpButton.isEnabled = true pageScopeBox.borderColor = .black case .none: firstPageImageView.image = NSImage(named: "KMImageNameSelect") } currentPageNumberTextField.stringValue = String(currentPageCount) let pageCount = pdfDocument?.pageCount totalPageNumberLabel.stringValue = String(format: "/ %ld", pageCount!) self.delete?.insertActionViewController(self, insertScopeType: insertScopeType, currentPage: currentPageCount, pageScope: pageScopeType) } lazy var pageScopeMenu: NSMenu = { let pageScopeMenu = NSMenu() var item = NSMenuItem() item.title = NSLocalizedString("After", comment: "") pageScopeMenu.addItem(item) item = NSMenuItem() item.title = NSLocalizedString("Before", comment: "") pageScopeMenu.addItem(item) return pageScopeMenu }() // MARK: Action @IBAction func pageScopePopUpButtonAction(_ sender: NSMenuItem) -> Void { let item = sender let index = item.tag if index == 0 { pageScopeType = .next } else if index == 1 { pageScopeType = .previous } reloadData() } @IBAction func selectButtonAction(_ sender: KMBox) -> Void { let openPanel = NSOpenPanel() openPanel.allowedFileTypes = ["pdf"] openPanel.beginSheetModal(for: self.view.window!) { result in if result == .OK { for fileURL in openPanel.urls { let pdfDoc = CPDFDocument(url: fileURL)! if (pdfDoc.isLocked) { } else { self.fromFilePath = fileURL self.selectFileTextField.stringValue = self.fromFilePath.path self.delete?.insertActionViewController(self, fromFilePath: self.fromFilePath) } } } } } }