I'm trying to implement dragging items from an NSCollectionView (not just dropping things on it).
In my example code, I'm registering the CollectionView from dragging:
collectionView.registerForDraggedTypes([.URL])
collectionView.setDraggingSourceOperationMask(.every, forLocal: false)
collectionView.setDraggingSourceOperationMask(.every, forLocal: true)
Then, I've implemented these methods from the NSCollectionViewDelegate protocol:
func collectionView(_ collectionView: NSCollectionView, canDragItemsAt indexPaths: Set<IndexPath>, with event: NSEvent) -> Bool {
return true
}
func collectionView(_ collectionView: NSCollectionView, pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting? {
return URL(fileURLWithPath: #file) as NSPasteboardWriting
}
func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forItemsAt indexPaths: Set<IndexPath>) { }
func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, endedAt screenPoint: NSPoint, dragOperation operation: NSDragOperation) { }
But neither of them is ever even called! Why not?
If I add these two methods:
func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
return .move
}
func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
return true
}
Then I can successfully drop files from the Desktop into the collection view, but still not the other way around.
What's going on?
Best regards, V.
Exactly the same here.
It looks like a bug (Xcode 9.4.1, Swit 4.1.2).
Just like you mentioned,
validateDropandacceptDropget called in my app.But (for example),
endedAtdoes not:The only workaround I could find (for the above
endedAtdelegate) was to subclassNSCollectionView(which I'm callingImageCollectionViewin the example below) and implement my owndelegate/protocol:(I had to call it
imageCollectionViewDelegateto not conflict with the already existingdelegateproperty.)Then, in my controller (which I'm calling
ImageCollectionViewController):That allows me to do stuff when dragging outside the collection.
In this very simple use case, just turning the destination view's highlight on/off:
Ideally all this extra code wouldn't be necessary.
I'm also looking for the proper way of handling this.