mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 20:03:01 +08:00
113 lines
2.9 KiB
Go
113 lines
2.9 KiB
Go
//go:build production
|
|
// +build production
|
|
|
|
package appng
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/wailsapp/wails/v2/internal/binding"
|
|
"github.com/wailsapp/wails/v2/internal/frontend"
|
|
"github.com/wailsapp/wails/v2/internal/frontend/desktop"
|
|
"github.com/wailsapp/wails/v2/internal/frontend/dispatcher"
|
|
"github.com/wailsapp/wails/v2/internal/frontend/runtime"
|
|
"github.com/wailsapp/wails/v2/internal/logger"
|
|
"github.com/wailsapp/wails/v2/internal/menumanager"
|
|
pkgLog "github.com/wailsapp/wails/v2/pkg/logger"
|
|
"github.com/wailsapp/wails/v2/pkg/options"
|
|
)
|
|
|
|
// App defines a Wails application structure
|
|
type App struct {
|
|
frontend frontend.Frontend
|
|
logger *logger.Logger
|
|
options *options.App
|
|
|
|
menuManager *menumanager.Manager
|
|
|
|
// Indicates if the app is in debug mode
|
|
debug bool
|
|
|
|
// OnStartup/OnShutdown
|
|
startupCallback func(ctx context.Context)
|
|
shutdownCallback func(ctx context.Context)
|
|
ctx context.Context
|
|
}
|
|
|
|
func (a *App) Run() error {
|
|
err := a.frontend.Run(a.ctx)
|
|
if a.shutdownCallback != nil {
|
|
a.shutdownCallback(a.ctx)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// CreateApp creates the app!
|
|
func CreateApp(appoptions *options.App) (*App, error) {
|
|
var err error
|
|
|
|
ctx := context.Background()
|
|
|
|
// Merge default options
|
|
options.MergeDefaults(appoptions)
|
|
|
|
debug := IsDebug()
|
|
ctx = context.WithValue(ctx, "debug", debug)
|
|
|
|
// Set up logger
|
|
myLogger := logger.New(appoptions.Logger)
|
|
myLogger.SetLogLevel(appoptions.LogLevel)
|
|
|
|
if !IsDebug() {
|
|
myLogger.SetLogLevel(pkgLog.ERROR)
|
|
}
|
|
ctx = context.WithValue(ctx, "logger", myLogger)
|
|
|
|
// Preflight Checks
|
|
err = PreflightChecks(appoptions, myLogger)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Create the menu manager
|
|
menuManager := menumanager.NewManager()
|
|
|
|
// Process the application menu
|
|
if appoptions.Menu != nil {
|
|
err = menuManager.SetApplicationMenu(appoptions.Menu)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
// Create binding exemptions - Ugly hack. There must be a better way
|
|
bindingExemptions := []interface{}{appoptions.OnStartup, appoptions.OnShutdown, appoptions.OnDomReady}
|
|
appBindings := binding.NewBindings(myLogger, appoptions.Bind, bindingExemptions)
|
|
eventHandler := runtime.NewEvents(myLogger)
|
|
ctx = context.WithValue(ctx, "events", eventHandler)
|
|
// Attach logger to context
|
|
if debug {
|
|
ctx = context.WithValue(ctx, "buildtype", "debug")
|
|
} else {
|
|
ctx = context.WithValue(ctx, "buildtype", "production")
|
|
}
|
|
|
|
messageDispatcher := dispatcher.NewDispatcher(ctx, myLogger, appBindings, eventHandler)
|
|
appFrontend := desktop.NewFrontend(ctx, appoptions, myLogger, appBindings, messageDispatcher)
|
|
eventHandler.AddFrontend(appFrontend)
|
|
|
|
result := &App{
|
|
ctx: ctx,
|
|
frontend: appFrontend,
|
|
logger: myLogger,
|
|
menuManager: menuManager,
|
|
startupCallback: appoptions.OnStartup,
|
|
shutdownCallback: appoptions.OnShutdown,
|
|
debug: debug,
|
|
options: appoptions,
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|