diff --git a/v2/internal/frontend/desktop/windows/frontend.go b/v2/internal/frontend/desktop/windows/frontend.go index 6dc2f9a60..9f81dcede 100644 --- a/v2/internal/frontend/desktop/windows/frontend.go +++ b/v2/internal/frontend/desktop/windows/frontend.go @@ -7,6 +7,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/wailsapp/wails/v2/internal/frontend/desktop/windows/win32" "io" "log" "net/http" @@ -589,30 +590,54 @@ func (f *Frontend) navigationCompleted(sender *edge.ICoreWebView2, args *edge.IC switch f.frontendOptions.WindowStartState { case options.Maximised: if !f.frontendOptions.DisableResize { - f.mainWindow.Maximise() + win32.ShowWindowMaximised(f.mainWindow.Handle()) } else { - f.mainWindow.Show() + win32.ShowWindow(f.mainWindow.Handle()) } - f.ShowWindow() - case options.Minimised: - f.mainWindow.Minimise() + win32.ShowWindowMinimised(f.mainWindow.Handle()) case options.Fullscreen: f.mainWindow.Fullscreen() - f.ShowWindow() + win32.ShowWindow(f.mainWindow.Handle()) default: if f.frontendOptions.Fullscreen { f.mainWindow.Fullscreen() } - f.ShowWindow() + win32.ShowWindow(f.mainWindow.Handle()) } + f.mainWindow.hasBeenShown = true + } func (f *Frontend) ShowWindow() { f.mainWindow.Invoke(func() { - if f.mainWindow.IsMinimised() { - f.mainWindow.Restore() + if !f.mainWindow.hasBeenShown { + f.mainWindow.hasBeenShown = true + switch f.frontendOptions.WindowStartState { + case options.Maximised: + if !f.frontendOptions.DisableResize { + win32.ShowWindowMaximised(f.mainWindow.Handle()) + } else { + win32.ShowWindow(f.mainWindow.Handle()) + } + case options.Minimised: + win32.RestoreWindow(f.mainWindow.Handle()) + case options.Fullscreen: + f.mainWindow.Fullscreen() + win32.ShowWindow(f.mainWindow.Handle()) + default: + if f.frontendOptions.Fullscreen { + f.mainWindow.Fullscreen() + } + win32.ShowWindow(f.mainWindow.Handle()) + } + } else { + if win32.IsWindowMinimised(f.mainWindow.Handle()) { + win32.RestoreWindow(f.mainWindow.Handle()) + } else { + win32.ShowWindow(f.mainWindow.Handle()) + } } w32.SetForegroundWindow(f.mainWindow.Handle()) w32.SetFocus(f.mainWindow.Handle()) diff --git a/v2/internal/frontend/desktop/windows/win32/consts.go b/v2/internal/frontend/desktop/windows/win32/consts.go index f84bb43ef..cc0a98a71 100644 --- a/v2/internal/frontend/desktop/windows/win32/consts.go +++ b/v2/internal/frontend/desktop/windows/win32/consts.go @@ -15,6 +15,7 @@ var ( moduser32 = syscall.NewLazyDLL("user32.dll") procSystemParametersInfo = moduser32.NewProc("SystemParametersInfoW") procGetWindowLong = moduser32.NewProc("GetWindowLongW") + procShowWindow = moduser32.NewProc("ShowWindow") ) var ( moddwmapi = syscall.NewLazyDLL("dwmapi.dll") diff --git a/v2/internal/frontend/desktop/windows/win32/window.go b/v2/internal/frontend/desktop/windows/win32/window.go index ebcd3e389..3ad15d3c1 100644 --- a/v2/internal/frontend/desktop/windows/win32/window.go +++ b/v2/internal/frontend/desktop/windows/win32/window.go @@ -16,6 +16,23 @@ const ( GWL_STYLE = -16 ) +const ( + SW_HIDE = 0 + SW_NORMAL = 1 + SW_SHOWNORMAL = 1 + SW_SHOWMINIMIZED = 2 + SW_MAXIMIZE = 3 + SW_SHOWMAXIMIZED = 3 + SW_SHOWNOACTIVATE = 4 + SW_SHOW = 5 + SW_MINIMIZE = 6 + SW_SHOWMINNOACTIVE = 7 + SW_SHOWNA = 8 + SW_RESTORE = 9 + SW_SHOWDEFAULT = 10 + SW_FORCEMINIMIZE = 11 +) + // http://msdn.microsoft.com/en-us/library/windows/desktop/bb773244.aspx type MARGINS struct { CxLeftWidth, CxRightWidth, CyTopHeight, CyBottomHeight int32 @@ -42,6 +59,21 @@ func IsWindowMinimised(hwnd uintptr) bool { return style&WS_MINIMIZE != 0 } +func RestoreWindow(hwnd uintptr) { + showWindow(hwnd, SW_RESTORE) +} + +func ShowWindow(hwnd uintptr) { + showWindow(hwnd, SW_SHOW) +} + +func ShowWindowMaximised(hwnd uintptr) { + showWindow(hwnd, SW_MAXIMIZE) +} +func ShowWindowMinimised(hwnd uintptr) { + showWindow(hwnd, SW_MINIMIZE) +} + func dwmExtendFrameIntoClientArea(hwnd uintptr, margins *MARGINS) error { ret, _, _ := procDwmExtendFrameIntoClientArea.Call( hwnd, @@ -61,3 +93,11 @@ func getWindowLong(hwnd uintptr, index int) int32 { return int32(ret) } + +func showWindow(hwnd uintptr, cmdshow int) bool { + ret, _, _ := procShowWindow.Call( + hwnd, + uintptr(cmdshow)) + + return ret != 0 +} diff --git a/v2/internal/frontend/desktop/windows/window.go b/v2/internal/frontend/desktop/windows/window.go index 3b9688567..6948cff5d 100644 --- a/v2/internal/frontend/desktop/windows/window.go +++ b/v2/internal/frontend/desktop/windows/window.go @@ -22,6 +22,7 @@ type Window struct { versionInfo *operatingsystem.WindowsVersionInfo isDarkMode bool isActive bool + hasBeenShown bool } func NewWindow(parent winc.Controller, appoptions *options.App, versionInfo *operatingsystem.WindowsVersionInfo) *Window {