From 3ae604e47404d3b4d374074f9944042f4da75df2 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Wed, 22 Mar 2023 20:42:20 +1100 Subject: [PATCH] Exported() -> CallableByJS() --- v3/examples/plugins/plugins/hashes/plugin.go | 2 +- v3/internal/plugins/template/go.mod.tmpl | 2 +- v3/internal/plugins/template/plugin.go.tmpl | 79 ++++++++------------ v3/pkg/application/bindings.go | 2 +- v3/pkg/application/plugins.go | 2 +- v3/plugins/README.md | 4 +- v3/plugins/browser/plugin.go | 2 +- v3/plugins/kvstore/kvstore.go | 2 +- 8 files changed, 41 insertions(+), 54 deletions(-) diff --git a/v3/examples/plugins/plugins/hashes/plugin.go b/v3/examples/plugins/plugins/hashes/plugin.go index 94b5fcee5..b2bbaef4d 100644 --- a/v3/examples/plugins/plugins/hashes/plugin.go +++ b/v3/examples/plugins/plugins/hashes/plugin.go @@ -26,7 +26,7 @@ func (r *Plugin) Init(_ *application.App) error { return nil } -func (r *Plugin) Exported() []string { +func (r *Plugin) CallableByJS() []string { return []string{ "Generate", } diff --git a/v3/internal/plugins/template/go.mod.tmpl b/v3/internal/plugins/template/go.mod.tmpl index 6f0477051..77249136f 100644 --- a/v3/internal/plugins/template/go.mod.tmpl +++ b/v3/internal/plugins/template/go.mod.tmpl @@ -1,4 +1,4 @@ -module github.com/myuser/wails-plugin-{{.Name}} +module {{.Name}} go 1.20 diff --git a/v3/internal/plugins/template/plugin.go.tmpl b/v3/internal/plugins/template/plugin.go.tmpl index 51f79e6dd..d9de328f6 100644 --- a/v3/internal/plugins/template/plugin.go.tmpl +++ b/v3/internal/plugins/template/plugin.go.tmpl @@ -1,10 +1,6 @@ -package github.com/myuser/wails-plugin-{{.PackageName}} +package {{.Name}} import ( - "crypto/md5" - "crypto/sha1" - "crypto/sha256" - "encoding/hex" "github.com/wailsapp/wails/v3/pkg/application" ) @@ -13,63 +9,54 @@ import ( // It must implement the application.Plugin interface. // Both the Init() and Shutdown() methods are called synchronously when the app starts and stops. -type Plugin struct{} +type Config struct { + // Add any configuration options here +} -func NewPlugin() *Plugin { - return &Plugin{} +type Plugin struct{ + config *Config + app *application.App +} + +func NewPlugin(config *Config) *Plugin { + return &Plugin{ + config: config, + } } // Shutdown is called when the app is shutting down // You can use this to clean up any resources you have allocated -func (r *Plugin) Shutdown() {} +func (p *Plugin) Shutdown() {} -// Name returns the name of the plugin -// You should use the go module format here (e.g. github.com/myuser/myrepo) -func (r *Plugin) Name() string { +// Name returns the name of the plugin. +// You should use the go module format e.g. github.com/myuser/myplugin +func (p *Plugin) Name() string { return "github.com/myuser/{{.Name}}" } -func (r *Plugin) Init(_ *application.App) error { +// Init is called when the app is starting up. You can use this to +// initialise any resources you need. You can also access the application +// instance via the app property. +func (p *Plugin) Init(app *application.App) error { + p.app = app return nil } +// Exported returns a list of exported methods that can be called from the frontend +func (p *Plugin) CallableByJS() []string { + return []string{ + "Greet", + } +} + // ---------------- Plugin Methods ---------------- // Plugin methods are just normal Go methods. You can add as many as you like. // The only requirement is that they are exported (start with a capital letter). // You can also return any type that is JSON serializable. +// Any methods that you want to be callable from the frontend must be returned by the +// CallableByJS() method above. // See https://golang.org/pkg/encoding/json/#Marshal for more information. -// Hashes contains all hashes of a string -type Hashes struct { - MD5 string - SHA1 string - SHA256 string -} - -// MD5 returns the MD5 hash of the given string -func (r *Plugin) MD5(s string) string { - md5Hash := md5.Sum([]byte(s)) - return hex.EncodeToString(md5Hash[:]) -} - -// SHA1 returns the SHA1 hash of the given string -func (r *Plugin) SHA1(s string) string { - sha1Hash := sha1.Sum([]byte(s)) - return hex.EncodeToString(sha1Hash[:]) -} - -// SHA256 returns the SHA256 hash of the given string -func (r *Plugin) SHA256(s string) string { - sha256Hash := sha256.Sum256([]byte(s)) - return hex.EncodeToString(sha256Hash[:]) -} - -// All returns all hashes of the given string -// This is an example of returning a struct from a plugin method. -func (r *Plugin) All(s string) Hashes { - return Hashes{ - MD5: r.MD5(s), - SHA1: r.SHA1(s), - SHA256: r.SHA256(s), - } +func (p *Plugin) Greet(name string) string { + return "Hello " + name } diff --git a/v3/pkg/application/bindings.go b/v3/pkg/application/bindings.go index 9ae792e2e..362ec9a96 100644 --- a/v3/pkg/application/bindings.go +++ b/v3/pkg/application/bindings.go @@ -114,7 +114,7 @@ func (b *Bindings) AddPlugins(plugins map[string]Plugin) error { return fmt.Errorf("cannot add plugin '%s' to app: %s", pluginID, err.Error()) } - exportedMethods := plugin.Exported() + exportedMethods := plugin.CallableByJS() for _, method := range methods { // Do not expose reserved methods diff --git a/v3/pkg/application/plugins.go b/v3/pkg/application/plugins.go index 5312796a2..7b3b1c9d9 100644 --- a/v3/pkg/application/plugins.go +++ b/v3/pkg/application/plugins.go @@ -5,7 +5,7 @@ type Plugin interface { Init(app *App) error Shutdown() // Exported is a list of method names that should be exposed to the frontend - Exported() []string + CallableByJS() []string } type PluginManager struct { diff --git a/v3/plugins/README.md b/v3/plugins/README.md index 2dae437a2..4edb0f81e 100644 --- a/v3/plugins/README.md +++ b/v3/plugins/README.md @@ -11,7 +11,7 @@ type Plugin interface { Name() string Init(*application.App) error Shutdown() - Exported() []string + CallableByJS() []string } ``` @@ -23,7 +23,7 @@ the application from starting. The `Shutdown()` method is called when the application is shutting down. -The `Exported()` method returns a list of exported functions that can be called from +The `CallableByJS()` method returns a list of exported functions that can be called from the frontend. These method names must exactly match the names of the methods exported by the plugin. diff --git a/v3/plugins/browser/plugin.go b/v3/plugins/browser/plugin.go index ab408c041..918f8bf33 100644 --- a/v3/plugins/browser/plugin.go +++ b/v3/plugins/browser/plugin.go @@ -26,7 +26,7 @@ func (p *Plugin) Init(_ *application.App) error { return nil } -func (p *Plugin) Exported() []string { +func (p *Plugin) CallableByJS() []string { return []string{ "OpenURL", "OpenFile", diff --git a/v3/plugins/kvstore/kvstore.go b/v3/plugins/kvstore/kvstore.go index 2f3b4b522..116fb4eb0 100644 --- a/v3/plugins/kvstore/kvstore.go +++ b/v3/plugins/kvstore/kvstore.go @@ -59,7 +59,7 @@ func (kvs *KeyValueStore) Init(app *application.App) error { return nil } -func (kvs *KeyValueStore) Exported() []string { +func (kvs *KeyValueStore) CallableByJS() []string { return []string{ "Set", "Get",