gui/window (Windows): give the keyboard focus to WebView2

The host window never moved the focus into the WebView2 control, so a
page got no keys after the start or after Alt-Tab until it was
clicked. Move it on WM_SETFOCUS and once the control exists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
terminarandClaude Opus 5.5 committed 2026-11-29 21:34:08 +01:00
1 parent 5dd4a86b68
commit 586e76889c
1 file changed
+10
+10
View File
@@ -67,6 +67,7 @@ const (
wmDestroy = 0x0002 wmDestroy = 0x0002
wmMove = 0x0003 wmMove = 0x0003
wmSize = 0x0005 wmSize = 0x0005
wmSetFocus = 0x0007
wmClose = 0x0010 wmClose = 0x0010
wmQuit = 0x0012 wmQuit = 0x0012
wmMoving = 0x0216 wmMoving = 0x0216
@@ -193,6 +194,7 @@ func newBackend(opt Options) (backend, string, string, error) {
return nil, "", "", errors.New("window: WebView2 could not be created (is the Microsoft Edge WebView2 Runtime installed?)") return nil, "", "", errors.New("window: WebView2 could not be created (is the Microsoft Edge WebView2 Runtime installed?)")
} }
c.Resize() c.Resize()
c.Focus() // the window got WM_SETFOCUS before the control existed
if s, err := c.GetSettings(); err == nil { if s, err := c.GetSettings(); err == nil {
_ = s.PutAreDevToolsEnabled(opt.Debug) _ = s.PutAreDevToolsEnabled(opt.Debug)
_ = s.PutAreDefaultContextMenusEnabled(opt.Debug) _ = s.PutAreDefaultContextMenusEnabled(opt.Debug)
@@ -220,6 +222,14 @@ func wndproc(hwnd, msg, wp, lp uintptr) uintptr {
_ = b.chromium.NotifyParentWindowPositionChanged() _ = b.chromium.NotifyParentWindowPositionChanged()
} }
b.reportFrame() b.reportFrame()
case wmSetFocus:
// The keyboard focus belongs in the WebView2 control (its own
// child window): without this a page gets no keys after the
// start or after Alt-Tab until it is clicked.
if b.chromium != nil {
b.chromium.Focus()
}
return 0
case wmApp: case wmApp:
b.runQueue() b.runQueue()
return 0 return 0