github.com/rribou/wails/v2@v2.6.3/pkg/runtime/runtime.go (about)

     1  package runtime
     2  
     3  import (
     4  	"context"
     5  	"log"
     6  	goruntime "runtime"
     7  
     8  	"github.com/rribou/wails/v2/internal/frontend"
     9  	"github.com/rribou/wails/v2/internal/logger"
    10  )
    11  
    12  const contextError = `An invalid context was passed. This method requires the specific context given in the lifecycle hooks:
    13  https://wails.io/docs/reference/runtime/intro`
    14  
    15  func getFrontend(ctx context.Context) frontend.Frontend {
    16  	if ctx == nil {
    17  		pc, _, _, _ := goruntime.Caller(1)
    18  		funcName := goruntime.FuncForPC(pc).Name()
    19  		log.Fatalf("cannot call '%s': %s", funcName, contextError)
    20  	}
    21  	result := ctx.Value("frontend")
    22  	if result != nil {
    23  		return result.(frontend.Frontend)
    24  	}
    25  	pc, _, _, _ := goruntime.Caller(1)
    26  	funcName := goruntime.FuncForPC(pc).Name()
    27  	log.Fatalf("cannot call '%s': %s", funcName, contextError)
    28  	return nil
    29  }
    30  func getLogger(ctx context.Context) *logger.Logger {
    31  	if ctx == nil {
    32  		pc, _, _, _ := goruntime.Caller(1)
    33  		funcName := goruntime.FuncForPC(pc).Name()
    34  		log.Fatalf("cannot call '%s': %s", funcName, contextError)
    35  	}
    36  	result := ctx.Value("logger")
    37  	if result != nil {
    38  		return result.(*logger.Logger)
    39  	}
    40  	pc, _, _, _ := goruntime.Caller(1)
    41  	funcName := goruntime.FuncForPC(pc).Name()
    42  	log.Fatalf("cannot call '%s': %s", funcName, contextError)
    43  	return nil
    44  }
    45  
    46  func getEvents(ctx context.Context) frontend.Events {
    47  	if ctx == nil {
    48  		pc, _, _, _ := goruntime.Caller(1)
    49  		funcName := goruntime.FuncForPC(pc).Name()
    50  		log.Fatalf("cannot call '%s': %s", funcName, contextError)
    51  	}
    52  	result := ctx.Value("events")
    53  	if result != nil {
    54  		return result.(frontend.Events)
    55  	}
    56  	pc, _, _, _ := goruntime.Caller(1)
    57  	funcName := goruntime.FuncForPC(pc).Name()
    58  	log.Fatalf("cannot call '%s': %s", funcName, contextError)
    59  	return nil
    60  }
    61  
    62  // Quit the application
    63  func Quit(ctx context.Context) {
    64  	if ctx == nil {
    65  		log.Fatalf("Error calling 'runtime.Quit': %s", contextError)
    66  	}
    67  	appFrontend := getFrontend(ctx)
    68  	appFrontend.Quit()
    69  }
    70  
    71  // Hide the application
    72  func Hide(ctx context.Context) {
    73  	if ctx == nil {
    74  		log.Fatalf("Error calling 'runtime.Hide': %s", contextError)
    75  	}
    76  	appFrontend := getFrontend(ctx)
    77  	appFrontend.Hide()
    78  }
    79  
    80  // Show the application if it is hidden
    81  func Show(ctx context.Context) {
    82  	if ctx == nil {
    83  		log.Fatalf("Error calling 'runtime.Show': %s", contextError)
    84  	}
    85  	appFrontend := getFrontend(ctx)
    86  	appFrontend.Show()
    87  }
    88  
    89  // EnvironmentInfo contains information about the environment
    90  type EnvironmentInfo struct {
    91  	BuildType string `json:"buildType"`
    92  	Platform  string `json:"platform"`
    93  	Arch      string `json:"arch"`
    94  }
    95  
    96  // Environment returns information about the environment
    97  func Environment(ctx context.Context) EnvironmentInfo {
    98  	var result EnvironmentInfo
    99  	buildType := ctx.Value("buildtype")
   100  	if buildType != nil {
   101  		result.BuildType = buildType.(string)
   102  	}
   103  	result.Platform = goruntime.GOOS
   104  	result.Arch = goruntime.GOARCH
   105  	return result
   106  }