From 23bfeac02a6600a4b945cf8da3deed04bc4c24b2 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Sun, 12 Feb 2023 07:58:14 +1100 Subject: [PATCH] Window event callbacks now take a WindowEventContext Improved event example --- v3/examples/events/assets/index.html | 9 ++------- v3/pkg/application/context_window_event.go | 14 ++++++++++++++ v3/pkg/application/webview_window.go | 8 ++++---- v3/pkg/application/webview_window_darwin.go | 2 +- 4 files changed, 21 insertions(+), 12 deletions(-) create mode 100644 v3/pkg/application/context_window_event.go diff --git a/v3/examples/events/assets/index.html b/v3/examples/events/assets/index.html index df15c1c50..20993e735 100644 --- a/v3/examples/events/assets/index.html +++ b/v3/examples/events/assets/index.html @@ -4,14 +4,12 @@ Title -

Events Demo


-
-
-
+The main program emits an event every 10s which will be displayed in the section below. +To send an event from this window, click here:
@@ -20,9 +18,6 @@ let currentHTML = document.getElementById("results").innerHTML; document.getElementById("results").innerHTML = currentHTML + "
" + JSON.stringify(data); }) - window.addEventListener("dragstart", (event) => - event.dataTransfer.setData("text/plain", "This text may be dragged") - ); \ No newline at end of file diff --git a/v3/pkg/application/context_window_event.go b/v3/pkg/application/context_window_event.go new file mode 100644 index 000000000..ca16b28f4 --- /dev/null +++ b/v3/pkg/application/context_window_event.go @@ -0,0 +1,14 @@ +package application + +var blankWindowEventContext = &WindowEventContext{} + +type WindowEventContext struct { + // contains filtered or unexported fields + data map[string]any +} + +func newWindowEventContext() *Context { + return &Context{ + data: make(map[string]any), + } +} diff --git a/v3/pkg/application/webview_window.go b/v3/pkg/application/webview_window.go index 5e416f9cc..63fce5262 100644 --- a/v3/pkg/application/webview_window.go +++ b/v3/pkg/application/webview_window.go @@ -75,7 +75,7 @@ type WebviewWindow struct { assets *assetserver.AssetServer messageProcessor *MessageProcessor - eventListeners map[uint][]func() + eventListeners map[uint][]func(ctx *WindowEventContext) eventListenersLock sync.RWMutex contextMenus map[string]*Menu @@ -113,7 +113,7 @@ func NewWindow(options *WebviewWindowOptions) *WebviewWindow { result := &WebviewWindow{ id: getWindowID(), options: options, - eventListeners: make(map[uint][]func()), + eventListeners: make(map[uint][]func(ctx *WindowEventContext)), contextMenus: make(map[string]*Menu), assets: srv, @@ -400,7 +400,7 @@ func (w *WebviewWindow) Center() { w.impl.center() } -func (w *WebviewWindow) On(eventType events.WindowEventType, callback func()) { +func (w *WebviewWindow) On(eventType events.WindowEventType, callback func(ctx *WindowEventContext)) { eventID := uint(eventType) w.eventListenersLock.Lock() defer w.eventListenersLock.Unlock() @@ -413,7 +413,7 @@ func (w *WebviewWindow) On(eventType events.WindowEventType, callback func()) { func (w *WebviewWindow) handleWindowEvent(id uint) { w.eventListenersLock.RLock() for _, callback := range w.eventListeners[id] { - go callback() + go callback(blankWindowEventContext) } w.eventListenersLock.RUnlock() } diff --git a/v3/pkg/application/webview_window_darwin.go b/v3/pkg/application/webview_window_darwin.go index c5aed5bfb..7038d6954 100644 --- a/v3/pkg/application/webview_window_darwin.go +++ b/v3/pkg/application/webview_window_darwin.go @@ -1122,7 +1122,7 @@ func (w *macosWebviewWindow) run() { w.setURL(w.parent.options.URL) } // We need to wait for the HTML to load before we can execute the javascript - w.parent.On(events.Mac.WebViewDidFinishNavigation, func() { + w.parent.On(events.Mac.WebViewDidFinishNavigation, func(_ *WindowEventContext) { if w.parent.options.JS != "" { w.execJS(w.parent.options.JS) }