KMImageToPDFWindowController.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // KMImageToPDFWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2022/11/15.
  6. //
  7. import Cocoa
  8. var imageToPDFController: KMImageToPDFWindowController?
  9. class KMImageToPDFWindowController: NSWindowController {
  10. @IBOutlet weak var titleLabel: NSTextField!
  11. @IBOutlet weak var batchPrecessingBackgroundView: NSView!
  12. @IBOutlet weak var batchPrecessingView: KMBatchProcessingView!
  13. @IBOutlet weak var chooseBackgroundView: NSView!
  14. @IBOutlet weak var chooseView: KMImageToPDFChooseView!
  15. @IBOutlet weak var closeBox: KMBox!
  16. @IBOutlet weak var closeButton: NSButton!
  17. var chooseData: KMImageToPDFChooseModel?
  18. var batchData: [KMBatchProcessingTableViewModel]?
  19. var inputType: DataNavigationViewButtonActionType? {
  20. didSet {
  21. self.batchPrecessingView.inputType = inputType
  22. }
  23. }
  24. deinit {
  25. print("KMImageToPDFWindowController 释放")
  26. }
  27. override func windowDidLoad() {
  28. super.windowDidLoad()
  29. self.window?.isMovableByWindowBackground = true
  30. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  31. self.window?.title = "Image to PDF"
  32. self.setup()
  33. self.updateLanguage()
  34. self.reloadData()
  35. self.addNotification()
  36. }
  37. func addNotification() {
  38. NotificationCenter.default.addObserver(self, selector: #selector(windowClose), name: NSWindow.willCloseNotification, object: nil)
  39. }
  40. func setup() {
  41. self.window?.contentView?.wantsLayer = true
  42. self.window?.contentView?.layer?.backgroundColor = NSColor.white.cgColor
  43. self.window?.contentView?.layer?.cornerRadius = 8
  44. self.window?.backgroundColor = NSColor.clear
  45. self.chooseBackgroundView.wantsLayer = true
  46. self.chooseBackgroundView.layer?.backgroundColor = NSColor(hex: "#F7F8FA").cgColor
  47. self.batchPrecessingView.delegate = self
  48. self.batchPrecessingView.inputType = self.inputType
  49. self.chooseView.delegate = self
  50. self.titleLabel.font = NSFont.SFProTextSemibold(16.0)
  51. self.titleLabel.textColor = NSColor(hex: "#252629")
  52. self.closeBox.moveCallback = { [weak self] (mouseEntered, mouseBox) in
  53. if mouseEntered {
  54. self?.closeButton.image = NSImage(named: "control_btn_icon_close_hov")
  55. } else {
  56. self?.closeButton.image = NSImage(named: "control_btn_icon_close")
  57. }
  58. }
  59. }
  60. func updateLanguage() {
  61. self.titleLabel.stringValue = NSLocalizedString("Image to PDF", comment: "")
  62. }
  63. func reloadData() {
  64. }
  65. //MARK: 打开文件
  66. static func openFiles(window: NSWindow) {
  67. if KMImageToPDFWindowController.isSampleController() {
  68. print("存在相同文件")
  69. let controller: KMImageToPDFWindowController = self.fetchSampleController()
  70. controller.inputType = .ImageToPDF
  71. controller.showWindow(window)
  72. } else {
  73. KMBatchProcessingView.openfiles(window: window) { openPanel in
  74. openPanel.canChooseDirectories = false
  75. openPanel.canChooseFiles = true
  76. openPanel.allowsMultipleSelection = true
  77. openPanel.allowedFileTypes = KMBatchProcessingTableViewModel.supportedImageTypes()
  78. openPanel.message = NSLocalizedString("Press ⌘/⇧ to select multiple files", comment: "")
  79. } completion: { (panel ,data) in
  80. if data.count != 0 {
  81. let imageToPDFWindow: KMImageToPDFWindowController = KMImageToPDFWindowController.init(windowNibName: "KMImageToPDFWindowController")
  82. // imageToPDFWindow.showWindow(window)
  83. // imageToPDFWindow.window?.orderFront(window)
  84. let point = CGPoint(x: window.frame.origin.x + window.frame.width / 2 - (imageToPDFWindow.window?.frame.size.width)! / 2,
  85. y: window.frame.origin.y + window.frame.height / 2 - (imageToPDFWindow.window?.frame.size.height)! / 2)
  86. window.addChildWindow(imageToPDFWindow.window!, ordered: NSWindow.OrderingMode.above)
  87. imageToPDFWindow.window?.center()
  88. // DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  89. // imageToPDFWindow.window?.setFrameOrigin(point)
  90. // imageToPDFWindow.window?.makeKeyAndOrderFront(nil)
  91. //// imageToPDFWindow.showWindow(window)
  92. // }
  93. // NSRect windowRect = imageToPDFWindow.window.frame
  94. // NSRect dialogRect = [[activeNodeDialog window] frame];
  95. // NSPoint pos;
  96. // pos.x = windowRect.origin.x + windowRect.size.width - dialogRect.size.width - 10;
  97. // pos.y = windowRect.origin.y + 32;
  98. // [[activeNodeDialog window] setFrameOrigin:pos];
  99. // [[activeNodeDialog window] makeKeyAndOrderFront:nil];
  100. imageToPDFWindow.batchPrecessingView.inputData = data
  101. imageToPDFWindow.inputType = .ImageToPDF
  102. imageToPDFController = imageToPDFWindow
  103. }
  104. }
  105. }
  106. }
  107. static func isSampleController() -> Bool {
  108. for window in NSApp.windows {
  109. let controller = window.windowController
  110. if controller is KMImageToPDFWindowController {
  111. return true
  112. }
  113. }
  114. return false
  115. }
  116. static func fetchSampleController() -> KMImageToPDFWindowController {
  117. for window in NSApp.windows {
  118. let controller = window.windowController
  119. if controller is KMImageToPDFWindowController {
  120. return controller as! KMImageToPDFWindowController
  121. }
  122. }
  123. return NSWindowController() as! KMImageToPDFWindowController
  124. }
  125. @objc func windowClose(notification: NSNotification) {
  126. let window: NSWindow = notification.object as? NSWindow ?? NSWindow()
  127. if window == self.window {
  128. NotificationCenter.default.removeObserver(self)
  129. self.batchPrecessingView.delegate = nil
  130. self.chooseView.delegate = nil
  131. imageToPDFController = nil
  132. }
  133. }
  134. }
  135. protocol KMImageToPDFWindowControllerAction {}
  136. extension KMImageToPDFWindowController: KMImageToPDFWindowControllerAction {
  137. @IBAction func closeButtonAction(_ sender: NSButton) {
  138. self.window?.close()
  139. }
  140. @IBAction func shrinkButtonAction(_ sender: NSButton) {
  141. }
  142. @IBAction func enlargeButtonAction(_ sender: NSButton) {
  143. }
  144. }
  145. extension KMImageToPDFWindowController: KMImageToPDFChooseViewDelegate {
  146. func exportAction(data: KMImageToPDFChooseModel) {
  147. print("导出")
  148. self.chooseData = data
  149. if self.batchData != nil {
  150. self.chooseData?.imageFilePaths = self.batchData
  151. KMImageToPDFManager.manager.exportPDF(model: self.chooseData!) { success, savePath, errors, OCRerrors in
  152. print(success)
  153. if success {
  154. NSWorkspace.shared.selectFile(savePath, inFileViewerRootedAtPath: "");
  155. } else {
  156. let alert = NSAlert()
  157. alert.alertStyle = .critical
  158. alert.messageText = NSLocalizedString("导出失败", comment: "")
  159. alert.runModal()
  160. return
  161. }
  162. } progress: { [unowned self]status in
  163. self.batchPrecessingView.reloadData()
  164. }
  165. }
  166. }
  167. }
  168. extension KMImageToPDFWindowController: KMBatchProcessingViewDelegate {
  169. func reloadData(data: [KMBatchProcessingTableViewModel]) {
  170. self.batchData = data
  171. self.chooseView.data.imageFilePaths = data
  172. self.chooseView.reloadData()
  173. }
  174. }