webview_go icon indicating copy to clipboard operation
webview_go copied to clipboard

Execution order on Windows is weird

Open Le0Developer opened this issue 3 months ago • 2 comments

The execution order on Windows is weird.

Example 1

package main

import (
	"fmt"

	webview "github.com/webview/webview_go"
)

func main() {
	view := webview.New(true)
	defer view.Destroy()
	view.SetTitle("Basic Example")
	view.SetSize(480, 320, webview.HintNone)
	view.SetHtml("<script>window.hello()</script>")
	view.Bind("hello", func() {
		fmt.Println("hello from js!")
	})
	view.Run()
}

For some reason the script appears to be executing early, because it can't find the window.hello() binding and errors. This is not happening on macOS.

Example 2

Also in this example the second view.Init script just doesn't execute at all.

package main

import (
	"fmt"

	webview "github.com/webview/webview_go"
)

func main() {
	view := webview.New(true)
	defer view.Destroy()
	view.SetTitle("Basic Example")
	view.SetSize(480, 320, webview.HintNone)
	view.Init("console.log('init 1', window.hello)")
	view.SetHtml("<script>console.log('js', window.hello);setTimeout(() => console.log('js after 100ms', window.hello), 100)</script>")
	view.Bind("hello", func() {})
	view.Init("console.log('init 2', window.hello)")
	view.Run()
}
Windows macOS
image image

Navigate() also works

The same can be reproduced with Navigate by replacing SetHtml(" with Navigate("data:text/html,.

Window exists before Run()

See this basic example:

package main

import (
	"time"

	webview "github.com/webview/webview_go"
)

func main() {
	view := webview.New(true)
	defer view.Destroy()
	time.Sleep(10 * time.Second)
	view.Run()
}

When running this on macOS the webview appears in the dock but no window is visible for 10s. However on Windows, a window pops up immediately but it's frozen for 10seconds.

Conclusion

SetHtml/Navigate interact weirdly on Windows. They appear to be starting immediately or earlier than .Run().

Le0Developer avatar Mar 22 '24 11:03 Le0Developer