From cc59655cb163e0acd6f3a59bb00de987aca7126c Mon Sep 17 00:00:00 2001 From: stffabi Date: Fri, 12 May 2023 08:03:36 +0200 Subject: [PATCH] [v3 windows] Add close handling: HideOnClose, QuitOnLastWindowClosed and DisableQuitOnLastWindowClosed --- v3/STATUS.md | 4 ++-- v3/pkg/application/application_windows.go | 2 +- v3/pkg/application/options_win.go | 3 +++ v3/pkg/application/webview_window_windows.go | 10 +++++++--- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/v3/STATUS.md b/v3/STATUS.md index 3f1e3678..4993cf68 100644 --- a/v3/STATUS.md +++ b/v3/STATUS.md @@ -164,7 +164,7 @@ An 'X' indicates that the option is not supported by the platform. | AlwaysOnTop | Y | | | | | URL | | | | | | DisableResize | Y | | | | -| Frameless | | | | | +| Frameless | Y | | | | | MinWidth | Y | | | | | MinHeight | Y | | | | | MaxWidth | Y | | | | @@ -178,7 +178,7 @@ An 'X' indicates that the option is not supported by the platform. | CSS | | | | | | X | | | | | | Y | | | | | -| HideOnClose | | | | | +| HideOnClose | Y | | | | | FullscreenButtonEnabled | | | | | | Hidden | Y | | | | | EnableFraudulentWebsiteWarnings | | | | | diff --git a/v3/pkg/application/application_windows.go b/v3/pkg/application/application_windows.go index 77d614b6..7a045007 100644 --- a/v3/pkg/application/application_windows.go +++ b/v3/pkg/application/application_windows.go @@ -220,7 +220,7 @@ func (m *windowsApp) unregisterWindow(w *windowsWebviewWindow) { delete(m.windowMap, w.hwnd) // If this was the last window... - if len(m.windowMap) == 0 { + if len(m.windowMap) == 0 && !m.parent.options.Windows.DisableQuitOnLastWindowClosed { w32.PostQuitMessage(0) } } diff --git a/v3/pkg/application/options_win.go b/v3/pkg/application/options_win.go index 6e640d3e..25003fde 100644 --- a/v3/pkg/application/options_win.go +++ b/v3/pkg/application/options_win.go @@ -6,6 +6,9 @@ type WindowsApplicationOptions struct { // 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) + + // DisableQuitOnLastWindowClosed disables the auto quit of the application if the last window has been closed. + DisableQuitOnLastWindowClosed bool } type BackdropType int32 diff --git a/v3/pkg/application/webview_window_windows.go b/v3/pkg/application/webview_window_windows.go index fa92ffa1..d9847d57 100644 --- a/v3/pkg/application/webview_window_windows.go +++ b/v3/pkg/application/webview_window_windows.go @@ -5,11 +5,12 @@ package application import ( "errors" "fmt" - "github.com/samber/lo" "strconv" "unicode/utf16" "unsafe" + "github.com/samber/lo" + "github.com/wailsapp/wails/v3/pkg/events" "github.com/wailsapp/wails/v3/pkg/w32" ) @@ -609,11 +610,14 @@ func (w *windowsWebviewWindow) WndProc(msg uint32, wparam, lparam uintptr) uintp case w32.WM_SIZE: return 0 case w32.WM_CLOSE: - w32.PostMessage(w.hwnd, w32.WM_QUIT, 0, 0) + if w.parent.options.HideOnClose { + w.hide() + return 0 // Do not let the DefWindowProc allow to close us + } + // Unregister the window with the application windowsApp := globalApplication.impl.(*windowsApp) windowsApp.unregisterWindow(w) - return 0 case w32.WM_GETMINMAXINFO: mmi := (*w32.MINMAXINFO)(unsafe.Pointer(lparam)) hasConstraints := false