@@ -13,6 +13,7 @@ import (
1313 "strings"
1414 "sync/atomic"
1515 "syscall"
16+ "time"
1617 "unsafe"
1718
1819 "github.com/wailsapp/go-webview2/internal/w32"
@@ -46,7 +47,14 @@ func globalErrorHandler(err error) {
4647}
4748
4849type Chromium struct {
49- hwnd uintptr
50+ hwnd uintptr
51+ padding struct {
52+ Left int32
53+ Top int32
54+ Right int32
55+ Bottom int32
56+ }
57+
5058 controller * ICoreWebView2Controller
5159 webview * ICoreWebView2
5260 inited uintptr
@@ -61,7 +69,6 @@ type Chromium struct {
6169 processFailed * ICoreWebView2ProcessFailedEventHandler
6270
6371 environment * ICoreWebView2Environment
64- padding Rect
6572 webview2RuntimeVersion string
6673
6774 // Settings
@@ -85,6 +92,12 @@ type Chromium struct {
8592
8693 // Error handling
8794 globalErrorCallback func (error )
95+
96+ shuttingDown bool
97+
98+ // Resize debouncing
99+ lastBounds * w32.Rect
100+ resizeTimer * time.Timer
88101}
89102
90103func NewChromium () * Chromium {
@@ -127,10 +140,13 @@ func NewChromium() *Chromium {
127140 */
128141 e .permissions = make (map [CoreWebView2PermissionKind ]CoreWebView2PermissionState )
129142 e .globalErrorCallback = globalErrorHandler
130-
131143 return e
132144}
133145
146+ func (e * Chromium ) ShuttingDown () {
147+ e .shuttingDown = true
148+ }
149+
134150func (e * Chromium ) errorCallback (err error ) {
135151 e .globalErrorCallback (err )
136152 os .Exit (1 )
@@ -197,16 +213,32 @@ func (e *Chromium) Embed(hwnd uintptr) bool {
197213}
198214
199215func (e * Chromium ) SetPadding (padding Rect ) {
200- if e .padding .Top == padding .Top && e .padding .Bottom == padding .Bottom &&
201- e .padding .Left == padding .Left && e .padding .Right == padding .Right {
216+ if e .padding .Left == padding .Left && e .padding .Top == padding .Top &&
217+ e .padding .Right == padding .Right && e .padding .Bottom == padding .Bottom {
202218
203219 return
204220 }
205221
206- e .padding = padding
222+ e .padding .Left = padding .Left
223+ e .padding .Top = padding .Top
224+ e .padding .Right = padding .Right
225+ e .padding .Bottom = padding .Bottom
207226 e .Resize ()
208227}
209228
229+ func (e * Chromium ) ResizeWithBounds (bounds * Rect ) {
230+ if e .hwnd == 0 {
231+ return
232+ }
233+
234+ bounds .Top += e .padding .Top
235+ bounds .Bottom -= e .padding .Bottom
236+ bounds .Left += e .padding .Left
237+ bounds .Right -= e .padding .Right
238+
239+ e .SetSize (* bounds )
240+ }
241+
210242func (e * Chromium ) Resize () {
211243 if e .hwnd == 0 {
212244 return
@@ -215,14 +247,10 @@ func (e *Chromium) Resize() {
215247 bounds , err := w32 .GetClientRect (e .hwnd )
216248 if err != nil {
217249 e .errorCallback (err )
250+ return
218251 }
219252
220- bounds .Top += e .padding .Top
221- bounds .Bottom -= e .padding .Bottom
222- bounds .Left += e .padding .Left
223- bounds .Right -= e .padding .Right
224-
225- e .SetSize (bounds )
253+ e .ResizeWithBounds (& bounds )
226254}
227255
228256func (e * Chromium ) Navigate (url string ) {
@@ -247,7 +275,7 @@ func (e *Chromium) Init(script string) {
247275}
248276
249277func (e * Chromium ) Eval (script string ) {
250- if e .webview == nil {
278+ if e .webview == nil || e . shuttingDown {
251279 return
252280 }
253281
@@ -309,6 +337,23 @@ func (e *Chromium) CreateCoreWebView2ControllerCompleted(res uintptr, controller
309337 controller .vtbl .AddRef .Call (uintptr (unsafe .Pointer (controller )))
310338 e .controller = controller
311339
340+ // Try to get ICoreWebView2Controller3 interface for better performance
341+ if controller3 := e .controller .GetICoreWebView2Controller3 (); controller3 != nil {
342+ // Use raw pixels mode for better performance during resize
343+ if err := controller3 .PutBoundsMode (COREWEBVIEW2_BOUNDS_MODE_USE_RAW_PIXELS ); err != nil {
344+ e .errorCallback (err )
345+ }
346+
347+ // Disable monitor scale changes since we're using raw pixels
348+ if err := controller3 .PutShouldDetectMonitorScaleChanges (false ); err != nil {
349+ e .errorCallback (err )
350+ }
351+
352+ // Set a fixed rasterization scale for better performance
353+ if err := controller3 .PutRasterizationScale (1.0 ); err != nil {
354+ e .errorCallback (err )
355+ }
356+ }
312357 var token _EventRegistrationToken
313358 e .webview , err = e .controller .GetCoreWebView2 ()
314359 if err != nil {
0 commit comments