KMImageToPDFWindowController.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. KMPrint("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.km_init(hex: "#F7F8FA").cgColor
  47. self.batchPrecessingView.delegate = self
  48. self.batchPrecessingView.inputType = self.inputType
  49. self.chooseView.delegate = self
  50. self.titleLabel.font = NSFont.SFProTextSemiboldFont(16.0)
  51. self.titleLabel.textColor = NSColor.km_init(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. KMPrint("存在相同文件")
  69. if let controller: KMImageToPDFWindowController = self.fetchSampleController() {
  70. controller.inputType = .ImageToPDF
  71. controller.showWindow(window)
  72. }
  73. } else {
  74. KMBatchProcessingView.openfiles(window: window) { openPanel in
  75. openPanel.canChooseDirectories = false
  76. openPanel.canChooseFiles = true
  77. openPanel.allowsMultipleSelection = true
  78. openPanel.allowedFileTypes = KMBatchProcessingTableViewModel.supportedImageTypes()
  79. openPanel.message = NSLocalizedString("Press ⌘/⇧ to select multiple files", comment: "")
  80. } completion: { (panel ,data) in
  81. if data.count != 0 {
  82. let imageToPDFWindow: KMImageToPDFWindowController = KMImageToPDFWindowController.init(windowNibName: "KMImageToPDFWindowController")
  83. // imageToPDFWindow.showWindow(window)
  84. // imageToPDFWindow.window?.orderFront(window)
  85. let point = CGPoint(x: window.frame.origin.x + window.frame.width / 2 - (imageToPDFWindow.window?.frame.size.width)! / 2,
  86. y: window.frame.origin.y + window.frame.height / 2 - (imageToPDFWindow.window?.frame.size.height)! / 2)
  87. window.addChildWindow(imageToPDFWindow.window!, ordered: NSWindow.OrderingMode.above)
  88. imageToPDFWindow.window?.center()
  89. // DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  90. // imageToPDFWindow.window?.setFrameOrigin(point)
  91. // imageToPDFWindow.window?.makeKeyAndOrderFront(nil)
  92. //// imageToPDFWindow.showWindow(window)
  93. // }
  94. // NSRect windowRect = imageToPDFWindow.window.frame
  95. // NSRect dialogRect = [[activeNodeDialog window] frame];
  96. // NSPoint pos;
  97. // pos.x = windowRect.origin.x + windowRect.size.width - dialogRect.size.width - 10;
  98. // pos.y = windowRect.origin.y + 32;
  99. // [[activeNodeDialog window] setFrameOrigin:pos];
  100. // [[activeNodeDialog window] makeKeyAndOrderFront:nil];
  101. imageToPDFWindow.batchPrecessingView.inputData = data
  102. imageToPDFWindow.inputType = .ImageToPDF
  103. imageToPDFController = imageToPDFWindow
  104. }
  105. }
  106. }
  107. }
  108. static func isSampleController() -> Bool {
  109. for window in NSApp.windows {
  110. let controller = window.windowController
  111. if controller is KMImageToPDFWindowController {
  112. return true
  113. }
  114. }
  115. return false
  116. }
  117. static func fetchSampleController() -> KMImageToPDFWindowController? {
  118. for window in NSApp.windows {
  119. let controller = window.windowController
  120. if controller is KMImageToPDFWindowController {
  121. return controller as! KMImageToPDFWindowController
  122. }
  123. }
  124. return nil
  125. }
  126. @objc func windowClose(notification: NSNotification) {
  127. let window: NSWindow = notification.object as? NSWindow ?? NSWindow()
  128. if window == self.window {
  129. NotificationCenter.default.removeObserver(self)
  130. self.batchPrecessingView.delegate = nil
  131. self.chooseView.delegate = nil
  132. imageToPDFController = nil
  133. }
  134. }
  135. }
  136. protocol KMImageToPDFWindowControllerAction {}
  137. extension KMImageToPDFWindowController: KMImageToPDFWindowControllerAction {
  138. @IBAction func closeButtonAction(_ sender: NSButton) {
  139. self.window?.close()
  140. }
  141. @IBAction func shrinkButtonAction(_ sender: NSButton) {
  142. }
  143. @IBAction func enlargeButtonAction(_ sender: NSButton) {
  144. }
  145. }
  146. extension KMImageToPDFWindowController: KMImageToPDFChooseViewDelegate {
  147. func exportAction(data: KMImageToPDFChooseModel) {
  148. KMPrint("导出")
  149. self.chooseData = data
  150. if self.batchData != nil {
  151. self.chooseData?.imageFilePaths = self.batchData
  152. KMImageToPDFManager.manager.exportPDF(model: self.chooseData!) { success, savePath, errors, OCRerrors in
  153. KMPrint(success)
  154. if success {
  155. NSWorkspace.shared.selectFile(savePath, inFileViewerRootedAtPath: "");
  156. } else {
  157. let alert = NSAlert()
  158. alert.alertStyle = .critical
  159. alert.messageText = NSLocalizedString("导出失败", comment: "")
  160. alert.runModal()
  161. return
  162. }
  163. } progress: { [unowned self]status in
  164. self.batchPrecessingView.reloadData()
  165. }
  166. }
  167. }
  168. }
  169. extension KMImageToPDFWindowController: KMBatchProcessingViewDelegate {
  170. func reloadData(data: [KMBatchProcessingTableViewModel]) {
  171. self.batchData = data
  172. self.chooseView.data.imageFilePaths = data
  173. self.chooseView.reloadData()
  174. }
  175. }