cocoa: allow to drag and drop URLs

This commit also improves the visual feedback to the user by showing a plus
icon in the mouse cursor when dragging supported types.

Fixes #469
This commit is contained in:
Nyx0uf 2014-01-09 14:42:43 +01:00 committed by Stefano Pigozzi
parent b6907a7bb5
commit 0f8558a723
1 changed files with 19 additions and 5 deletions

View File

@ -33,7 +33,8 @@
- (id)initWithFrame:(NSRect)frame { - (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame]; self = [super initWithFrame:frame];
if (self) { if (self) {
[self registerForDraggedTypes:@[NSFilenamesPboardType]]; [self registerForDraggedTypes:@[NSFilenamesPboardType,
NSURLPboardType]];
} }
return self; return self;
} }
@ -234,14 +235,27 @@
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{ {
return NSDragOperationEvery; NSPasteboard *pboard = [sender draggingPasteboard];
NSArray *types = [pboard types];
if ([types containsObject:NSFilenamesPboardType] ||
[types containsObject:NSURLPboardType])
return NSDragOperationCopy;
else
return NSDragOperationNone;
} }
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{ {
NSPasteboard *pboard = [sender draggingPasteboard]; NSPasteboard *pboard = [sender draggingPasteboard];
NSArray *pbitems = [pboard propertyListForType:NSFilenamesPboardType]; if ([[pboard types] containsObject:NSURLPboardType]) {
[self.adapter handleFilesArray:pbitems]; NSURL *file_url = [NSURL URLFromPasteboard:pboard];
return YES; [self.adapter handleFilesArray:@[[file_url absoluteString]]];
return YES;
} else if ([[pboard types] containsObject:NSFilenamesPboardType]) {
NSArray *pbitems = [pboard propertyListForType:NSFilenamesPboardType];
[self.adapter handleFilesArray:pbitems];
return YES;
}
return NO;
} }
@end @end