diff --git a/v3/V3 Changes.md b/v3/V3 Changes.md index 6f99084ed..03c3eefd4 100644 --- a/v3/V3 Changes.md +++ b/v3/V3 Changes.md @@ -43,8 +43,8 @@ When emitting an event in JS, it now sends the event to the application. This wi ## 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. ## ClipBoard @@ -61,11 +61,11 @@ Dialogs are now available in JavaScript! ## 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 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. diff --git a/v3/examples/drag-n-drop/main.go b/v3/examples/drag-n-drop/main.go index 5719b454f..8a7974c26 100644 --- a/v3/examples/drag-n-drop/main.go +++ b/v3/examples/drag-n-drop/main.go @@ -32,6 +32,7 @@ func main() { TitleBar: application.MacTitleBarHiddenInsetUnified, InvisibleTitleBarHeight: 50, }, + EnableDragAndDrop: true, }) window.On(events.FilesDropped, func(ctx *application.WindowEventContext) { diff --git a/v3/pkg/application/options_webview_window.go b/v3/pkg/application/options_webview_window.go index 70e2af337..c8e748beb 100644 --- a/v3/pkg/application/options_webview_window.go +++ b/v3/pkg/application/options_webview_window.go @@ -39,6 +39,7 @@ type WebviewWindowOptions struct { Hidden bool EnableFraudulentWebsiteWarnings bool Zoom float64 + EnableDragAndDrop bool } var WebviewWindowDefaults = &WebviewWindowOptions{ diff --git a/v3/pkg/application/webview_drag.m b/v3/pkg/application/webview_drag.m index 32d8f40b9..057e68d95 100644 --- a/v3/pkg/application/webview_drag.m +++ b/v3/pkg/application/webview_drag.m @@ -22,7 +22,7 @@ extern void processDragItems(unsigned int windowId, char** arr, int length); - (NSDragOperation)draggingEntered:(id)sender { NSPasteboard *pasteboard = [sender draggingPasteboard]; if ([[pasteboard types] containsObject:NSFilenamesPboardType]) { - processWindowEvent(self.windowId, EventWebViewDraggingEntered); + processWindowEvent(self.windowId, EventWindowFileDraggingEntered); return NSDragOperationCopy; } return NSDragOperationNone; @@ -30,7 +30,7 @@ extern void processDragItems(unsigned int windowId, char** arr, int length); - (void)draggingExited:(id)sender { - NSLog(@"I am here!!!!"); + processWindowEvent(self.windowId, EventWindowFileDraggingExited); } - (BOOL)prepareForDragOperation:(id)sender { @@ -39,6 +39,7 @@ extern void processDragItems(unsigned int windowId, char** arr, int length); - (BOOL)performDragOperation:(id)sender { NSPasteboard *pasteboard = [sender draggingPasteboard]; + processWindowEvent(self.windowId, EventWindowFileDraggingPerformed); if ([[pasteboard types] containsObject:NSFilenamesPboardType]) { NSArray *files = [pasteboard propertyListForType:NSFilenamesPboardType]; NSUInteger count = [files count]; diff --git a/v3/pkg/application/webview_window.go b/v3/pkg/application/webview_window.go index 7bb910213..dc2a7ddbb 100644 --- a/v3/pkg/application/webview_window.go +++ b/v3/pkg/application/webview_window.go @@ -657,8 +657,6 @@ func (w *WebviewWindow) error(message string, args ...any) { } func (w *WebviewWindow) handleDragAndDropMessage(event *dragAndDropMessage) { - println("Drag and drop message received for " + w.Name()) - // Print filenames ctx := newWindowEventContext() ctx.setDroppedFiles(event.filenames) for _, listener := range w.eventListeners[uint(events.FilesDropped)] { diff --git a/v3/pkg/application/webview_window.m b/v3/pkg/application/webview_window.m index 3f33bcca5..be302a135 100644 --- a/v3/pkg/application/webview_window.m +++ b/v3/pkg/application/webview_window.m @@ -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 { if( hasListeners(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 @end diff --git a/v3/pkg/application/webview_window_darwin.go b/v3/pkg/application/webview_window_darwin.go index 7038d6954..4c0877494 100644 --- a/v3/pkg/application/webview_window_darwin.go +++ b/v3/pkg/application/webview_window_darwin.go @@ -18,7 +18,7 @@ package application extern void registerListener(unsigned int event); // 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; if (frameless) { @@ -72,10 +72,12 @@ void* windowNew(unsigned int id, int width, int height, bool fraudulentWebsiteWa delegate.webView = webView; delegate.hideOnClose = false; - WebviewDrag* dragView = [[WebviewDrag alloc] initWithFrame:NSMakeRect(0, 0, width-1, height-1)]; - [view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; - [view addSubview:dragView]; - dragView.windowId = id; + if( enableDragAndDrop ) { + WebviewDrag* dragView = [[WebviewDrag alloc] initWithFrame:NSMakeRect(0, 0, width-1, height-1)]; + [view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; + [view addSubview:dragView]; + dragView.windowId = id; + } return window; } @@ -1064,6 +1066,7 @@ func (w *macosWebviewWindow) run() { C.int(w.parent.options.Height), C.bool(w.parent.options.EnableFraudulentWebsiteWarnings), C.bool(w.parent.options.Frameless), + C.bool(w.parent.options.EnableDragAndDrop), ) w.setTitle(w.parent.options.Title) w.setAlwaysOnTop(w.parent.options.AlwaysOnTop) diff --git a/v3/pkg/events/events.go b/v3/pkg/events/events.go index 2a6238388..b73b9d767 100644 --- a/v3/pkg/events/events.go +++ b/v3/pkg/events/events.go @@ -130,8 +130,9 @@ type macEvents struct { WebViewDidReceiveServerRedirectForProvisionalNavigation WindowEventType WebViewDidFinishNavigation WindowEventType WebViewDidCommitNavigation WindowEventType - WebViewDraggingEntered WindowEventType - WebViewDraggingPerformed WindowEventType + WindowFileDraggingEntered WindowEventType + WindowFileDraggingPerformed WindowEventType + WindowFileDraggingExited WindowEventType } func newMacEvents() macEvents { @@ -256,7 +257,8 @@ func newMacEvents() macEvents { WebViewDidReceiveServerRedirectForProvisionalNavigation: 1141, WebViewDidFinishNavigation: 1142, WebViewDidCommitNavigation: 1143, - WebViewDraggingEntered: 1144, - WebViewDraggingPerformed: 1145, + WindowFileDraggingEntered: 1144, + WindowFileDraggingPerformed: 1145, + WindowFileDraggingExited: 1146, } } diff --git a/v3/pkg/events/events.h b/v3/pkg/events/events.h index 0520a6480..2013d82f2 100644 --- a/v3/pkg/events/events.h +++ b/v3/pkg/events/events.h @@ -126,10 +126,11 @@ extern void processWindowEvent(unsigned int, unsigned int); #define EventWebViewDidReceiveServerRedirectForProvisionalNavigation 1141 #define EventWebViewDidFinishNavigation 1142 #define EventWebViewDidCommitNavigation 1143 -#define EventWebViewDraggingEntered 1144 -#define EventWebViewDraggingPerformed 1145 +#define EventWindowFileDraggingEntered 1144 +#define EventWindowFileDraggingPerformed 1145 +#define EventWindowFileDraggingExited 1146 -#define MAX_EVENTS 1146 +#define MAX_EVENTS 1147 #endif \ No newline at end of file diff --git a/v3/pkg/events/events.txt b/v3/pkg/events/events.txt index 10ce2af5a..333931721 100644 --- a/v3/pkg/events/events.txt +++ b/v3/pkg/events/events.txt @@ -118,7 +118,7 @@ mac:WebViewDidStartProvisionalNavigation mac:WebViewDidReceiveServerRedirectForProvisionalNavigation mac:WebViewDidFinishNavigation mac:WebViewDidCommitNavigation -mac:WebViewDraggingEntered -mac:WebViewDraggingPerformed - +mac:WindowFileDraggingEntered +mac:WindowFileDraggingPerformed +mac:WindowFileDraggingExited