Files

108 lines
2.8 KiB
Go
Raw Permalink Normal View History

2021-07-25 15:37:30 +10:00
package runtime
2021-07-25 15:04:01 +10:00
import (
"context"
2021-08-15 21:07:34 +10:00
"log"
goruntime "runtime"
"github.com/wailsapp/wails/v2/internal/frontend"
"github.com/wailsapp/wails/v2/internal/logger"
2021-07-25 15:04:01 +10:00
)
2021-12-22 06:28:47 +11:00
const contextError = `An invalid context was passed. This method requires the specific context given in the lifecycle hooks:
https://wails.io/docs/reference/runtime/intro`
2021-08-15 21:07:34 +10:00
func getFrontend(ctx context.Context) frontend.Frontend {
2021-09-09 20:11:48 +10:00
if ctx == nil {
pc, _, _, _ := goruntime.Caller(1)
funcName := goruntime.FuncForPC(pc).Name()
2022-07-30 09:20:18 +10:00
log.Fatalf("cannot call '%s': %s", funcName, contextError)
2021-09-09 20:11:48 +10:00
}
2021-08-15 21:07:34 +10:00
result := ctx.Value("frontend")
if result != nil {
return result.(frontend.Frontend)
}
pc, _, _, _ := goruntime.Caller(1)
funcName := goruntime.FuncForPC(pc).Name()
2021-12-22 06:28:47 +11:00
log.Fatalf("cannot call '%s': %s", funcName, contextError)
2021-08-15 21:07:34 +10:00
return nil
}
2023-11-12 12:30:49 +11:00
2021-08-25 20:30:31 +10:00
func getLogger(ctx context.Context) *logger.Logger {
2021-09-09 20:11:48 +10:00
if ctx == nil {
pc, _, _, _ := goruntime.Caller(1)
funcName := goruntime.FuncForPC(pc).Name()
2022-07-30 09:20:18 +10:00
log.Fatalf("cannot call '%s': %s", funcName, contextError)
2021-09-09 20:11:48 +10:00
}
2021-08-25 20:30:31 +10:00
result := ctx.Value("logger")
if result != nil {
return result.(*logger.Logger)
}
pc, _, _, _ := goruntime.Caller(1)
funcName := goruntime.FuncForPC(pc).Name()
2021-12-22 06:28:47 +11:00
log.Fatalf("cannot call '%s': %s", funcName, contextError)
2021-08-25 20:30:31 +10:00
return nil
}
2021-08-15 21:07:34 +10:00
func getEvents(ctx context.Context) frontend.Events {
2021-09-09 20:11:48 +10:00
if ctx == nil {
pc, _, _, _ := goruntime.Caller(1)
funcName := goruntime.FuncForPC(pc).Name()
2022-07-30 09:20:18 +10:00
log.Fatalf("cannot call '%s': %s", funcName, contextError)
2021-09-09 20:11:48 +10:00
}
2021-08-15 21:07:34 +10:00
result := ctx.Value("events")
if result != nil {
return result.(frontend.Events)
}
pc, _, _, _ := goruntime.Caller(1)
funcName := goruntime.FuncForPC(pc).Name()
2021-12-22 06:28:47 +11:00
log.Fatalf("cannot call '%s': %s", funcName, contextError)
2021-08-15 21:07:34 +10:00
return nil
}
2021-07-25 15:04:01 +10:00
// Quit the application
2021-07-25 15:22:13 +10:00
func Quit(ctx context.Context) {
2021-09-09 20:11:48 +10:00
if ctx == nil {
2022-07-30 09:20:18 +10:00
log.Fatalf("Error calling 'runtime.Quit': %s", contextError)
2021-09-09 20:11:48 +10:00
}
2021-08-15 21:07:34 +10:00
appFrontend := getFrontend(ctx)
appFrontend.Quit()
2021-07-25 15:04:01 +10:00
}
2022-02-04 07:52:45 +11:00
// Hide the application
func Hide(ctx context.Context) {
if ctx == nil {
2022-07-30 09:20:18 +10:00
log.Fatalf("Error calling 'runtime.Hide': %s", contextError)
}
appFrontend := getFrontend(ctx)
appFrontend.Hide()
}
// Show the application if it is hidden
func Show(ctx context.Context) {
if ctx == nil {
2022-07-30 09:20:18 +10:00
log.Fatalf("Error calling 'runtime.Show': %s", contextError)
}
appFrontend := getFrontend(ctx)
appFrontend.Show()
}
2022-07-07 21:07:42 +10:00
// EnvironmentInfo contains information about the environment
2022-02-04 07:52:45 +11:00
type EnvironmentInfo struct {
2022-07-07 21:07:42 +10:00
BuildType string `json:"buildType"`
Platform string `json:"platform"`
Arch string `json:"arch"`
2022-02-04 07:52:45 +11:00
}
2022-07-07 21:07:42 +10:00
// Environment returns information about the environment
2022-02-04 07:52:45 +11:00
func Environment(ctx context.Context) EnvironmentInfo {
var result EnvironmentInfo
buildType := ctx.Value("buildtype")
if buildType != nil {
result.BuildType = buildType.(string)
}
result.Platform = goruntime.GOOS
result.Arch = goruntime.GOARCH
2022-02-04 07:52:45 +11:00
return result
}