From 9f567fe2bcc39a95ea3ea80ec34ce60cbf1cb6eb Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Fri, 29 Dec 2023 12:48:26 +1100 Subject: [PATCH] Application: increase message buffer sizes, process all messages in goroutine, Add `IsDebug` to environment info. --- .../runtime/desktop/@wailsio/runtime/wml.js | 28 ++++++++------- v3/pkg/application/application.go | 27 +++++++------- v3/pkg/application/environment.go | 11 ++++-- v3/pkg/application/events.go | 6 ++-- v3/pkg/application/messageprocessor.go | 35 ++----------------- .../messageprocessor_contextmenu.go | 2 +- v3/pkg/application/messageprocessor_dialog.go | 12 +++---- v3/pkg/application/messageprocessor_events.go | 2 +- .../application/messageprocessor_screens.go | 2 +- v3/pkg/application/messageprocessor_system.go | 3 +- v3/pkg/application/messageprocessor_window.go | 2 +- 11 files changed, 56 insertions(+), 74 deletions(-) diff --git a/v3/internal/runtime/desktop/@wailsio/runtime/wml.js b/v3/internal/runtime/desktop/@wailsio/runtime/wml.js index 670362daa..d62b55f81 100644 --- a/v3/internal/runtime/desktop/@wailsio/runtime/wml.js +++ b/v3/internal/runtime/desktop/@wailsio/runtime/wml.js @@ -49,22 +49,23 @@ function addWMLEventListeners() { }); } + /** - * Calls a method on the window object. - * - * @param {string} method - The name of the method to call on the window object. - * - * @return {void} + * Calls a method on a specified window. + * @param {string} windowName - The name of the window to call the method on. + * @param {string} method - The name of the method to call. */ -function callWindowMethod(method) { - // TODO: Make this a parameter! - let windowName = ''; - let targetWindow = Get(''); +function callWindowMethod(windowName, method) { + let targetWindow = Get(windowName); let methodMap = WindowMethods(targetWindow); if (!methodMap.has(method)) { console.log("Window method " + method + " not found"); } - methodMap.get(method)(); + try { + methodMap.get(method)(); + } catch (e) { + console.error("Error calling window method '" + method + "': " + e); + } } /** @@ -78,18 +79,19 @@ function addWMLWindowListeners() { elements.forEach(function (element) { const windowMethod = element.getAttribute('wml-window'); const confirm = element.getAttribute('wml-confirm'); - const trigger = element.getAttribute('wml-trigger') || "click"; + const trigger = element.getAttribute('wml-trigger') || 'click'; + const targetWindow = element.getAttribute('wml-target-window') || ''; let callback = function () { if (confirm) { Question({Title: "Confirm", Message:confirm, Buttons:[{Label:"Yes"},{Label:"No", IsDefault:true}]}).then(function (result) { if (result !== "No") { - callWindowMethod(windowMethod); + callWindowMethod(targetWindow, windowMethod); } }); return; } - callWindowMethod(windowMethod); + callWindowMethod(targetWindow, windowMethod); }; // Remove existing listeners diff --git a/v3/pkg/application/application.go b/v3/pkg/application/application.go index afa612531..20f0b30dc 100644 --- a/v3/pkg/application/application.go +++ b/v3/pkg/application/application.go @@ -179,14 +179,14 @@ type windowMessage struct { message string } -var windowMessageBuffer = make(chan *windowMessage) +var windowMessageBuffer = make(chan *windowMessage, 5) type dragAndDropMessage struct { windowId uint filenames []string } -var windowDragAndDropBuffer = make(chan *dragAndDropMessage) +var windowDragAndDropBuffer = make(chan *dragAndDropMessage, 5) func addDragAndDropMessage(windowId uint, filenames []string) { windowDragAndDropBuffer <- &dragAndDropMessage{ @@ -206,7 +206,7 @@ type webViewAssetRequest struct { windowName string } -var windowKeyEvents = make(chan *windowKeyEvent) +var windowKeyEvents = make(chan *windowKeyEvent, 5) type windowKeyEvent struct { windowId uint @@ -224,7 +224,7 @@ func (r *webViewAssetRequest) Header() (http.Header, error) { return hh, nil } -var webviewRequests = make(chan *webViewAssetRequest) +var webviewRequests = make(chan *webViewAssetRequest, 5) type eventHook struct { callback func(event *Event) @@ -435,44 +435,44 @@ func (a *App) Run() error { go func() { for { event := <-applicationEvents - a.handleApplicationEvent(event) + go a.handleApplicationEvent(event) } }() go func() { for { event := <-windowEvents - a.handleWindowEvent(event) + go a.handleWindowEvent(event) } }() go func() { for { request := <-webviewRequests - a.handleWebViewRequest(request) + go a.handleWebViewRequest(request) } }() go func() { for { event := <-windowMessageBuffer - a.handleWindowMessage(event) + go a.handleWindowMessage(event) } }() go func() { for { event := <-windowKeyEvents - a.handleWindowKeyEvent(event) + go a.handleWindowKeyEvent(event) } }() go func() { for { dragAndDropMessage := <-windowDragAndDropBuffer - a.handleDragAndDropMessage(dragAndDropMessage) + go a.handleDragAndDropMessage(dragAndDropMessage) } }() go func() { for { menuItemID := <-menuItemClicked - a.handleMenuItemClicked(menuItemID) + go a.handleMenuItemClicked(menuItemID) } }() @@ -828,7 +828,8 @@ func (a *App) BrowserOpenFile(path string) error { func (a *App) Environment() EnvironmentInfo { return EnvironmentInfo{ - OS: runtime.GOOS, - Arch: runtime.GOARCH, + OS: runtime.GOOS, + Arch: runtime.GOARCH, + Debug: a.isDebugMode, } } diff --git a/v3/pkg/application/environment.go b/v3/pkg/application/environment.go index dbc7f3ced..4a0f303d9 100644 --- a/v3/pkg/application/environment.go +++ b/v3/pkg/application/environment.go @@ -1,6 +1,13 @@ package application +// EnvironmentInfo represents information about the current environment. +// +// Fields: +// - OS: the operating system that the program is running on. +// - Arch: the architecture of the operating system. +// - Debug: indicates whether debug mode is enabled. type EnvironmentInfo struct { - OS string - Arch string + OS string + Arch string + Debug bool } diff --git a/v3/pkg/application/events.go b/v3/pkg/application/events.go index e1e0b899d..bbb6bc8ba 100644 --- a/v3/pkg/application/events.go +++ b/v3/pkg/application/events.go @@ -29,16 +29,16 @@ func (w *Event) Cancel() { w.Cancelled = true } -var applicationEvents = make(chan *Event) +var applicationEvents = make(chan *Event, 5) type windowEvent struct { WindowID uint EventID uint } -var windowEvents = make(chan *windowEvent) +var windowEvents = make(chan *windowEvent, 5) -var menuItemClicked = make(chan uint) +var menuItemClicked = make(chan uint, 5) type WailsEvent struct { Name string `json:"name"` diff --git a/v3/pkg/application/messageprocessor.go b/v3/pkg/application/messageprocessor.go index 1eef916c0..1f21c3ab2 100644 --- a/v3/pkg/application/messageprocessor.go +++ b/v3/pkg/application/messageprocessor.go @@ -65,41 +65,12 @@ func (m *MessageProcessor) getTargetWindow(r *http.Request) (Window, string) { func (m *MessageProcessor) HandleRuntimeCall(rw http.ResponseWriter, r *http.Request) { object := r.URL.Query().Get("object") - if object != "" { - m.HandleRuntimeCallWithIDs(rw, r) + if object == "" { + m.httpError(rw, "Invalid runtime call") return } - //// Read "method" from query string - //method := r.URL.Query().Get("method") - //if method == "" { - // m.httpError(rw, "No method specified") - // return - //} - //splitMethod := strings.Split(method, ".") - //if len(splitMethod) != 2 { - // m.httpError(rw, "Invalid method format") - // return - //} - //// Get the object - //object = splitMethod[0] - //// Get the method - //method = splitMethod[1] - // - //params := QueryParams(r.URL.Query()) - // - //targetWindow := m.getTargetWindow(r) - //if targetWindow == nil { - // m.httpError(rw, "No valid window found") - // return - //} - // - //switch object { - //case "call": - // m.processCallMethod(method, rw, r, targetWindow, params) - //default: - // m.httpError(rw, "Unknown runtime call: %s", object) - //} + m.HandleRuntimeCallWithIDs(rw, r) } func (m *MessageProcessor) HandleRuntimeCallWithIDs(rw http.ResponseWriter, r *http.Request) { diff --git a/v3/pkg/application/messageprocessor_contextmenu.go b/v3/pkg/application/messageprocessor_contextmenu.go index 8b655d0eb..c7c8a86a8 100644 --- a/v3/pkg/application/messageprocessor_contextmenu.go +++ b/v3/pkg/application/messageprocessor_contextmenu.go @@ -35,6 +35,6 @@ func (m *MessageProcessor) processContextMenuMethod(method int, rw http.Response m.httpError(rw, "Unknown contextmenu method: %d", method) } - m.Info("Runtime:", "method", "ContextMenu."+contextmenuMethodNames[method]) + m.Info("Runtime Call:", "method", "ContextMenu."+contextmenuMethodNames[method]) } diff --git a/v3/pkg/application/messageprocessor_dialog.go b/v3/pkg/application/messageprocessor_dialog.go index 9c8dd73f9..bbddd4e6b 100644 --- a/v3/pkg/application/messageprocessor_dialog.go +++ b/v3/pkg/application/messageprocessor_dialog.go @@ -91,7 +91,7 @@ func (m *MessageProcessor) processDialogMethod(method int, rw http.ResponseWrite dialog.AddButtons(options.Buttons) dialog.Show() m.ok(rw) - m.Info("Runtime:", "method", methodName, "options", options) + m.Info("Runtime Call:", "method", methodName, "options", options) case DialogOpenFile: var options OpenFileDialogOptions @@ -119,7 +119,7 @@ func (m *MessageProcessor) processDialogMethod(method int, rw http.ResponseWrite return } m.dialogCallback(window, dialogID, string(result), true) - m.Info("Runtime:", "method", methodName, "result", result) + m.Info("Runtime Call:", "method", methodName, "result", result) } } else { file, err := dialog.PromptForSingleSelection() @@ -128,11 +128,11 @@ func (m *MessageProcessor) processDialogMethod(method int, rw http.ResponseWrite return } m.dialogCallback(window, dialogID, file, false) - m.Info("Runtime:", "method", methodName, "result", file) + m.Info("Runtime Call:", "method", methodName, "result", file) } }() m.ok(rw) - m.Info("Runtime:", "method", methodName, "options", options) + m.Info("Runtime Call:", "method", methodName, "options", options) case DialogSaveFile: var options SaveFileDialogOptions @@ -154,10 +154,10 @@ func (m *MessageProcessor) processDialogMethod(method int, rw http.ResponseWrite return } m.dialogCallback(window, dialogID, file, false) - m.Info("Runtime:", "method", methodName, "result", file) + m.Info("Runtime Call:", "method", methodName, "result", file) }() m.ok(rw) - m.Info("Runtime:", "method", methodName, "options", options) + m.Info("Runtime Call:", "method", methodName, "options", options) default: m.httpError(rw, "Unknown dialog method: %d", method) diff --git a/v3/pkg/application/messageprocessor_events.go b/v3/pkg/application/messageprocessor_events.go index 7831f94d1..ce5ab553e 100644 --- a/v3/pkg/application/messageprocessor_events.go +++ b/v3/pkg/application/messageprocessor_events.go @@ -35,6 +35,6 @@ func (m *MessageProcessor) processEventsMethod(method int, rw http.ResponseWrite return } - m.Info("Runtime:", "method", "Events."+eventsMethodNames[method], "name", event.Name, "sender", event.Sender, "data", event.Data, "cancelled", event.Cancelled) + m.Info("Runtime Call:", "method", "Events."+eventsMethodNames[method], "name", event.Name, "sender", event.Sender, "data", event.Data, "cancelled", event.Cancelled) } diff --git a/v3/pkg/application/messageprocessor_screens.go b/v3/pkg/application/messageprocessor_screens.go index 4d379a5cb..7339ebd35 100644 --- a/v3/pkg/application/messageprocessor_screens.go +++ b/v3/pkg/application/messageprocessor_screens.go @@ -44,6 +44,6 @@ func (m *MessageProcessor) processScreensMethod(method int, rw http.ResponseWrit m.httpError(rw, "Unknown screens method: %d", method) } - m.Info("Runtime:", "method", "Screens."+screensMethodNames[method]) + m.Info("Runtime Call:", "method", "Screens."+screensMethodNames[method]) } diff --git a/v3/pkg/application/messageprocessor_system.go b/v3/pkg/application/messageprocessor_system.go index 4930161eb..6375176e0 100644 --- a/v3/pkg/application/messageprocessor_system.go +++ b/v3/pkg/application/messageprocessor_system.go @@ -11,6 +11,7 @@ const ( var systemMethodNames = map[int]string{ SystemIsDarkMode: "IsDarkMode", + Environment: "Environment", } func (m *MessageProcessor) processSystemMethod(method int, rw http.ResponseWriter, r *http.Request, window Window, params QueryParams) { @@ -24,6 +25,6 @@ func (m *MessageProcessor) processSystemMethod(method int, rw http.ResponseWrite m.httpError(rw, "Unknown system method: %d", method) } - m.Info("Runtime:", "method", "System."+systemMethodNames[method]) + m.Info("Runtime Call:", "method", "System."+systemMethodNames[method]) } diff --git a/v3/pkg/application/messageprocessor_window.go b/v3/pkg/application/messageprocessor_window.go index d5463c35f..aa69a9e77 100644 --- a/v3/pkg/application/messageprocessor_window.go +++ b/v3/pkg/application/messageprocessor_window.go @@ -255,5 +255,5 @@ func (m *MessageProcessor) processWindowMethod(method int, rw http.ResponseWrite m.httpError(rw, "Unknown window method id: %d", method) } - m.Info("Runtime:", "method", "Window."+windowMethodNames[method]) + m.Info("Runtime Call:", "method", "Window."+windowMethodNames[method]) }