5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 17:52:29 +08:00

Add "DragAndDropEnabled" option for window

Add "FileDraggingExited" event
This commit is contained in:
Lea Anthony 2023-02-14 08:26:34 +11:00
parent 260fd061f6
commit 045a830fbc
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
10 changed files with 48 additions and 35 deletions

View File

@ -43,8 +43,8 @@ When emitting an event in JS, it now sends the event to the application. This wi
## Window ## Window
The Window API has largely remained the same, however there are a few changes to note. The Window API has largely remained the same, however the methods are now on an instance of a window rather than the runtime.
Some notable differences are:
- Windows now have a Name that identifies them. This is used to identify the window when emitting events. - Windows now have a Name that identifies them. This is used to identify the window when emitting events.
## ClipBoard ## ClipBoard
@ -61,11 +61,11 @@ Dialogs are now available in JavaScript!
## Drag and Drop ## Drag and Drop
TBD Native drag and drop can be enabled per-window. Simply set the `EnableDragAndDrop` window config option to `true` and the window will allow files to be dragged onto it. When this happens, the `events.FilesDropped` event will be emitted. The filenames can then be retrieved from the WindowEventContext using the `DroppedFiles()` method. This returns a slice of strings containing the filenames.
## Context Menus ## Context Menus
Context menus are contextual menus that are shown when the user right clicks on an element. Creating a context menu is the same as creating a standard menu , by using `app.NewMenu()`. To make the context menu available to a window, call `window.RegisterContextMenu(name, menu)`. The name will be the id of the context menu and used by the frontend. Context menus are contextual menus that are shown when the user right-clicks on an element. Creating a context menu is the same as creating a standard menu , by using `app.NewMenu()`. To make the context menu available to a window, call `window.RegisterContextMenu(name, menu)`. The name will be the id of the context menu and used by the frontend.
To indicate that an element has a context menu, add the `data-contextmenu` attribute to the element. The value of this attribute should be the name of a context menu previously registered with the window. To indicate that an element has a context menu, add the `data-contextmenu` attribute to the element. The value of this attribute should be the name of a context menu previously registered with the window.

View File

@ -32,6 +32,7 @@ func main() {
TitleBar: application.MacTitleBarHiddenInsetUnified, TitleBar: application.MacTitleBarHiddenInsetUnified,
InvisibleTitleBarHeight: 50, InvisibleTitleBarHeight: 50,
}, },
EnableDragAndDrop: true,
}) })
window.On(events.FilesDropped, func(ctx *application.WindowEventContext) { window.On(events.FilesDropped, func(ctx *application.WindowEventContext) {

View File

@ -39,6 +39,7 @@ type WebviewWindowOptions struct {
Hidden bool Hidden bool
EnableFraudulentWebsiteWarnings bool EnableFraudulentWebsiteWarnings bool
Zoom float64 Zoom float64
EnableDragAndDrop bool
} }
var WebviewWindowDefaults = &WebviewWindowOptions{ var WebviewWindowDefaults = &WebviewWindowOptions{

View File

@ -22,7 +22,7 @@ extern void processDragItems(unsigned int windowId, char** arr, int length);
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender { - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
NSPasteboard *pasteboard = [sender draggingPasteboard]; NSPasteboard *pasteboard = [sender draggingPasteboard];
if ([[pasteboard types] containsObject:NSFilenamesPboardType]) { if ([[pasteboard types] containsObject:NSFilenamesPboardType]) {
processWindowEvent(self.windowId, EventWebViewDraggingEntered); processWindowEvent(self.windowId, EventWindowFileDraggingEntered);
return NSDragOperationCopy; return NSDragOperationCopy;
} }
return NSDragOperationNone; return NSDragOperationNone;
@ -30,7 +30,7 @@ extern void processDragItems(unsigned int windowId, char** arr, int length);
- (void)draggingExited:(id<NSDraggingInfo>)sender { - (void)draggingExited:(id<NSDraggingInfo>)sender {
NSLog(@"I am here!!!!"); processWindowEvent(self.windowId, EventWindowFileDraggingExited);
} }
- (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender { - (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender {
@ -39,6 +39,7 @@ extern void processDragItems(unsigned int windowId, char** arr, int length);
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender { - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
NSPasteboard *pasteboard = [sender draggingPasteboard]; NSPasteboard *pasteboard = [sender draggingPasteboard];
processWindowEvent(self.windowId, EventWindowFileDraggingPerformed);
if ([[pasteboard types] containsObject:NSFilenamesPboardType]) { if ([[pasteboard types] containsObject:NSFilenamesPboardType]) {
NSArray *files = [pasteboard propertyListForType:NSFilenamesPboardType]; NSArray *files = [pasteboard propertyListForType:NSFilenamesPboardType];
NSUInteger count = [files count]; NSUInteger count = [files count];

View File

@ -657,8 +657,6 @@ func (w *WebviewWindow) error(message string, args ...any) {
} }
func (w *WebviewWindow) handleDragAndDropMessage(event *dragAndDropMessage) { func (w *WebviewWindow) handleDragAndDropMessage(event *dragAndDropMessage) {
println("Drag and drop message received for " + w.Name())
// Print filenames
ctx := newWindowEventContext() ctx := newWindowEventContext()
ctx.setDroppedFiles(event.filenames) ctx.setDroppedFiles(event.filenames)
for _, listener := range w.eventListeners[uint(events.FilesDropped)] { for _, listener := range w.eventListeners[uint(events.FilesDropped)] {

View File

@ -525,6 +525,24 @@ extern bool hasListeners(unsigned int);
} }
} }
- (void)windowFileDraggingEntered:(NSNotification *)notification {
if( hasListeners(EventWindowFileDraggingEntered) ) {
processWindowEvent(self.windowId, EventWindowFileDraggingEntered);
}
}
- (void)windowFileDraggingPerformed:(NSNotification *)notification {
if( hasListeners(EventWindowFileDraggingPerformed) ) {
processWindowEvent(self.windowId, EventWindowFileDraggingPerformed);
}
}
- (void)windowFileDraggingExited:(NSNotification *)notification {
if( hasListeners(EventWindowFileDraggingExited) ) {
processWindowEvent(self.windowId, EventWindowFileDraggingExited);
}
}
- (void)webView:(WKWebView *)webview didStartProvisionalNavigation:(WKNavigation *)navigation { - (void)webView:(WKWebView *)webview didStartProvisionalNavigation:(WKNavigation *)navigation {
if( hasListeners(EventWebViewDidStartProvisionalNavigation) ) { if( hasListeners(EventWebViewDidStartProvisionalNavigation) ) {
processWindowEvent(self.windowId, EventWebViewDidStartProvisionalNavigation); processWindowEvent(self.windowId, EventWebViewDidStartProvisionalNavigation);
@ -549,17 +567,5 @@ extern bool hasListeners(unsigned int);
} }
} }
- (void)webView:(WKWebView *)webview draggingEntered:(WKNavigation *)navigation {
if( hasListeners(EventWebViewDraggingEntered) ) {
processWindowEvent(self.windowId, EventWebViewDraggingEntered);
}
}
- (void)webView:(WKWebView *)webview draggingPerformed:(WKNavigation *)navigation {
if( hasListeners(EventWebViewDraggingPerformed) ) {
processWindowEvent(self.windowId, EventWebViewDraggingPerformed);
}
}
// GENERATED EVENTS END // GENERATED EVENTS END
@end @end

View File

@ -18,7 +18,7 @@ package application
extern void registerListener(unsigned int event); extern void registerListener(unsigned int event);
// Create a new Window // Create a new Window
void* windowNew(unsigned int id, int width, int height, bool fraudulentWebsiteWarningEnabled, bool frameless) { void* windowNew(unsigned int id, int width, int height, bool fraudulentWebsiteWarningEnabled, bool frameless, bool enableDragAndDrop) {
NSWindowStyleMask styleMask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable; NSWindowStyleMask styleMask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
if (frameless) { if (frameless) {
@ -72,10 +72,12 @@ void* windowNew(unsigned int id, int width, int height, bool fraudulentWebsiteWa
delegate.webView = webView; delegate.webView = webView;
delegate.hideOnClose = false; delegate.hideOnClose = false;
WebviewDrag* dragView = [[WebviewDrag alloc] initWithFrame:NSMakeRect(0, 0, width-1, height-1)]; if( enableDragAndDrop ) {
[view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; WebviewDrag* dragView = [[WebviewDrag alloc] initWithFrame:NSMakeRect(0, 0, width-1, height-1)];
[view addSubview:dragView]; [view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
dragView.windowId = id; [view addSubview:dragView];
dragView.windowId = id;
}
return window; return window;
} }
@ -1064,6 +1066,7 @@ func (w *macosWebviewWindow) run() {
C.int(w.parent.options.Height), C.int(w.parent.options.Height),
C.bool(w.parent.options.EnableFraudulentWebsiteWarnings), C.bool(w.parent.options.EnableFraudulentWebsiteWarnings),
C.bool(w.parent.options.Frameless), C.bool(w.parent.options.Frameless),
C.bool(w.parent.options.EnableDragAndDrop),
) )
w.setTitle(w.parent.options.Title) w.setTitle(w.parent.options.Title)
w.setAlwaysOnTop(w.parent.options.AlwaysOnTop) w.setAlwaysOnTop(w.parent.options.AlwaysOnTop)

View File

@ -130,8 +130,9 @@ type macEvents struct {
WebViewDidReceiveServerRedirectForProvisionalNavigation WindowEventType WebViewDidReceiveServerRedirectForProvisionalNavigation WindowEventType
WebViewDidFinishNavigation WindowEventType WebViewDidFinishNavigation WindowEventType
WebViewDidCommitNavigation WindowEventType WebViewDidCommitNavigation WindowEventType
WebViewDraggingEntered WindowEventType WindowFileDraggingEntered WindowEventType
WebViewDraggingPerformed WindowEventType WindowFileDraggingPerformed WindowEventType
WindowFileDraggingExited WindowEventType
} }
func newMacEvents() macEvents { func newMacEvents() macEvents {
@ -256,7 +257,8 @@ func newMacEvents() macEvents {
WebViewDidReceiveServerRedirectForProvisionalNavigation: 1141, WebViewDidReceiveServerRedirectForProvisionalNavigation: 1141,
WebViewDidFinishNavigation: 1142, WebViewDidFinishNavigation: 1142,
WebViewDidCommitNavigation: 1143, WebViewDidCommitNavigation: 1143,
WebViewDraggingEntered: 1144, WindowFileDraggingEntered: 1144,
WebViewDraggingPerformed: 1145, WindowFileDraggingPerformed: 1145,
WindowFileDraggingExited: 1146,
} }
} }

View File

@ -126,10 +126,11 @@ extern void processWindowEvent(unsigned int, unsigned int);
#define EventWebViewDidReceiveServerRedirectForProvisionalNavigation 1141 #define EventWebViewDidReceiveServerRedirectForProvisionalNavigation 1141
#define EventWebViewDidFinishNavigation 1142 #define EventWebViewDidFinishNavigation 1142
#define EventWebViewDidCommitNavigation 1143 #define EventWebViewDidCommitNavigation 1143
#define EventWebViewDraggingEntered 1144 #define EventWindowFileDraggingEntered 1144
#define EventWebViewDraggingPerformed 1145 #define EventWindowFileDraggingPerformed 1145
#define EventWindowFileDraggingExited 1146
#define MAX_EVENTS 1146 #define MAX_EVENTS 1147
#endif #endif

View File

@ -118,7 +118,7 @@ mac:WebViewDidStartProvisionalNavigation
mac:WebViewDidReceiveServerRedirectForProvisionalNavigation mac:WebViewDidReceiveServerRedirectForProvisionalNavigation
mac:WebViewDidFinishNavigation mac:WebViewDidFinishNavigation
mac:WebViewDidCommitNavigation mac:WebViewDidCommitNavigation
mac:WebViewDraggingEntered mac:WindowFileDraggingEntered
mac:WebViewDraggingPerformed mac:WindowFileDraggingPerformed
mac:WindowFileDraggingExited