From 86f5049d29633f8169b1214e99f794e53f1374c9 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Fri, 27 Dec 2024 12:05:11 +1100 Subject: [PATCH] Improve move & resize debouncer. Fix double event. Fix shutdown issue with webview2. --- v3/go.mod | 2 +- v3/go.sum | 2 ++ v3/pkg/application/webview_window_linux.go | 16 ++++++---- v3/pkg/application/webview_window_options.go | 8 +++++ v3/pkg/application/webview_window_windows.go | 32 +++++++------------- 5 files changed, 32 insertions(+), 28 deletions(-) diff --git a/v3/go.mod b/v3/go.mod index 8deda3568..5596f1a2e 100644 --- a/v3/go.mod +++ b/v3/go.mod @@ -30,7 +30,7 @@ require ( github.com/pterm/pterm v0.12.51 github.com/samber/lo v1.38.1 github.com/tc-hib/winres v0.3.1 - github.com/wailsapp/go-webview2 v1.0.18 + github.com/wailsapp/go-webview2 v1.0.19-0.20241227010006-11c6e567b916 github.com/wailsapp/mimetype v1.4.1 github.com/wailsapp/task/v3 v3.40.1-patched3 golang.org/x/sys v0.28.0 diff --git a/v3/go.sum b/v3/go.sum index 3938c67b9..4d5fc0411 100644 --- a/v3/go.sum +++ b/v3/go.sum @@ -305,6 +305,8 @@ github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc= github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/wailsapp/go-webview2 v1.0.18 h1:SSSCoLA+MYikSp1U0WmvELF/4c3x5kH8Vi31TKyZ4yk= github.com/wailsapp/go-webview2 v1.0.18/go.mod h1:qJmWAmAmaniuKGZPWwne+uor3AHMB5PFhqiK0Bbj8kc= +github.com/wailsapp/go-webview2 v1.0.19-0.20241227010006-11c6e567b916 h1:W0UQJWILiXJOOCg7ec5xJNqxkg4Ced5KCGO4tFAf13w= +github.com/wailsapp/go-webview2 v1.0.19-0.20241227010006-11c6e567b916/go.mod h1:qJmWAmAmaniuKGZPWwne+uor3AHMB5PFhqiK0Bbj8kc= github.com/wailsapp/mimetype v1.4.1 h1:pQN9ycO7uo4vsUUuPeHEYoUkLVkaRntMnHJxVwYhwHs= github.com/wailsapp/mimetype v1.4.1/go.mod h1:9aV5k31bBOv5z6u+QP8TltzvNGJPmNJD4XlAL3U+j3o= github.com/wailsapp/task/v3 v3.40.1-patched3 h1:i6O1WNdSur9CGaiMDIYGjsmj/qS4465zqv+WEs6sPRs= diff --git a/v3/pkg/application/webview_window_linux.go b/v3/pkg/application/webview_window_linux.go index a02f2aa47..641a2ff22 100644 --- a/v3/pkg/application/webview_window_linux.go +++ b/v3/pkg/application/webview_window_linux.go @@ -13,10 +13,6 @@ import ( "github.com/wailsapp/wails/v3/pkg/events" ) -const ( - windowDidMoveDebounceMS = 200 -) - type dragInfo struct { XRoot int YRoot int @@ -240,10 +236,18 @@ func (w *linuxWebviewWindow) run() { } if w.moveDebouncer == nil { - w.moveDebouncer = debounce.New(time.Duration(windowDidMoveDebounceMS) * time.Millisecond) + debounceMS := w.parent.options.Linux.WindowDidMoveDebounceMS + if debounceMS == 0 { + debounceMS = 50 // Default value + } + w.moveDebouncer = debounce.New(time.Duration(debounceMS) * time.Millisecond) } if w.resizeDebouncer == nil { - w.resizeDebouncer = debounce.New(time.Duration(windowDidMoveDebounceMS) * time.Millisecond) + debounceMS := w.parent.options.Linux.WindowDidMoveDebounceMS + if debounceMS == 0 { + debounceMS = 50 // Default value + } + w.resizeDebouncer = debounce.New(time.Duration(debounceMS) * time.Millisecond) } // Register the capabilities diff --git a/v3/pkg/application/webview_window_options.go b/v3/pkg/application/webview_window_options.go index c454e5b1b..af9c3be5a 100644 --- a/v3/pkg/application/webview_window_options.go +++ b/v3/pkg/application/webview_window_options.go @@ -253,6 +253,11 @@ type WindowsWindow struct { // Default: 0 ResizeDebounceMS uint16 + // WindowDidMoveDebounceMS is the amount of time to debounce the WindowDidMove event + // when moving the window + // Default: 0 + WindowDidMoveDebounceMS uint16 + // Disable the menu bar for this window // Default: false DisableMenu bool @@ -526,4 +531,7 @@ type LinuxWindow struct { // Client code may override this behavior by passing a non-nil Options and set // WebviewGpuPolicy as needed. WebviewGpuPolicy WebviewGpuPolicy + + // WindowDidMoveDebounceMS is the debounce time in milliseconds for the WindowDidMove event + WindowDidMoveDebounceMS uint16 } diff --git a/v3/pkg/application/webview_window_windows.go b/v3/pkg/application/webview_window_windows.go index 73220cd32..c82df1f1b 100644 --- a/v3/pkg/application/webview_window_windows.go +++ b/v3/pkg/application/webview_window_windows.go @@ -28,10 +28,6 @@ import ( "github.com/wailsapp/wails/v3/pkg/w32" ) -const ( - windowDidMoveDebounceMS = 200 -) - var edgeMap = map[string]uintptr{ "n-resize": w32.HTTOP, "ne-resize": w32.HTTOPRIGHT, @@ -286,6 +282,15 @@ func (w *windowsWebviewWindow) run() { w.setupChromium() + if options.Windows.WindowDidMoveDebounceMS == 0 { + options.Windows.WindowDidMoveDebounceMS = 50 + } + w.moveDebouncer = debounce.New(time.Duration(options.Windows.WindowDidMoveDebounceMS) * time.Millisecond) + + if options.Windows.ResizeDebounceMS > 0 { + w.resizeDebouncer = debounce.New(time.Duration(options.Windows.ResizeDebounceMS) * time.Millisecond) + } + // Initialise the window buttons w.setMinimiseButtonState(options.MinimiseButtonState) w.setMaximiseButtonState(options.MaximiseButtonState) @@ -386,11 +391,6 @@ func (w *windowsWebviewWindow) run() { // Trigger a resize to ensure the window is sized correctly w.chromium.Resize() } - - if options.Windows.ResizeDebounceMS > 0 { - w.resizeDebouncer = debounce.New(time.Duration(options.Windows.ResizeDebounceMS) * time.Millisecond) - } - } func (w *windowsWebviewWindow) center() { @@ -1081,9 +1081,6 @@ func (w *windowsWebviewWindow) WndProc(msg uint32, wparam, lparam uintptr) uintp w.parent.emit(events.Windows.WindowSetFocus) case w32.WM_MOVE, w32.WM_MOVING: _ = w.chromium.NotifyParentWindowPositionChanged() - if w.moveDebouncer == nil { - w.moveDebouncer = debounce.New(time.Duration(windowDidMoveDebounceMS) * time.Millisecond) - } w.moveDebouncer(func() { w.parent.emit(events.Windows.WindowDidMove) }) @@ -1132,6 +1129,7 @@ func (w *windowsWebviewWindow) WndProc(msg uint32, wparam, lparam uintptr) uintp Bottom: height, } InvokeSync(func() { + time.Sleep(1 * time.Nanosecond) w.chromium.ResizeWithBounds(bounds) atomic.StoreInt32(&resizePending, 0) w.parent.emit(events.Windows.WindowDidResize) @@ -1142,7 +1140,7 @@ func (w *windowsWebviewWindow) WndProc(msg uint32, wparam, lparam uintptr) uintp // If the window is frameless, and we are minimizing, then we need to suppress the Resize on the // WebView2. If we don't do this, restoring does not work as expected and first restores with some wrong // size during the restore animation and only fully renders when the animation is done. This highly - // depends on the content in the WebView, see https://github.com/wailsapp/wails/issues/1319 + // depends on the content in the WebView, see https://github.com/MicrosoftEdge/WebView2Feedback/issues/2549 } else if w.resizeDebouncer != nil { w.resizeDebouncer(doResize) } else { @@ -1599,14 +1597,6 @@ func (w *windowsWebviewWindow) setupChromium() { } } - // event mapping - w.parent.OnWindowEvent(events.Windows.WindowDidMove, func(e *WindowEvent) { - w.parent.emit(events.Common.WindowDidMove) - }) - w.parent.OnWindowEvent(events.Windows.WindowDidResize, func(e *WindowEvent) { - w.parent.emit(events.Common.WindowDidResize) - }) - // We will get round to this //if chromium.HasCapability(edge.AllowExternalDrop) { // err := chromium.AllowExternalDrag(w.parent.options.EnableDragAndDrop)