KMCompareWindowController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // KMCompareWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/11/14.
  6. //
  7. import Cocoa
  8. typealias KMCompareWindowControllerContentComplete = (_ controller: KMCompareWindowController, _ pdfCompareContent: CPDFCompareContent, _ results: [CPDFCompareResults] ,_ oldDocument: CPDFDocument, _ document: CPDFDocument) -> Void
  9. typealias KMCompareWindowControllerCoveringComplete = (_ controller: KMCompareWindowController, _ document: CPDFDocument) -> Void
  10. class KMCompareWindowController: KMBaseWindowController {
  11. @IBOutlet weak var compareView: KMCompareView!
  12. var pdfCompareContent: CPDFCompareContent?
  13. var maskView: KMBookletMaskView?
  14. var progressController: SKProgressController?
  15. var filePath: String = "" {
  16. didSet {
  17. if compareView != nil {
  18. compareView.filePath = filePath
  19. }
  20. }
  21. }
  22. var fileType: KMCompareFilesType = .content {
  23. didSet {
  24. if compareView != nil {
  25. compareView.fileType = fileType
  26. }
  27. }
  28. }
  29. var password: String = "" {
  30. didSet {
  31. if compareView != nil {
  32. self.compareView.password = password
  33. }
  34. }
  35. }
  36. var contentComplete: KMCompareWindowControllerContentComplete?
  37. var coveringComplete :KMCompareWindowControllerCoveringComplete?
  38. override func windowDidLoad() {
  39. super.windowDidLoad()
  40. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  41. compareView.password = password
  42. compareView.filePath = filePath
  43. compareView.fileType = fileType
  44. compareView.cancelAction = { [unowned self] view in
  45. cancelAction?(self)
  46. }
  47. compareView.doneAction = { [unowned self] view, config in
  48. self.compareAction(config: config)
  49. }
  50. }
  51. func compareAction(config: KMCompareFilesConfig) {
  52. compareView.doneButton.isEnabled = false
  53. self.showWaitting()
  54. guard let pdfOldDocument = config.fileOldAttribute.pdfDocument, let pdfNewDocument = config.fileNewAttribute.pdfDocument else {
  55. let alert = NSAlert()
  56. alert.alertStyle = .critical
  57. alert.messageText = NSLocalizedString("Please select two files to compare", comment: "")
  58. alert.runModal()
  59. self.hideWaitting()
  60. return
  61. }
  62. let fileManager = FileManager.default
  63. let fileOldPath = config.fileOldAttribute.pdfDocument?.documentURL.path
  64. let fileNewPath = config.fileNewAttribute.pdfDocument?.documentURL.path
  65. if !fileManager.fileExists(atPath: fileOldPath!) || !fileManager.fileExists(atPath: fileNewPath!){
  66. let alert = NSAlert()
  67. alert.alertStyle = .critical
  68. alert.messageText = NSLocalizedString("The file has been deleted, please reselect a file.", comment: "")
  69. alert.runModal()
  70. self.hideWaitting()
  71. return
  72. }
  73. if (config.fileNewAttribute.fetchSelectPages().count == 0) ||
  74. (!config.fileNewAttribute.bAllPage && config.fileNewAttribute.pagesString.count < 1) {
  75. let alert = NSAlert()
  76. alert.alertStyle = .critical
  77. alert.messageText = String(format: "%@ %@", (config.fileNewAttribute.pdfDocument?.documentURL.path.lastPathComponent.lastPathComponent)!, NSLocalizedString("Invalid page range. Please reselect the page range.", comment: ""))
  78. alert.runModal()
  79. config.fileNewAttribute.bAllPage = true
  80. config.fileNewAttribute.pagesType = .all
  81. compareView.reloadData()
  82. self.hideWaitting()
  83. return
  84. }
  85. if (config.fileOldAttribute.fetchSelectPages().count == 0) || (!config.fileOldAttribute.bAllPage && config.fileOldAttribute.pagesString.count < 1) {
  86. let alert = NSAlert()
  87. alert.alertStyle = .critical
  88. alert.messageText = String(format: "%@ %@", (config.fileOldAttribute.pdfDocument?.documentURL.path.lastPathComponent.lastPathComponent)!, NSLocalizedString("Invalid page range. Please reselect the page range.", comment: ""))
  89. alert.runModal()
  90. config.fileOldAttribute.bAllPage = true
  91. config.fileOldAttribute.pagesType = .all
  92. compareView.reloadData()
  93. self.hideWaitting()
  94. return
  95. }
  96. let filePath = config.fileOldAttribute.pdfDocument?.documentURL.path
  97. let pdfDocument = CPDFDocument(url: URL(fileURLWithPath: filePath!))
  98. let filePath1 = config.fileNewAttribute.pdfDocument?.documentURL.path
  99. let pdfDocument1 = CPDFDocument(url: URL(fileURLWithPath: filePath1!))
  100. DispatchQueue.global().async {
  101. let oldDoc = CPDFDocument(url: config.fileOldAttribute.pdfDocument?.documentURL)
  102. oldDoc!.unlock(withPassword: config.fileOldAttribute.password)
  103. let doc = CPDFDocument(url: config.fileNewAttribute.pdfDocument?.documentURL)
  104. doc!.unlock(withPassword: config.fileNewAttribute.password)
  105. if self.compareView.fileType == .coverting {
  106. let pdfCompareOverlay = CPDFCompareOverlay(oldDocument: oldDoc, oldPageRange: config.fileOldAttribute.pagesString, newDocument: doc, newPageRange: config.fileNewAttribute.pagesString)
  107. let oldStrokeColor = config.oldStrokeColor
  108. pdfCompareOverlay?.setOldDocumentStroke(oldStrokeColor())
  109. pdfCompareOverlay?.setOldDocumentStrokeOpacity(config.oldStrokeOpacity())
  110. let newStrokeColor = config.newStrokeColor
  111. pdfCompareOverlay?.setNewDocumentStroke(newStrokeColor())
  112. pdfCompareOverlay?.setNewDocumentStrokeOpacity(config.newStrokeOpacity())
  113. pdfCompareOverlay?.setNewDocumentFillOpacity(config.newFillOpacity())
  114. pdfCompareOverlay?.setOldDocumentFillOpacity(config.oldFillOpacity())
  115. pdfCompareOverlay?.setNoFill(config.isNOFill())
  116. pdfCompareOverlay?.setBlendMod(config.blendMod())
  117. if (pdfCompareOverlay?.compare() == true) {
  118. DispatchQueue.main.async {
  119. self.progressController?.doubleValue = 30.0
  120. }
  121. guard let document = pdfCompareOverlay?.comparisonDocument() else {
  122. DispatchQueue.main.async {
  123. let alert = NSAlert()
  124. alert.alertStyle = .critical
  125. alert.messageText = NSLocalizedString("Failure", comment: "")
  126. alert.runModal()
  127. self.hideWaitting()
  128. }
  129. return
  130. }
  131. DispatchQueue.main.async {
  132. self.hideWaitting()
  133. self.coveringComplete?(self, document)
  134. }
  135. debugPrint("合并成功")
  136. }
  137. } else {
  138. var results: [CPDFCompareResults] = []
  139. var pdfCompareContent: CPDFCompareContent? = nil
  140. // if let _pdfCompareContent = self._pdfCompareContent {
  141. // pdfCompareContent = _pdfCompareContent
  142. // }
  143. //
  144. // if self._compareCancel {
  145. // return
  146. // }
  147. pdfCompareContent = CPDFCompareContent(oldDocument: oldDoc, newDocument: doc)
  148. pdfCompareContent?.setDelete(config.deleteColor())
  149. pdfCompareContent?.setReplace(config.replaceColor())
  150. pdfCompareContent?.setInsert(config.insertColor())
  151. pdfCompareContent?.setDeleteOpacity(config.deleteOpacity())
  152. pdfCompareContent?.setReplaceOpacity(config.replaceOpacity())
  153. pdfCompareContent?.setInsertOpacity(config.insertOpacity())
  154. let maxIndex = max(config.fileOldAttribute.fetchSelectPages().count, config.fileNewAttribute.fetchSelectPages().count)
  155. var compareType: CPDFCompareType = .all
  156. if config.isCompareText() && !config.isCompareImage() {
  157. compareType = .text
  158. } else if !config.isCompareText() && config.isCompareImage() {
  159. compareType = .image
  160. }
  161. // if self._compareCancel {
  162. // return
  163. // }
  164. DispatchQueue.main.async {
  165. self.progressController?.doubleValue = 30.0
  166. }
  167. for i in 0..<maxIndex {
  168. let oldPageIndex: Int
  169. if i >= config.fileOldAttribute.fetchSelectPages().count {
  170. oldPageIndex = Int(oldDoc!.pageCount) + i
  171. } else {
  172. oldPageIndex = Int(truncating: config.fileOldAttribute.fetchSelectPages()[i] as NSNumber) - 1
  173. }
  174. let newPageIndex: Int
  175. if i >= config.fileNewAttribute.fetchSelectPages().count {
  176. newPageIndex = Int(doc!.pageCount) + i
  177. } else {
  178. newPageIndex = Int(truncating: config.fileNewAttribute.fetchSelectPages()[i] as NSNumber) - 1
  179. }
  180. if let compareResults = pdfCompareContent?.compareOldPageIndex(oldPageIndex, newPageIndex: newPageIndex, type: compareType, isDrawHighlight: true) {
  181. results.append(compareResults)
  182. }
  183. // DispatchQueue.main.async {
  184. // if let compareLoadingWVC = self.compareLoadingWVC {
  185. // compareLoadingWVC.progress = CGFloat(i) / CGFloat(maxIndex)
  186. // }
  187. // }
  188. }
  189. DispatchQueue.main.async {
  190. self.hideWaitting()
  191. self.contentComplete?(self, pdfCompareContent!, results, oldDoc!, doc!)
  192. if results.count > 0 {
  193. // Handle success case
  194. } else {
  195. let alert = NSAlert()
  196. alert.alertStyle = .critical
  197. alert.messageText = NSLocalizedString("There is no difference between the two documents.", comment: "")
  198. alert.runModal()
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }
  205. extension KMCompareWindowController {
  206. func showWaitting() {
  207. if self.maskView == nil {
  208. self.maskView = KMBookletMaskView(frame: CGRect(x: 0, y: 0, width: self.window?.frame.size.width ?? 0, height: self.window?.frame.size.height ?? 0))
  209. }
  210. self.window?.contentView?.addSubview(self.maskView!)
  211. let progress = SKProgressController()
  212. progress.window?.backgroundColor = NSColor.km_init(hex: "#36383B")
  213. progress.window?.contentView?.wantsLayer = true
  214. progress.window?.contentView?.layer?.backgroundColor = NSColor.km_init(hex: "#36383B").cgColor
  215. progress.progressField.textColor = NSColor.white
  216. progress.message = NSLocalizedString("Translating...", comment: "")
  217. progress.closeBlock = { [unowned self] in
  218. }
  219. self.progressController = progress
  220. self.window?.beginSheet(progress.window!)
  221. }
  222. func hideWaitting() {
  223. DispatchQueue.main.async {
  224. self.progressController?.doubleValue = 99.0
  225. }
  226. self.maskView?.removeFromSuperview()
  227. if (self.progressController != nil) {
  228. self.window?.endSheet((self.progressController?.window)!)
  229. self.progressController = nil
  230. }
  231. }
  232. }