From 8ddd29d285c42d15731e68c039331db19cfb3d84 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Tue, 3 Oct 2023 08:37:11 +1100 Subject: [PATCH] [linux] Implement events --- .../runtime/desktop/api/event_types.js | 3 + v3/pkg/application/application_linux.go | 13 ++- v3/pkg/application/events_common_linux.go | 20 +++++ v3/pkg/events/events.go | 88 +++++++++++-------- v3/pkg/events/events.txt | 1 + v3/pkg/events/events_linux.go | 21 +++++ v3/pkg/events/events_linux.h | 14 +++ v3/tasks/events/generate.go | 79 ++++++++++++++--- 8 files changed, 183 insertions(+), 56 deletions(-) create mode 100644 v3/pkg/application/events_common_linux.go create mode 100644 v3/pkg/events/events_linux.go create mode 100644 v3/pkg/events/events_linux.h diff --git a/v3/internal/runtime/desktop/api/event_types.js b/v3/internal/runtime/desktop/api/event_types.js index e2a7857e3..f130789a2 100644 --- a/v3/internal/runtime/desktop/api/event_types.js +++ b/v3/internal/runtime/desktop/api/event_types.js @@ -147,6 +147,9 @@ export const EventTypes = { WindowFileDraggingPerformed: "mac:WindowFileDraggingPerformed", WindowFileDraggingExited: "mac:WindowFileDraggingExited", }, + Linux: { + SystemThemeChanged: "linux:SystemThemeChanged", +}, Common: { ApplicationStarted: "common:ApplicationStarted", WindowMaximise: "common:WindowMaximise", diff --git a/v3/pkg/application/application_linux.go b/v3/pkg/application/application_linux.go index 27c04a89f..bf64a9d2d 100644 --- a/v3/pkg/application/application_linux.go +++ b/v3/pkg/application/application_linux.go @@ -3,13 +3,10 @@ package application import ( - "fmt" "log" "os" "strings" "sync" - - "github.com/wailsapp/wails/v3/pkg/events" ) func init() { @@ -104,10 +101,12 @@ func (m *linuxApp) run() error { // Add a hook to the ApplicationDidFinishLaunching event // FIXME: add Wails specific events - i.e. Shouldn't platform specific ones be translated to Wails events? - m.parent.On(events.Mac.ApplicationDidFinishLaunching, func(evt *Event) { - // Do we need to do anything now? - fmt.Println("events.Mac.ApplicationDidFinishLaunching received!") - }) + //m.parent.On(events.Mac.ApplicationDidFinishLaunching, func(evt *Event) { + // // Do we need to do anything now? + // fmt.Println("events.Mac.ApplicationDidFinishLaunching received!") + //}) + + m.setupCommonEvents() return appRun(m.application) } diff --git a/v3/pkg/application/events_common_linux.go b/v3/pkg/application/events_common_linux.go new file mode 100644 index 000000000..bafe8379d --- /dev/null +++ b/v3/pkg/application/events_common_linux.go @@ -0,0 +1,20 @@ +//go:build linux + +package application + +import "github.com/wailsapp/wails/v3/pkg/events" + +var commonApplicationEventMap = map[events.ApplicationEventType]events.ApplicationEventType{ + events.Linux.SystemThemeChanged: events.Common.ThemeChanged, +} + +func (m *linuxApp) setupCommonEvents() { + for sourceEvent, targetEvent := range commonApplicationEventMap { + sourceEvent := sourceEvent + targetEvent := targetEvent + m.parent.On(sourceEvent, func(event *Event) { + event.Id = uint(targetEvent) + applicationEvents <- event + }) + } +} diff --git a/v3/pkg/events/events.go b/v3/pkg/events/events.go index fd0134f78..d674ea0e6 100644 --- a/v3/pkg/events/events.go +++ b/v3/pkg/events/events.go @@ -33,25 +33,25 @@ type commonEvents struct { func newCommonEvents() commonEvents { return commonEvents{ - ApplicationStarted: 1167, - WindowMaximise: 1168, - WindowUnMaximise: 1169, - WindowFullscreen: 1170, - WindowUnFullscreen: 1171, - WindowRestore: 1172, - WindowMinimise: 1173, - WindowUnMinimise: 1174, - WindowClosing: 1175, - WindowZoom: 1176, - WindowZoomIn: 1177, - WindowZoomOut: 1178, - WindowZoomReset: 1179, - WindowFocus: 1180, - WindowLostFocus: 1181, - WindowShow: 1182, - WindowHide: 1183, - WindowDPIChanged: 1184, - ThemeChanged: 1185, + ApplicationStarted: 1168, + WindowMaximise: 1169, + WindowUnMaximise: 1170, + WindowFullscreen: 1171, + WindowUnFullscreen: 1172, + WindowRestore: 1173, + WindowMinimise: 1174, + WindowUnMinimise: 1175, + WindowClosing: 1176, + WindowZoom: 1177, + WindowZoomIn: 1178, + WindowZoomOut: 1179, + WindowZoomReset: 1180, + WindowFocus: 1181, + WindowLostFocus: 1182, + WindowShow: 1183, + WindowHide: 1184, + WindowDPIChanged: 1185, + ThemeChanged: 1186, } } @@ -311,6 +311,18 @@ func newMacEvents() macEvents { } } +var Linux = newLinuxEvents() + +type linuxEvents struct { + SystemThemeChanged ApplicationEventType +} + +func newLinuxEvents() linuxEvents { + return linuxEvents{ + SystemThemeChanged: 1167, + } +} + var Windows = newWindowsEvents() type windowsEvents struct { @@ -509,23 +521,23 @@ var eventToJS = map[uint]string{ 1164: "windows:WindowClose", 1165: "windows:WindowSetFocus", 1166: "windows:WindowKillFocus", - 1167: "common:ApplicationStarted", - 1168: "common:WindowMaximise", - 1169: "common:WindowUnMaximise", - 1170: "common:WindowFullscreen", - 1171: "common:WindowUnFullscreen", - 1172: "common:WindowRestore", - 1173: "common:WindowMinimise", - 1174: "common:WindowUnMinimise", - 1175: "common:WindowClosing", - 1176: "common:WindowZoom", - 1177: "common:WindowZoomIn", - 1178: "common:WindowZoomOut", - 1179: "common:WindowZoomReset", - 1180: "common:WindowFocus", - 1181: "common:WindowLostFocus", - 1182: "common:WindowShow", - 1183: "common:WindowHide", - 1184: "common:WindowDPIChanged", - 1185: "common:ThemeChanged", + 1168: "common:ApplicationStarted", + 1169: "common:WindowMaximise", + 1170: "common:WindowUnMaximise", + 1171: "common:WindowFullscreen", + 1172: "common:WindowUnFullscreen", + 1173: "common:WindowRestore", + 1174: "common:WindowMinimise", + 1175: "common:WindowUnMinimise", + 1176: "common:WindowClosing", + 1177: "common:WindowZoom", + 1178: "common:WindowZoomIn", + 1179: "common:WindowZoomOut", + 1180: "common:WindowZoomReset", + 1181: "common:WindowFocus", + 1182: "common:WindowLostFocus", + 1183: "common:WindowShow", + 1184: "common:WindowHide", + 1185: "common:WindowDPIChanged", + 1186: "common:ThemeChanged", } diff --git a/v3/pkg/events/events.txt b/v3/pkg/events/events.txt index 288714acf..2f724e7d4 100644 --- a/v3/pkg/events/events.txt +++ b/v3/pkg/events/events.txt @@ -141,6 +141,7 @@ windows:WindowUnMinimise windows:WindowClose windows:WindowSetFocus windows:WindowKillFocus +linux:SystemThemeChanged common:ApplicationStarted common:WindowMaximise common:WindowUnMaximise diff --git a/v3/pkg/events/events_linux.go b/v3/pkg/events/events_linux.go new file mode 100644 index 000000000..a7bc3b1fc --- /dev/null +++ b/v3/pkg/events/events_linux.go @@ -0,0 +1,21 @@ +//go:build linux + +package events + +/* +#include "events_linux.h" +#include +#include + +bool hasListener[MAX_EVENTS] = {false}; + +void registerListener(unsigned int event) { + hasListener[event] = true; +} + +bool hasListeners(unsigned int event) { + return hasListener[event]; +} + +*/ +import "C" diff --git a/v3/pkg/events/events_linux.h b/v3/pkg/events/events_linux.h new file mode 100644 index 000000000..58aa558ba --- /dev/null +++ b/v3/pkg/events/events_linux.h @@ -0,0 +1,14 @@ +//go:build linux + +#ifndef _events_linux_h +#define _events_linux_h + +extern void processApplicationEvent(unsigned int, void* data); +extern void processWindowEvent(unsigned int, unsigned int); + +#define EventSystemThemeChanged 1167 + +#define MAX_EVENTS 2 + + +#endif \ No newline at end of file diff --git a/v3/tasks/events/generate.go b/v3/tasks/events/generate.go index 970f8b3a9..82ba932aa 100644 --- a/v3/tasks/events/generate.go +++ b/v3/tasks/events/generate.go @@ -36,6 +36,16 @@ func newMacEvents() macEvents { $$MACEVENTSVALUES } } +var Linux = newLinuxEvents() + +type linuxEvents struct { +$$LINUXEVENTSDECL} + +func newLinuxEvents() linuxEvents { + return linuxEvents{ +$$LINUXEVENTSVALUES } +} + var Windows = newWindowsEvents() type windowsEvents struct { @@ -55,15 +65,27 @@ $$EVENTTOJS} ` -var eventsH = `//go:build darwin +var eventsDarwinH = `//go:build darwin -#ifndef _events_h -#define _events_h +#ifndef _events_darwin_h +#define _events_darwin_h extern void processApplicationEvent(unsigned int, void* data); extern void processWindowEvent(unsigned int, unsigned int); -$$CHEADEREVENTS +$$CDARWINHEADEREVENTS + +#endif` + +var eventsLinuxH = `//go:build linux + +#ifndef _events_linux_h +#define _events_linux_h + +extern void processApplicationEvent(unsigned int, void* data); +extern void processWindowEvent(unsigned int, unsigned int); + +$$CLINUXHEADEREVENTS #endif` @@ -73,6 +95,8 @@ export const EventTypes = { $$WINDOWSJSEVENTS }, Mac: { $$MACJSEVENTS }, + Linux: { +$$LINUXJSEVENTS}, Common: { $$COMMONJSEVENTS }, }; @@ -87,7 +111,7 @@ func main() { macEventsDecl := bytes.NewBufferString("") macEventsValues := bytes.NewBufferString("") - cHeaderEvents := bytes.NewBufferString("") + cDarwinHeaderEvents := bytes.NewBufferString("") windowDelegateEvents := bytes.NewBufferString("") applicationDelegateEvents := bytes.NewBufferString("") webviewDelegateEvents := bytes.NewBufferString("") @@ -104,8 +128,14 @@ func main() { eventToJS := bytes.NewBufferString("") + linuxEventsDecl := bytes.NewBufferString("") + linuxEventsValues := bytes.NewBufferString("") + linuxJSEvents := bytes.NewBufferString("") + cLinuxHeaderEvents := bytes.NewBufferString("") + var id int var maxMacEvents int + var maxLinuxEvents int var line []byte // Loop over each line in the file for id, line = range bytes.Split(eventNames, []byte{'\n'}) { @@ -135,6 +165,22 @@ func main() { // Add to buffer switch platform { + case "linux": + eventType := "ApplicationEventType" + if strings.HasPrefix(event, "Window") { + eventType = "WindowEventType" + } + if strings.HasPrefix(event, "WebView") { + eventType = "WindowEventType" + } + cLinuxHeaderEvents.WriteString("#define Event" + eventTitle + " " + strconv.Itoa(id) + "\n") + linuxEventsDecl.WriteString("\t" + eventTitle + " " + eventType + "\n") + linuxEventsValues.WriteString("\t\t" + event + ": " + strconv.Itoa(id) + ",\n") + linuxJSEvents.WriteString("\t\t" + event + ": \"" + strings.TrimSpace(string(line)) + "\",\n") + maxLinuxEvents++ + if ignoreEvent { + continue + } case "mac": eventType := "ApplicationEventType" if strings.HasPrefix(event, "Window") { @@ -146,9 +192,9 @@ func main() { macEventsDecl.WriteString("\t" + eventTitle + " " + eventType + "\n") macEventsValues.WriteString("\t\t" + event + ": " + strconv.Itoa(id) + ",\n") macJSEvents.WriteString("\t\t" + event + ": \"" + strings.TrimSpace(string(line)) + "\",\n") - cHeaderEvents.WriteString("#define Event" + eventTitle + " " + strconv.Itoa(id) + "\n") + cDarwinHeaderEvents.WriteString("#define Event" + eventTitle + " " + strconv.Itoa(id) + "\n") eventToJS.WriteString("\t" + strconv.Itoa(id) + ": \"" + strings.TrimSpace(string(line)) + "\",\n") - maxMacEvents = id + maxMacEvents++ if ignoreEvent { continue } @@ -210,10 +256,13 @@ func main() { } } - cHeaderEvents.WriteString("\n#define MAX_EVENTS " + strconv.Itoa(maxMacEvents+1) + "\n") + cLinuxHeaderEvents.WriteString("\n#define MAX_EVENTS " + strconv.Itoa(maxLinuxEvents+1) + "\n") + cDarwinHeaderEvents.WriteString("\n#define MAX_EVENTS " + strconv.Itoa(maxMacEvents+1) + "\n") // Save the eventsGo template substituting the values and decls templateToWrite := strings.ReplaceAll(eventsGo, "$$MACEVENTSDECL", macEventsDecl.String()) + templateToWrite = strings.ReplaceAll(templateToWrite, "$$LINUXEVENTSDECL", linuxEventsDecl.String()) + templateToWrite = strings.ReplaceAll(templateToWrite, "$$LINUXEVENTSVALUES", linuxEventsValues.String()) templateToWrite = strings.ReplaceAll(templateToWrite, "$$MACEVENTSVALUES", macEventsValues.String()) templateToWrite = strings.ReplaceAll(templateToWrite, "$$WINDOWSEVENTSDECL", windowsEventsDecl.String()) templateToWrite = strings.ReplaceAll(templateToWrite, "$$WINDOWSEVENTSVALUES", windowsEventsValues.String()) @@ -228,15 +277,23 @@ func main() { // Save the eventsJS template substituting the values and decls templateToWrite = strings.ReplaceAll(eventsJS, "$$MACJSEVENTS", macJSEvents.String()) templateToWrite = strings.ReplaceAll(templateToWrite, "$$WINDOWSJSEVENTS", windowsJSEvents.String()) + templateToWrite = strings.ReplaceAll(templateToWrite, "$$LINUXJSEVENTS", linuxJSEvents.String()) templateToWrite = strings.ReplaceAll(templateToWrite, "$$COMMONJSEVENTS", commonJSEvents.String()) err = os.WriteFile("../../internal/runtime/desktop/api/event_types.js", []byte(templateToWrite), 0644) if err != nil { panic(err) } - // Save the eventsH template substituting the values and decls - templateToWrite = strings.ReplaceAll(eventsH, "$$CHEADEREVENTS", cHeaderEvents.String()) - err = os.WriteFile("../../pkg/events/events.h", []byte(templateToWrite), 0644) + // Save the eventsDarwinH template substituting the values and decls + templateToWrite = strings.ReplaceAll(eventsDarwinH, "$$CDARWINHEADEREVENTS", cDarwinHeaderEvents.String()) + err = os.WriteFile("../../pkg/events/events_darwin.h", []byte(templateToWrite), 0644) + if err != nil { + panic(err) + } + + // Save the eventsDarwinH template substituting the values and decls + templateToWrite = strings.ReplaceAll(eventsLinuxH, "$$CLINUXHEADEREVENTS", cLinuxHeaderEvents.String()) + err = os.WriteFile("../../pkg/events/events_linux.h", []byte(templateToWrite), 0644) if err != nil { panic(err) }