From ca8d41dd3b3f3835f87ccbc147b8ed57e5322b2e Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Sun, 25 Jul 2021 15:37:30 +1000 Subject: [PATCH] [windows-x] Refactor runtime again --- v2/internal/bridge/client.go | 12 ++-- v2/internal/bridge/dialog_client.go | 12 ++-- v2/internal/ffenestri/ffenestri_client.go | 9 +-- .../ffenestri/ffenestri_client_windows.go | 23 ++++---- .../messagedispatcher/dispatchclient.go | 12 ++-- .../messagedispatcher/messagedispatcher.go | 13 ++--- v2/internal/subsystem/call.go | 14 ++--- v2/internal/webserver/websockets.go | 12 ++-- v2/pkg/runtime/{dialog => }/dialog.go | 22 +++---- v2/pkg/runtime/events.go | 45 ++++++++++++++ v2/pkg/runtime/{log => }/log.go | 2 +- v2/pkg/runtime/{menu => }/menu.go | 4 +- .../runtime/{system/system.go => runtime.go} | 2 +- v2/pkg/runtime/{window => }/window.go | 58 +++++++++---------- 14 files changed, 142 insertions(+), 98 deletions(-) rename v2/pkg/runtime/{dialog => }/dialog.go (86%) create mode 100644 v2/pkg/runtime/events.go rename v2/pkg/runtime/{log => }/log.go (98%) rename v2/pkg/runtime/{menu => }/menu.go (95%) rename v2/pkg/runtime/{system/system.go => runtime.go} (94%) rename v2/pkg/runtime/{window => }/window.go (51%) diff --git a/v2/internal/bridge/client.go b/v2/internal/bridge/client.go index ac5a5617a..a6e727d3c 100644 --- a/v2/internal/bridge/client.go +++ b/v2/internal/bridge/client.go @@ -1,7 +1,7 @@ package bridge import ( - "github.com/wailsapp/wails/v2/pkg/runtime/dialog" + "github.com/wailsapp/wails/v2/pkg/runtime" ) type BridgeClient struct { @@ -34,23 +34,23 @@ func (b BridgeClient) CallResult(message string) { b.session.sendMessage("c" + message) } -func (b BridgeClient) OpenFileDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) { +func (b BridgeClient) OpenFileDialog(dialogOptions runtime.OpenDialogOptions, callbackID string) { // Handled by dialog_client } -func (b BridgeClient) OpenMultipleFilesDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) { +func (b BridgeClient) OpenMultipleFilesDialog(dialogOptions runtime.OpenDialogOptions, callbackID string) { // Handled by dialog_client } -func (b BridgeClient) OpenDirectoryDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) { +func (b BridgeClient) OpenDirectoryDialog(dialogOptions runtime.OpenDialogOptions, callbackID string) { // Handled by dialog_client } -func (b BridgeClient) SaveDialog(dialogOptions dialog.SaveDialogOptions, callbackID string) { +func (b BridgeClient) SaveDialog(dialogOptions runtime.SaveDialogOptions, callbackID string) { // Handled by dialog_client } -func (b BridgeClient) MessageDialog(dialogOptions dialog.MessageDialogOptions, callbackID string) { +func (b BridgeClient) MessageDialog(dialogOptions runtime.MessageDialogOptions, callbackID string) { // Handled by dialog_client } diff --git a/v2/internal/bridge/dialog_client.go b/v2/internal/bridge/dialog_client.go index f9cc63141..bc65c13cd 100644 --- a/v2/internal/bridge/dialog_client.go +++ b/v2/internal/bridge/dialog_client.go @@ -2,6 +2,7 @@ package bridge import ( "fmt" + "github.com/wailsapp/wails/v2/pkg/runtime" "os/exec" "strconv" "strings" @@ -11,7 +12,6 @@ import ( "github.com/wailsapp/wails/v2/internal/logger" "github.com/leaanthony/slicer" - "github.com/wailsapp/wails/v2/pkg/runtime/dialog" ) type DialogClient struct { @@ -37,17 +37,17 @@ func (d *DialogClient) NotifyEvent(message string) { func (d *DialogClient) CallResult(message string) { } -func (d *DialogClient) OpenDirectoryDialog(options dialog.OpenDialogOptions, callbackID string) { +func (d *DialogClient) OpenDirectoryDialog(options runtime.OpenDialogOptions, callbackID string) { } -func (d *DialogClient) OpenFileDialog(options dialog.OpenDialogOptions, callbackID string) { +func (d *DialogClient) OpenFileDialog(options runtime.OpenDialogOptions, callbackID string) { } -func (d *DialogClient) OpenMultipleFilesDialog(options dialog.OpenDialogOptions, callbackID string) { +func (d *DialogClient) OpenMultipleFilesDialog(options runtime.OpenDialogOptions, callbackID string) { } -func (d *DialogClient) SaveDialog(options dialog.SaveDialogOptions, callbackID string) { +func (d *DialogClient) SaveDialog(options runtime.SaveDialogOptions, callbackID string) { } -func (d *DialogClient) MessageDialog(options dialog.MessageDialogOptions, callbackID string) { +func (d *DialogClient) MessageDialog(options runtime.MessageDialogOptions, callbackID string) { osa, err := exec.LookPath("osascript") if err != nil { diff --git a/v2/internal/ffenestri/ffenestri_client.go b/v2/internal/ffenestri/ffenestri_client.go index 3fb43f236..48a8f6392 100644 --- a/v2/internal/ffenestri/ffenestri_client.go +++ b/v2/internal/ffenestri/ffenestri_client.go @@ -8,6 +8,7 @@ package ffenestri import "C" import ( + runtime2 "github.com/wailsapp/wails/v2/pkg/runtime" "runtime" "strconv" "strings" @@ -127,7 +128,7 @@ func (c *Client) WindowSetColour(colour int) { } // OpenFileDialog will open a dialog with the given title and filter -func (c *Client) OpenFileDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) { +func (c *Client) OpenFileDialog(dialogOptions runtime2.OpenDialogOptions, callbackID string) { filters := []string{} if runtime.GOOS == "darwin" { for _, filter := range dialogOptions.Filters { @@ -151,7 +152,7 @@ func (c *Client) OpenFileDialog(dialogOptions dialog.OpenDialogOptions, callback } // OpenDirectoryDialog will open a dialog with the given title and filter -func (c *Client) OpenDirectoryDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) { +func (c *Client) OpenDirectoryDialog(dialogOptions runtime2.OpenDialogOptions, callbackID string) { filters := []string{} if runtime.GOOS == "darwin" { for _, filter := range dialogOptions.Filters { @@ -175,7 +176,7 @@ func (c *Client) OpenDirectoryDialog(dialogOptions dialog.OpenDialogOptions, cal } // OpenMultipleFilesDialog will open a dialog with the given title and filter -func (c *Client) OpenMultipleFilesDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) { +func (c *Client) OpenMultipleFilesDialog(dialogOptions runtime2.OpenDialogOptions, callbackID string) { filters := []string{} if runtime.GOOS == "darwin" { for _, filter := range dialogOptions.Filters { @@ -219,7 +220,7 @@ func (c *Client) SaveDialog(dialogOptions *dialog.SaveDialog, callbackID string) } // MessageDialog will open a message dialog with the given options -func (c *Client) MessageDialog(dialogOptions dialog.MessageDialogOptions, callbackID string) { +func (c *Client) MessageDialog(dialogOptions runtime2.MessageDialogOptions, callbackID string) { // Sanity check button length if len(dialogOptions.Buttons) > 4 { diff --git a/v2/internal/ffenestri/ffenestri_client_windows.go b/v2/internal/ffenestri/ffenestri_client_windows.go index 7a1d950bd..542fbf79a 100644 --- a/v2/internal/ffenestri/ffenestri_client_windows.go +++ b/v2/internal/ffenestri/ffenestri_client_windows.go @@ -10,13 +10,12 @@ import "C" import ( "encoding/json" "github.com/leaanthony/go-common-file-dialog/cfd" + "github.com/wailsapp/wails/v2/pkg/runtime" "golang.org/x/sys/windows" "log" "strconv" "syscall" - "github.com/wailsapp/wails/v2/pkg/runtime/dialog" - "github.com/wailsapp/wails/v2/internal/logger" ) @@ -131,7 +130,7 @@ func (c *Client) WindowSetColour(colour int) { C.SetColour(c.app.app, r, g, b, a) } -func convertFilters(filters []dialog.FileFilter) []cfd.FileFilter { +func convertFilters(filters []runtime.FileFilter) []cfd.FileFilter { var result []cfd.FileFilter for _, filter := range filters { result = append(result, cfd.FileFilter(filter)) @@ -140,7 +139,7 @@ func convertFilters(filters []dialog.FileFilter) []cfd.FileFilter { } // OpenFileDialog will open a dialog with the given title and filter -func (c *Client) OpenFileDialog(options dialog.OpenDialogOptions, callbackID string) { +func (c *Client) OpenFileDialog(options runtime.OpenDialogOptions, callbackID string) { config := cfd.DialogConfig{ Folder: options.DefaultDirectory, FileFilters: convertFilters(options.Filters), @@ -166,7 +165,7 @@ func (c *Client) OpenFileDialog(options dialog.OpenDialogOptions, callbackID str } // OpenDirectoryDialog will open a dialog with the given title and filter -func (c *Client) OpenDirectoryDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) { +func (c *Client) OpenDirectoryDialog(dialogOptions runtime.OpenDialogOptions, callbackID string) { config := cfd.DialogConfig{ Title: dialogOptions.Title, Role: "PickFolder", @@ -191,7 +190,7 @@ func (c *Client) OpenDirectoryDialog(dialogOptions dialog.OpenDialogOptions, cal } // OpenMultipleFilesDialog will open a dialog with the given title and filter -func (c *Client) OpenMultipleFilesDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) { +func (c *Client) OpenMultipleFilesDialog(dialogOptions runtime.OpenDialogOptions, callbackID string) { config := cfd.DialogConfig{ Title: dialogOptions.Title, Role: "OpenMultipleFiles", @@ -222,7 +221,7 @@ func (c *Client) OpenMultipleFilesDialog(dialogOptions dialog.OpenDialogOptions, } // SaveDialog will open a dialog with the given title and filter -func (c *Client) SaveDialog(dialogOptions dialog.SaveDialogOptions, callbackID string) { +func (c *Client) SaveDialog(dialogOptions runtime.SaveDialogOptions, callbackID string) { saveDialog, err := cfd.NewSaveFileDialog(cfd.DialogConfig{ Title: dialogOptions.Title, Role: "SaveFile", @@ -246,7 +245,7 @@ func (c *Client) SaveDialog(dialogOptions dialog.SaveDialogOptions, callbackID s } // MessageDialog will open a message dialog with the given options -func (c *Client) MessageDialog(options dialog.MessageDialogOptions, callbackID string) { +func (c *Client) MessageDialog(options runtime.MessageDialogOptions, callbackID string) { title, err := syscall.UTF16PtrFromString(options.Title) if err != nil { @@ -258,13 +257,13 @@ func (c *Client) MessageDialog(options dialog.MessageDialogOptions, callbackID s } var flags uint32 switch options.Type { - case dialog.InfoDialog: + case runtime.InfoDialog: flags = windows.MB_OK | windows.MB_ICONINFORMATION - case dialog.ErrorDialog: + case runtime.ErrorDialog: flags = windows.MB_ICONERROR | windows.MB_OK - case dialog.QuestionDialog: + case runtime.QuestionDialog: flags = windows.MB_YESNO - case dialog.WarningDialog: + case runtime.WarningDialog: flags = windows.MB_OK | windows.MB_ICONWARNING } diff --git a/v2/internal/messagedispatcher/dispatchclient.go b/v2/internal/messagedispatcher/dispatchclient.go index 76a0c024a..af693660c 100644 --- a/v2/internal/messagedispatcher/dispatchclient.go +++ b/v2/internal/messagedispatcher/dispatchclient.go @@ -2,11 +2,11 @@ package messagedispatcher import ( "fmt" + "github.com/wailsapp/wails/v2/pkg/runtime" "github.com/wailsapp/wails/v2/internal/logger" "github.com/wailsapp/wails/v2/internal/messagedispatcher/message" "github.com/wailsapp/wails/v2/internal/servicebus" - "github.com/wailsapp/wails/v2/pkg/runtime/dialog" ) // Client defines what a frontend client can do @@ -14,11 +14,11 @@ type Client interface { Quit() NotifyEvent(message string) CallResult(message string) - OpenFileDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) - OpenMultipleFilesDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) - OpenDirectoryDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) - SaveDialog(dialogOptions dialog.SaveDialogOptions, callbackID string) - MessageDialog(dialogOptions dialog.MessageDialogOptions, callbackID string) + OpenFileDialog(dialogOptions runtime.OpenDialogOptions, callbackID string) + OpenMultipleFilesDialog(dialogOptions runtime.OpenDialogOptions, callbackID string) + OpenDirectoryDialog(dialogOptions runtime.OpenDialogOptions, callbackID string) + SaveDialog(dialogOptions runtime.SaveDialogOptions, callbackID string) + MessageDialog(dialogOptions runtime.MessageDialogOptions, callbackID string) WindowSetTitle(title string) WindowShow() WindowHide() diff --git a/v2/internal/messagedispatcher/messagedispatcher.go b/v2/internal/messagedispatcher/messagedispatcher.go index 0c20c737e..55828b0da 100644 --- a/v2/internal/messagedispatcher/messagedispatcher.go +++ b/v2/internal/messagedispatcher/messagedispatcher.go @@ -3,12 +3,11 @@ package messagedispatcher import ( "context" "encoding/json" + "github.com/wailsapp/wails/v2/pkg/runtime" "strconv" "strings" "sync" - "github.com/wailsapp/wails/v2/pkg/runtime/dialog" - "github.com/wailsapp/wails/v2/internal/crypto" "github.com/wailsapp/wails/v2/internal/logger" "github.com/wailsapp/wails/v2/internal/messagedispatcher/message" @@ -411,7 +410,7 @@ func (d *Dispatcher) processDialogMessage(result *servicebus.Message) { dialogType := splitTopic[2] switch dialogType { case "open": - dialogOptions, ok := result.Data().(dialog.OpenDialogOptions) + dialogOptions, ok := result.Data().(runtime.OpenDialogOptions) if !ok { d.logger.Error("Invalid data for 'dialog:select:open' : %#v", result.Data()) return @@ -425,7 +424,7 @@ func (d *Dispatcher) processDialogMessage(result *servicebus.Message) { client.frontend.OpenFileDialog(dialogOptions, callbackID) } case "openmultiple": - dialogOptions, ok := result.Data().(dialog.OpenDialogOptions) + dialogOptions, ok := result.Data().(runtime.OpenDialogOptions) if !ok { d.logger.Error("Invalid data for 'dialog:select:openmultiple' : %#v", result.Data()) return @@ -439,7 +438,7 @@ func (d *Dispatcher) processDialogMessage(result *servicebus.Message) { client.frontend.OpenMultipleFilesDialog(dialogOptions, callbackID) } case "directory": - dialogOptions, ok := result.Data().(dialog.OpenDialogOptions) + dialogOptions, ok := result.Data().(runtime.OpenDialogOptions) if !ok { d.logger.Error("Invalid data for 'dialog:select:directory' : %#v", result.Data()) return @@ -453,7 +452,7 @@ func (d *Dispatcher) processDialogMessage(result *servicebus.Message) { client.frontend.OpenDirectoryDialog(dialogOptions, callbackID) } case "save": - dialogOptions, ok := result.Data().(dialog.SaveDialogOptions) + dialogOptions, ok := result.Data().(runtime.SaveDialogOptions) if !ok { d.logger.Error("Invalid data for 'dialog:select:save' : %#v", result.Data()) return @@ -467,7 +466,7 @@ func (d *Dispatcher) processDialogMessage(result *servicebus.Message) { client.frontend.SaveDialog(dialogOptions, callbackID) } case "message": - dialogOptions, ok := result.Data().(dialog.MessageDialogOptions) + dialogOptions, ok := result.Data().(runtime.MessageDialogOptions) if !ok { d.logger.Error("Invalid data for 'dialog:select:message' : %#v", result.Data()) return diff --git a/v2/internal/subsystem/call.go b/v2/internal/subsystem/call.go index d67e86f71..f9ee4fd47 100644 --- a/v2/internal/subsystem/call.go +++ b/v2/internal/subsystem/call.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "fmt" - "github.com/wailsapp/wails/v2/pkg/runtime/dialog" + "github.com/wailsapp/wails/v2/pkg/runtime" "strings" "sync" @@ -126,34 +126,34 @@ func (c *Call) processSystemCall(payload *message.CallMessage, clientID string) callName := strings.TrimPrefix(payload.Name, ".wails.") switch callName { case "Dialog.Open": - var dialogOptions dialog.OpenDialogOptions + var dialogOptions runtime.OpenDialogOptions err := json.Unmarshal(payload.Args[0], &dialogOptions) if err != nil { c.logger.Error("Error decoding: %s", err) } - result, err := dialog.OpenFile(c.ctx, dialogOptions) + result, err := runtime.OpenFileDialog(c.ctx, dialogOptions) if err != nil { c.logger.Error("Error: %s", err) } c.sendResult(result, payload, clientID) case "Dialog.Save": - var dialogOptions dialog.SaveDialogOptions + var dialogOptions runtime.SaveDialogOptions err := json.Unmarshal(payload.Args[0], &dialogOptions) if err != nil { c.logger.Error("Error decoding: %s", err) } - result, err := dialog.SaveFile(c.ctx, dialogOptions) + result, err := runtime.SaveFileDialog(c.ctx, dialogOptions) if err != nil { c.logger.Error("Error: %s", err) } c.sendResult(result, payload, clientID) case "Dialog.Message": - var dialogOptions dialog.MessageDialogOptions + var dialogOptions runtime.MessageDialogOptions err := json.Unmarshal(payload.Args[0], &dialogOptions) if err != nil { c.logger.Error("Error decoding: %s", err) } - result, err := dialog.Message(c.ctx, dialogOptions) + result, err := runtime.MessageDialog(c.ctx, dialogOptions) if err != nil { c.logger.Error("Error: %s", err) } diff --git a/v2/internal/webserver/websockets.go b/v2/internal/webserver/websockets.go index 663a41532..474ccfded 100644 --- a/v2/internal/webserver/websockets.go +++ b/v2/internal/webserver/websockets.go @@ -3,7 +3,7 @@ package webserver import ( "context" "github.com/wailsapp/wails/v2/pkg/menu" - "github.com/wailsapp/wails/v2/pkg/runtime/dialog" + "github.com/wailsapp/wails/v2/pkg/runtime" "net/http" "strings" @@ -40,7 +40,7 @@ func (wc *WebClient) UpdateTrayMenuLabel(trayMenuJSON string) { wc.logger.Info("Not implemented in server build") } -func (wc *WebClient) MessageDialog(dialogOptions dialog.MessageDialogOptions, callbackID string) { +func (wc *WebClient) MessageDialog(dialogOptions runtime.MessageDialogOptions, callbackID string) { wc.logger.Info("Not implemented in server build") } @@ -56,19 +56,19 @@ func (wc *WebClient) UpdateContextMenu(contextMenuJSON string) { wc.logger.Info("Not implemented in server build") } -func (wc *WebClient) OpenFileDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) { +func (wc *WebClient) OpenFileDialog(dialogOptions runtime.OpenDialogOptions, callbackID string) { wc.logger.Info("Not implemented in server build") } -func (wc *WebClient) OpenMultipleFilesDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) { +func (wc *WebClient) OpenMultipleFilesDialog(dialogOptions runtime.OpenDialogOptions, callbackID string) { wc.logger.Info("Not implemented in server build") } -func (wc *WebClient) OpenDirectoryDialog(dialogOptions dialog.OpenDialogOptions, callbackID string) { +func (wc *WebClient) OpenDirectoryDialog(dialogOptions runtime.OpenDialogOptions, callbackID string) { wc.logger.Info("Not implemented in server build") } -func (wc *WebClient) SaveDialog(dialogOptions dialog.SaveDialogOptions, callbackID string) { +func (wc *WebClient) SaveDialog(dialogOptions runtime.SaveDialogOptions, callbackID string) { wc.logger.Info("Not implemented in server build") } diff --git a/v2/pkg/runtime/dialog/dialog.go b/v2/pkg/runtime/dialog.go similarity index 86% rename from v2/pkg/runtime/dialog/dialog.go rename to v2/pkg/runtime/dialog.go index 2c2b33432..d23bc50d8 100644 --- a/v2/pkg/runtime/dialog/dialog.go +++ b/v2/pkg/runtime/dialog.go @@ -1,6 +1,6 @@ // +build !experimental -package dialog +package runtime import ( "context" @@ -77,8 +77,8 @@ func processTitleAndFilter(params ...string) (string, string) { return title, filter } -// OpenDirectory prompts the user to select a directory -func OpenDirectory(ctx context.Context, dialogOptions OpenDialogOptions) (string, error) { +// OpenDirectoryDialog prompts the user to select a directory +func OpenDirectoryDialog(ctx context.Context, dialogOptions OpenDialogOptions) (string, error) { bus := servicebus.ExtractBus(ctx) @@ -104,8 +104,8 @@ func OpenDirectory(ctx context.Context, dialogOptions OpenDialogOptions) (string return result.Data().(string), nil } -// OpenFile prompts the user to select a file -func OpenFile(ctx context.Context, dialogOptions OpenDialogOptions) (string, error) { +// OpenFileDialog prompts the user to select a file +func OpenFileDialog(ctx context.Context, dialogOptions OpenDialogOptions) (string, error) { bus := servicebus.ExtractBus(ctx) @@ -131,8 +131,8 @@ func OpenFile(ctx context.Context, dialogOptions OpenDialogOptions) (string, err return result.Data().(string), nil } -// OpenMultipleFiles prompts the user to select a file -func OpenMultipleFiles(ctx context.Context, dialogOptions OpenDialogOptions) ([]string, error) { +// OpenMultipleFilesDialog prompts the user to select a file +func OpenMultipleFilesDialog(ctx context.Context, dialogOptions OpenDialogOptions) ([]string, error) { bus := servicebus.ExtractBus(ctx) uniqueCallback := crypto.RandomID() @@ -156,8 +156,8 @@ func OpenMultipleFiles(ctx context.Context, dialogOptions OpenDialogOptions) ([] return result.Data().([]string), nil } -// SaveFile prompts the user to select a file -func SaveFile(ctx context.Context, dialogOptions SaveDialogOptions) (string, error) { +// SaveFileDialog prompts the user to select a file +func SaveFileDialog(ctx context.Context, dialogOptions SaveDialogOptions) (string, error) { bus := servicebus.ExtractBus(ctx) uniqueCallback := crypto.RandomID() @@ -181,8 +181,8 @@ func SaveFile(ctx context.Context, dialogOptions SaveDialogOptions) (string, err return result.Data().(string), nil } -// Message show a message to the user -func Message(ctx context.Context, dialogOptions MessageDialogOptions) (string, error) { +// MessageDialog show a message dialog to the user +func MessageDialog(ctx context.Context, dialogOptions MessageDialogOptions) (string, error) { bus := servicebus.ExtractBus(ctx) diff --git a/v2/pkg/runtime/events.go b/v2/pkg/runtime/events.go new file mode 100644 index 000000000..e65d9d73d --- /dev/null +++ b/v2/pkg/runtime/events.go @@ -0,0 +1,45 @@ +// +build !experimental + +package runtime + +import ( + "context" + "github.com/wailsapp/wails/v2/internal/messagedispatcher/message" + "github.com/wailsapp/wails/v2/internal/servicebus" +) + +// On registers a listener for a particular event +func On(ctx context.Context, eventName string, callback func(optionalData ...interface{})) { + + bus := servicebus.ExtractBus(ctx) + + eventMessage := &message.OnEventMessage{ + Name: eventName, + Callback: callback, + Counter: -1, + } + bus.Publish("event:on", eventMessage) +} + +// Once registers a listener for a particular event. After the first callback, the +// listener is deleted. +func Once(ctx context.Context, eventName string, callback func(optionalData ...interface{})) { + bus := servicebus.ExtractBus(ctx) + eventMessage := &message.OnEventMessage{ + Name: eventName, + Callback: callback, + Counter: 1, + } + bus.Publish("event:on", eventMessage) +} + +// Emit pass through +func Emit(ctx context.Context, eventName string, optionalData ...interface{}) { + bus := servicebus.ExtractBus(ctx) + eventMessage := &message.EventMessage{ + Name: eventName, + Data: optionalData, + } + + bus.Publish("event:emit:from:g", eventMessage) +} diff --git a/v2/pkg/runtime/log/log.go b/v2/pkg/runtime/log.go similarity index 98% rename from v2/pkg/runtime/log/log.go rename to v2/pkg/runtime/log.go index 1c3e40010..423f719f3 100644 --- a/v2/pkg/runtime/log/log.go +++ b/v2/pkg/runtime/log.go @@ -1,6 +1,6 @@ // +build !experimental -package log +package runtime import ( "context" diff --git a/v2/pkg/runtime/menu/menu.go b/v2/pkg/runtime/menu.go similarity index 95% rename from v2/pkg/runtime/menu/menu.go rename to v2/pkg/runtime/menu.go index e9876b5b4..325fb3575 100644 --- a/v2/pkg/runtime/menu/menu.go +++ b/v2/pkg/runtime/menu.go @@ -1,6 +1,6 @@ -// +build !experimental\ +// +build !experimental -package menu +package runtime import ( "context" diff --git a/v2/pkg/runtime/system/system.go b/v2/pkg/runtime/runtime.go similarity index 94% rename from v2/pkg/runtime/system/system.go rename to v2/pkg/runtime/runtime.go index 235c4aaf5..4926c8c6b 100644 --- a/v2/pkg/runtime/system/system.go +++ b/v2/pkg/runtime/runtime.go @@ -1,6 +1,6 @@ // +build !experimental -package system +package runtime import ( "context" diff --git a/v2/pkg/runtime/window/window.go b/v2/pkg/runtime/window.go similarity index 51% rename from v2/pkg/runtime/window/window.go rename to v2/pkg/runtime/window.go index fd5db3172..fe0a68ae9 100644 --- a/v2/pkg/runtime/window/window.go +++ b/v2/pkg/runtime/window.go @@ -1,6 +1,6 @@ // +build !experimental -package window +package runtime import ( "context" @@ -9,90 +9,90 @@ import ( "github.com/wailsapp/wails/v2/internal/servicebus" ) -// SetTitle sets the title of the window -func SetTitle(ctx context.Context, title string) { +// WindowSetTitle sets the title of the window +func WindowSetTitle(ctx context.Context, title string) { bus := servicebus.ExtractBus(ctx) bus.Publish("window:settitle", title) } -// Fullscreen makes the window fullscreen -func Fullscreen(ctx context.Context) { +// WindowFullscreen makes the window fullscreen +func WindowFullscreen(ctx context.Context) { bus := servicebus.ExtractBus(ctx) bus.Publish("window:fullscreen", "") } -// UnFullscreen makes the window UnFullscreen -func UnFullscreen(ctx context.Context) { +// WindowUnFullscreen makes the window UnFullscreen +func WindowUnFullscreen(ctx context.Context) { bus := servicebus.ExtractBus(ctx) bus.Publish("window:unfullscreen", "") } -// Center the window on the current screen -func Center(ctx context.Context) { +// WindowCenter the window on the current screen +func WindowCenter(ctx context.Context) { bus := servicebus.ExtractBus(ctx) bus.Publish("window:center", "") } -// Show shows the window if hidden -func Show(ctx context.Context) { +// WindowShow shows the window if hidden +func WindowShow(ctx context.Context) { bus := servicebus.ExtractBus(ctx) bus.Publish("window:show", "") } -// Hide the window -func Hide(ctx context.Context) { +// WindowHide the window +func WindowHide(ctx context.Context) { bus := servicebus.ExtractBus(ctx) bus.Publish("window:hide", "") } -// SetSize sets the size of the window -func SetSize(ctx context.Context, width int, height int) { +// WindowSetSize sets the size of the window +func WindowSetSize(ctx context.Context, width int, height int) { bus := servicebus.ExtractBus(ctx) message := fmt.Sprintf("window:size:%d:%d", width, height) bus.Publish(message, "") } -// SetSize sets the size of the window -func SetMinSize(ctx context.Context, width int, height int) { +// WindowSetSize sets the size of the window +func WindowSetMinSize(ctx context.Context, width int, height int) { bus := servicebus.ExtractBus(ctx) message := fmt.Sprintf("window:minsize:%d:%d", width, height) bus.Publish(message, "") } -// SetSize sets the size of the window -func SetMaxSize(ctx context.Context, width int, height int) { +// WindowSetSize sets the size of the window +func WindowSetMaxSize(ctx context.Context, width int, height int) { bus := servicebus.ExtractBus(ctx) message := fmt.Sprintf("window:maxsize:%d:%d", width, height) bus.Publish(message, "") } -// SetPosition sets the position of the window -func SetPosition(ctx context.Context, x int, y int) { +// WindowSetPosition sets the position of the window +func WindowSetPosition(ctx context.Context, x int, y int) { bus := servicebus.ExtractBus(ctx) message := fmt.Sprintf("window:position:%d:%d", x, y) bus.Publish(message, "") } -// Maximise the window -func Maximise(ctx context.Context) { +// WindowMaximise the window +func WindowMaximise(ctx context.Context) { bus := servicebus.ExtractBus(ctx) bus.Publish("window:maximise", "") } -// Unmaximise the window -func Unmaximise(ctx context.Context) { +// WindowUnmaximise the window +func WindowUnmaximise(ctx context.Context) { bus := servicebus.ExtractBus(ctx) bus.Publish("window:unmaximise", "") } -// Minimise the window -func Minimise(ctx context.Context) { +// WindowMinimise the window +func WindowMinimise(ctx context.Context) { bus := servicebus.ExtractBus(ctx) bus.Publish("window:minimise", "") } -// Unminimise the window -func Unminimise(ctx context.Context) { +// WindowUnminimise the window +func WindowUnminimise(ctx context.Context) { bus := servicebus.ExtractBus(ctx) bus.Publish("window:unminimise", "") }