Converting to modern API for drag and drop

This commit is contained in:
Michael Starke
2018-11-06 12:11:15 +01:00
parent 0ff6597487
commit 2b95621181
2 changed files with 14 additions and 22 deletions

View File

@@ -31,18 +31,12 @@
@implementation MPEntryTableDataSource @implementation MPEntryTableDataSource
- (BOOL)tableView:(NSTableView *)tableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard { - (id<NSPasteboardWriting>)tableView:(NSTableView *)tableView pasteboardWriterForRow:(NSInteger)row {
NSMutableArray *entries = [[NSMutableArray alloc] initWithCapacity:rowIndexes.count]; id item = self.viewController.entryArrayController.arrangedObjects[row];
[rowIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) { if([item isKindOfClass:KPKEntry.class]) {
[entries addObject:self.viewController.entryArrayController.arrangedObjects[idx]]; return item;
}];
for(KPKEntry *entry in entries) {
if(![entry isKindOfClass:[KPKEntry class]]) {
return NO;
}
} }
[pboard writeObjects:entries]; return nil;
return YES;
} }
@end @end

View File

@@ -31,22 +31,20 @@
@property (weak) KPKGroup *localDraggedGroup; @property (weak) KPKGroup *localDraggedGroup;
@property (weak) KPKEntry *localDraggedEntry; @property (weak) KPKEntry *localDraggedEntry;
@property (strong) NSArray<KPKGroup *> *draggedGroups;
@property (strong) NSArray<KPKEntry *> *draggedEntries;
@end @end
@implementation MPOutlineDataSource @implementation MPOutlineDataSource
- (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pasteboard { - (id<NSPasteboardWriting>)outlineView:(NSOutlineView *)outlineView pasteboardWriterForItem:(id)item {
if(items.count != 1) { id representedObject = [item representedObject];
return NO; if([representedObject isKindOfClass:KPKGroup.class]) {
KPKGroup *group = representedObject;
return group;
} }
self.localDraggedGroup = nil; return nil;
id item = [items.lastObject representedObject];
if(![item isKindOfClass:KPKGroup.class]) {
return NO;
}
KPKGroup *draggedGroup = item;
[pasteboard writeObjects:@[draggedGroup]];
return (nil != draggedGroup.parent);
} }
- (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id<NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index { - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id<NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index {