Implement file promises in the artworks collection view
This commit is contained in:
4 files changed
+195
-29
No files matched your search
@@ -231,6 +231,16 @@ final class ArtworkSelectorViewItemView: NSView {
|
||||
}
|
||||
}
|
||||
|
||||
var imageFrame: NSRect {
|
||||
get {
|
||||
if let image = imageLayer.contents as? NSImage {
|
||||
AVMakeRect(aspectRatio: image.size, insideRect: imageLayer.bounds)
|
||||
} else {
|
||||
NSRect(x: 0, y: 0, width: 64, height: 32)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
final class ArtworkSelectorViewItem: NSCollectionViewItem {
|
||||
@@ -310,6 +320,26 @@ final class ArtworkSelectorViewItem: NSCollectionViewItem {
|
||||
}
|
||||
}
|
||||
|
||||
override var draggingImageComponents: [NSDraggingImageComponent] {
|
||||
var components = Array<NSDraggingImageComponent>()
|
||||
|
||||
if let itemView {
|
||||
let icon = NSDraggingImageComponent(key: .icon)
|
||||
icon.contents = itemView.image
|
||||
icon.frame = itemView.imageFrame
|
||||
components.append(icon)
|
||||
}
|
||||
|
||||
if let textField, textField.stringValue.isEmpty == false {
|
||||
let label = NSDraggingImageComponent(key: .label)
|
||||
label.contents = textField.stringValue
|
||||
label.frame = textField.frame
|
||||
components.append(label)
|
||||
}
|
||||
|
||||
return components
|
||||
}
|
||||
|
||||
override func prepareForReuse() {
|
||||
title = nil
|
||||
subtitle = nil
|
||||
|
||||
@@ -14,7 +14,7 @@ extension NSPasteboard.PasteboardType {
|
||||
static let artworkDragType = NSPasteboard.PasteboardType("org.subler.artworkdragdrop")
|
||||
}
|
||||
|
||||
class MovieViewController: PropertyView, NSTableViewDataSource, ExpandedTableViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegate, NSDraggingDestination {
|
||||
class MovieViewController: PropertyView, NSTableViewDataSource, ExpandedTableViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegate, NSDraggingDestination, NSFilePromiseProviderDelegate {
|
||||
|
||||
var metadata: MP42Metadata {
|
||||
didSet {
|
||||
@@ -129,6 +129,7 @@ class MovieViewController: PropertyView, NSTableViewDataSource, ExpandedTableVie
|
||||
// Determine the kind of source drag originating from this app.
|
||||
// Note, if you want to allow your app to drag items to the Finder's trash can, add ".delete".
|
||||
artworksView.setDraggingSourceOperationMask([.copy, .delete], forLocal: false)
|
||||
artworksView.setDraggingSourceOperationMask([.copy, .move], forLocal: true)
|
||||
|
||||
reloadData()
|
||||
}
|
||||
@@ -973,10 +974,74 @@ class MovieViewController: PropertyView, NSTableViewDataSource, ExpandedTableVie
|
||||
return true
|
||||
}
|
||||
|
||||
func collectionView(_ collectionView: NSCollectionView, writeItemsAt indexPaths: Set<IndexPath>, to pasteboard: NSPasteboard) -> Bool {
|
||||
pasteboard.declareTypes([.artworkDragType], owner: nil)
|
||||
pasteboard.setData(NSKeyedArchiver.archivedData(withRootObject: indexPaths), forType: .artworkDragType)
|
||||
return indexPaths.isEmpty == false
|
||||
func filePromiseProvider(_ filePromiseProvider: NSFilePromiseProvider, fileNameForType fileType: String) -> String {
|
||||
if let userInfo = filePromiseProvider.userInfo as? [String: AnyObject] {
|
||||
return "Untitled." + (userInfo[FilePromiseProvider.UserInfoKeys.extensionKey] as! String)
|
||||
} else {
|
||||
return "Untitled.tiff"
|
||||
}
|
||||
}
|
||||
|
||||
func filePromiseProvider(_ filePromiseProvider: NSFilePromiseProvider, writePromiseTo url: URL, completionHandler: @escaping (Error?) -> Void) {
|
||||
if let userInfo = filePromiseProvider.userInfo as? [String: AnyObject] {
|
||||
do {
|
||||
if let indexPathData = userInfo[FilePromiseProvider.UserInfoKeys.indexPathKey] as? Data {
|
||||
if let indexPath = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(indexPathData) as? IndexPath {
|
||||
let item = artworks[indexPath.last!]
|
||||
if let image = item.imageValue {
|
||||
try image.data?.write(to: url)
|
||||
completionHandler(nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
fatalError("failed to unarchive indexPath from promise provider.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Dragging Source Support - Required for multi-image drag and drop.
|
||||
Return a custom object that implements NSPasteboardWriting (or simply use NSPasteboardItem), or nil to prevent dragging for the item.
|
||||
*/
|
||||
func collectionView(_ collectionView: NSCollectionView,
|
||||
pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting? {
|
||||
var provider: NSFilePromiseProvider?
|
||||
|
||||
let item = artworks[indexPath.last!]
|
||||
guard let image = item.imageValue else { return provider }
|
||||
|
||||
let fileExtension = {
|
||||
switch image.type {
|
||||
case MP42_ART_BMP:
|
||||
return "bmp"
|
||||
case MP42_ART_GIF:
|
||||
return "gif"
|
||||
case MP42_ART_JPEG:
|
||||
return "jpeg"
|
||||
case MP42_ART_PNG:
|
||||
return "png"
|
||||
default:
|
||||
return "tiff"
|
||||
}
|
||||
}()
|
||||
|
||||
if #available(macOS 11.0, *) {
|
||||
let typeIdentifier = UTType(filenameExtension: fileExtension)
|
||||
provider = FilePromiseProvider(fileType: typeIdentifier!.identifier, delegate: self)
|
||||
} else {
|
||||
let typeIdentifier =
|
||||
UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension as CFString, nil)
|
||||
provider = FilePromiseProvider(fileType: typeIdentifier!.takeRetainedValue() as String, delegate: self)
|
||||
}
|
||||
|
||||
do {
|
||||
let data = try NSKeyedArchiver.archivedData(withRootObject: indexPath, requiringSecureCoding: false)
|
||||
provider!.userInfo = [FilePromiseProvider.UserInfoKeys.extensionKey: fileExtension as Any,
|
||||
FilePromiseProvider.UserInfoKeys.indexPathKey: data]
|
||||
} catch {
|
||||
fatalError("failed to archive indexPath to pasteboard")
|
||||
}
|
||||
return provider
|
||||
}
|
||||
|
||||
func collectionView(_ collectionView: NSCollectionView,
|
||||
@@ -989,11 +1054,9 @@ class MovieViewController: PropertyView, NSTableViewDataSource, ExpandedTableVie
|
||||
|
||||
let pasteboard = draggingInfo.draggingPasteboard
|
||||
|
||||
if let draggingSource = draggingInfo.draggingSource as? NSCollectionView {
|
||||
if draggingSource == collectionView {
|
||||
// Drag source came from our own table view.
|
||||
dragOperation = [.move]
|
||||
}
|
||||
if let draggingSource = draggingInfo.draggingSource as? NSCollectionView, draggingSource == collectionView {
|
||||
// Drag source came from our own collection view.
|
||||
dragOperation = [.move]
|
||||
} else {
|
||||
// Drag source came from another app.
|
||||
// Search through the array of NSPasteboardItems.
|
||||
@@ -1036,6 +1099,8 @@ class MovieViewController: PropertyView, NSTableViewDataSource, ExpandedTableVie
|
||||
}
|
||||
|
||||
func dropInternalArtworks(_ collectionView: NSCollectionView, draggingInfo: NSDraggingInfo, indexPath: IndexPath) {
|
||||
var indexes = Set<IndexPath>()
|
||||
|
||||
draggingInfo.enumerateDraggingItems(
|
||||
options: NSDraggingItemEnumerationOptions.concurrent,
|
||||
for: collectionView,
|
||||
@@ -1045,14 +1110,28 @@ class MovieViewController: PropertyView, NSTableViewDataSource, ExpandedTableVie
|
||||
if let pasteboardItem = draggingItem.item as? NSPasteboardItem {
|
||||
do {
|
||||
if let indexPathData = pasteboardItem.data(forType: .artworkDragType),
|
||||
let artworkIndexPath = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(indexPathData) as? Set<IndexPath> {
|
||||
print(artworkIndexPath)
|
||||
let itemIndexPath = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(indexPathData) as? IndexPath {
|
||||
indexes.insert(itemIndexPath)
|
||||
}
|
||||
} catch {
|
||||
Swift.debugPrint("failed to unarchive indexPath for dropped photo item.")
|
||||
Swift.debugPrint("failed to unarchive indexPath for dropped item.")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if let last = indexPath.last {
|
||||
let itemIndexes = indexes.compactMap { $0.last }
|
||||
let draggedItems = itemIndexes.map { artworks[$0] }
|
||||
let destinationIndex = last - itemIndexes.filter { $0 < last }.count
|
||||
|
||||
var modifiedArtworks = IndexSet(artworks.indices).subtracting(IndexSet(itemIndexes)).map { artworks[$0] }
|
||||
|
||||
for item in draggedItems.reversed() {
|
||||
modifiedArtworks.insert(item, at: destinationIndex)
|
||||
}
|
||||
|
||||
replace(metadataArtworks: artworks, withItems: modifiedArtworks)
|
||||
}
|
||||
}
|
||||
|
||||
// The temporary directory URL you use to accept file promises.
|
||||
@@ -1092,7 +1171,7 @@ class MovieViewController: PropertyView, NSTableViewDataSource, ExpandedTableVie
|
||||
}
|
||||
|
||||
// Find the proper drop location relative to the provided indexPath.
|
||||
func dropLocation(indexPath: IndexPath) -> IndexPath {
|
||||
static func dropLocation(indexPath: IndexPath) -> IndexPath {
|
||||
var toIndexPath = indexPath
|
||||
if indexPath.item == 0 {
|
||||
toIndexPath = IndexPath(item: indexPath.item, section: indexPath.section)
|
||||
@@ -1103,7 +1182,7 @@ class MovieViewController: PropertyView, NSTableViewDataSource, ExpandedTableVie
|
||||
}
|
||||
|
||||
func dropExternalArtworks(_ collectionView: NSCollectionView, draggingInfo: NSDraggingInfo, indexPath: IndexPath) {
|
||||
let toIndexPath = dropLocation(indexPath: indexPath)
|
||||
let toIndexPath = MovieViewController.dropLocation(indexPath: indexPath)
|
||||
|
||||
if handlePromisedDrops(draggingInfo: draggingInfo, toIndexPath: toIndexPath) {
|
||||
// Successfully processed the dragged items that were promised.
|
||||
@@ -1170,20 +1249,7 @@ class MovieViewController: PropertyView, NSTableViewDataSource, ExpandedTableVie
|
||||
}
|
||||
}
|
||||
|
||||
// override func imageBrowser(_ aBrowser: IKImageBrowserView!, moveItemsAt indexes: IndexSet!, to destinationIndex: Int) -> Bool {
|
||||
// let destinationIndex = destinationIndex - indexes.count(in: 0..<destinationIndex)
|
||||
//
|
||||
// let items = indexes.map { artworks[$0] }
|
||||
// var modifiedArtworks = IndexSet(artworks.indices).subtracting(indexes).map { artworks[$0] }
|
||||
//
|
||||
// for item in items.reversed() {
|
||||
// modifiedArtworks.insert(item, at: destinationIndex)
|
||||
// }
|
||||
//
|
||||
// replace(metadataArtworks: artworks, withItems: modifiedArtworks)
|
||||
// return true
|
||||
// }
|
||||
//
|
||||
|
||||
// override func imageBrowser(_ aBrowser: IKImageBrowserView!, writeItemsAt itemIndexes: IndexSet!, to pasteboard: NSPasteboard!) -> Int {
|
||||
// pasteboard.declareTypes([artworksPBoardType, .tiff], owner: nil)
|
||||
//
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -53,6 +53,7 @@
|
||||
A956CE651F99E5BB00093992 /* ActivityWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A956CE641F99E5BB00093992 /* ActivityWindowController.swift */; };
|
||||
A956CE691F99E93D00093992 /* ButtonToolbarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = A956CE681F99E93D00093992 /* ButtonToolbarItem.swift */; };
|
||||
A956CE6B1F99EAAD00093992 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = A956CE6A1F99EAAD00093992 /* Logger.swift */; };
|
||||
A9586F672C2EF0750016350E /* FilePromiseProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9586F662C2EF0750016350E /* FilePromiseProvider.swift */; };
|
||||
A959001A202AF21800FD8F23 /* ProgressViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9590018202AF21800FD8F23 /* ProgressViewController.swift */; };
|
||||
A961476713F568210068F161 /* Queue.xib in Resources */ = {isa = PBXBuildFile; fileRef = A961476513F568210068F161 /* Queue.xib */; };
|
||||
A962D9EE1F8B7F9F00802EEF /* FileImportController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A962D9ED1F8B7F9F00802EEF /* FileImportController.swift */; };
|
||||
@@ -281,6 +282,7 @@
|
||||
A957E5B12040083E00A5F776 /* it */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = it; path = it.lproj/OffsetViewController.strings; sourceTree = "<group>"; };
|
||||
A957E5B22040083E00A5F776 /* it */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = it; path = it.lproj/ProgressViewController.strings; sourceTree = "<group>"; };
|
||||
A957E5B32040083E00A5F776 /* it */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = it; path = it.lproj/DocumentWindowController.strings; sourceTree = "<group>"; };
|
||||
A9586F662C2EF0750016350E /* FilePromiseProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilePromiseProvider.swift; sourceTree = "<group>"; };
|
||||
A9590018202AF21800FD8F23 /* ProgressViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ProgressViewController.swift; path = Classes/ProgressViewController.swift; sourceTree = "<group>"; };
|
||||
A962D9ED1F8B7F9F00802EEF /* FileImportController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = FileImportController.swift; path = Classes/FileImportController.swift; sourceTree = "<group>"; };
|
||||
A9676A2F1FB6E85300C1E300 /* LanguageTransformer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = LanguageTransformer.swift; path = Classes/LanguageTransformer.swift; sourceTree = "<group>"; };
|
||||
@@ -477,6 +479,7 @@
|
||||
A909440E1DD4C85B00E9F89C /* Table View */,
|
||||
A9A77C4D1F306FA70018023B /* AttributedStyles.swift */,
|
||||
A956CE681F99E93D00093992 /* ButtonToolbarItem.swift */,
|
||||
A9586F662C2EF0750016350E /* FilePromiseProvider.swift */,
|
||||
);
|
||||
name = "Other Sources";
|
||||
sourceTree = "<group>";
|
||||
@@ -830,6 +833,7 @@
|
||||
files = (
|
||||
A959001A202AF21800FD8F23 /* ProgressViewController.swift in Sources */,
|
||||
A91576EB204ECA6E00D93CE7 /* Token.swift in Sources */,
|
||||
A9586F672C2EF0750016350E /* FilePromiseProvider.swift in Sources */,
|
||||
A9676A341FB6EAAD00C1E300 /* ItemViewController.swift in Sources */,
|
||||
A91F0FA023797F2800C9DCC9 /* PropertyView.swift in Sources */,
|
||||
A98D6BAB21FDC62800F0D886 /* Queue.swift in Sources */,
|
||||
|
||||
Reference in new issue
Block a user