From b49f135e31f59ab2d81b2e8136cdc8a5b62d9c30 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Fri, 15 Sep 2023 17:12:35 +1000 Subject: [PATCH] Add context to application/common events --- v3/examples/events/main.go | 9 ++++ v3/pkg/application/application.go | 18 +++---- v3/pkg/application/application_darwin.go | 2 +- v3/pkg/application/application_windows.go | 17 ++++--- .../application/context_application_event.go | 51 +++++++++++++++++++ v3/pkg/application/events.go | 32 ++++++++---- v3/pkg/application/events_common_darwin.go | 3 +- v3/pkg/application/events_common_windows.go | 3 +- 8 files changed, 106 insertions(+), 29 deletions(-) create mode 100644 v3/pkg/application/context_application_event.go diff --git a/v3/examples/events/main.go b/v3/examples/events/main.go index adba40feb..8e19a0977 100644 --- a/v3/examples/events/main.go +++ b/v3/examples/events/main.go @@ -43,6 +43,15 @@ func main() { } }) + app.On(events.Windows.SystemThemeChanged, func(event *application.Event) { + log.Println("System theme changed!") + if event.Context().IsDarkMode() { + log.Println("System is now using dark mode!") + } else { + log.Println("System is now using light mode!") + } + }) + // Platform agnostic events app.On(events.Common.ApplicationStarted, func(event *application.Event) { println("events.Common.ApplicationStarted fired!") diff --git a/v3/pkg/application/application.go b/v3/pkg/application/application.go index 1b85871cd..2eba568f1 100644 --- a/v3/pkg/application/application.go +++ b/v3/pkg/application/application.go @@ -30,7 +30,7 @@ func init() { } type EventListener struct { - callback func(*Event) + callback func(app *Event) } func Get() *App { @@ -196,7 +196,7 @@ func (r *webViewAssetRequest) Header() (http.Header, error) { var webviewRequests = make(chan *webViewAssetRequest) type eventHook struct { - callback func(*Event) + callback func(event *Event) } type App struct { @@ -461,31 +461,29 @@ func (a *App) Run() error { return nil } -func (a *App) handleApplicationEvent(event uint) { +func (a *App) handleApplicationEvent(event *Event) { a.applicationEventListenersLock.RLock() - listeners, ok := a.applicationEventListeners[event] + listeners, ok := a.applicationEventListeners[event.Id] a.applicationEventListenersLock.RUnlock() if !ok { return } - thisEvent := &Event{} - // Process Hooks a.applicationEventHooksLock.RLock() - hooks, ok := a.applicationEventHooks[event] + hooks, ok := a.applicationEventHooks[event.Id] a.applicationEventHooksLock.RUnlock() if ok { for _, thisHook := range hooks { - thisHook.callback(thisEvent) - if thisEvent.Cancelled { + thisHook.callback(event) + if event.Cancelled { return } } } for _, listener := range listeners { - go listener.callback(thisEvent) + go listener.callback(event) } } diff --git a/v3/pkg/application/application_darwin.go b/v3/pkg/application/application_darwin.go index 07be7ffa6..7fa631b22 100644 --- a/v3/pkg/application/application_darwin.go +++ b/v3/pkg/application/application_darwin.go @@ -238,7 +238,7 @@ func newPlatformApp(app *App) *macosApp { //export processApplicationEvent func processApplicationEvent(eventID C.uint) { - applicationEvents <- uint(eventID) + applicationEvents <- NewApplicationEvent(uint(eventID)) } //export processWindowEvent diff --git a/v3/pkg/application/application_windows.go b/v3/pkg/application/application_windows.go index 196abc611..de4d647d4 100644 --- a/v3/pkg/application/application_windows.go +++ b/v3/pkg/application/application_windows.go @@ -237,7 +237,12 @@ func (m *windowsApp) wndProc(hwnd w32.HWND, msg uint32, wParam, lParam uintptr) if settingChanged == "ImmersiveColorSet" { isDarkMode := w32.IsCurrentlyDarkMode() if isDarkMode != m.isCurrentlyDarkMode { - applicationEvents <- uint(events.Windows.SystemThemeChanged) + eventContext := newApplicationEventContext() + eventContext.setIsDarkMode(isDarkMode) + applicationEvents <- &Event{ + Id: uint(events.Windows.SystemThemeChanged), + ctx: eventContext, + } m.isCurrentlyDarkMode = isDarkMode } } @@ -245,15 +250,15 @@ func (m *windowsApp) wndProc(hwnd w32.HWND, msg uint32, wParam, lParam uintptr) case w32.WM_POWERBROADCAST: switch wParam { case w32.PBT_APMPOWERSTATUSCHANGE: - applicationEvents <- uint(events.Windows.APMPowerStatusChange) + applicationEvents <- NewApplicationEvent(int(events.Windows.APMPowerStatusChange)) case w32.PBT_APMSUSPEND: - applicationEvents <- uint(events.Windows.APMSuspend) + applicationEvents <- NewApplicationEvent(int(events.Windows.APMSuspend)) case w32.PBT_APMRESUMEAUTOMATIC: - applicationEvents <- uint(events.Windows.APMResumeAutomatic) + applicationEvents <- NewApplicationEvent(int(events.Windows.APMResumeAutomatic)) case w32.PBT_APMRESUMESUSPEND: - applicationEvents <- uint(events.Windows.APMResumeSuspend) + applicationEvents <- NewApplicationEvent(int(events.Windows.APMResumeSuspend)) case w32.PBT_POWERSETTINGCHANGE: - applicationEvents <- uint(events.Windows.APMPowerSettingChange) + applicationEvents <- NewApplicationEvent(int(events.Windows.APMPowerSettingChange)) } return 0 } diff --git a/v3/pkg/application/context_application_event.go b/v3/pkg/application/context_application_event.go new file mode 100644 index 000000000..ba455fb32 --- /dev/null +++ b/v3/pkg/application/context_application_event.go @@ -0,0 +1,51 @@ +package application + +var blankApplicationEventContext = &ApplicationEventContext{} + +const ( + // FilesDropped is the event name for when files are dropped on the window + openedFiles = "openedFiles" +) + +type ApplicationEventContext struct { + // contains filtered or unexported fields + data map[string]any +} + +func (c ApplicationEventContext) OpenedFiles() []string { + files, ok := c.data[openedFiles] + if !ok { + return nil + } + result, ok := files.([]string) + if !ok { + return nil + } + return result +} + +func (c ApplicationEventContext) setOpenedFiles(files []string) { + c.data[openedFiles] = files +} + +func (c ApplicationEventContext) setIsDarkMode(mode bool) { + c.data["isDarkMode"] = mode +} + +func (c ApplicationEventContext) IsDarkMode() bool { + mode, ok := c.data["isDarkMode"] + if !ok { + return false + } + result, ok := mode.(bool) + if !ok { + return false + } + return result +} + +func newApplicationEventContext() *ApplicationEventContext { + return &ApplicationEventContext{ + data: make(map[string]any), + } +} diff --git a/v3/pkg/application/events.go b/v3/pkg/application/events.go index ff85d5dd3..280c128d7 100644 --- a/v3/pkg/application/events.go +++ b/v3/pkg/application/events.go @@ -7,21 +7,33 @@ import ( "github.com/samber/lo" ) -var applicationEvents = make(chan uint) +type Event struct { + Id uint + ctx *ApplicationEventContext + Cancelled bool +} + +func (w *Event) Context() *ApplicationEventContext { + return w.ctx +} + +func NewApplicationEvent(id int) *Event { + return &Event{ + Id: uint(id), + } +} + +func (w *Event) Cancel() { + w.Cancelled = true +} + +var applicationEvents = make(chan *Event) type windowEvent struct { WindowID uint EventID uint } -type Event struct { - Cancelled bool -} - -func (e *Event) Cancel() { - e.Cancelled = true -} - var windowEvents = make(chan *windowEvent) var menuItemClicked = make(chan uint) @@ -37,7 +49,7 @@ func (e *WailsEvent) Cancel() { e.Cancelled = true } -var commonEvents = make(chan uint) +var commonEvents = make(chan *Event) func (e WailsEvent) ToJSON() string { marshal, err := json.Marshal(&e) diff --git a/v3/pkg/application/events_common_darwin.go b/v3/pkg/application/events_common_darwin.go index 9594a3d40..f149e9aa0 100644 --- a/v3/pkg/application/events_common_darwin.go +++ b/v3/pkg/application/events_common_darwin.go @@ -12,7 +12,8 @@ var commonApplicationEventMap = map[events.ApplicationEventType]events.Applicati func (m *macosApp) setupCommonEvents() { for sourceEvent, targetEvent := range commonApplicationEventMap { m.parent.On(sourceEvent, func(event *Event) { - applicationEvents <- uint(targetEvent) + event.Id = uint(targetEvent) + applicationEvents <- event }) } } diff --git a/v3/pkg/application/events_common_windows.go b/v3/pkg/application/events_common_windows.go index 44f8fb0ae..9ea29217b 100644 --- a/v3/pkg/application/events_common_windows.go +++ b/v3/pkg/application/events_common_windows.go @@ -11,7 +11,8 @@ var commonApplicationEventMap = map[events.ApplicationEventType]events.Applicati func (m *windowsApp) setupCommonEvents() { for sourceEvent, targetEvent := range commonApplicationEventMap { m.parent.On(sourceEvent, func(event *Event) { - applicationEvents <- uint(targetEvent) + event.Id = uint(targetEvent) + applicationEvents <- event }) } }