5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 01:19:12 +08:00

Move Bind() into app config

This commit is contained in:
Lea Anthony 2021-01-26 07:04:12 +11:00
parent fe0f0e29e8
commit fe87463b78
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
6 changed files with 23 additions and 46 deletions

View File

@ -8,9 +8,10 @@ package app
// will be unknown and the application will not work as expected. // will be unknown and the application will not work as expected.
import ( import (
"github.com/wailsapp/wails/v2/internal/logger"
"os" "os"
"github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/pkg/options" "github.com/wailsapp/wails/v2/pkg/options"
) )
@ -38,7 +39,3 @@ func (a *App) Run() error {
os.Exit(1) os.Exit(1)
return nil return nil
} }
// Bind the dummy interface
func (a *App) Bind(_ interface{}) {
}

View File

@ -86,7 +86,7 @@ func CreateApp(appoptions *options.App) (*App, error) {
window: window, window: window,
servicebus: servicebus.New(myLogger), servicebus: servicebus.New(myLogger),
logger: myLogger, logger: myLogger,
bindings: binding.NewBindings(myLogger), bindings: binding.NewBindings(myLogger, appoptions.Bind),
menuManager: menuManager, menuManager: menuManager,
startupCallback: appoptions.Startup, startupCallback: appoptions.Startup,
shutdownCallback: appoptions.Shutdown, shutdownCallback: appoptions.Shutdown,
@ -216,14 +216,3 @@ func (a *App) Run() error {
return result return result
} }
// Bind a struct to the application by passing in
// a pointer to it
func (a *App) Bind(structPtr interface{}) {
// Add the struct to the bindings
err := a.bindings.Add(structPtr)
if err != nil {
a.logger.Fatal("Error during binding: " + err.Error())
}
}

View File

@ -75,7 +75,7 @@ func CreateApp(options *Options) *App {
webserver: webserver.NewWebServer(myLogger), webserver: webserver.NewWebServer(myLogger),
servicebus: servicebus.New(myLogger), servicebus: servicebus.New(myLogger),
logger: myLogger, logger: myLogger,
bindings: binding.NewBindings(myLogger), bindings: binding.NewBindings(myLogger, options.Bind),
} }
// Initialise the app // Initialise the app
@ -192,14 +192,3 @@ func (a *App) Run() error {
return cli.Run() return cli.Run()
} }
// Bind a struct to the application by passing in
// a pointer to it
func (a *App) Bind(structPtr interface{}) {
// Add the struct to the bindings
err := a.bindings.Add(structPtr)
if err != nil {
a.logger.Fatal("Error during binding: " + err.Error())
}
}

View File

@ -3,10 +3,11 @@
package app package app
import ( import (
"github.com/wailsapp/wails/v2/pkg/options"
"os" "os"
"path/filepath" "path/filepath"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/leaanthony/clir" "github.com/leaanthony/clir"
"github.com/wailsapp/wails/v2/internal/binding" "github.com/wailsapp/wails/v2/internal/binding"
"github.com/wailsapp/wails/v2/internal/logger" "github.com/wailsapp/wails/v2/internal/logger"
@ -58,7 +59,7 @@ func CreateApp(appoptions *options.App) (*App, error) {
result := &App{ result := &App{
appType: "server", appType: "server",
bindings: binding.NewBindings(myLogger), bindings: binding.NewBindings(myLogger, options.Bind),
logger: myLogger, logger: myLogger,
servicebus: servicebus.New(myLogger), servicebus: servicebus.New(myLogger),
webserver: webserver.NewWebServer(myLogger), webserver: webserver.NewWebServer(myLogger),
@ -170,14 +171,3 @@ func (a *App) Run() error {
return cli.Run() return cli.Run()
} }
// Bind a struct to the application by passing in
// a pointer to it
func (a *App) Bind(structPtr interface{}) {
// Add the struct to the bindings
err := a.bindings.Add(structPtr)
if err != nil {
a.logger.Fatal("Error during binding: " + err.Error())
}
}

View File

@ -13,11 +13,21 @@ type Bindings struct {
} }
// NewBindings returns a new Bindings object // NewBindings returns a new Bindings object
func NewBindings(logger *logger.Logger) *Bindings { func NewBindings(logger *logger.Logger, structPointersToBind []interface{}) *Bindings {
return &Bindings{ result := &Bindings{
db: newDB(), db: newDB(),
logger: logger.CustomLogger("Bindings"), logger: logger.CustomLogger("Bindings"),
} }
// Add the structs to bind
for _, ptr := range structPointersToBind {
err := result.Add(ptr)
if err != nil {
logger.Fatal("Error during binding: " + err.Error())
}
}
return result
} }
// Add the given struct methods to the Bindings // Add the given struct methods to the Bindings

View File

@ -1,11 +1,12 @@
package options package options
import ( import (
wailsruntime "github.com/wailsapp/wails/v2/internal/runtime"
"github.com/wailsapp/wails/v2/pkg/menu"
"log" "log"
"runtime" "runtime"
wailsruntime "github.com/wailsapp/wails/v2/internal/runtime"
"github.com/wailsapp/wails/v2/pkg/menu"
"github.com/imdario/mergo" "github.com/imdario/mergo"
"github.com/wailsapp/wails/v2/pkg/logger" "github.com/wailsapp/wails/v2/pkg/logger"
"github.com/wailsapp/wails/v2/pkg/options/mac" "github.com/wailsapp/wails/v2/pkg/options/mac"
@ -33,6 +34,7 @@ type App struct {
LogLevel logger.LogLevel LogLevel logger.LogLevel
Startup func(*wailsruntime.Runtime) `json:"-"` Startup func(*wailsruntime.Runtime) `json:"-"`
Shutdown func() `json:"-"` Shutdown func() `json:"-"`
Bind []interface{}
} }
// MergeDefaults will set the minimum default values for an application // MergeDefaults will set the minimum default values for an application