I am trying to get drag and drop working in a swift 4 Macos application with a NSBrowser control. The code I have at the moment looks like this:
// Drag and Drop
func browser(_ browser: NSBrowser,
canDragRowsWith rowIndexes: IndexSet,
inColumn column: Int,
with event: NSEvent) -> Bool {
if column != 0 {
quizBrowser.canDragRows(with: rowIndexes, inColumn: column, with: event)
return true
}
return false
}
func browser(_ browser: NSBrowser,
writeRowsWith rowIndexes: IndexSet,
inColumn column: Int,
to pasteboard: NSPasteboard) -> Bool {
if column != 0 {
let row: Int = rowIndexes.last!
let item = quizBrowser.item(atRow: row, inColumn: column) as? BrowserItem
pasteboard.declareTypes([NSPasteboard.PasteboardType.string], owner: self)
pasteboard.setString(item!.name, forType: NSPasteboard.PasteboardType.string)
return true
}
return false
}
func browser(_ browser: NSBrowser,
validateDrop info: NSDraggingInfo,
proposedRow row: UnsafeMutablePointer<Int>,
column: UnsafeMutablePointer<Int>,
dropOperation: UnsafeMutablePointer<NSBrowser.DropOperation>) -> NSDragOperation {
return NSDragOperation.move
}
func browser(_ browser: NSBrowser,
acceptDrop info: NSDraggingInfo,
atRow row: Int,
column: Int,
dropOperation: NSBrowser.DropOperation) -> Bool {
let pboard = info.draggingPasteboard
let rowData = pboard().data(forType: NSPasteboard.PasteboardType.string)
return true
}
What I am seeing is that the first two functions are being called [canDragRowsWith and writeRowswith], and the setString command is returning true, so it looks as though the value has been saved to the pasteboard.
But the other two functions are not being called. So while I can see the row in the NSBrowser being dragged, it looks as though the control isn't registered to accept the drop. I have also added this in the viewDidLoad function
quizBrowser.setDraggingSourceOperationMask(NSDragOperation.generic, forLocal: true)
Does anyone have an idea what I am missing? Does anyone have a swift 3/4 example of drag and drop using an NSBrowser that they are will to share?
Thanks
Before a view can receive a drag operation, you need to register the data types that it can accept by invoking
func registerForDraggedTypes(_ newTypes: [NSPasteboard.PasteboardType])