123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- //
- // KMImageToPDFWindowController.swift
- // PDF Master
- //
- // Created by lizhe on 2022/11/15.
- //
- import Cocoa
- var imageToPDFController: KMImageToPDFWindowController?
- class KMImageToPDFWindowController: NSWindowController {
-
- @IBOutlet weak var titleLabel: NSTextField!
- @IBOutlet weak var batchPrecessingBackgroundView: NSView!
- @IBOutlet weak var batchPrecessingView: KMBatchProcessingView!
- @IBOutlet weak var chooseBackgroundView: NSView!
- @IBOutlet weak var chooseView: KMImageToPDFChooseView!
- @IBOutlet weak var closeBox: KMBox!
- @IBOutlet weak var closeButton: NSButton!
- var chooseData: KMImageToPDFChooseModel?
- var batchData: [KMBatchProcessingTableViewModel]?
- var inputType: DataNavigationViewButtonActionType? {
- didSet {
- self.batchPrecessingView.inputType = inputType
- }
- }
-
- deinit {
- print("KMImageToPDFWindowController 释放")
- }
-
- override func windowDidLoad() {
- super.windowDidLoad()
- self.window?.isMovableByWindowBackground = true
-
- // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
- self.window?.title = "Image to PDF"
- self.setup()
- self.updateLanguage()
- self.reloadData()
- self.addNotification()
- }
-
- func addNotification() {
- NotificationCenter.default.addObserver(self, selector: #selector(windowClose), name: NSWindow.willCloseNotification, object: nil)
- }
-
- func setup() {
- self.window?.contentView?.wantsLayer = true
- self.window?.contentView?.layer?.backgroundColor = NSColor.white.cgColor
- self.window?.contentView?.layer?.cornerRadius = 8
- self.window?.backgroundColor = NSColor.clear
-
- self.chooseBackgroundView.wantsLayer = true
- self.chooseBackgroundView.layer?.backgroundColor = NSColor(hex: "#F7F8FA").cgColor
-
- self.batchPrecessingView.delegate = self
- self.batchPrecessingView.inputType = self.inputType
- self.chooseView.delegate = self
-
- self.titleLabel.font = NSFont.SFProTextSemibold(16.0)
- self.titleLabel.textColor = NSColor(hex: "#252629")
-
- self.closeBox.moveCallback = { [weak self] (mouseEntered, mouseBox) in
- if mouseEntered {
- self?.closeButton.image = NSImage(named: "control_btn_icon_close_hov")
- } else {
- self?.closeButton.image = NSImage(named: "control_btn_icon_close")
- }
- }
- }
-
- func updateLanguage() {
- self.titleLabel.stringValue = NSLocalizedString("Image to PDF", comment: "")
- }
- func reloadData() {
-
- }
-
- //MARK: 打开文件
- static func openFiles(window: NSWindow) {
- if KMImageToPDFWindowController.isSampleController() {
- print("存在相同文件")
- let controller: KMImageToPDFWindowController = self.fetchSampleController()
- controller.inputType = .ImageToPDF
- controller.showWindow(window)
- } else {
- KMBatchProcessingView.openfiles(window: window) { openPanel in
- openPanel.canChooseDirectories = false
- openPanel.canChooseFiles = true
- openPanel.allowsMultipleSelection = true
- openPanel.allowedFileTypes = KMBatchProcessingTableViewModel.supportedImageTypes()
- openPanel.message = NSLocalizedString("Press ⌘/⇧ to select multiple files", comment: "")
- } completion: { (panel ,data) in
- if data.count != 0 {
- let imageToPDFWindow: KMImageToPDFWindowController = KMImageToPDFWindowController.init(windowNibName: "KMImageToPDFWindowController")
- // imageToPDFWindow.showWindow(window)
- // imageToPDFWindow.window?.orderFront(window)
- let point = CGPoint(x: window.frame.origin.x + window.frame.width / 2 - (imageToPDFWindow.window?.frame.size.width)! / 2,
- y: window.frame.origin.y + window.frame.height / 2 - (imageToPDFWindow.window?.frame.size.height)! / 2)
- window.addChildWindow(imageToPDFWindow.window!, ordered: NSWindow.OrderingMode.above)
- imageToPDFWindow.window?.center()
- // DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
- // imageToPDFWindow.window?.setFrameOrigin(point)
- // imageToPDFWindow.window?.makeKeyAndOrderFront(nil)
- //// imageToPDFWindow.showWindow(window)
- // }
-
-
- // NSRect windowRect = imageToPDFWindow.window.frame
- // NSRect dialogRect = [[activeNodeDialog window] frame];
- // NSPoint pos;
- // pos.x = windowRect.origin.x + windowRect.size.width - dialogRect.size.width - 10;
- // pos.y = windowRect.origin.y + 32;
- // [[activeNodeDialog window] setFrameOrigin:pos];
- // [[activeNodeDialog window] makeKeyAndOrderFront:nil];
-
- imageToPDFWindow.batchPrecessingView.inputData = data
- imageToPDFWindow.inputType = .ImageToPDF
- imageToPDFController = imageToPDFWindow
- }
- }
- }
- }
-
- static func isSampleController() -> Bool {
- for window in NSApp.windows {
- let controller = window.windowController
- if controller is KMImageToPDFWindowController {
- return true
- }
- }
- return false
- }
-
- static func fetchSampleController() -> KMImageToPDFWindowController {
- for window in NSApp.windows {
- let controller = window.windowController
- if controller is KMImageToPDFWindowController {
- return controller as! KMImageToPDFWindowController
- }
- }
-
- return NSWindowController() as! KMImageToPDFWindowController
- }
-
- @objc func windowClose(notification: NSNotification) {
- let window: NSWindow = notification.object as? NSWindow ?? NSWindow()
- if window == self.window {
- NotificationCenter.default.removeObserver(self)
- self.batchPrecessingView.delegate = nil
- self.chooseView.delegate = nil
- imageToPDFController = nil
- }
- }
- }
- protocol KMImageToPDFWindowControllerAction {}
- extension KMImageToPDFWindowController: KMImageToPDFWindowControllerAction {
- @IBAction func closeButtonAction(_ sender: NSButton) {
- self.window?.close()
- }
-
- @IBAction func shrinkButtonAction(_ sender: NSButton) {
-
- }
-
- @IBAction func enlargeButtonAction(_ sender: NSButton) {
-
- }
- }
- extension KMImageToPDFWindowController: KMImageToPDFChooseViewDelegate {
- func exportAction(data: KMImageToPDFChooseModel) {
- print("导出")
- self.chooseData = data
-
- if self.batchData != nil {
- self.chooseData?.imageFilePaths = self.batchData
- KMImageToPDFManager.manager.exportPDF(model: self.chooseData!) { success, savePath, errors, OCRerrors in
- print(success)
- if success {
- NSWorkspace.shared.selectFile(savePath, inFileViewerRootedAtPath: "");
- } else {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = NSLocalizedString("导出失败", comment: "")
- alert.runModal()
- return
- }
- } progress: { [unowned self]status in
- self.batchPrecessingView.reloadData()
- }
- }
- }
- }
- extension KMImageToPDFWindowController: KMBatchProcessingViewDelegate {
- func reloadData(data: [KMBatchProcessingTableViewModel]) {
- self.batchData = data
- self.chooseView.data.imageFilePaths = data
- self.chooseView.reloadData()
- }
- }
|