5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-07 07:39:55 +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>
<div class="result" id="result">Please enter your name below 👇</div> <div class="result" id="result">Please enter your name below 👇</div>
<div class="input-box" id="input"> <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> <button class="btn" onclick="greet()">Greet</button>
</div> </div>
<script src='bindings_main.js'></script> <script src='bindings_main.js'></script>

View File

@ -100,9 +100,6 @@ type WebviewWindowOptions struct {
// Windows options // Windows options
Windows WindowsWindow Windows WindowsWindow
// Focused indicates the window should be focused when initially shown
Focused bool
// ShouldClose is called when the window is about to close. // 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. // Return true to allow the window to close, or false to prevent it from closing.
ShouldClose func(window *WebviewWindow) bool ShouldClose func(window *WebviewWindow) bool

View File

@ -4,11 +4,12 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/leaanthony/u"
"runtime" "runtime"
"strings" "strings"
"sync" "sync"
"github.com/leaanthony/u"
"github.com/samber/lo" "github.com/samber/lo"
"github.com/wailsapp/wails/v3/pkg/events" "github.com/wailsapp/wails/v3/pkg/events"
) )
@ -1050,10 +1051,6 @@ func (w *WebviewWindow) NativeWindowHandle() (uintptr, error) {
} }
func (w *WebviewWindow) Focus() { func (w *WebviewWindow) Focus() {
if w.impl == nil {
w.options.Focused = true
return
}
InvokeSync(w.impl.focus) InvokeSync(w.impl.focus)
w.emit(events.Common.WindowFocus) w.emit(events.Common.WindowFocus)
} }

View File

@ -5,11 +5,6 @@ package application
import ( import (
"errors" "errors"
"fmt" "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" "net/url"
"path" "path"
"strconv" "strconv"
@ -20,6 +15,12 @@ import (
"unicode/utf16" "unicode/utf16"
"unsafe" "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/samber/lo"
"github.com/wailsapp/go-webview2/pkg/edge" "github.com/wailsapp/go-webview2/pkg/edge"
@ -320,10 +321,6 @@ func (w *windowsWebviewWindow) run() {
w.center() w.center()
} }
if options.Focused {
w.Focus()
}
if options.Frameless { if options.Frameless {
// Trigger a resize to ensure the window is sized correctly // Trigger a resize to ensure the window is sized correctly
w.chromium.Resize() w.chromium.Resize()
@ -363,10 +360,6 @@ func (w *windowsWebviewWindow) size() (int, int) {
return width, height return width, height
} }
func (w *windowsWebviewWindow) Focus() {
w32.SetForegroundWindow(w.hwnd)
}
func (w *windowsWebviewWindow) update() { func (w *windowsWebviewWindow) update() {
w32.UpdateWindow(w.hwnd) w32.UpdateWindow(w.hwnd)
} }
@ -955,10 +948,12 @@ func (w *windowsWebviewWindow) WndProc(msg uint32, wparam, lparam uintptr) uintp
return 0 return 0
} }
w.parent.emit(events.Windows.WindowKillFocus) w.parent.emit(events.Windows.WindowKillFocus)
case w32.WM_SETFOCUS: case w32.WM_ENTERSIZEMOVE:
w.parent.emit(events.Windows.WindowSetFocus) // This is needed to close open dropdowns when moving the window https://github.com/MicrosoftEdge/WebView2Feedback/issues/2290
case w32.WM_NCLBUTTONDOWN:
w32.SetFocus(w.hwnd) w32.SetFocus(w.hwnd)
case w32.WM_SETFOCUS:
w.focus()
w.parent.emit(events.Windows.WindowSetFocus)
case w32.WM_MOVE, w32.WM_MOVING: case w32.WM_MOVE, w32.WM_MOVING:
_ = w.chromium.NotifyParentWindowPositionChanged() _ = w.chromium.NotifyParentWindowPositionChanged()
// Check for keypress // Check for keypress
@ -1530,6 +1525,7 @@ func (w *windowsWebviewWindow) navigationCompleted(sender *edge.ICoreWebView2, a
} }
w.hasStarted = true w.hasStarted = true
wasFocused := w.isFocused()
// Hack to make it visible: https://github.com/MicrosoftEdge/WebView2Feedback/issues/1077#issuecomment-825375026 // Hack to make it visible: https://github.com/MicrosoftEdge/WebView2Feedback/issues/1077#issuecomment-825375026
err := w.chromium.Hide() err := w.chromium.Hide()
if err != nil { if err != nil {
@ -1539,6 +1535,9 @@ func (w *windowsWebviewWindow) navigationCompleted(sender *edge.ICoreWebView2, a
if err != nil { if err != nil {
globalApplication.fatal(err.Error()) globalApplication.fatal(err.Error())
} }
if wasFocused {
w.focus()
}
//f.mainWindow.hasBeenShown = true //f.mainWindow.hasBeenShown = true