Port new coca drag'n drop code from upstream

This commit is contained in:
Kovid Goyal 2018-11-20 10:24:55 +05:30
parent dedd80c6b5
commit 39e6e91d69
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -560,8 +560,7 @@ static GLFWapplicationshouldhandlereopenfun handle_reopen_callback = NULL;
markedText = [[NSMutableAttributedString alloc] init]; markedText = [[NSMutableAttributedString alloc] init];
[self updateTrackingAreas]; [self updateTrackingAreas];
[self registerForDraggedTypes:[NSArray arrayWithObjects: [self registerForDraggedTypes:@[NSPasteboardTypeFileURL]];
NSPasteboardTypeFileURL, nil]];
} }
return self; return self;
@ -915,28 +914,9 @@ is_ascii_control_char(char x) {
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{ {
if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) // HACK: We don't know what to say here because we don't know what the
== NSDragOperationGeneric) // application wants to do with the paths
{
[self setNeedsDisplay:YES];
return NSDragOperationGeneric; return NSDragOperationGeneric;
}
return NSDragOperationNone;
}
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
[self setNeedsDisplay:YES];
return YES;
}
- (NSArray *)fileURLWithDraggingInfo:(id <NSDraggingInfo>)sender
{
NSPasteboard *pasteboard = [sender draggingPasteboard];
NSDictionary *options = [NSDictionary dictionaryWithObject:@YES forKey:NSPasteboardURLReadingFileURLsOnlyKey];
NSArray *results = [pasteboard readObjectsForClasses:[NSArray arrayWithObject:[NSURL class]] options:options];
return results;
} }
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
@ -945,22 +925,23 @@ is_ascii_control_char(char x) {
_glfwInputCursorPos(window, _glfwInputCursorPos(window,
[sender draggingLocation].x, [sender draggingLocation].x,
contentRect.size.height - [sender draggingLocation].y); contentRect.size.height - [sender draggingLocation].y);
const NSArray* files = [self fileURLWithDraggingInfo:sender]; NSPasteboard* pasteboard = [sender draggingPasteboard];
if (!files) return NO; NSDictionary* options = @{NSPasteboardURLReadingFileURLsOnlyKey:@YES};
const NSUInteger count = [files count]; NSArray* urls = [pasteboard readObjectsForClasses:@[[NSURL class]]
options:options];
if (!urls) return NO;
const NSUInteger count = [urls count];
if (count) if (count)
{ {
NSEnumerator* e = [files objectEnumerator];
char** paths = calloc(count, sizeof(char*)); char** paths = calloc(count, sizeof(char*));
NSUInteger i;
for (i = 0; i < count; i++) for (NSUInteger i = 0; i < count; i++)
paths[i] = _glfw_strdup([[[e nextObject] path] UTF8String]); paths[i] = _glfw_strdup([[urls objectAtIndex:i] fileSystemRepresentation]);
_glfwInputDrop(window, (int) count, (const char**) paths); _glfwInputDrop(window, (int) count, (const char**) paths);
for (i = 0; i < count; i++) for (NSUInteger i = 0; i < count; i++)
free(paths[i]); free(paths[i]);
free(paths); free(paths);
} }
@ -968,11 +949,6 @@ is_ascii_control_char(char x) {
return YES; return YES;
} }
- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
[self setNeedsDisplay:YES];
}
- (BOOL)hasMarkedText - (BOOL)hasMarkedText
{ {
return [markedText length] > 0; return [markedText length] > 0;