1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- //
- // KMFilePromiseProvider.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2023/6/16.
- //
- import Cocoa
- extension NSPasteboard.PasteboardType {
- static let localDraggedTypes = NSPasteboard.PasteboardType("com.local.draggedtypes")
- }
- // Drop & Drag
- class KMFilePromiseProvider: NSFilePromiseProvider {
- struct UserInfoKeys {
- static let indexPathKey = "indexPath"
- static let urlKey = "url"
- }
-
- // Add our own internal drag type (row drag and drop reordering).
- // Add the .fileURL drag type (to promise files to other apps).
- var kmSupportTypes: [NSPasteboard.PasteboardType] = [.fileURL, .localDraggedTypes]
-
- /** Required:
- Return an array of UTI strings of data types the receiver can write to the pasteboard.
- By default, data for the first returned type is put onto the pasteboard immediately, with the remaining types being promised.
- To change the default behavior, implement -writingOptionsForType:pasteboard: and return
- NSPasteboardWritingPromised to lazily provided data for types, return no option to provide the data for that type immediately.
-
- Use the pasteboard argument to provide different types based on the pasteboard name, if desired.
- Do not perform other pasteboard operations in the function implementation.
- */
- override func writableTypes(for pasteboard: NSPasteboard) -> [NSPasteboard.PasteboardType] {
- var types = super.writableTypes(for: pasteboard)
- for _type in self.kmSupportTypes {
- types.append(_type)
- }
- return types
- }
-
- /** Required:
- Return the appropriate property list object for the provided type.
- This will commonly be the NSData for that data type. However, if this function returns either a string, or any other property-list type,
- the pasteboard will automatically convert these items to the correct NSData format required for the pasteboard.
- */
- override func pasteboardPropertyList(forType type: NSPasteboard.PasteboardType) -> Any? {
- guard let userInfoDict = userInfo as? [String: Any] else { return nil }
- switch type {
- case .fileURL:
- // Incoming type is "public.file-url", return (from our userInfo) the item's URL.
- if let url = userInfoDict[KMFilePromiseProvider.UserInfoKeys.urlKey] as? NSURL {
- return url.pasteboardPropertyList(forType: type)
- }
- case .localDraggedTypes:
- // Incoming type is "com.mycompany.mydragdrop", return (from our userInfo) the item's indexPath.
- let indexPathData = userInfoDict[KMFilePromiseProvider.UserInfoKeys.indexPathKey]
- return indexPathData
- default:
- break
- }
- return super.pasteboardPropertyList(forType: type)
- }
-
- /** Optional:
- Returns options for writing data of a type to a pasteboard.
- Use the pasteboard argument to provide different options based on the pasteboard name, if desired.
- Do not perform other pasteboard operations in the function implementation.
- */
- public override func writingOptions(forType type: NSPasteboard.PasteboardType, pasteboard: NSPasteboard)
- -> NSPasteboard.WritingOptions {
- return super.writingOptions(forType: type, pasteboard: pasteboard)
- }
- }
|