github.com/secoba/wails/v2@v2.6.4/internal/app/app_production.go (about)

     1  //go:build production
     2  
     3  package app
     4  
     5  import (
     6  	"context"
     7  
     8  	"github.com/secoba/wails/v2/internal/binding"
     9  	"github.com/secoba/wails/v2/internal/frontend/desktop"
    10  	"github.com/secoba/wails/v2/internal/frontend/dispatcher"
    11  	"github.com/secoba/wails/v2/internal/frontend/runtime"
    12  	"github.com/secoba/wails/v2/internal/logger"
    13  	"github.com/secoba/wails/v2/internal/menumanager"
    14  	"github.com/secoba/wails/v2/pkg/options"
    15  )
    16  
    17  func (a *App) Run() error {
    18  	err := a.frontend.Run(a.ctx)
    19  	a.frontend.RunMainLoop()
    20  	a.frontend.WindowClose()
    21  	if a.shutdownCallback != nil {
    22  		a.shutdownCallback(a.ctx)
    23  	}
    24  	return err
    25  }
    26  
    27  // CreateApp creates the app!
    28  func CreateApp(appoptions *options.App) (*App, error) {
    29  	var err error
    30  
    31  	ctx := context.Background()
    32  
    33  	// Merge default options
    34  	options.MergeDefaults(appoptions)
    35  
    36  	debug := IsDebug()
    37  	devtoolsEnabled := IsDevtoolsEnabled()
    38  	ctx = context.WithValue(ctx, "debug", debug)
    39  	ctx = context.WithValue(ctx, "devtoolsEnabled", devtoolsEnabled)
    40  
    41  	// Set up logger
    42  	myLogger := logger.New(appoptions.Logger)
    43  	if IsDebug() {
    44  		myLogger.SetLogLevel(appoptions.LogLevel)
    45  	} else {
    46  		myLogger.SetLogLevel(appoptions.LogLevelProduction)
    47  	}
    48  	ctx = context.WithValue(ctx, "logger", myLogger)
    49  	ctx = context.WithValue(ctx, "obfuscated", IsObfuscated())
    50  
    51  	// Preflight Checks
    52  	err = PreflightChecks(appoptions, myLogger)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	// Create the menu manager
    58  	menuManager := menumanager.NewManager()
    59  
    60  	// Process the application menu
    61  	if appoptions.Menu != nil {
    62  		err = menuManager.SetApplicationMenu(appoptions.Menu)
    63  		if err != nil {
    64  			return nil, err
    65  		}
    66  	}
    67  
    68  	// Create binding exemptions - Ugly hack. There must be a better way
    69  	bindingExemptions := []interface{}{
    70  		appoptions.OnStartup,
    71  		appoptions.OnShutdown,
    72  		appoptions.OnDomReady,
    73  		appoptions.OnBeforeClose,
    74  	}
    75  	appBindings := binding.NewBindings(myLogger, appoptions.Bind, bindingExemptions, IsObfuscated())
    76  	eventHandler := runtime.NewEvents(myLogger)
    77  	ctx = context.WithValue(ctx, "events", eventHandler)
    78  	// Attach logger to context
    79  	if debug {
    80  		ctx = context.WithValue(ctx, "buildtype", "debug")
    81  	} else {
    82  		ctx = context.WithValue(ctx, "buildtype", "production")
    83  	}
    84  
    85  	messageDispatcher := dispatcher.NewDispatcher(ctx, myLogger, appBindings, eventHandler, appoptions.ErrorFormatter)
    86  	appFrontend := desktop.NewFrontend(ctx, appoptions, myLogger, appBindings, messageDispatcher)
    87  	eventHandler.AddFrontend(appFrontend)
    88  
    89  	ctx = context.WithValue(ctx, "frontend", appFrontend)
    90  	result := &App{
    91  		ctx:              ctx,
    92  		frontend:         appFrontend,
    93  		logger:           myLogger,
    94  		menuManager:      menuManager,
    95  		startupCallback:  appoptions.OnStartup,
    96  		shutdownCallback: appoptions.OnShutdown,
    97  		debug:            debug,
    98  		devtoolsEnabled:  devtoolsEnabled,
    99  		options:          appoptions,
   100  	}
   101  
   102  	return result, nil
   103  
   104  }