5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 02:20:47 +08:00

[windows] Improved focus handling

Make sure the webview is focused on initial show if the window
is focused and make behaviour consistent with macOS
This commit is contained in:
stffabi 2023-12-06 21:55:04 +01:00
parent 6262a4fac7
commit b37d0f1cf4
4 changed files with 18 additions and 25 deletions

View File

@ -77,7 +77,7 @@
</div>
<div class="result" id="result">Please enter your name below 👇</div>
<div class="input-box" id="input">
<input autocomplete="off" class="input" id="name" type="text"/>
<input autofocus autocomplete="off" class="input" id="name" type="text"/>
<button class="btn" onclick="greet()">Greet</button>
</div>
<script src='bindings_main.js'></script>

View File

@ -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

View File

@ -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)
}

View File

@ -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