KMCompareView.swift 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. //
  2. // KMCompareView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/11/14.
  6. //
  7. import Cocoa
  8. typealias KMCompareViewCancelAction = (_ view: KMCompareView) -> Void
  9. typealias KMCompareViewDoneAction = (_ view: KMCompareView, _ config: KMCompareFilesConfig) -> Void
  10. enum KMCompareFilesType {
  11. case content
  12. case coverting
  13. }
  14. let CPDFOldFileQKSelectedPathsKey = "CPDFOldFileQKSelectedPathsKey"
  15. let CPDFNewFileQKSelectedPathsKey = "CPDFNewFileQKSelectedPathsKey"
  16. let CPDFMaxQKSelectedPathsCount = 5
  17. class KMCompareView: KMBaseXibView {
  18. @IBOutlet weak var cancelButton: NSButton!
  19. @IBOutlet weak var doneButton: NSButton!
  20. @IBOutlet weak var compareTypeSegment: NSSegmentedControl!
  21. @IBOutlet weak var typeSegWidthConst: NSLayoutConstraint!
  22. @IBOutlet weak var comparePreviewBox: NSBox!
  23. @IBOutlet weak var oldDocumentsTitle: NSTextField!
  24. @IBOutlet weak var compareOldPreviewView: KMCompareFilesView!
  25. @IBOutlet weak var oldFileQKSelectedBox: NSComboBox!
  26. @IBOutlet weak var oldFileSelectBtn: NSButton!
  27. @IBOutlet weak var oldPDFView: PDFView!
  28. @IBOutlet weak var currentOldPageLabel: NSTextField!
  29. @IBOutlet weak var totalPaOldgeLabel: NSTextField!
  30. @IBOutlet weak var compareNewPreviewView: KMCompareFilesView!
  31. @IBOutlet weak var documentsNewTitle: NSTextField!
  32. @IBOutlet weak var fileQKNewSelectedBox: NSComboBox!
  33. @IBOutlet weak var fileSelectNewBtn: NSButton!
  34. @IBOutlet weak var pdfNewView: PDFView!
  35. @IBOutlet weak var addFileContentView: KMCompareFilesView!
  36. @IBOutlet weak var addFileImageView: NSImageView!
  37. @IBOutlet weak var addFileAddImageFramView: KMCompareFilesView!
  38. @IBOutlet weak var currentNewPageLabel: NSTextField!
  39. @IBOutlet weak var totalPaNewgeLabel: NSTextField!
  40. @IBOutlet weak var addFileTitle: NSTextField!
  41. @IBOutlet weak var addFileMag: NSTextField!
  42. @IBOutlet weak var pageRangeField: NSTextField!
  43. @IBOutlet weak var oldFileRangeField: NSTextField!
  44. @IBOutlet weak var pageRangeOldComboBox: NSComboBox!
  45. @IBOutlet weak var fileNewRangeField: NSTextField!
  46. @IBOutlet weak var pageRangeNewComboBox: NSComboBox!
  47. @IBOutlet weak var compareTextTypeBtn: NSButton!
  48. @IBOutlet weak var compareImageTypeBtn: NSButton!
  49. @IBOutlet weak var compareSettingsBtn: NSButton!
  50. @IBOutlet weak var settingBtnTopLayout: NSLayoutConstraint!
  51. var contentSettingWindowController: KMCompareContentSettingWindowController?
  52. var coveringSettingWindowController: KMCompareCoveringSettingWindowController?
  53. var pdfCompareContent: CPDFCompareContent?
  54. var password: String = "" {
  55. didSet {
  56. self.oldPDFView.document?.unlock(withPassword: password)
  57. }
  58. }
  59. var filePath: String? {
  60. didSet {
  61. if filePath?.count != 0 {
  62. self.addFilePath(filePath: filePath!, password: password)
  63. }
  64. }
  65. }
  66. private var pdfOldDocument: PDFDocument?
  67. var oldFileQKSelectedPaths: [String] = []
  68. var oldFilePaths: [String] = []
  69. private var pdfNewDocument: PDFDocument?
  70. var fileQKNewSelectedPaths: [String] = []
  71. var newFilePaths: [String] = []
  72. var fileType: KMCompareFilesType = .content {
  73. didSet {
  74. self.updateFileCompareType(fileType: fileType)
  75. }
  76. }
  77. var cancelAction: KMCompareViewCancelAction?
  78. var doneAction: KMCompareViewDoneAction?
  79. convenience init(pdfDocument: PDFDocument) {
  80. self.init()
  81. self.pdfOldDocument = pdfDocument
  82. let document: PDFDocument = PDFDocument.init(url: pdfDocument.documentURL!)!
  83. let cDocument: CPDFDocument = CPDFDocument.init(url: pdfDocument.documentURL!)
  84. let file: KMFileAttribute = KMFileAttribute()
  85. file.pdfDocument = cDocument
  86. let config: KMCompareFilesConfig = KMCompareFilesConfig.init()
  87. config.fileOldAttribute = file
  88. }
  89. convenience init(filePath: String, password: String) {
  90. self.init()
  91. self.pdfOldDocument = PDFDocument.init(url: NSURL(fileURLWithPath: filePath) as URL)
  92. let pdfDocument = PDFDocument.init(url: NSURL(fileURLWithPath: filePath) as URL)
  93. let cDocument = CPDFDocument.init(url: NSURL(fileURLWithPath: filePath) as URL)
  94. if pdfDocument!.isLocked {
  95. pdfDocument!.unlock(withPassword: password)
  96. }
  97. if self.pdfOldDocument!.isLocked {
  98. self.pdfOldDocument!.unlock(withPassword: password)
  99. }
  100. let file: KMFileAttribute = KMFileAttribute()
  101. file.pdfDocument = cDocument
  102. let config = KMCompareFilesConfig.defaultConfig
  103. config.fileOldAttribute = file
  104. config.fileOldAttribute.password = password
  105. }
  106. override func addNotification() {
  107. NotificationCenter.default.addObserver(self, selector: #selector(pageChangeNotification), name: NSNotification.Name.PDFViewPageChanged, object: nil)
  108. }
  109. override func setup() {
  110. fileQKNewSelectedBox.delegate = self
  111. oldFileQKSelectedBox.delegate = self
  112. compareTypeSegment.action = #selector(segmentedControlClicked)
  113. compareTypeSegment.target = self
  114. compareTypeSegment.wantsLayer = true
  115. addFileContentView.wantsLayer = true
  116. addFileImageView.ignoresMultiClick = true
  117. fileQKNewSelectedBox.isEnabled = false
  118. oldFileQKSelectedBox.isEnabled = false
  119. fileQKNewSelectedBox.cell!.isEnabled = false
  120. oldFileQKSelectedBox.cell!.isEnabled = false
  121. currentOldPageLabel.delegate = self
  122. currentNewPageLabel.delegate = self
  123. oldPDFView.wantsLayer = true
  124. pdfNewView.wantsLayer = true
  125. // vsMaskView.wantsLayer = true;
  126. oldPDFView.layer?.backgroundColor = NSColor.clear.cgColor
  127. pdfNewView.layer?.backgroundColor = NSColor.clear.cgColor
  128. // vsMaskView.layer.backgroundColor = NSColor.clear.CGColor
  129. compareTypeSegment.wantsLayer = true
  130. compareTypeSegment.layer?.cornerRadius = 5.0
  131. compareTypeSegment.layer?.masksToBounds = true
  132. compareTypeSegment.layer?.backgroundColor = NSColor.clear.cgColor
  133. addFileAddImageFramView.wantsLayer = true
  134. addFileAddImageFramView.layer?.backgroundColor = NSColor.clear.cgColor
  135. self.compareOldPreviewView.dragSuccessBlock = { [unowned self] filePath in
  136. self.updateDocument(filePath: filePath) { fileAttitude in
  137. }
  138. }
  139. self.compareNewPreviewView.dragSuccessBlock = { [unowned self] filePath in
  140. self.updateDocument(filePath: filePath, isNew: true) { fileAttitude in
  141. }
  142. }
  143. self.addFileContentView.dragSuccessBlock = { [unowned self] filePath in
  144. self.updateDocument(filePath: filePath, isNew: true) { fileAttitude in
  145. }
  146. }
  147. self.addFileAddImageFramView.dragSuccessBlock = { [unowned self] filePath in
  148. self.updateDocument(filePath: filePath, isNew: true) { fileAttitude in
  149. }
  150. }
  151. self.addFileContentView.mouseUpBack = { [unowned self] view in
  152. self.chooseFileAction(isNew: true)
  153. }
  154. KMCompareFilesConfig.defaultConfig.fileNewAttribute.pdfDocument = nil
  155. }
  156. override func updateLanguage() {
  157. compareTypeSegment.setLabel(NSLocalizedString("Side-by-Side View", comment: ""), forSegment: 0)
  158. compareTypeSegment.setLabel(NSLocalizedString("Compare by File Overlay", comment: ""), forSegment: 1)
  159. compareTypeSegment.setToolTip(NSLocalizedString("Side-by-Side View", comment: ""), forSegment: 0)
  160. compareTypeSegment.setToolTip(NSLocalizedString("Compare by File Overlay", comment: ""), forSegment: 1)
  161. oldDocumentsTitle.stringValue = NSLocalizedString("Old File", comment: "")
  162. documentsNewTitle.stringValue = NSLocalizedString("New File", comment: "")
  163. oldFileSelectBtn.title = NSLocalizedString("Choose...", comment: "")
  164. fileSelectNewBtn.title = NSLocalizedString("Choose...", comment: "")
  165. oldFileSelectBtn.toolTip = NSLocalizedString("Choose...", comment: "")
  166. fileSelectNewBtn.toolTip = NSLocalizedString("Choose...", comment: "")
  167. addFileTitle.stringValue = NSLocalizedString("Select File", comment: "")
  168. addFileMag.stringValue = NSLocalizedString("Click to add", comment: "")
  169. oldFileRangeField.stringValue = NSLocalizedString("Old File:", comment: "")
  170. fileNewRangeField.stringValue = NSLocalizedString("New File:", comment: "")
  171. pageRangeField.stringValue = NSLocalizedString("Page Range", comment: "")
  172. pageRangeOldComboBox.removeAllItems()
  173. pageRangeOldComboBox.addItems(withObjectValues: [NSLocalizedString("All Pages", comment: ""),
  174. NSLocalizedString("Odd Pages Only", comment: ""),
  175. NSLocalizedString("Even Pages Only",comment: ""),
  176. NSLocalizedString("e.g. 1,3-5,10",comment: "")])
  177. pageRangeOldComboBox.placeholderString = NSLocalizedString("e.g. 1,3-5,10", comment: "")
  178. pageRangeNewComboBox.addItems(withObjectValues: [NSLocalizedString("All Pages", comment: ""),
  179. NSLocalizedString("Odd Pages Only", comment: ""),
  180. NSLocalizedString("Even Pages Only",comment: ""),
  181. NSLocalizedString("e.g. 1,3-5,10",comment: "")])
  182. self.pageRangeNewComboBox.placeholderString = NSLocalizedString("e.g. 1,3-5,10", comment: "")
  183. self.compareTextTypeBtn.title = NSLocalizedString("Compare text", comment: "")
  184. self.compareImageTypeBtn.title = NSLocalizedString("Compare image", comment: "")
  185. let settingsString = " " + NSLocalizedString("Settings", comment: "")
  186. compareSettingsBtn.title = settingsString
  187. compareSettingsBtn.state = .off
  188. compareSettingsBtn.setTitleColor(KMAppearance.Layout.h1Color())
  189. cancelButton.title = NSLocalizedString("Cancel", comment: "")
  190. doneButton.title = NSLocalizedString("Compare", comment: "")
  191. }
  192. override func updateUI() {
  193. if KMAppearance.isDarkMode() {
  194. self.compareTypeSegment.layer?.backgroundColor = NSColor(red: 50.0/255.0, green: 53.0/255.0, blue: 54.0/255.0, alpha: 1).cgColor
  195. self.addFileContentView.layer?.backgroundColor = NSColor.black.cgColor
  196. } else {
  197. self.compareTypeSegment.layer?.backgroundColor = NSColor(red: 247.0/255.0, green: 247.0/255.0, blue: 247.0/255.0, alpha: 1).cgColor
  198. self.addFileContentView.layer?.backgroundColor = NSColor.white.cgColor
  199. }
  200. }
  201. override func reloadData() {
  202. if(pageRangeOldComboBox.indexOfSelectedItem == 3) {
  203. self.window?.makeFirstResponder(pageRangeOldComboBox)
  204. pageRangeOldComboBox.stringValue = ""
  205. pageRangeOldComboBox.isSelectable = true
  206. }
  207. self.updatePageRangeData(view: pageRangeNewComboBox, file: KMCompareFilesConfig.defaultConfig.fileNewAttribute, isNew: true)
  208. self.updatePageRangeData(view: pageRangeOldComboBox, file: KMCompareFilesConfig.defaultConfig.fileOldAttribute)
  209. if (KMCompareFilesConfig.defaultConfig.isCompareText)() {
  210. self.compareTextTypeBtn.state = .on;
  211. } else {
  212. self.compareTextTypeBtn.state = .off;
  213. }
  214. if (KMCompareFilesConfig.defaultConfig.isCompareImage)() {
  215. self.compareImageTypeBtn.state = .on;
  216. } else {
  217. self.compareImageTypeBtn.state = .off;
  218. }
  219. self.updateSelectBoxData()
  220. self.updatePageState()
  221. self.updatePageState(isNew: true)
  222. }
  223. @objc func pageChangeNotification(notification: Notification) {
  224. self.updatePageState()
  225. self.updatePageState(isNew: true)
  226. }
  227. }
  228. extension KMCompareView {
  229. func addFilePath(filePath: String, isNew: Bool = false, password: String = "") {
  230. DispatchQueue.main.async {
  231. let document: PDFDocument = PDFDocument.init(url: NSURL(fileURLWithPath: filePath) as URL) ?? PDFDocument()
  232. let cDocument: CPDFDocument = CPDFDocument.init(url: NSURL(fileURLWithPath: filePath) as URL)!
  233. let file: KMFileAttribute = KMFileAttribute()
  234. // file.pdfDocument = cDocument
  235. // file.myPDFDocument = document
  236. file.password = password
  237. file.filePath = filePath
  238. document.unlock(withPassword: password)
  239. cDocument.unlock(withPassword: password)
  240. if isNew {
  241. KMCompareFilesConfig.defaultConfig.fileNewAttribute = file
  242. self.pdfNewDocument = document
  243. self.pdfNewView.document = document
  244. self.pdfNewView.autoScales = true
  245. self.pdfNewView.delegate = self
  246. } else {
  247. KMCompareFilesConfig.defaultConfig.fileOldAttribute = file
  248. self.pdfOldDocument = document
  249. self.oldPDFView.document = document
  250. self.oldPDFView.autoScales = true
  251. self.oldPDFView.delegate = self
  252. }
  253. self.addFileQKSelectPath(filePath: filePath, isNew: isNew)
  254. self.reloadData()
  255. }
  256. }
  257. func addFileQKSelectPath(filePath: String, isNew: Bool = false) {
  258. let pdfdocument = PDFDocument(url: NSURL(fileURLWithPath: filePath) as URL)
  259. guard let pdfdocument = pdfdocument else { return }
  260. var key = ""
  261. if isNew {
  262. key = CPDFNewFileQKSelectedPathsKey
  263. } else {
  264. key = CPDFOldFileQKSelectedPathsKey
  265. }
  266. //取出
  267. var filePaths: [String] = UserDefaults.standard.object(forKey: key) as? [String] ?? []
  268. if filePaths.count > CPDFMaxQKSelectedPathsCount {
  269. filePaths.removeLast()
  270. }
  271. if !filePaths.contains(filePath) {
  272. filePaths.insert(filePath, at: 0)
  273. }
  274. //存储
  275. UserDefaults.standard.setValue(filePaths, forKey: key)
  276. UserDefaults.standard.synchronize()
  277. let fileName = filePath.lastPathComponent
  278. self.updateSelectBoxData()
  279. if isNew {
  280. self.fileQKNewSelectedBox.selectItem(withObjectValue: fileName)
  281. self.fileQKNewSelectedBox.toolTip = fileName
  282. } else {
  283. self.oldFileQKSelectedBox.selectItem(withObjectValue: fileName)
  284. self.oldFileQKSelectedBox.toolTip = fileName
  285. }
  286. }
  287. func updateSelectBoxData() {
  288. self.oldFileQKSelectedPaths = self.updateSelectBoxItemData()
  289. self.oldFileQKSelectedBox.removeAllItems()
  290. self.oldFileQKSelectedBox.addItems(withObjectValues: self.oldFileQKSelectedPaths)
  291. self.fileQKNewSelectedPaths = self.updateSelectBoxItemData(isNew: true)
  292. self.fileQKNewSelectedBox.removeAllItems()
  293. self.fileQKNewSelectedBox.addItems(withObjectValues: self.fileQKNewSelectedPaths)
  294. if self.fileQKNewSelectedPaths.count > 0 {
  295. self.fileQKNewSelectedBox.isEnabled = true
  296. } else {
  297. self.fileQKNewSelectedBox.isEnabled = false
  298. }
  299. if self.oldFileQKSelectedPaths.count > 0 {
  300. self.oldFileQKSelectedBox.isEnabled = true
  301. } else {
  302. self.oldFileQKSelectedBox.isEnabled = false
  303. }
  304. self.oldFileQKSelectedBox.isEditable = false
  305. self.fileQKNewSelectedBox.isEditable = false
  306. }
  307. func updateSelectBoxItemData(isNew: Bool = false) -> [String] {
  308. let defaults = UserDefaults.standard
  309. var fileSelectedCachePaths: [String] = []
  310. if isNew {
  311. fileSelectedCachePaths = defaults.value(forKey: CPDFNewFileQKSelectedPathsKey) as? [String] ?? []
  312. self.newFilePaths = fileSelectedCachePaths
  313. } else {
  314. fileSelectedCachePaths = defaults.value(forKey: CPDFOldFileQKSelectedPathsKey) as? [String] ?? []
  315. self.oldFilePaths = fileSelectedCachePaths
  316. }
  317. var fileSelectedPaths: [String] = []
  318. var filePaths: [String] = []
  319. let fileManager = FileManager.default
  320. for path in fileSelectedCachePaths {
  321. if fileManager.fileExists(atPath: path) {
  322. // #if VERSION_DMG
  323. fileSelectedPaths.append(path.lastPathComponent)
  324. filePaths.append(path)
  325. // #else
  326. // let url = URL(fileURLWithPath: path)
  327. //
  328. // if let bookmarkData = try? url.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil) {
  329. // AppSandboxFileAccess.fileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, forURL: url)
  330. // AppSandboxFileAccess.fileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, forURL: URL(fileURLWithPath: url.path ?? url.absoluteString))
  331. //
  332. // fileOldSelectedPaths.append(path)
  333. // } else {
  334. // AppSandboxFileAccess.fileAccess().accessFileURL(url, persistPermission: yearMask) {
  335. // if let bookmarkData = try? url.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil) {
  336. // fileOldSelectedPaths.append(path)
  337. // }
  338. // }
  339. // }
  340. // #endif
  341. }
  342. }
  343. if isNew {
  344. self.newFilePaths = filePaths
  345. } else {
  346. self.oldFilePaths = filePaths
  347. }
  348. return fileSelectedPaths
  349. }
  350. func updatePageState(isNew: Bool = false) {
  351. var pageDocument = PDFDocument()
  352. var pdfView = PDFView()
  353. var currentPageLabel = NSTextField()
  354. var totalPageLabel = NSTextField()
  355. if isNew {
  356. addFileContentView.isHidden = false
  357. guard let document = KMCompareFilesConfig.defaultConfig.fileNewAttribute.pdfDocument else { return }
  358. pdfView = pdfNewView
  359. pageDocument = pdfView.document ?? PDFDocument()
  360. currentPageLabel = currentNewPageLabel
  361. totalPageLabel = totalPaNewgeLabel
  362. addFileContentView.isHidden = true
  363. } else {
  364. guard let document = KMCompareFilesConfig.defaultConfig.fileOldAttribute.pdfDocument else { return }
  365. pdfView = oldPDFView
  366. pageDocument = pdfView.document ?? PDFDocument()
  367. currentPageLabel = currentOldPageLabel
  368. totalPageLabel = totalPaOldgeLabel
  369. }
  370. // 隐藏PDFView滑动条
  371. pdfView.documentView?.enclosingScrollView?.hasVerticalScroller = false
  372. pdfView.documentView?.enclosingScrollView?.hasHorizontalScroller = false
  373. let pageCount = pageDocument.pageCount
  374. var currentPageIndex = 0
  375. if(pdfView.currentPage != nil) {
  376. currentPageIndex = Int(pageDocument.index(for: pdfView.currentPage!))
  377. }
  378. if(pageCount > 0) {
  379. currentPageLabel.stringValue = "\(currentPageIndex + 1)"
  380. } else {
  381. currentPageLabel.stringValue = "\(currentPageIndex)"
  382. }
  383. totalPageLabel.stringValue = "/ \(pageCount)"
  384. }
  385. func updateDocument(filePath: String, isNew: Bool = false, completion: @escaping (_ fileAttitude: KMFileAttribute?) -> Void) {
  386. let isAccessFilePath = AppSandboxFileAccess().accessFileURL(NSURL(fileURLWithPath: filePath) as URL, persistPermission: true) {
  387. }
  388. if isAccessFilePath {
  389. print("有权限")
  390. var pdfDocument = CPDFDocument()
  391. if isNew {
  392. pdfDocument = KMCompareFilesConfig.defaultConfig.fileNewAttribute.pdfDocument
  393. } else {
  394. pdfDocument = KMCompareFilesConfig.defaultConfig.fileOldAttribute.pdfDocument
  395. }
  396. // guard let pdfDocument = pdfDocument else {
  397. // completion(nil)
  398. // return
  399. // }
  400. if (pdfDocument?.documentURL.path == filePath) {
  401. let alert = NSAlert()
  402. alert.alertStyle = NSAlert.Style.critical
  403. alert.messageText = NSLocalizedString("There is no difference between the two documents.", comment: "")
  404. alert.runModal()
  405. completion(nil)
  406. return
  407. } else {
  408. KMBaseWindowController.checkPassword(url: NSURL(fileURLWithPath: filePath) as URL, type: .owner) { success, resultPassword in
  409. if success {
  410. let file = KMFileAttribute()
  411. file.pdfDocument = pdfDocument
  412. file.password = resultPassword
  413. completion(file)
  414. } else {
  415. completion(nil)
  416. }
  417. }
  418. }
  419. } else {
  420. print("无权限")
  421. }
  422. }
  423. func updatePageRangeData(view: NSComboBox, file: KMFileAttribute, isNew: Bool = false) {
  424. switch (file.pagesType) {
  425. case .all:
  426. view.selectItem(at: 0)
  427. view.isEditable = false
  428. view.delegate = nil
  429. break;
  430. case .odd:
  431. view.selectItem(at: 1)
  432. view.isEditable = false
  433. view.delegate = nil
  434. break;
  435. case .even:
  436. view.selectItem(at: 2)
  437. view.isEditable = false
  438. view.delegate = nil
  439. break;
  440. case .custom:
  441. view.delegate = self
  442. view.stringValue = file.pagesString
  443. view.isEditable = true
  444. break;
  445. default: break
  446. }
  447. }
  448. func updateFileCompareType(fileType: KMCompareFilesType) {
  449. if fileType == .content {
  450. compareTypeSegment.selectedSegment = 0
  451. compareTextTypeBtn.isHidden = false
  452. compareImageTypeBtn.isHidden = false
  453. settingBtnTopLayout.constant = 81
  454. } else {
  455. compareTypeSegment.selectedSegment = 1
  456. compareTextTypeBtn.isHidden = true
  457. compareImageTypeBtn.isHidden = true
  458. settingBtnTopLayout.constant = 20
  459. }
  460. }
  461. func updatePageRange(view: NSComboBox, isNew: Bool = false) {
  462. let config = KMCompareFilesConfig.defaultConfig
  463. var index = pageRangeOldComboBox.indexOfSelectedItem
  464. var file = config.fileOldAttribute
  465. if isNew {
  466. index = pageRangeNewComboBox.indexOfSelectedItem
  467. file = config.fileNewAttribute
  468. }
  469. switch index {
  470. case 0:
  471. file.bAllPage = true
  472. file.pagesType = .all
  473. view.delegate = nil
  474. view.isEditable = false
  475. case 1:
  476. file.bAllPage = false
  477. file.pagesType = .odd
  478. view.delegate = nil
  479. view.isEditable = false
  480. case 2:
  481. file.bAllPage = false
  482. file.pagesType = .even
  483. view.delegate = nil
  484. view.isEditable = false
  485. case 3:
  486. file.bAllPage = false
  487. file.pagesType = .custom
  488. view.delegate = self
  489. view.stringValue = ""
  490. view.isEditable = true
  491. view.isSelectable = true
  492. self.window?.makeFirstResponder(view)
  493. default:
  494. break
  495. }
  496. }
  497. }
  498. extension KMCompareView {
  499. @IBAction func cancelButtonAction(_ sender: Any) {
  500. guard let callBack = cancelAction else { return }
  501. callBack(self)
  502. }
  503. @IBAction func convertButtonAction(_ sender: Any) {
  504. guard let callBack = doneAction else { return }
  505. if KMCompareFilesConfig.defaultConfig.fileOldAttribute.pagesType == .custom {
  506. KMCompareFilesConfig.defaultConfig.fileOldAttribute.pagesString = self.pageRangeOldComboBox.stringValue
  507. }
  508. if KMCompareFilesConfig.defaultConfig.fileNewAttribute.pagesType == .custom {
  509. KMCompareFilesConfig.defaultConfig.fileNewAttribute.pagesString = self.pageRangeNewComboBox.stringValue
  510. }
  511. callBack(self, KMCompareFilesConfig.defaultConfig)
  512. }
  513. @IBAction func oldFilesSelectBoxAction(_ sender: Any) {
  514. let selectIndex = self.oldFileQKSelectedBox.indexOfSelectedItem
  515. if selectIndex >= 0 {
  516. let selectItem = self.oldFileQKSelectedBox.itemObjectValue(at: selectIndex)
  517. let filePath = self.oldFilePaths[selectIndex]
  518. self.updateDocument(filePath: filePath) { fileAttitude in
  519. if fileAttitude != nil {
  520. self.addFilePath(filePath: filePath , isNew: false, password: fileAttitude?.password ?? "")
  521. } else {
  522. self.oldFileQKSelectedBox.selectItem(withObjectValue: self.oldPDFView.document?.documentURL?.path.lastPathComponent)
  523. }
  524. }
  525. }
  526. }
  527. @IBAction func newFilesSelectBoxAction(_ sender: Any) {
  528. let selectIndex = self.fileQKNewSelectedBox.indexOfSelectedItem
  529. if selectIndex >= 0 {
  530. let selectItem = self.fileQKNewSelectedBox.itemObjectValue(at: selectIndex)
  531. let filePath = self.newFilePaths[selectIndex]
  532. self.updateDocument(filePath: filePath) { fileAttitude in
  533. if fileAttitude != nil {
  534. self.addFilePath(filePath: filePath , isNew: true, password: fileAttitude?.password ?? "")
  535. } else {
  536. self.fileQKNewSelectedBox.selectItem(withObjectValue: self.pdfNewView.document?.documentURL?.path.lastPathComponent)
  537. }
  538. }
  539. }
  540. }
  541. @IBAction func oldChooseButtonAction(_ sender: Any) {
  542. self.chooseFileAction()
  543. }
  544. @IBAction func newChooseButtonAction(_ sender: Any) {
  545. self.chooseFileAction(isNew: true)
  546. }
  547. @IBAction func oldNextButtonAction(_ sender: Any) {
  548. self.oldPDFView.goToNextPage(nil)
  549. self.updatePageState()
  550. }
  551. @IBAction func oldPreviousButtonAction(_ sender: Any) {
  552. self.oldPDFView.goToPreviousPage(nil)
  553. self.updatePageState()
  554. }
  555. @IBAction func newNextButtonAction(_ sender: Any) {
  556. self.pdfNewView.goToNextPage(nil)
  557. self.updatePageState(isNew: true)
  558. }
  559. @IBAction func newPreviousButtonAction(_ sender: Any) {
  560. self.pdfNewView.goToPreviousPage(nil)
  561. self.updatePageState(isNew: true)
  562. }
  563. @IBAction func oldPageRangeBoxAction(_ sender: Any) {
  564. self.updatePageRange(view: self.pageRangeOldComboBox)
  565. }
  566. @IBAction func newPageRangeBoxAction(_ sender: Any) {
  567. self.updatePageRange(view: self.pageRangeNewComboBox, isNew: true)
  568. }
  569. @IBAction func compareTextButtonAction(_ sender: NSButton) {
  570. let isCompareText = sender.state == .on ?true:false
  571. KMCompareFilesConfig.defaultConfig.setIsCompareText(isCompareText)
  572. }
  573. @IBAction func compareImageButtonAction(_ sender: NSButton) {
  574. let isCompareImage = sender.state == .on ?true:false
  575. KMCompareFilesConfig.defaultConfig.setIsCompareImage(isCompareImage)
  576. }
  577. @IBAction func settingButtonAction(_ sender: Any) {
  578. if fileType == .content {
  579. if contentSettingWindowController == nil {
  580. let controller = KMCompareContentSettingWindowController(windowNibName: "KMCompareContentSettingWindowController")
  581. contentSettingWindowController = controller
  582. }
  583. NSWindow.currentWindow().addChildWindow(contentSettingWindowController!.window!, ordered: .above)
  584. contentSettingWindowController!.window?.center()
  585. } else {
  586. if coveringSettingWindowController == nil {
  587. let controller = KMCompareCoveringSettingWindowController(windowNibName: "KMCompareCoveringSettingWindowController")
  588. coveringSettingWindowController = controller
  589. }
  590. NSWindow.currentWindow().addChildWindow(coveringSettingWindowController!.window!, ordered: .above)
  591. coveringSettingWindowController!.window?.center()
  592. }
  593. }
  594. @objc func segmentedControlClicked(sender: NSSegmentedControl) {
  595. let selectedType = self.compareTypeSegment.selectedSegment
  596. var type: KMCompareFilesType = .content
  597. if (selectedType == 0) {
  598. type = .content;
  599. } else {
  600. type = .coverting;
  601. }
  602. self.fileType = type
  603. }
  604. func chooseFileAction(isNew: Bool = false) {
  605. // fileQKNewSelectedBox.resignFirstResponder()
  606. // self.window?.makeFirstResponder(nil)
  607. let openPanel = NSOpenPanel()
  608. openPanel.allowedFileTypes = ["pdf"]
  609. openPanel.allowsMultipleSelection = false
  610. openPanel.beginSheetModal(for: self.window!) { (result) in
  611. if result == NSApplication.ModalResponse.OK {
  612. #if VERSION_DMG
  613. // Add code for VERSION_DMG
  614. #else
  615. // if let url = openPanel.url {
  616. // do {
  617. // let bookmarkData = try url.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil)
  618. // AppSandboxFileAccess.fileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, forURL: url)
  619. // AppSandboxFileAccess.fileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, forURL: URL(fileURLWithPath: url.path ?? url.absoluteString))
  620. // } catch {
  621. // // Handle error
  622. // print("Error creating bookmark data: \(error)")
  623. // }
  624. // }
  625. #endif
  626. if let filePath = openPanel.url?.path {
  627. let url = NSURL(fileURLWithPath: filePath)
  628. AppSandboxFileAccess().persistPermissionURL(url as URL)
  629. let bookmarkData = try?url.bookmarkData(options: NSURL.BookmarkCreationOptions.withSecurityScope, includingResourceValuesForKeys: nil, relativeTo: nil)
  630. if bookmarkData != nil {
  631. AppSandboxFileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData! as Data, for: url as URL)
  632. AppSandboxFileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData! as Data, for: NSURL(fileURLWithPath: url.path!) as URL)
  633. }
  634. // let isAccessFilePath = AppSandboxFileAccess().accessFileURL(url as URL, persistPermission: true) {
  635. //
  636. // }
  637. //
  638. // if (isAccessFilePath) {
  639. // print("获取权限成功")
  640. // let url = NSURL(fileURLWithPath: filePath)
  641. // let bookmarkData = try?url.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil, relativeTo: nil)
  642. // if bookmarkData != nil {
  643. // AppSandboxFileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, for: url as URL)
  644. // AppSandboxFileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, for: NSURL(fileURLWithPath: url.path!) as URL)
  645. // }
  646. // } else {
  647. // print("获取权限失败")
  648. // }
  649. // NSURL *url = [NSURL fileURLWithPath:folderPath];
  650. // [[AppSandboxFileAccess fileAccess] persistPermissionURL:url];
  651. // NSData *bookmarkData = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope includingResourceValuesForKeys:nil relativeToURL:nil error:NULL];
  652. // if (bookmarkData) {
  653. // [[AppSandboxFileAccess fileAccess].bookmarkPersistanceDelegate setBookmarkData:bookmarkData forURL:url];
  654. // [[AppSandboxFileAccess fileAccess].bookmarkPersistanceDelegate setBookmarkData:bookmarkData forURL:[NSURL fileURLWithPath:(url.path?:url.absoluteString)]];
  655. // }
  656. self.updateDocument(filePath: filePath, isNew: isNew) { [unowned self] file in
  657. if let _ = file {
  658. self.addFilePath(filePath: filePath, isNew: isNew, password: file?.password ?? "")
  659. }
  660. }
  661. }
  662. }
  663. }
  664. }
  665. }
  666. extension KMCompareView: NSComboBoxDelegate {
  667. }
  668. extension KMCompareView: PDFViewDelegate {
  669. }