mirror of
https://github.com/wailsapp/wails.git
synced 2026-03-14 22:55:48 +01:00
Add context to application/common events
This commit is contained in:
parent
fff266f50d
commit
b49f135e31
8 changed files with 105 additions and 28 deletions
|
|
@ -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!")
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
51
v3/pkg/application/context_application_event.go
Normal file
51
v3/pkg/application/context_application_event.go
Normal 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),
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue