KMCompareFilesView.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // KMCompareFilesView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/11/15.
  6. //
  7. import Cocoa
  8. typealias KMCompareFilesViewDragEnteredCallBack = (Bool) -> Void
  9. typealias KMCompareFilesViewDragSuccessCallBack = (String) -> Void
  10. typealias KMCompareFilesViewMouseUpCallBack = (KMCompareFilesView) -> Void
  11. class KMCompareFilesView: NSView {
  12. var dragEnteredBlock: KMCompareFilesViewDragEnteredCallBack?
  13. var dragSuccessBlock: KMCompareFilesViewDragSuccessCallBack?
  14. var mouseUpBack: KMCompareFilesViewMouseUpCallBack?
  15. override func awakeFromNib() {
  16. super.awakeFromNib()
  17. registerForDraggedTypes([NSPasteboard.PasteboardType.fileURL])
  18. }
  19. override func draggingExited(_ sender: NSDraggingInfo?) {
  20. if let block = dragEnteredBlock {
  21. block(false)
  22. }
  23. }
  24. override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
  25. if let block = dragEnteredBlock {
  26. block(true)
  27. }
  28. guard let pboard = sender.draggingPasteboard.propertyList(forType: NSPasteboard.PasteboardType.fileURL) as? [String: Any],
  29. let fileNames = pboard[NSPasteboard.PasteboardType.fileURL.rawValue] as? [String],
  30. let path = fileNames.first, (path as NSString).pathExtension.lowercased() == "pdf" else {
  31. self.layer?.borderColor = NSColor.clear.cgColor
  32. return []
  33. }
  34. return NSDragOperation.copy
  35. }
  36. override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
  37. guard let pboard = sender.draggingPasteboard.propertyList(forType: NSPasteboard.PasteboardType.fileURL) as? [String: Any],
  38. let fileNames = pboard[NSPasteboard.PasteboardType.fileURL.rawValue] as? [String],
  39. let path = fileNames.first, (path as NSString).pathExtension.lowercased() == "pdf" else {
  40. return false
  41. }
  42. #if VERSION_DMG
  43. // Add code for VERSION_DMG
  44. #else
  45. // let url = URL(fileURLWithPath: path)
  46. // if let bookmarkData = try? url.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil) {
  47. // AppSandboxFileAccess.fileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, forURL: url)
  48. // AppSandboxFileAccess.fileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, forURL: URL(fileURLWithPath: url.path ?? url.absoluteString))
  49. // }
  50. #endif
  51. if let block = dragSuccessBlock {
  52. block(path)
  53. }
  54. return true
  55. }
  56. override func mouseUp(with event: NSEvent) {
  57. super.mouseUp(with: event)
  58. if let block = mouseUpBack {
  59. block(self)
  60. }
  61. }
  62. }