Add context to application/common events

This commit is contained in:
Lea Anthony 2023-09-15 17:12:35 +10:00
commit b49f135e31
No known key found for this signature in database
GPG key ID: 33DAF7BB90A58405
8 changed files with 105 additions and 28 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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