5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 00:41:59 +08:00

[windows-x] Support events

This commit is contained in:
Lea Anthony 2021-08-11 21:26:41 +10:00
parent d83fd1c2b4
commit 316e4de8e2
3 changed files with 44 additions and 6 deletions

View File

@ -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!

View File

@ -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) {

View File

@ -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)
}