From 4de5e1b9f1bcb09b62a2682786547430eed86f09 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Sat, 24 Jun 2023 13:23:50 +1000 Subject: [PATCH] [v3] Add WindowUnFocus event --- v3/DEVELOPMENT.md | 22 +++++++++++++++++++- v3/pkg/application/webview_window_darwin.go | 5 +++++ v3/pkg/application/webview_window_windows.go | 4 ++++ v3/pkg/events/events.go | 8 ++++--- v3/pkg/events/events.txt | 1 + 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/v3/DEVELOPMENT.md b/v3/DEVELOPMENT.md index 04ec5a8e..1c7a9e36 100644 --- a/v3/DEVELOPMENT.md +++ b/v3/DEVELOPMENT.md @@ -58,4 +58,24 @@ The runtime is located in `v3/internal/runtime`. When the runtime is updated, th ```shell wails task runtime:build -``` \ No newline at end of file +``` + +### Events + +Events are defined in `v3/pkg/events`. When adding a new event, the following steps need to be taken: + +- Add the event to the `events.txt` file +- Run `wails task events:generate` + +There are a number of types of events: platform specific application and window events + common events. The common events are useful for cross-platform event handling, but you aren't limited to the "lowest common denominator". You can use the platform specific events if you need to. + +When adding a common event, ensure that the platform specific events are mapped. An example of this is in `window_webview_darwin.go`: + +```go + // Translate ShouldClose to common WindowClosing event + w.parent.On(events.Mac.WindowShouldClose, func(_ *WindowEventContext) { + w.parent.emit(events.Common.WindowClosing) + }) +``` + +NOTE: We may try to automate this in the future by adding the mapping to the event definition. \ No newline at end of file diff --git a/v3/pkg/application/webview_window_darwin.go b/v3/pkg/application/webview_window_darwin.go index 34841bba..7a073ec7 100644 --- a/v3/pkg/application/webview_window_darwin.go +++ b/v3/pkg/application/webview_window_darwin.go @@ -1070,6 +1070,11 @@ func (w *macosWebviewWindow) run() { w.parent.emit(events.Common.WindowClosing) }) + // Translate WindowDidUnfocus to common WindowUnFocus event + w.parent.On(events.Mac.WindowDidUnfocus, func(_ *WindowEventContext) { + w.parent.emit(events.Common.WindowUnFocus) + }) + if w.parent.options.HTML != "" { w.setHTML(w.parent.options.HTML) } diff --git a/v3/pkg/application/webview_window_windows.go b/v3/pkg/application/webview_window_windows.go index f4f9a4bf..f90e8357 100644 --- a/v3/pkg/application/webview_window_windows.go +++ b/v3/pkg/application/webview_window_windows.go @@ -735,6 +735,10 @@ func (w *windowsWebviewWindow) WndProc(msg uint32, wparam, lparam uintptr) uintp case w32.WM_CLOSE: w.parent.emit(events.Common.WindowClosing) return 0 + case w32.WM_KILLFOCUS: + w.parent.emit(events.Common.WindowUnFocus) + case w32.WM_SETFOCUS: + w.parent.emit(events.Common.WindowFocus) case w32.WM_NCLBUTTONDOWN: w32.SetFocus(w.hwnd) case w32.WM_MOVE, w32.WM_MOVING: diff --git a/v3/pkg/events/events.go b/v3/pkg/events/events.go index fa503014..57a99b7e 100644 --- a/v3/pkg/events/events.go +++ b/v3/pkg/events/events.go @@ -24,6 +24,7 @@ type commonEvents struct { WindowZoomOut WindowEventType WindowZoomReset WindowEventType WindowFocus WindowEventType + WindowUnFocus WindowEventType WindowShow WindowEventType WindowHide WindowEventType WindowDPIChanged WindowEventType @@ -45,9 +46,10 @@ func newCommonEvents() commonEvents { WindowZoomOut: 1165, WindowZoomReset: 1166, WindowFocus: 1167, - WindowShow: 1168, - WindowHide: 1169, - WindowDPIChanged: 1170, + WindowUnFocus: 1168, + WindowShow: 1169, + WindowHide: 1170, + WindowDPIChanged: 1171, } } diff --git a/v3/pkg/events/events.txt b/v3/pkg/events/events.txt index 51af3aae..e2d1f9a8 100644 --- a/v3/pkg/events/events.txt +++ b/v3/pkg/events/events.txt @@ -142,6 +142,7 @@ common:WindowZoomIn common:WindowZoomOut common:WindowZoomReset common:WindowFocus +common:WindowUnFocus common:WindowShow common:WindowHide common:WindowDPIChanged