5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-17 09:29:30 +08:00

[v3 mac] Basic common event support. Taskfile refactor

This commit is contained in:
Lea Anthony 2023-05-17 07:58:40 +10:00
parent 3094c19cf3
commit f210357bfe
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
10 changed files with 129 additions and 63 deletions

View File

@ -44,7 +44,7 @@ Webview Window Interface Methods
| isMinimised() bool | Y | | Y | | | isMinimised() bool | Y | | Y | |
| maximise() | Y | | Y | | | maximise() | Y | | Y | |
| minimise() | Y | | Y | | | minimise() | Y | | Y | |
| nativeWindowHandle() (uintptr, error) | Y | | | | | nativeWindowHandle() (uintptr, error) | Y | | Y | |
| on(eventID uint) | | | Y | | | on(eventID uint) | | | Y | |
| openContextMenu(menu *Menu, data *ContextMenuData) | | | Y | | | openContextMenu(menu *Menu, data *ContextMenuData) | | | Y | |
| position() (int, int) | Y | | Y | | | position() (int, int) | Y | | Y | |

View File

@ -4,73 +4,73 @@ version: '3'
tasks: tasks:
build-runtime-debug: build:runtime:debug:
dir: internal/runtime dir: internal/runtime
internal: true internal: true
cmds: cmds:
- npx esbuild desktop/main.js --bundle --sourcemap=inline --outfile=runtime_debug_desktop_{{.PLATFORM}}.js --define:DEBUG=true --define:WINDOWS={{.WINDOWS}} --define:DARWIN={{.DARWIN}} --define:LINUX={{.LINUX}} --define:PLATFORM={{.PLATFORM}} - npx esbuild desktop/main.js --bundle --sourcemap=inline --outfile=runtime_debug_desktop_{{.PLATFORM}}.js --define:DEBUG=true --define:WINDOWS={{.WINDOWS}} --define:DARWIN={{.DARWIN}} --define:LINUX={{.LINUX}} --define:PLATFORM={{.PLATFORM}}
build-runtime-debug-windows: build:runtime:debug:windows:
cmds: cmds:
- task: build-runtime-debug - task: build:runtime:debug
vars: vars:
WINDOWS: true WINDOWS: true
DARWIN: false DARWIN: false
LINUX: false LINUX: false
PLATFORM: windows PLATFORM: windows
build-runtime-debug-linux: build:runtime:debug:linux:
cmds: cmds:
- task: build-runtime-debug - task: build:runtime:debug
vars: vars:
WINDOWS: false WINDOWS: false
DARWIN: false DARWIN: false
LINUX: true LINUX: true
PLATFORM: linux PLATFORM: linux
build-runtime-debug-darwin: build:runtime:debug:darwin:
cmds: cmds:
- task: build-runtime-debug - task: build:runtime:debug
vars: vars:
WINDOWS: false WINDOWS: false
DARWIN: true DARWIN: true
LINUX: false LINUX: false
PLATFORM: darwin PLATFORM: darwin
build-runtime-production: build:runtime:production:
dir: internal/runtime dir: internal/runtime
internal: true internal: true
cmds: cmds:
- npx esbuild desktop/main.js --bundle --minify --outfile=runtime_production_desktop_{{.PLATFORM}}.js --define:DEBUG=true --define:WINDOWS={{.WINDOWS}} --define:DARWIN={{.DARWIN}} --define:LINUX={{.LINUX}} --define:PLATFORM={{.PLATFORM}} - npx esbuild desktop/main.js --bundle --minify --outfile=runtime_production_desktop_{{.PLATFORM}}.js --define:DEBUG=true --define:WINDOWS={{.WINDOWS}} --define:DARWIN={{.DARWIN}} --define:LINUX={{.LINUX}} --define:PLATFORM={{.PLATFORM}}
build-runtime-production-windows: build:runtime:production:windows:
cmds: cmds:
- task: build-runtime-production - task: build:runtime:production
vars: vars:
WINDOWS: true WINDOWS: true
DARWIN: false DARWIN: false
LINUX: false LINUX: false
PLATFORM: windows PLATFORM: windows
build-runtime-production-linux: build:runtime:production:linux:
cmds: cmds:
- task: build-runtime-production - task: build:runtime:production
vars: vars:
WINDOWS: false WINDOWS: false
DARWIN: false DARWIN: false
LINUX: true LINUX: true
PLATFORM: linux PLATFORM: linux
build-runtime-production-darwin: build:runtime:production:darwin:
cmds: cmds:
- task: build-runtime-production - task: build:runtime:production
vars: vars:
WINDOWS: false WINDOWS: false
DARWIN: true DARWIN: true
LINUX: false LINUX: false
PLATFORM: darwin PLATFORM: darwin
install-runtime-dev-deps: install:runtime-dev-deps:
dir: internal/runtime/dev dir: internal/runtime/dev
internal: true internal: true
sources: sources:
@ -79,7 +79,7 @@ tasks:
- npm install - npm install
install-runtime-deps: install:runtime-deps:
dir: internal/runtime dir: internal/runtime
internal: true internal: true
sources: sources:
@ -87,35 +87,35 @@ tasks:
cmds: cmds:
- npm install - npm install
test-runtime: test:runtime:
dir: internal/runtime dir: internal/runtime
cmds: cmds:
- npx vitest run - npx vitest run
update-runtime: update:runtime:
dir: internal/runtime dir: internal/runtime
cmds: cmds:
- npx npm-check-updates -u - npx npm-check-updates -u
build-runtime-all: build:runtime-all:
dir: internal/runtime dir: internal/runtime
deps: deps:
- build-runtime-production-darwin - build:runtime:production:darwin
- build-runtime-production-windows - build:runtime:production:windows
- build-runtime-production-linux - build:runtime:production:linux
- build-runtime-debug-darwin - build:runtime:debug:darwin
- build-runtime-debug-windows - build:runtime:debug:windows
- build-runtime-debug-linux - build:runtime:debug:linux
cmds: cmds:
- cmd: echo "build complete" - cmd: echo "build complete"
build-runtime: build:runtime:
dir: internal/runtime dir: internal/runtime
deps: deps:
- install-runtime-deps - install:runtime-deps
cmds: cmds:
- task: build-runtime-all - task: build:runtime-all
recreate-template-dir: recreate-template-dir:
dir: internal/templates dir: internal/templates
@ -125,7 +125,7 @@ tasks:
- rm -rf {{.TEMPLATE_DIR}} - rm -rf {{.TEMPLATE_DIR}}
- mkdir -p {{.TEMPLATE_DIR}} - mkdir -p {{.TEMPLATE_DIR}}
generate-template: generate:template:
dir: internal/templates/{{.TEMPLATE}} dir: internal/templates/{{.TEMPLATE}}
deps: deps:
- task: recreate-template-dir - task: recreate-template-dir
@ -152,49 +152,54 @@ tasks:
- go install - go install
- echo "Reinstalled wails CLI" - echo "Reinstalled wails CLI"
generate-templates: generate:events:
dir: tasks/events
cmds:
- go run generate.go
generate:templates:
dir: internal/templates/ dir: internal/templates/
deps: deps:
- task: generate-template - task: generate:template
vars: vars:
TEMPLATE: svelte TEMPLATE: svelte
- task: generate-template - task: generate:template
vars: vars:
TEMPLATE: svelte-ts TEMPLATE: svelte-ts
- task: generate-template - task: generate:template
vars: vars:
TEMPLATE: vue TEMPLATE: vue
- task: generate-template - task: generate:template
vars: vars:
TEMPLATE: vue-ts TEMPLATE: vue-ts
- task: generate-template - task: generate:template
vars: vars:
TEMPLATE: react TEMPLATE: react
- task: generate-template - task: generate:template
vars: vars:
TEMPLATE: react-ts TEMPLATE: react-ts
- task: generate-template - task: generate:template
vars: vars:
TEMPLATE: preact TEMPLATE: preact
- task: generate-template - task: generate:template
vars: vars:
TEMPLATE: preact-ts TEMPLATE: preact-ts
- task: generate-template - task: generate:template
vars: vars:
TEMPLATE: lit TEMPLATE: lit
- task: generate-template - task: generate:template
vars: vars:
TEMPLATE: lit-ts TEMPLATE: lit-ts
- task: generate-template - task: generate:template
vars: vars:
TEMPLATE: vanilla TEMPLATE: vanilla
- task: generate-template - task: generate:template
vars: vars:
TEMPLATE: vanilla-ts TEMPLATE: vanilla-ts
- task: generate-template - task: generate:template
vars: vars:
TEMPLATE: react-swc TEMPLATE: react-swc
- task: generate-template - task: generate:template
vars: vars:
TEMPLATE: react-swc-ts TEMPLATE: react-swc-ts
cmds: cmds:

View File

@ -26,10 +26,12 @@ func main() {
}, },
}) })
// Custom event handling
app.Events.On("myevent", func(e *application.WailsEvent) { app.Events.On("myevent", func(e *application.WailsEvent) {
log.Printf("[Go] WailsEvent received: %+v\n", e) log.Printf("[Go] WailsEvent received: %+v\n", e)
}) })
// OS specific application events
app.On(events.Mac.ApplicationDidFinishLaunching, func() { app.On(events.Mac.ApplicationDidFinishLaunching, func() {
for { for {
log.Println("Sending event") log.Println("Sending event")
@ -41,6 +43,11 @@ func main() {
} }
}) })
// Platform agnostic events
app.On(events.Common.ApplicationStarted, func() {
println("events.Common.ApplicationStarted fired!")
})
app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
Title: "Events Demo", Title: "Events Demo",
Mac: application.MacWindow{ Mac: application.MacWindow{

View File

@ -186,6 +186,7 @@ func (m *macosApp) run() error {
C.setActivationPolicy(C.int(m.parent.options.Mac.ActivationPolicy)) C.setActivationPolicy(C.int(m.parent.options.Mac.ActivationPolicy))
C.activateIgnoringOtherApps() C.activateIgnoringOtherApps()
}) })
m.setupCommonEvents()
// setup event listeners // setup event listeners
for eventID := range m.parent.applicationEventListeners { for eventID := range m.parent.applicationEventListeners {
m.on(eventID) m.on(eventID)

View File

@ -24,6 +24,8 @@ type WailsEvent struct {
Sender string `json:"sender"` Sender string `json:"sender"`
} }
var commonEvents = make(chan uint)
func (e WailsEvent) ToJSON() string { func (e WailsEvent) ToJSON() string {
marshal, err := json.Marshal(&e) marshal, err := json.Marshal(&e)
if err != nil { if err != nil {

View File

@ -0,0 +1,17 @@
//go:build darwin
package application
import "github.com/wailsapp/wails/v3/pkg/events"
var commonApplicationEventMap = map[events.ApplicationEventType]events.ApplicationEventType{
events.Mac.ApplicationDidFinishLaunching: events.Common.ApplicationStarted,
}
func (m *macosApp) setupCommonEvents() {
for sourceEvent, targetEvent := range commonApplicationEventMap {
m.parent.On(sourceEvent, func() {
applicationEvents <- uint(targetEvent)
})
}
}

View File

@ -1,14 +1,11 @@
//go:build darwin //go:build darwin
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#import "webview_window_darwin.h" #import "webview_window_darwin.h"
#import "../events/events.h" #import "../events/events.h"
extern void processMessage(unsigned int, const char*); extern void processMessage(unsigned int, const char*);
extern void processURLRequest(unsigned int, void *); extern void processURLRequest(unsigned int, void *);
extern bool hasListeners(unsigned int); extern bool hasListeners(unsigned int);
@implementation WebviewWindow @implementation WebviewWindow
- (WebviewWindow*) initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation; - (WebviewWindow*) initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation;
{ {
@ -50,7 +47,6 @@ extern bool hasListeners(unsigned int);
[super dealloc]; [super dealloc];
} }
@end @end
@implementation WebviewWindowDelegate @implementation WebviewWindowDelegate
- (BOOL)windowShouldClose:(NSWindow *)sender { - (BOOL)windowShouldClose:(NSWindow *)sender {
if( self.hideOnClose ) { if( self.hideOnClose ) {

View File

@ -7,6 +7,18 @@ const (
FilesDropped WindowEventType = iota FilesDropped WindowEventType = iota
) )
var Common = newCommonEvents()
type commonEvents struct {
ApplicationStarted ApplicationEventType
}
func newCommonEvents() commonEvents {
return commonEvents{
ApplicationStarted: 1152,
}
}
var Mac = newMacEvents() var Mac = newMacEvents()
type macEvents struct { type macEvents struct {

View File

@ -126,3 +126,4 @@ windows:APMSuspend
windows:APMResumeAutomatic windows:APMResumeAutomatic
windows:APMResumeSuspend windows:APMResumeSuspend
windows:APMPowerSettingChange windows:APMPowerSettingChange
common:ApplicationStarted

View File

@ -16,6 +16,16 @@ const (
FilesDropped WindowEventType = iota FilesDropped WindowEventType = iota
) )
var Common = newCommonEvents()
type commonEvents struct {
$$COMMONEVENTSDECL}
func newCommonEvents() commonEvents {
return commonEvents{
$$COMMONEVENTSVALUES }
}
var Mac = newMacEvents() var Mac = newMacEvents()
type macEvents struct { type macEvents struct {
@ -68,6 +78,9 @@ func main() {
windowsEventsDecl := bytes.NewBufferString("") windowsEventsDecl := bytes.NewBufferString("")
windowsEventsValues := bytes.NewBufferString("") windowsEventsValues := bytes.NewBufferString("")
commonEventsDecl := bytes.NewBufferString("")
commonEventsValues := bytes.NewBufferString("")
var id int var id int
var maxMacEvents int var maxMacEvents int
var line []byte var line []byte
@ -145,6 +158,16 @@ func main() {
`) `)
} }
case "common":
eventType := "ApplicationEventType"
if strings.HasPrefix(event, "Window") {
eventType = "WindowEventType"
}
if strings.HasPrefix(event, "WebView") {
eventType = "WindowEventType"
}
commonEventsDecl.WriteString("\t" + eventTitle + " " + eventType + "\n")
commonEventsValues.WriteString("\t\t" + event + ": " + strconv.Itoa(id) + ",\n")
case "windows": case "windows":
eventType := "ApplicationEventType" eventType := "ApplicationEventType"
if strings.HasPrefix(event, "Window") { if strings.HasPrefix(event, "Window") {
@ -200,6 +223,8 @@ func main() {
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())
templateToWrite = strings.ReplaceAll(templateToWrite, "$$COMMONEVENTSDECL", commonEventsDecl.String())
templateToWrite = strings.ReplaceAll(templateToWrite, "$$COMMONEVENTSVALUES", commonEventsValues.String())
err = os.WriteFile("../../pkg/events/events.go", []byte(templateToWrite), 0644) err = os.WriteFile("../../pkg/events/events.go", []byte(templateToWrite), 0644)
if err != nil { if err != nil {
panic(err) panic(err)
@ -213,7 +238,7 @@ func main() {
} }
// Load the window_delegate.m file // Load the window_delegate.m file
windowDelegate, err := os.ReadFile("../../pkg/application/webview_window.m") windowDelegate, err := os.ReadFile("../../pkg/application/webview_window_darwin.m")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -243,13 +268,13 @@ func main() {
} }
} }
} }
err = os.WriteFile("../../pkg/application/webview_window.m", buffer.Bytes(), 0755) err = os.WriteFile("../../pkg/application/webview_window_darwin.m", buffer.Bytes(), 0755)
if err != nil { if err != nil {
panic(err) panic(err)
} }
// Load the app_delegate.m file // Load the app_delegate.m file
appDelegate, err := os.ReadFile("../../pkg/application/app_delegate.m") appDelegate, err := os.ReadFile("../../pkg/application/application_darwin_delegate.m")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -277,7 +302,7 @@ func main() {
} }
} }
} }
err = os.WriteFile("../../pkg/application/app_delegate.m", buffer.Bytes(), 0755) err = os.WriteFile("../../pkg/application/application_darwin_delegate.m", buffer.Bytes(), 0755)
if err != nil { if err != nil {
panic(err) panic(err)
} }