From 7f3fdd697722316643c1b49a777302fd197e8fb6 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Sun, 30 Apr 2023 09:49:50 +1000 Subject: [PATCH] [v3 windows] Add `WndProcInterceptor` for custom message processing --- v3/V3 Changes.md | 14 ++++++++++++++ v3/pkg/application/application_windows.go | 9 +++++++++ v3/pkg/application/options_application.go | 1 + v3/pkg/application/options_win.go | 8 ++++++++ 4 files changed, 32 insertions(+) diff --git a/v3/V3 Changes.md b/v3/V3 Changes.md index 9ec98b8e9..f391029cf 100644 --- a/v3/V3 Changes.md +++ b/v3/V3 Changes.md @@ -202,3 +202,17 @@ On Windows, if the `BackgroundType` is set to `BackgroundTypeTranslucent`, the t - `Tabbed` - The window will use the tabbed effect +## Windows Application Options + +### WndProcInterceptor + +If this is set, the WndProc will be intercepted and the function will be called. This allows you to handle Windows +messages directly. The function should have the following signature: + +```go +func(hwnd uintptr, msg uint32, wParam, lParam uintptr) (returnValue uintptr, shouldReturn) +``` + +The `shouldReturn` value should be set to `true` if the returnValue should be returned by the main wndProc method. +If it is set to `false`, the return value will be ignored and the message will continue to be processed by the main +wndProc method. \ No newline at end of file diff --git a/v3/pkg/application/application_windows.go b/v3/pkg/application/application_windows.go index 7253d7cbc..82c65abf1 100644 --- a/v3/pkg/application/application_windows.go +++ b/v3/pkg/application/application_windows.go @@ -128,6 +128,15 @@ func (m *windowsApp) wndProc(hwnd w32.HWND, msg uint32, wParam, lParam uintptr) m.invokeCallback(wParam, lParam) return 0 } + + // If the WndProcInterceptor is set in options, pass the message on + if m.parent.options.Windows.WndProcInterceptor != nil { + returnValue, shouldReturn := m.parent.options.Windows.WndProcInterceptor(hwnd, msg, wParam, lParam) + if shouldReturn { + return returnValue + } + } + switch msg { case w32.WM_SETTINGCHANGE: settingChanged := w32.UTF16PtrToString((*uint16)(unsafe.Pointer(lParam))) diff --git a/v3/pkg/application/options_application.go b/v3/pkg/application/options_application.go index 0a768a0b7..f37b2dc7e 100644 --- a/v3/pkg/application/options_application.go +++ b/v3/pkg/application/options_application.go @@ -12,6 +12,7 @@ type Options struct { Description string Icon []byte Mac MacOptions + Windows WindowsApplicationOptions Bind []any Logger struct { Silent bool diff --git a/v3/pkg/application/options_win.go b/v3/pkg/application/options_win.go index 55f69a7e2..e060d9e9c 100644 --- a/v3/pkg/application/options_win.go +++ b/v3/pkg/application/options_win.go @@ -1,5 +1,13 @@ package application +type WindowsApplicationOptions struct { + // WndProcInterceptor is a function that will be called for every message sent in the application. + // Use this to hook into the main message loop. This is useful for handling custom window messages. + // If `shouldReturn` is `true` then `returnCode` will be returned by the main message loop. + // If `shouldReturn` is `false` then returnCode will be ignored and the message will be processed by the main message loop. + WndProcInterceptor func(hwnd uintptr, msg uint32, wParam, lParam uintptr) (returnCode uintptr, shouldReturn bool) +} + type BackdropType int32 const (