// // 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() } }