mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 18:29:53 +08:00
Add Exported() []string
to plugin API
This commit is contained in:
parent
b656bd2194
commit
4165caa02e
@ -26,6 +26,12 @@ func (r *Plugin) Init(_ *application.App) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Plugin) Exported() []string {
|
||||||
|
return []string{
|
||||||
|
"Generate",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------- Plugin Methods ----------------
|
// ---------------- Plugin Methods ----------------
|
||||||
|
|
||||||
type Hashes struct {
|
type Hashes struct {
|
||||||
|
@ -1 +1 @@
|
|||||||
{"url":null,"url2":"https://reddit.com"}
|
{"url2":"https://reddit.com"}
|
@ -24,6 +24,7 @@ var reservedPluginMethods = []string{
|
|||||||
"Name",
|
"Name",
|
||||||
"Init",
|
"Init",
|
||||||
"Shutdown",
|
"Shutdown",
|
||||||
|
"Exported",
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parameter defines a Go method parameter
|
// Parameter defines a Go method parameter
|
||||||
@ -113,10 +114,17 @@ 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()
|
||||||
|
|
||||||
for _, method := range methods {
|
for _, method := range methods {
|
||||||
|
// Do not expose reserved methods
|
||||||
if lo.Contains(reservedPluginMethods, method.Name) {
|
if lo.Contains(reservedPluginMethods, method.Name) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
// Do not expose methods that are not in the exported list
|
||||||
|
if !lo.Contains(exportedMethods, method.Name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
packageName := "wails-plugins"
|
packageName := "wails-plugins"
|
||||||
structName := pluginID
|
structName := pluginID
|
||||||
methodName := method.Name
|
methodName := method.Name
|
||||||
|
@ -4,6 +4,8 @@ type Plugin interface {
|
|||||||
Name() string
|
Name() string
|
||||||
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() []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type PluginManager struct {
|
type PluginManager struct {
|
||||||
|
29
v3/plugins/README.md
Normal file
29
v3/plugins/README.md
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
# Plugins
|
||||||
|
|
||||||
|
Plugins are a way to extend the functionality of your Wails application.
|
||||||
|
|
||||||
|
## Creating a plugin
|
||||||
|
|
||||||
|
Plugins are standard Go structure that adhere to the following interface:
|
||||||
|
|
||||||
|
```go
|
||||||
|
type Plugin interface {
|
||||||
|
Name() string
|
||||||
|
Init(*application.App) error
|
||||||
|
Shutdown()
|
||||||
|
Exported() []string
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The `Name()` method returns the name of the plugin. This is used for logging purposes.
|
||||||
|
|
||||||
|
The `Init(*application.App) error` method is called when the plugin is loaded. The `*application.App`
|
||||||
|
parameter is the application that the plugin is being loaded into. Any errors will prevent
|
||||||
|
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 frontend. These method names must exactly match the names of the methods exported
|
||||||
|
by the plugin.
|
||||||
|
|
@ -26,6 +26,13 @@ func (p *Plugin) Init(_ *application.App) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Plugin) Exported() []string {
|
||||||
|
return []string{
|
||||||
|
"OpenURL",
|
||||||
|
"OpenFile",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------- Plugin Methods ----------------
|
// ---------------- Plugin Methods ----------------
|
||||||
|
|
||||||
func (p *Plugin) OpenURL(url string) error {
|
func (p *Plugin) OpenURL(url string) error {
|
||||||
|
@ -3,6 +3,7 @@ package kvstore
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/wailsapp/wails/v3/pkg/application"
|
"github.com/wailsapp/wails/v3/pkg/application"
|
||||||
|
"github.com/wailsapp/wails/v3/pkg/logger"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
@ -14,6 +15,7 @@ type KeyValueStore struct {
|
|||||||
data map[string]any
|
data map[string]any
|
||||||
unsaved bool
|
unsaved bool
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
|
app *application.App
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
@ -47,7 +49,8 @@ func (kvs *KeyValueStore) Name() string {
|
|||||||
|
|
||||||
// Init is called when the plugin is loaded. It is passed the application.App
|
// Init is called when the plugin is loaded. It is passed the application.App
|
||||||
// instance. This is where you should do any setup.
|
// instance. This is where you should do any setup.
|
||||||
func (kvs *KeyValueStore) Init(_ *application.App) error {
|
func (kvs *KeyValueStore) Init(app *application.App) error {
|
||||||
|
kvs.app = app
|
||||||
err := kvs.open(kvs.config.Filename)
|
err := kvs.open(kvs.config.Filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -56,9 +59,17 @@ func (kvs *KeyValueStore) Init(_ *application.App) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (kvs *KeyValueStore) Exported() []string {
|
||||||
|
return []string{
|
||||||
|
"Set",
|
||||||
|
"Get",
|
||||||
|
"Save",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------- Plugin Methods ----------------
|
// ---------------- Plugin Methods ----------------
|
||||||
|
|
||||||
func (kvs *KeyValueStore) open(filename string) error {
|
func (kvs *KeyValueStore) open(filename string) (err error) {
|
||||||
kvs.filename = filename
|
kvs.filename = filename
|
||||||
kvs.data = make(map[string]any)
|
kvs.data = make(map[string]any)
|
||||||
|
|
||||||
@ -69,7 +80,19 @@ func (kvs *KeyValueStore) open(filename string) error {
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer func() {
|
||||||
|
err2 := file.Close()
|
||||||
|
if err2 != nil {
|
||||||
|
if err == nil {
|
||||||
|
err = err2
|
||||||
|
} else {
|
||||||
|
kvs.app.Log(&logger.Message{
|
||||||
|
Level: "error",
|
||||||
|
Message: err.Error(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
bytes, err := io.ReadAll(file)
|
bytes, err := io.ReadAll(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -120,7 +143,11 @@ func (kvs *KeyValueStore) Get(key string) any {
|
|||||||
// Set sets the value for the given key. If AutoSave is true, the store is saved to disk.
|
// Set sets the value for the given key. If AutoSave is true, the store is saved to disk.
|
||||||
func (kvs *KeyValueStore) Set(key string, value any) error {
|
func (kvs *KeyValueStore) Set(key string, value any) error {
|
||||||
kvs.lock.Lock()
|
kvs.lock.Lock()
|
||||||
kvs.data[key] = value
|
if value == nil {
|
||||||
|
delete(kvs.data, key)
|
||||||
|
} else {
|
||||||
|
kvs.data[key] = value
|
||||||
|
}
|
||||||
kvs.lock.Unlock()
|
kvs.lock.Unlock()
|
||||||
if kvs.config.AutoSave {
|
if kvs.config.AutoSave {
|
||||||
err := kvs.Save()
|
err := kvs.Save()
|
||||||
|
Loading…
Reference in New Issue
Block a user