[v2] Use invoke to dispatch callbacks on windows

This commit is contained in:
stffabi
2021-11-24 11:15:49 +01:00
parent 8ef8b2528b
commit 1d87a81f63

View File

@@ -7,15 +7,12 @@ import (
"github.com/leaanthony/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, appoptions *options.App) *Window {
@@ -86,32 +83,9 @@ func NewWindow(parent winc.Controller, appoptions *options.App) *Window {
}
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)
return winc.RunMainLoop()
}
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)
w.Invoke(f)
}