5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 02:31:58 +08:00
wails/v2/internal/app/app_production.go
Lea Anthony 052b9222c1
Add garble support (#1793)
Co-authored-by: AlbinoDrought <sean@albinodrought.com>
Co-authored-by: stffabi <stffabi@users.noreply.github.com>
2022-09-13 10:05:37 +10:00

121 lines
3.0 KiB
Go

//go:build production
package app
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"
"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) Shutdown() {
a.frontend.Quit()
}
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)
if IsDebug() {
myLogger.SetLogLevel(appoptions.LogLevel)
} else {
myLogger.SetLogLevel(appoptions.LogLevelProduction)
}
ctx = context.WithValue(ctx, "logger", myLogger)
ctx = context.WithValue(ctx, "obfuscated", IsObfuscated())
// 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,
appoptions.OnBeforeClose,
}
appBindings := binding.NewBindings(myLogger, appoptions.Bind, bindingExemptions, IsObfuscated())
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
}