diff --git a/v2/internal/appng/app.go b/v2/internal/appng/app.go index 53e72f4c8..b9e310c1f 100644 --- a/v2/internal/appng/app.go +++ b/v2/internal/appng/app.go @@ -31,7 +31,11 @@ type App struct { } func (a *App) Run() error { - return a.frontend.Run(a.ctx) + err := a.frontend.Run(a.ctx) + if a.shutdownCallback != nil { + a.shutdownCallback() + } + return err } // CreateApp creates the app! diff --git a/v2/internal/frontend/windows/frontend.go b/v2/internal/frontend/windows/frontend.go index 9f3ef1bdb..970a5dc88 100644 --- a/v2/internal/frontend/windows/frontend.go +++ b/v2/internal/frontend/windows/frontend.go @@ -65,7 +65,7 @@ func (f *Frontend) Run(ctx context.Context) error { f.frontendOptions.Startup(ctx) }() - winc.RunMainLoop() + mainWindow.Run() return nil } @@ -169,18 +169,18 @@ type EventNotify struct { } func (f *Frontend) Notify(name string, data ...interface{}) { - runtime.LockOSThread() notification := EventNotify{ Name: name, Data: data, } - _, err := json.Marshal(notification) + payload, err := json.Marshal(notification) if err != nil { f.logger.Error(err.Error()) return } - f.chromium.Eval(`alert("test");`) - //f.chromium.Eval(`window.wails.EventsNotify('` + string(payload) + `');`) + f.mainWindow.Dispatch(func() { + f.chromium.Eval(`window.wails.EventsNotify('` + string(payload) + `');`) + }) } func (f *Frontend) processRequest(req *edge.ICoreWebView2WebResourceRequest, args *edge.ICoreWebView2WebResourceRequestedEventArgs) { diff --git a/v2/internal/frontend/windows/window.go b/v2/internal/frontend/windows/window.go index 694105fb5..389ae3eec 100644 --- a/v2/internal/frontend/windows/window.go +++ b/v2/internal/frontend/windows/window.go @@ -5,12 +5,15 @@ import ( "github.com/tadvi/winc/w32" "github.com/wailsapp/wails/v2/pkg/menu" "github.com/wailsapp/wails/v2/pkg/options" + "sync" ) type Window struct { winc.Form frontendOptions *options.App applicationMenu *menu.Menu + m sync.Mutex + dispatchq []func() } func NewWindow(parent winc.Controller, options *options.App) *Window { @@ -70,3 +73,34 @@ func NewWindow(parent winc.Controller, options *options.App) *Window { return result } + +func (w *Window) Run() int { + var m w32.MSG + + for w32.GetMessage(&m, 0, 0, 0) != 0 { + if m.Message == w32.WM_APP { + // Credit: https://github.com/jchv/go-webview2 + w.m.Lock() + q := append([]func(){}, w.dispatchq...) + w.dispatchq = []func(){} + w.m.Unlock() + for _, v := range q { + v() + } + } + if !w.PreTranslateMessage(&m) { + w32.TranslateMessage(&m) + w32.DispatchMessage(&m) + } + } + + w32.GdiplusShutdown() + return int(m.WParam) +} + +func (w *Window) Dispatch(f func()) { + w.m.Lock() + w.dispatchq = append(w.dispatchq, f) + w.m.Unlock() + w32.PostMainThreadMessage(w32.WM_APP, 0, 0) +}