KMFilePromiseProvider.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // KMFilePromiseProvider.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/6/16.
  6. //
  7. import Cocoa
  8. extension NSPasteboard.PasteboardType {
  9. static let localDraggedTypes = NSPasteboard.PasteboardType("com.local.draggedtypes")
  10. }
  11. // Drop & Drag
  12. class KMFilePromiseProvider: NSFilePromiseProvider {
  13. struct UserInfoKeys {
  14. static let indexPathKey = "indexPath"
  15. static let urlKey = "url"
  16. }
  17. // Add our own internal drag type (row drag and drop reordering).
  18. // Add the .fileURL drag type (to promise files to other apps).
  19. var kmSupportTypes: [NSPasteboard.PasteboardType] = [.fileURL, .localDraggedTypes]
  20. /** Required:
  21. Return an array of UTI strings of data types the receiver can write to the pasteboard.
  22. By default, data for the first returned type is put onto the pasteboard immediately, with the remaining types being promised.
  23. To change the default behavior, implement -writingOptionsForType:pasteboard: and return
  24. NSPasteboardWritingPromised to lazily provided data for types, return no option to provide the data for that type immediately.
  25. Use the pasteboard argument to provide different types based on the pasteboard name, if desired.
  26. Do not perform other pasteboard operations in the function implementation.
  27. */
  28. override func writableTypes(for pasteboard: NSPasteboard) -> [NSPasteboard.PasteboardType] {
  29. var types = super.writableTypes(for: pasteboard)
  30. for _type in self.kmSupportTypes {
  31. types.append(_type)
  32. }
  33. return types
  34. }
  35. /** Required:
  36. Return the appropriate property list object for the provided type.
  37. This will commonly be the NSData for that data type. However, if this function returns either a string, or any other property-list type,
  38. the pasteboard will automatically convert these items to the correct NSData format required for the pasteboard.
  39. */
  40. override func pasteboardPropertyList(forType type: NSPasteboard.PasteboardType) -> Any? {
  41. guard let userInfoDict = userInfo as? [String: Any] else { return nil }
  42. switch type {
  43. case .fileURL:
  44. // Incoming type is "public.file-url", return (from our userInfo) the item's URL.
  45. if let url = userInfoDict[KMFilePromiseProvider.UserInfoKeys.urlKey] as? NSURL {
  46. return url.pasteboardPropertyList(forType: type)
  47. }
  48. case .localDraggedTypes:
  49. // Incoming type is "com.mycompany.mydragdrop", return (from our userInfo) the item's indexPath.
  50. let indexPathData = userInfoDict[KMFilePromiseProvider.UserInfoKeys.indexPathKey]
  51. return indexPathData
  52. default:
  53. break
  54. }
  55. return super.pasteboardPropertyList(forType: type)
  56. }
  57. /** Optional:
  58. Returns options for writing data of a type to a pasteboard.
  59. Use the pasteboard argument to provide different options based on the pasteboard name, if desired.
  60. Do not perform other pasteboard operations in the function implementation.
  61. */
  62. public override func writingOptions(forType type: NSPasteboard.PasteboardType, pasteboard: NSPasteboard)
  63. -> NSPasteboard.WritingOptions {
  64. return super.writingOptions(forType: type, pasteboard: pasteboard)
  65. }
  66. }