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

Exported() -> CallableByJS()

This commit is contained in:
Lea Anthony 2023-03-22 20:42:20 +11:00
parent 42fb91bc73
commit 3ae604e474
8 changed files with 41 additions and 54 deletions

View File

@ -26,7 +26,7 @@ func (r *Plugin) Init(_ *application.App) error {
return nil return nil
} }
func (r *Plugin) Exported() []string { func (r *Plugin) CallableByJS() []string {
return []string{ return []string{
"Generate", "Generate",
} }

View File

@ -1,4 +1,4 @@
module github.com/myuser/wails-plugin-{{.Name}} module {{.Name}}
go 1.20 go 1.20

View File

@ -1,10 +1,6 @@
package github.com/myuser/wails-plugin-{{.PackageName}} package {{.Name}}
import ( import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"github.com/wailsapp/wails/v3/pkg/application" "github.com/wailsapp/wails/v3/pkg/application"
) )
@ -13,63 +9,54 @@ import (
// It must implement the application.Plugin interface. // It must implement the application.Plugin interface.
// Both the Init() and Shutdown() methods are called synchronously when the app starts and stops. // 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 { type Plugin struct{
return &Plugin{} config *Config
app *application.App
}
func NewPlugin(config *Config) *Plugin {
return &Plugin{
config: config,
}
} }
// Shutdown is called when the app is shutting down // Shutdown is called when the app is shutting down
// You can use this to clean up any resources you have allocated // 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 // Name returns the name of the plugin.
// You should use the go module format here (e.g. github.com/myuser/myrepo) // You should use the go module format e.g. github.com/myuser/myplugin
func (r *Plugin) Name() string { func (p *Plugin) Name() string {
return "github.com/myuser/{{.Name}}" 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 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 ----------------
// Plugin methods are just normal Go methods. You can add as many as you like. // 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). // The only requirement is that they are exported (start with a capital letter).
// You can also return any type that is JSON serializable. // 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. // See https://golang.org/pkg/encoding/json/#Marshal for more information.
// Hashes contains all hashes of a string func (p *Plugin) Greet(name string) string {
type Hashes struct { return "Hello " + name
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),
}
} }

View File

@ -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()) return fmt.Errorf("cannot add plugin '%s' to app: %s", pluginID, err.Error())
} }
exportedMethods := plugin.Exported() exportedMethods := plugin.CallableByJS()
for _, method := range methods { for _, method := range methods {
// Do not expose reserved methods // Do not expose reserved methods

View File

@ -5,7 +5,7 @@ type Plugin interface {
Init(app *App) error Init(app *App) error
Shutdown() Shutdown()
// Exported is a list of method names that should be exposed to the frontend // Exported is a list of method names that should be exposed to the frontend
Exported() []string CallableByJS() []string
} }
type PluginManager struct { type PluginManager struct {

View File

@ -11,7 +11,7 @@ type Plugin interface {
Name() string Name() string
Init(*application.App) error Init(*application.App) error
Shutdown() 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 `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 the frontend. These method names must exactly match the names of the methods exported
by the plugin. by the plugin.

View File

@ -26,7 +26,7 @@ func (p *Plugin) Init(_ *application.App) error {
return nil return nil
} }
func (p *Plugin) Exported() []string { func (p *Plugin) CallableByJS() []string {
return []string{ return []string{
"OpenURL", "OpenURL",
"OpenFile", "OpenFile",

View File

@ -59,7 +59,7 @@ func (kvs *KeyValueStore) Init(app *application.App) error {
return nil return nil
} }
func (kvs *KeyValueStore) Exported() []string { func (kvs *KeyValueStore) CallableByJS() []string {
return []string{ return []string{
"Set", "Set",
"Get", "Get",