mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-03 14:42:03 +08:00
[windows-x] Support events
This commit is contained in:
parent
d83fd1c2b4
commit
316e4de8e2
@ -31,7 +31,11 @@ type App struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) Run() error {
|
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!
|
// CreateApp creates the app!
|
||||||
|
@ -65,7 +65,7 @@ func (f *Frontend) Run(ctx context.Context) error {
|
|||||||
f.frontendOptions.Startup(ctx)
|
f.frontendOptions.Startup(ctx)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
winc.RunMainLoop()
|
mainWindow.Run()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,18 +169,18 @@ type EventNotify struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *Frontend) Notify(name string, data ...interface{}) {
|
func (f *Frontend) Notify(name string, data ...interface{}) {
|
||||||
runtime.LockOSThread()
|
|
||||||
notification := EventNotify{
|
notification := EventNotify{
|
||||||
Name: name,
|
Name: name,
|
||||||
Data: data,
|
Data: data,
|
||||||
}
|
}
|
||||||
_, err := json.Marshal(notification)
|
payload, err := json.Marshal(notification)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
f.logger.Error(err.Error())
|
f.logger.Error(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
f.chromium.Eval(`alert("test");`)
|
f.mainWindow.Dispatch(func() {
|
||||||
//f.chromium.Eval(`window.wails.EventsNotify('` + string(payload) + `');`)
|
f.chromium.Eval(`window.wails.EventsNotify('` + string(payload) + `');`)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Frontend) processRequest(req *edge.ICoreWebView2WebResourceRequest, args *edge.ICoreWebView2WebResourceRequestedEventArgs) {
|
func (f *Frontend) processRequest(req *edge.ICoreWebView2WebResourceRequest, args *edge.ICoreWebView2WebResourceRequestedEventArgs) {
|
||||||
|
@ -5,12 +5,15 @@ import (
|
|||||||
"github.com/tadvi/winc/w32"
|
"github.com/tadvi/winc/w32"
|
||||||
"github.com/wailsapp/wails/v2/pkg/menu"
|
"github.com/wailsapp/wails/v2/pkg/menu"
|
||||||
"github.com/wailsapp/wails/v2/pkg/options"
|
"github.com/wailsapp/wails/v2/pkg/options"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Window struct {
|
type Window struct {
|
||||||
winc.Form
|
winc.Form
|
||||||
frontendOptions *options.App
|
frontendOptions *options.App
|
||||||
applicationMenu *menu.Menu
|
applicationMenu *menu.Menu
|
||||||
|
m sync.Mutex
|
||||||
|
dispatchq []func()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWindow(parent winc.Controller, options *options.App) *Window {
|
func NewWindow(parent winc.Controller, options *options.App) *Window {
|
||||||
@ -70,3 +73,34 @@ func NewWindow(parent winc.Controller, options *options.App) *Window {
|
|||||||
|
|
||||||
return result
|
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)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user