5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 06:39:30 +08:00
wails/v2/internal/frontend/desktop/darwin/WailsWebView.m
Jakub Peleška 022a5ffec4
Fix Drag & Drop JS runtime (#3516)
* Feature/1090 native drag and drop for file and folder (#3203)

* implement basic dnd for linux

* implemented windows

* progress changed linux handling and added coordinates to drop

* progress fix drop coordinates on windows

* progress remove log from windows

* progress move js

* update js after merge

* fix event listener registration

* fix segfault on non file drag

* remove logs, fix coordinates

* minor changes, simplify to drop only

* rename EnableWails -> EnableFileDrop

* add documentation (PR id missing)

* add PR id to changelog

* fix remove casting from malloc

* fix nil check for OnFileDrop's callback

* fix nil check for OnFileDrop skip event when nil

* add error message for nil callback in OnFileDrop

---------

Co-authored-by: lyimmi <lelvente.zambo@gmail.com>
Co-authored-by: Lea Anthony <lea.anthony@gmail.com>

* implement native drag and drop for macOS (#3250)

* implement native drag and drop for macOS

* update docs

* add to changelog

* update docs (macOS is supported)

* Fix windows DragAndDrop options

* Fix class unset on dragleave for full frame elements

* improve class unset (nested elements and borders case)

* Fix runtime drop target detection and CSS class assignment

* Edit changelog

* Fix drag-and-drop options in references

* Update v2/internal/frontend/desktop/darwin/WailsWebView.m

* Update v2/internal/frontend/desktop/darwin/WailsWebView.m

---------

Co-authored-by: Zámbó, Levente <levente.zambo@gmail.com>
Co-authored-by: lyimmi <lelvente.zambo@gmail.com>
Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
Co-authored-by: Andrey Pshenkin <andrey.pshenkin@gmail.com>
Co-authored-by: Pavel Binar <pavel@beamtransfer.io>
2024-06-10 15:15:28 +10:00

123 lines
3.6 KiB
Objective-C

#import "WailsWebView.h"
#import "message.h"
@implementation WailsWebView
@synthesize disableWebViewDragAndDrop;
@synthesize enableDragAndDrop;
- (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender
{
if ( !enableDragAndDrop ) {
return [super prepareForDragOperation: sender];
}
if ( disableWebViewDragAndDrop ) {
return YES;
}
return [super prepareForDragOperation: sender];
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
if ( !enableDragAndDrop ) {
return [super performDragOperation: sender];
}
NSPasteboard *pboard = [sender draggingPasteboard];
// if no types, then we'll just let the WKWebView handle the drag-n-drop as normal
NSArray<NSPasteboardType> * types = [pboard types];
if( !types )
return [super performDragOperation: sender];
// getting all NSURL types
NSArray<Class> *url_class = @[[NSURL class]];
NSDictionary *options = @{};
NSArray<NSURL*> *files = [pboard readObjectsForClasses:url_class options:options];
// collecting all file paths
NSMutableArray *files_strs = [[NSMutableArray alloc] init];
for (NSURL *url in files)
{
const char *fs_path = [url fileSystemRepresentation]; //Will be UTF-8 encoded
NSString *fs_path_str = [[NSString alloc] initWithCString:fs_path encoding:NSUTF8StringEncoding];
[files_strs addObject:fs_path_str];
// NSLog( @"performDragOperation: file path: %s", fs_path );
}
NSString *joined=[files_strs componentsJoinedByString:@"\n"];
// Release the array of file paths
[files_strs release];
int dragXLocation = [sender draggingLocation].x - [self frame].origin.x;
int dragYLocation = [self frame].size.height - [sender draggingLocation].y; // Y coordinate is inverted, so we need to subtract from the height
// NSLog( @"draggingUpdated: X coord: %d", dragXLocation );
// NSLog( @"draggingUpdated: Y coord: %d", dragYLocation );
NSString *message = [NSString stringWithFormat:@"DD:%d:%d:%@", dragXLocation, dragYLocation, joined];
const char* res = message.UTF8String;
processMessage(res);
if ( disableWebViewDragAndDrop ) {
return YES;
}
return [super performDragOperation: sender];
}
- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender {
if ( !enableDragAndDrop ) {
return [super draggingUpdated: sender];
}
NSPasteboard *pboard = [sender draggingPasteboard];
// if no types, then we'll just let the WKWebView handle the drag-n-drop as normal
NSArray<NSPasteboardType> * types = [pboard types];
if( !types ) {
return [super draggingUpdated: sender];
}
if ( disableWebViewDragAndDrop ) {
// we should call supper as otherwise events will not pass
[super draggingUpdated: sender];
// pass NSDragOperationGeneric = 4 to show regular hover for drag and drop. As we want to ignore webkit behaviours that depends on webpage
return 4;
}
return [super draggingUpdated: sender];
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
if ( !enableDragAndDrop ) {
return [super draggingEntered: sender];
}
NSPasteboard *pboard = [sender draggingPasteboard];
// if no types, then we'll just let the WKWebView handle the drag-n-drop as normal
NSArray<NSPasteboardType> * types = [pboard types];
if( !types ) {
return [super draggingEntered: sender];
}
if ( disableWebViewDragAndDrop ) {
// we should call supper as otherwise events will not pass
[super draggingEntered: sender];
// pass NSDragOperationGeneric = 4 to show regular hover for drag and drop. As we want to ignore webkit behaviours that depends on webpage
return 4;
}
return [super draggingEntered: sender];
}
@end