123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- //
- // KMHomeInsertActionViewController.swift
- // PDF Reader Pro
- //
- // 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)
- }
- }
- }
- }
- }
- }
|