mirror of
https://github.com/MacPass/MacPass.git
synced 2025-12-25 00:39:25 +00:00
Group view has rudimentary drag'n'drop support
This commit is contained in:
@@ -9,43 +9,45 @@
|
||||
#import "MPOutlineDataSource.h"
|
||||
#import "MPDocument.h"
|
||||
#import "KdbLib.h"
|
||||
#import "KdbGroup+Undo.h"
|
||||
|
||||
NSString *const MPPasteBoardType = @"com.hicknhack.macpass.pasteboard";
|
||||
|
||||
@implementation MPOutlineDataSource
|
||||
|
||||
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
|
||||
if(!item) {
|
||||
return 1;
|
||||
|
||||
- (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pasteboard {
|
||||
[_draggedItem release];
|
||||
_draggedItem = nil;
|
||||
[pasteboard setString:@"Weee" forType:MPPasteBoardType];
|
||||
if([items count] == 1) {
|
||||
_draggedItem = [[[items lastObject] representedObject] retain];
|
||||
return (nil != _draggedItem.parent);
|
||||
}
|
||||
if( [item isKindOfClass:[KdbGroup class]]) {
|
||||
KdbGroup *group = item;
|
||||
return [[group groups] count];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
|
||||
if(!item) {
|
||||
MPDocument *document = [[[outlineView window] windowController] document];
|
||||
return document.root;
|
||||
}
|
||||
if( [item isKindOfClass:[KdbGroup class]]) {
|
||||
KdbGroup *group = item;
|
||||
if( [[group groups] count] > index ) {
|
||||
return [group groups][index];
|
||||
}
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
|
||||
if(!item) {
|
||||
MPDocument *document = [[[outlineView window] windowController] document];
|
||||
return ([[document.root groups] count] > 0);
|
||||
}
|
||||
if([item isKindOfClass:[KdbGroup class]])
|
||||
{
|
||||
KdbGroup *group = item;
|
||||
return ([[group groups] count] > 0);
|
||||
}
|
||||
return NO;
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id<NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index {
|
||||
if(_draggedItem) {
|
||||
KdbGroup *target = [item representedObject];
|
||||
if( target == nil) {
|
||||
return NSDragOperationNone; // Draggin over root
|
||||
}
|
||||
BOOL validParent = ( _draggedItem.parent == target && index != NSOutlineViewDropOnItemIndex);
|
||||
if(validParent || _draggedItem.parent != target) {
|
||||
return NSDragOperationMove;
|
||||
info.animatesToDestination = YES;
|
||||
}
|
||||
}
|
||||
return NSDragOperationNone;
|
||||
}
|
||||
|
||||
- (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id<NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index {
|
||||
NSLog(@"Drag %@ to: %@ index: %ld", _draggedItem, [item representedObject], index);
|
||||
KdbGroup *target = [item representedObject];
|
||||
BOOL accepted = (target != _draggedItem.parent);
|
||||
info.animatesToDestination = YES;
|
||||
[_draggedItem moveToGroupUndoable:target];
|
||||
return accepted;
|
||||
}
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user