diff --git a/v3/examples/binding/assets/index.html b/v3/examples/binding/assets/index.html index 692c3d8db..e437080e4 100644 --- a/v3/examples/binding/assets/index.html +++ b/v3/examples/binding/assets/index.html @@ -77,7 +77,7 @@
Please enter your name below 👇
- +
diff --git a/v3/pkg/application/options_webview_window.go b/v3/pkg/application/options_webview_window.go index daabe69f8..d2bccba45 100644 --- a/v3/pkg/application/options_webview_window.go +++ b/v3/pkg/application/options_webview_window.go @@ -100,9 +100,6 @@ type WebviewWindowOptions struct { // Windows options Windows WindowsWindow - // Focused indicates the window should be focused when initially shown - Focused bool - // ShouldClose is called when the window is about to close. // Return true to allow the window to close, or false to prevent it from closing. ShouldClose func(window *WebviewWindow) bool diff --git a/v3/pkg/application/webview_window.go b/v3/pkg/application/webview_window.go index a17a40676..e145d916b 100644 --- a/v3/pkg/application/webview_window.go +++ b/v3/pkg/application/webview_window.go @@ -4,11 +4,12 @@ import ( "encoding/json" "errors" "fmt" - "github.com/leaanthony/u" "runtime" "strings" "sync" + "github.com/leaanthony/u" + "github.com/samber/lo" "github.com/wailsapp/wails/v3/pkg/events" ) @@ -1050,10 +1051,6 @@ func (w *WebviewWindow) NativeWindowHandle() (uintptr, error) { } func (w *WebviewWindow) Focus() { - if w.impl == nil { - w.options.Focused = true - return - } InvokeSync(w.impl.focus) w.emit(events.Common.WindowFocus) } diff --git a/v3/pkg/application/webview_window_windows.go b/v3/pkg/application/webview_window_windows.go index 22d23bfe9..bbe99aa12 100644 --- a/v3/pkg/application/webview_window_windows.go +++ b/v3/pkg/application/webview_window_windows.go @@ -5,11 +5,6 @@ package application import ( "errors" "fmt" - "github.com/bep/debounce" - "github.com/wailsapp/go-webview2/webviewloader" - "github.com/wailsapp/wails/v3/internal/assetserver" - "github.com/wailsapp/wails/v3/internal/assetserver/webview" - "github.com/wailsapp/wails/v3/internal/capabilities" "net/url" "path" "strconv" @@ -20,6 +15,12 @@ import ( "unicode/utf16" "unsafe" + "github.com/bep/debounce" + "github.com/wailsapp/go-webview2/webviewloader" + "github.com/wailsapp/wails/v3/internal/assetserver" + "github.com/wailsapp/wails/v3/internal/assetserver/webview" + "github.com/wailsapp/wails/v3/internal/capabilities" + "github.com/samber/lo" "github.com/wailsapp/go-webview2/pkg/edge" @@ -320,10 +321,6 @@ func (w *windowsWebviewWindow) run() { w.center() } - if options.Focused { - w.Focus() - } - if options.Frameless { // Trigger a resize to ensure the window is sized correctly w.chromium.Resize() @@ -363,10 +360,6 @@ func (w *windowsWebviewWindow) size() (int, int) { return width, height } -func (w *windowsWebviewWindow) Focus() { - w32.SetForegroundWindow(w.hwnd) -} - func (w *windowsWebviewWindow) update() { w32.UpdateWindow(w.hwnd) } @@ -955,10 +948,12 @@ func (w *windowsWebviewWindow) WndProc(msg uint32, wparam, lparam uintptr) uintp return 0 } w.parent.emit(events.Windows.WindowKillFocus) - case w32.WM_SETFOCUS: - w.parent.emit(events.Windows.WindowSetFocus) - case w32.WM_NCLBUTTONDOWN: + case w32.WM_ENTERSIZEMOVE: + // This is needed to close open dropdowns when moving the window https://github.com/MicrosoftEdge/WebView2Feedback/issues/2290 w32.SetFocus(w.hwnd) + case w32.WM_SETFOCUS: + w.focus() + w.parent.emit(events.Windows.WindowSetFocus) case w32.WM_MOVE, w32.WM_MOVING: _ = w.chromium.NotifyParentWindowPositionChanged() // Check for keypress @@ -1530,6 +1525,7 @@ func (w *windowsWebviewWindow) navigationCompleted(sender *edge.ICoreWebView2, a } w.hasStarted = true + wasFocused := w.isFocused() // Hack to make it visible: https://github.com/MicrosoftEdge/WebView2Feedback/issues/1077#issuecomment-825375026 err := w.chromium.Hide() if err != nil { @@ -1539,6 +1535,9 @@ func (w *windowsWebviewWindow) navigationCompleted(sender *edge.ICoreWebView2, a if err != nil { globalApplication.fatal(err.Error()) } + if wasFocused { + w.focus() + } //f.mainWindow.hasBeenShown = true