5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-10 02:40:26 +08:00

[linux] Implement events

This commit is contained in:
Lea Anthony 2023-10-03 08:37:11 +11:00
parent dc8cbcf410
commit 8ddd29d285
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
8 changed files with 183 additions and 56 deletions

View File

@ -147,6 +147,9 @@ export const EventTypes = {
WindowFileDraggingPerformed: "mac:WindowFileDraggingPerformed", WindowFileDraggingPerformed: "mac:WindowFileDraggingPerformed",
WindowFileDraggingExited: "mac:WindowFileDraggingExited", WindowFileDraggingExited: "mac:WindowFileDraggingExited",
}, },
Linux: {
SystemThemeChanged: "linux:SystemThemeChanged",
},
Common: { Common: {
ApplicationStarted: "common:ApplicationStarted", ApplicationStarted: "common:ApplicationStarted",
WindowMaximise: "common:WindowMaximise", WindowMaximise: "common:WindowMaximise",

View File

@ -3,13 +3,10 @@
package application package application
import ( import (
"fmt"
"log" "log"
"os" "os"
"strings" "strings"
"sync" "sync"
"github.com/wailsapp/wails/v3/pkg/events"
) )
func init() { func init() {
@ -104,10 +101,12 @@ func (m *linuxApp) run() error {
// Add a hook to the ApplicationDidFinishLaunching event // Add a hook to the ApplicationDidFinishLaunching event
// FIXME: add Wails specific events - i.e. Shouldn't platform specific ones be translated to Wails events? // 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) { //m.parent.On(events.Mac.ApplicationDidFinishLaunching, func(evt *Event) {
// Do we need to do anything now? // // Do we need to do anything now?
fmt.Println("events.Mac.ApplicationDidFinishLaunching received!") // fmt.Println("events.Mac.ApplicationDidFinishLaunching received!")
}) //})
m.setupCommonEvents()
return appRun(m.application) return appRun(m.application)
} }

View File

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

View File

@ -33,25 +33,25 @@ type commonEvents struct {
func newCommonEvents() commonEvents { func newCommonEvents() commonEvents {
return commonEvents{ return commonEvents{
ApplicationStarted: 1167, ApplicationStarted: 1168,
WindowMaximise: 1168, WindowMaximise: 1169,
WindowUnMaximise: 1169, WindowUnMaximise: 1170,
WindowFullscreen: 1170, WindowFullscreen: 1171,
WindowUnFullscreen: 1171, WindowUnFullscreen: 1172,
WindowRestore: 1172, WindowRestore: 1173,
WindowMinimise: 1173, WindowMinimise: 1174,
WindowUnMinimise: 1174, WindowUnMinimise: 1175,
WindowClosing: 1175, WindowClosing: 1176,
WindowZoom: 1176, WindowZoom: 1177,
WindowZoomIn: 1177, WindowZoomIn: 1178,
WindowZoomOut: 1178, WindowZoomOut: 1179,
WindowZoomReset: 1179, WindowZoomReset: 1180,
WindowFocus: 1180, WindowFocus: 1181,
WindowLostFocus: 1181, WindowLostFocus: 1182,
WindowShow: 1182, WindowShow: 1183,
WindowHide: 1183, WindowHide: 1184,
WindowDPIChanged: 1184, WindowDPIChanged: 1185,
ThemeChanged: 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() var Windows = newWindowsEvents()
type windowsEvents struct { type windowsEvents struct {
@ -509,23 +521,23 @@ var eventToJS = map[uint]string{
1164: "windows:WindowClose", 1164: "windows:WindowClose",
1165: "windows:WindowSetFocus", 1165: "windows:WindowSetFocus",
1166: "windows:WindowKillFocus", 1166: "windows:WindowKillFocus",
1167: "common:ApplicationStarted", 1168: "common:ApplicationStarted",
1168: "common:WindowMaximise", 1169: "common:WindowMaximise",
1169: "common:WindowUnMaximise", 1170: "common:WindowUnMaximise",
1170: "common:WindowFullscreen", 1171: "common:WindowFullscreen",
1171: "common:WindowUnFullscreen", 1172: "common:WindowUnFullscreen",
1172: "common:WindowRestore", 1173: "common:WindowRestore",
1173: "common:WindowMinimise", 1174: "common:WindowMinimise",
1174: "common:WindowUnMinimise", 1175: "common:WindowUnMinimise",
1175: "common:WindowClosing", 1176: "common:WindowClosing",
1176: "common:WindowZoom", 1177: "common:WindowZoom",
1177: "common:WindowZoomIn", 1178: "common:WindowZoomIn",
1178: "common:WindowZoomOut", 1179: "common:WindowZoomOut",
1179: "common:WindowZoomReset", 1180: "common:WindowZoomReset",
1180: "common:WindowFocus", 1181: "common:WindowFocus",
1181: "common:WindowLostFocus", 1182: "common:WindowLostFocus",
1182: "common:WindowShow", 1183: "common:WindowShow",
1183: "common:WindowHide", 1184: "common:WindowHide",
1184: "common:WindowDPIChanged", 1185: "common:WindowDPIChanged",
1185: "common:ThemeChanged", 1186: "common:ThemeChanged",
} }

View File

@ -141,6 +141,7 @@ windows:WindowUnMinimise
windows:WindowClose windows:WindowClose
windows:WindowSetFocus windows:WindowSetFocus
windows:WindowKillFocus windows:WindowKillFocus
linux:SystemThemeChanged
common:ApplicationStarted common:ApplicationStarted
common:WindowMaximise common:WindowMaximise
common:WindowUnMaximise common:WindowUnMaximise

View File

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

View File

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

View File

@ -36,6 +36,16 @@ func newMacEvents() macEvents {
$$MACEVENTSVALUES } $$MACEVENTSVALUES }
} }
var Linux = newLinuxEvents()
type linuxEvents struct {
$$LINUXEVENTSDECL}
func newLinuxEvents() linuxEvents {
return linuxEvents{
$$LINUXEVENTSVALUES }
}
var Windows = newWindowsEvents() var Windows = newWindowsEvents()
type windowsEvents struct { type windowsEvents struct {
@ -55,15 +65,27 @@ $$EVENTTOJS}
` `
var eventsH = `//go:build darwin var eventsDarwinH = `//go:build darwin
#ifndef _events_h #ifndef _events_darwin_h
#define _events_h #define _events_darwin_h
extern void processApplicationEvent(unsigned int, void* data); extern void processApplicationEvent(unsigned int, void* data);
extern void processWindowEvent(unsigned int, unsigned int); 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` #endif`
@ -73,6 +95,8 @@ export const EventTypes = {
$$WINDOWSJSEVENTS }, $$WINDOWSJSEVENTS },
Mac: { Mac: {
$$MACJSEVENTS }, $$MACJSEVENTS },
Linux: {
$$LINUXJSEVENTS},
Common: { Common: {
$$COMMONJSEVENTS }, $$COMMONJSEVENTS },
}; };
@ -87,7 +111,7 @@ func main() {
macEventsDecl := bytes.NewBufferString("") macEventsDecl := bytes.NewBufferString("")
macEventsValues := bytes.NewBufferString("") macEventsValues := bytes.NewBufferString("")
cHeaderEvents := bytes.NewBufferString("") cDarwinHeaderEvents := bytes.NewBufferString("")
windowDelegateEvents := bytes.NewBufferString("") windowDelegateEvents := bytes.NewBufferString("")
applicationDelegateEvents := bytes.NewBufferString("") applicationDelegateEvents := bytes.NewBufferString("")
webviewDelegateEvents := bytes.NewBufferString("") webviewDelegateEvents := bytes.NewBufferString("")
@ -104,8 +128,14 @@ func main() {
eventToJS := bytes.NewBufferString("") eventToJS := bytes.NewBufferString("")
linuxEventsDecl := bytes.NewBufferString("")
linuxEventsValues := bytes.NewBufferString("")
linuxJSEvents := bytes.NewBufferString("")
cLinuxHeaderEvents := bytes.NewBufferString("")
var id int var id int
var maxMacEvents int var maxMacEvents int
var maxLinuxEvents int
var line []byte var line []byte
// Loop over each line in the file // Loop over each line in the file
for id, line = range bytes.Split(eventNames, []byte{'\n'}) { for id, line = range bytes.Split(eventNames, []byte{'\n'}) {
@ -135,6 +165,22 @@ func main() {
// Add to buffer // Add to buffer
switch platform { 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": case "mac":
eventType := "ApplicationEventType" eventType := "ApplicationEventType"
if strings.HasPrefix(event, "Window") { if strings.HasPrefix(event, "Window") {
@ -146,9 +192,9 @@ func main() {
macEventsDecl.WriteString("\t" + eventTitle + " " + eventType + "\n") macEventsDecl.WriteString("\t" + eventTitle + " " + eventType + "\n")
macEventsValues.WriteString("\t\t" + event + ": " + strconv.Itoa(id) + ",\n") macEventsValues.WriteString("\t\t" + event + ": " + strconv.Itoa(id) + ",\n")
macJSEvents.WriteString("\t\t" + event + ": \"" + strings.TrimSpace(string(line)) + "\",\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") eventToJS.WriteString("\t" + strconv.Itoa(id) + ": \"" + strings.TrimSpace(string(line)) + "\",\n")
maxMacEvents = id maxMacEvents++
if ignoreEvent { if ignoreEvent {
continue 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 // Save the eventsGo template substituting the values and decls
templateToWrite := strings.ReplaceAll(eventsGo, "$$MACEVENTSDECL", macEventsDecl.String()) 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, "$$MACEVENTSVALUES", macEventsValues.String())
templateToWrite = strings.ReplaceAll(templateToWrite, "$$WINDOWSEVENTSDECL", windowsEventsDecl.String()) templateToWrite = strings.ReplaceAll(templateToWrite, "$$WINDOWSEVENTSDECL", windowsEventsDecl.String())
templateToWrite = strings.ReplaceAll(templateToWrite, "$$WINDOWSEVENTSVALUES", windowsEventsValues.String()) templateToWrite = strings.ReplaceAll(templateToWrite, "$$WINDOWSEVENTSVALUES", windowsEventsValues.String())
@ -228,15 +277,23 @@ func main() {
// Save the eventsJS template substituting the values and decls // Save the eventsJS template substituting the values and decls
templateToWrite = strings.ReplaceAll(eventsJS, "$$MACJSEVENTS", macJSEvents.String()) templateToWrite = strings.ReplaceAll(eventsJS, "$$MACJSEVENTS", macJSEvents.String())
templateToWrite = strings.ReplaceAll(templateToWrite, "$$WINDOWSJSEVENTS", windowsJSEvents.String()) templateToWrite = strings.ReplaceAll(templateToWrite, "$$WINDOWSJSEVENTS", windowsJSEvents.String())
templateToWrite = strings.ReplaceAll(templateToWrite, "$$LINUXJSEVENTS", linuxJSEvents.String())
templateToWrite = strings.ReplaceAll(templateToWrite, "$$COMMONJSEVENTS", commonJSEvents.String()) templateToWrite = strings.ReplaceAll(templateToWrite, "$$COMMONJSEVENTS", commonJSEvents.String())
err = os.WriteFile("../../internal/runtime/desktop/api/event_types.js", []byte(templateToWrite), 0644) err = os.WriteFile("../../internal/runtime/desktop/api/event_types.js", []byte(templateToWrite), 0644)
if err != nil { if err != nil {
panic(err) panic(err)
} }
// Save the eventsH template substituting the values and decls // Save the eventsDarwinH template substituting the values and decls
templateToWrite = strings.ReplaceAll(eventsH, "$$CHEADEREVENTS", cHeaderEvents.String()) templateToWrite = strings.ReplaceAll(eventsDarwinH, "$$CDARWINHEADEREVENTS", cDarwinHeaderEvents.String())
err = os.WriteFile("../../pkg/events/events.h", []byte(templateToWrite), 0644) 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 { if err != nil {
panic(err) panic(err)
} }