5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 16:02:07 +08:00
wails/v3/pkg/application/webview_drag.m
Lea Anthony 045a830fbc
Add "DragAndDropEnabled" option for window
Add "FileDraggingExited" event
2023-02-15 18:56:52 +11:00

61 lines
1.6 KiB
Objective-C

//go:build darwin
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import "webview_drag.h"
#import "../events/events.h"
extern void processDragItems(unsigned int windowId, char** arr, int length);
@implementation WebviewDrag
- (instancetype)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
[self registerForDraggedTypes:@[NSFilenamesPboardType]];
}
return self;
}
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
NSPasteboard *pasteboard = [sender draggingPasteboard];
if ([[pasteboard types] containsObject:NSFilenamesPboardType]) {
processWindowEvent(self.windowId, EventWindowFileDraggingEntered);
return NSDragOperationCopy;
}
return NSDragOperationNone;
}
- (void)draggingExited:(id<NSDraggingInfo>)sender {
processWindowEvent(self.windowId, EventWindowFileDraggingExited);
}
- (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender {
return YES;
}
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
NSPasteboard *pasteboard = [sender draggingPasteboard];
processWindowEvent(self.windowId, EventWindowFileDraggingPerformed);
if ([[pasteboard types] containsObject:NSFilenamesPboardType]) {
NSArray *files = [pasteboard propertyListForType:NSFilenamesPboardType];
NSUInteger count = [files count];
char** cArray = (char**)malloc(count * sizeof(char*));
for (NSUInteger i = 0; i < count; i++) {
NSString* str = files[i];
cArray[i] = (char*)[str UTF8String];
}
processDragItems(self.windowId, cArray, (int)count);
free(cArray);
return YES;
}
return NO;
}
@end