Implement file promises in the artworks collection view

This commit is contained in:
Damiano Galassi committed 2024-06-29 12:01:54 +02:00
1 parent da3a30c72f
commit a1bb044164
4 files changed
+195 -29

No files matched your search

+66
View File
@@ -0,0 +1,66 @@
//
// FilePromiseProvider.swift
// Subler
//
// Created by Damiano Galassi on 28/06/24.
//
import Cocoa
class FilePromiseProvider: NSFilePromiseProvider {
struct UserInfoKeys {
static let indexPathKey = "indexPath"
static let extensionKey = "extension"
}
/** 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)
types.append(.artworkDragType) // Add our own internal drag type (row drag and drop reordering).
// types.append(.fileURL) // Add the .fileURL drag type (to promise files to other apps).
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[FilePromiseProvider.UserInfoKeys.urlKey] as? NSURL {
// return url.pasteboardPropertyList(forType: type)
// }
case .artworkDragType:
// Incoming type is "com.mycompany.mydragdrop", return (from our userInfo) the item's indexPath.
let indexPathData = userInfoDict[FilePromiseProvider.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)
}
}