Skip to content

Commit

Permalink
[linux] Implement events
Browse files Browse the repository at this point in the history
  • Loading branch information
leaanthony committed Oct 2, 2023
1 parent dc8cbcf commit 8ddd29d
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 56 deletions.
3 changes: 3 additions & 0 deletions v3/internal/runtime/desktop/api/event_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ export const EventTypes = {
WindowFileDraggingPerformed: "mac:WindowFileDraggingPerformed",
WindowFileDraggingExited: "mac:WindowFileDraggingExited",
},
Linux: {
SystemThemeChanged: "linux:SystemThemeChanged",
},
Common: {
ApplicationStarted: "common:ApplicationStarted",
WindowMaximise: "common:WindowMaximise",
Expand Down
13 changes: 6 additions & 7 deletions v3/pkg/application/application_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
package application

import (
"fmt"
"log"
"os"
"strings"
"sync"

"github.com/wailsapp/wails/v3/pkg/events"
)

func init() {
Expand Down Expand Up @@ -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)
}
Expand Down
20 changes: 20 additions & 0 deletions v3/pkg/application/events_common_linux.go
Original file line number Diff line number Diff line change
@@ -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
})
}
}
88 changes: 50 additions & 38 deletions v3/pkg/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
}
1 change: 1 addition & 0 deletions v3/pkg/events/events.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ windows:WindowUnMinimise
windows:WindowClose
windows:WindowSetFocus
windows:WindowKillFocus
linux:SystemThemeChanged
common:ApplicationStarted
common:WindowMaximise
common:WindowUnMaximise
Expand Down
21 changes: 21 additions & 0 deletions v3/pkg/events/events_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//go:build linux

package events

/*
#include "events_linux.h"
#include <stdlib.h>
#include <stdbool.h>
bool hasListener[MAX_EVENTS] = {false};
void registerListener(unsigned int event) {
hasListener[event] = true;
}
bool hasListeners(unsigned int event) {
return hasListener[event];
}
*/
import "C"
14 changes: 14 additions & 0 deletions v3/pkg/events/events_linux.h
Original file line number Diff line number Diff line change
@@ -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
79 changes: 68 additions & 11 deletions v3/tasks/events/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -55,15 +65,27 @@ $$EVENTTOJS}
`

var eventsH = `//go:build darwin
var eventsDarwinH = `//go:build darwin
#ifndef _events_darwin_h
#define _events_darwin_h
extern void processApplicationEvent(unsigned int, void* data);
extern void processWindowEvent(unsigned int, unsigned int);
$$CDARWINHEADEREVENTS
#endif`

#ifndef _events_h
#define _events_h
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);
$$CHEADEREVENTS
$$CLINUXHEADEREVENTS
#endif`

Expand All @@ -73,6 +95,8 @@ export const EventTypes = {
$$WINDOWSJSEVENTS },
Mac: {
$$MACJSEVENTS },
Linux: {
$$LINUXJSEVENTS},
Common: {
$$COMMONJSEVENTS },
};
Expand All @@ -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("")
Expand All @@ -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'}) {
Expand Down Expand Up @@ -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") {
Expand All @@ -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
}
Expand Down Expand Up @@ -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())
Expand All @@ -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)
}
Expand Down

0 comments on commit 8ddd29d

Please sign in to comment.