diff --git a/v3/pkg/application/application.go b/v3/pkg/application/application.go index 81aa23d5a..dbd716743 100644 --- a/v3/pkg/application/application.go +++ b/v3/pkg/application/application.go @@ -76,7 +76,7 @@ func New(appOptions Options) *App { result.logStartup() result.logPlatformInfo() - result.Events = NewWailsEventProcessor(result.dispatchEventToWindows) + result.Events = NewWailsEventProcessor(result.dispatchEventToListeners) messageProc := NewMessageProcessor(result.Logger) opts := &assetserver.Options{ @@ -326,6 +326,10 @@ type App struct { // signalHandler is used to handle signals signalHandler *signal.SignalHandler + + // Wails Event Listener related + wailsEventListenerLock sync.Mutex + wailsEventListeners []WailsEventListener } func (a *App) init() { @@ -336,6 +340,7 @@ func (a *App) init() { a.keyBindings = make(map[string]func(window *WebviewWindow)) a.Logger = a.options.Logger a.pid = os.Getpid() + a.wailsEventListeners = make([]WailsEventListener, 0) } func (a *App) getSystemTrayID() uint { @@ -400,6 +405,12 @@ func (a *App) RegisterHook(eventType events.ApplicationEventType, callback func( } } +func (a *App) RegisterListener(listener WailsEventListener) { + a.wailsEventListenerLock.Lock() + a.wailsEventListeners = append(a.wailsEventListeners, listener) + a.wailsEventListenerLock.Unlock() +} + func (a *App) NewWebviewWindow() *WebviewWindow { return a.NewWebviewWindowWithOptions(WebviewWindowOptions{}) } @@ -774,10 +785,16 @@ func SaveFileDialogWithOptions(s *SaveFileDialogOptions) *SaveFileDialogStruct { return result } -func (a *App) dispatchEventToWindows(event *WailsEvent) { +func (a *App) dispatchEventToListeners(event *WailsEvent) { + listeners := a.wailsEventListeners + for _, window := range a.windows { window.DispatchWailsEvent(event) } + + for _, listener := range listeners { + listener.DispatchWailsEvent(event) + } } func (a *App) IsDarkMode() bool { diff --git a/v3/pkg/application/events.go b/v3/pkg/application/events.go index bbb6bc8ba..fedcf5643 100644 --- a/v3/pkg/application/events.go +++ b/v3/pkg/application/events.go @@ -60,6 +60,12 @@ func (e WailsEvent) ToJSON() string { return string(marshal) } +// WailsEventListener is an interface that can be implemented to listen for Wails events +// It is used by the RegisterListener method of the Application. +type WailsEventListener interface { + DispatchWailsEvent(event *WailsEvent) +} + type hook struct { callback func(*WailsEvent) }