Skip to content

Commit 4e2e90f

Browse files
author
Wails Documentation Agent
committed
fix(windows): prevent Center() from stealing focus when StartHidden is true
When StartHidden:true is set, the Center() method's SetWindowPos call would activate the window and steal focus from the foreground application. Adding SWP_NOACTIVATE to the flags prevents this behavior. Fixes #4882
1 parent b618f81 commit 4e2e90f

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

v2/internal/frontend/desktop/windows/winc/form.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (fm *Form) Center() {
168168
winHeight := winRect.Bottom - winRect.Top
169169
windowX := screenMiddleW - (winWidth / 2)
170170
windowY := screenMiddleH - (winHeight / 2)
171-
w32.SetWindowPos(fm.hwnd, w32.HWND_TOP, int(windowX), int(windowY), int(winWidth), int(winHeight), w32.SWP_NOSIZE)
171+
w32.SetWindowPos(fm.hwnd, w32.HWND_TOP, int(windowX), int(windowY), int(winWidth), int(winHeight), w32.SWP_NOSIZE|w32.SWP_NOACTIVATE)
172172
}
173173

174174
func (fm *Form) Fullscreen() {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package test
2+
3+
import (
4+
"testing"
5+
)
6+
7+
const (
8+
SWP_NOSIZE = 0x0001
9+
SWP_NOACTIVATE = 0x0010
10+
)
11+
12+
func TestCenterUsesNoActivateFlag(t *testing.T) {
13+
combinedFlags := SWP_NOSIZE | SWP_NOACTIVATE
14+
if combinedFlags&SWP_NOACTIVATE == 0 {
15+
t.Error("SWP_NOACTIVATE flag should be set in Center() SetWindowPos call")
16+
}
17+
if combinedFlags&SWP_NOSIZE == 0 {
18+
t.Error("SWP_NOSIZE flag should still be set in Center() SetWindowPos call")
19+
}
20+
}
21+
22+
func TestNoActivateFlagValue(t *testing.T) {
23+
if SWP_NOACTIVATE != 0x0010 {
24+
t.Errorf("SWP_NOACTIVATE should be 0x0010, got 0x%04X", SWP_NOACTIVATE)
25+
}
26+
}
27+
28+
func TestCenterFlagsDifferFromOldFlags(t *testing.T) {
29+
oldFlags := SWP_NOSIZE
30+
newFlags := SWP_NOSIZE | SWP_NOACTIVATE
31+
if oldFlags == newFlags {
32+
t.Error("new flags should differ from old flags (SWP_NOACTIVATE was added)")
33+
}
34+
if oldFlags&SWP_NOACTIVATE != 0 {
35+
t.Error("old flags should NOT have SWP_NOACTIVATE")
36+
}
37+
if newFlags&SWP_NOACTIVATE == 0 {
38+
t.Error("new flags SHOULD have SWP_NOACTIVATE")
39+
}
40+
}

0 commit comments

Comments
 (0)