webview / webview_go

Go language bindings for the webview library.
MIT License
176 stars 27 forks source link

Execution order on Windows is weird #37

Open Le0Developer opened 3 months ago

Le0Developer commented 3 months ago

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().

SteffenL commented 3 months ago

Have you tried to call bind Bind (and Init) before SetHtml?

Le0Developer commented 3 months ago

Yeah, but I'd expect the behavior to be consistent on all platforms.