Files

175 lines
4.3 KiB
Go
Raw Permalink Normal View History

2019-01-08 07:58:46 +11:00
package wails
import (
2019-10-23 14:04:41 +11:00
"os"
"syscall"
"github.com/syossan27/tebata"
2019-01-08 07:58:46 +11:00
"github.com/wailsapp/wails/cmd"
2019-07-12 10:12:15 +10:00
"github.com/wailsapp/wails/lib/binding"
"github.com/wailsapp/wails/lib/event"
"github.com/wailsapp/wails/lib/interfaces"
2019-07-20 19:32:30 +10:00
"github.com/wailsapp/wails/lib/ipc"
"github.com/wailsapp/wails/lib/logger"
"github.com/wailsapp/wails/lib/renderer"
2020-08-30 15:46:31 +10:00
wailsruntime "github.com/wailsapp/wails/runtime"
2019-01-08 07:58:46 +11:00
)
// -------------------------------- Compile time Flags ------------------------------
2019-02-04 08:45:12 +11:00
// BuildMode indicates what mode we are in
2019-02-16 06:58:30 +11:00
var BuildMode = cmd.BuildModeProd
2019-02-02 13:58:55 +11:00
2020-08-31 21:17:04 +10:00
// Runtime is the Go Runtime struct
2020-08-30 15:46:31 +10:00
type Runtime = wailsruntime.Runtime
2020-08-31 21:17:04 +10:00
// Store is a state store used for syncing with
// the front end
2020-08-30 15:46:31 +10:00
type Store = wailsruntime.Store
2020-08-31 21:17:04 +10:00
// CustomLogger is a specialised logger
2020-08-30 15:46:31 +10:00
type CustomLogger = logger.CustomLogger
2019-01-08 07:58:46 +11:00
// ----------------------------------------------------------------------------------
// App defines the main application struct
type App struct {
2019-07-20 19:32:30 +10:00
config *AppConfig // The Application configuration object
cli *cmd.Cli // In debug mode, we have a cli
renderer interfaces.Renderer // The renderer is what we will render the app to
logLevel string // The log level of the app
ipc interfaces.IPCManager // Handles the IPC calls
log *logger.CustomLogger // Logger
bindingManager interfaces.BindingManager // Handles binding of Go code to renderer
eventManager interfaces.EventManager // Handles all the events
runtime interfaces.Runtime // The runtime object for registered structs
2019-01-08 07:58:46 +11:00
}
// CreateApp creates the application window with the given configuration
// If none given, the defaults are used
2019-07-12 10:22:19 +10:00
func CreateApp(optionalConfig ...*AppConfig) *App {
var userConfig *AppConfig
2019-01-08 07:58:46 +11:00
if len(optionalConfig) > 0 {
userConfig = optionalConfig[0]
}
result := &App{
2019-11-27 22:55:19 +11:00
logLevel: "debug",
2019-07-12 10:12:15 +10:00
renderer: renderer.NewWebView(),
ipc: ipc.NewManager(),
bindingManager: binding.NewManager(),
eventManager: event.NewManager(),
log: logger.NewCustomLogger("App"),
2019-01-08 07:58:46 +11:00
}
2019-07-12 10:12:15 +10:00
appconfig, err := newConfig(userConfig)
2019-01-08 07:58:46 +11:00
if err != nil {
result.log.Fatalf("Cannot use custom HTML: %s", err.Error())
}
result.config = appconfig
// Set up the CLI if not in release mode
2019-02-16 07:02:16 +11:00
if BuildMode != cmd.BuildModeProd {
2019-01-08 07:58:46 +11:00
result.cli = result.setupCli()
2019-01-10 22:48:54 +11:00
} else {
// Disable Inspector in release mode
2019-01-08 07:58:46 +11:00
result.config.DisableInspector = true
}
2019-11-28 22:47:41 +11:00
// Platform specific init
platformInit()
2019-11-27 22:22:10 +11:00
2019-01-08 07:58:46 +11:00
return result
}
// Run the app
2019-01-10 22:48:54 +11:00
func (a *App) Run() error {
2019-10-23 14:04:41 +11:00
2019-02-16 07:02:16 +11:00
if BuildMode != cmd.BuildModeProd {
2019-01-10 22:48:54 +11:00
return a.cli.Run()
}
2019-01-16 08:19:30 +11:00
a.logLevel = "error"
2019-04-25 20:05:39 +10:00
err := a.start()
if err != nil {
a.log.Error(err.Error())
}
return err
2019-01-08 07:58:46 +11:00
}
func (a *App) start() error {
// Set the log level
2019-07-12 10:12:15 +10:00
logger.SetLogLevel(a.logLevel)
2019-01-08 07:58:46 +11:00
// Log starup
a.log.Info("Starting")
2019-07-20 19:32:30 +10:00
// Check if we are to run in bridge mode
2019-02-16 07:02:16 +11:00
if BuildMode == cmd.BuildModeBridge {
2020-01-25 18:07:53 -06:00
a.renderer = renderer.NewBridge()
2019-01-08 07:58:46 +11:00
}
// Initialise the renderer
err := a.renderer.Initialise(a.config, a.ipc, a.eventManager)
if err != nil {
return err
}
2019-10-23 14:04:41 +11:00
// Start signal handler
t := tebata.New(os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
t.Reserve(func() {
a.log.Debug("SIGNAL CAUGHT! Starting Shutdown")
a.renderer.Close()
})
2019-01-08 07:58:46 +11:00
// Start event manager and give it our renderer
2019-07-12 10:12:15 +10:00
a.eventManager.Start(a.renderer)
2019-01-08 07:58:46 +11:00
// Start the IPC Manager and give it the event manager and binding manager
2019-07-12 10:12:15 +10:00
a.ipc.Start(a.eventManager, a.bindingManager)
2019-01-08 07:58:46 +11:00
// Create the runtime
2020-08-30 15:46:31 +10:00
a.runtime = wailsruntime.NewRuntime(a.eventManager, a.renderer)
2019-01-08 07:58:46 +11:00
// Start binding manager and give it our renderer
2019-07-12 10:12:15 +10:00
err = a.bindingManager.Start(a.renderer, a.runtime)
2019-01-08 07:58:46 +11:00
if err != nil {
return err
}
2019-10-23 14:04:41 +11:00
// Defer the shutdown
defer a.shutdown()
2019-01-08 07:58:46 +11:00
// Run the renderer
2019-10-23 14:04:41 +11:00
err = a.renderer.Run()
if err != nil {
return err
}
return nil
}
// shutdown the app
func (a *App) shutdown() {
// Make sure this is only called once
a.log.Debug("Shutting down")
// Shutdown Binding Manager
a.bindingManager.Shutdown()
// Shutdown IPC Manager
a.ipc.Shutdown()
// Shutdown Event Manager
a.eventManager.Shutdown()
a.log.Debug("Cleanly Shutdown")
2019-01-08 07:58:46 +11:00
}
// Bind allows the user to bind the given object
// with the application
func (a *App) Bind(object interface{}) {
2019-07-12 10:12:15 +10:00
a.bindingManager.Bind(object)
2019-01-08 07:58:46 +11:00
}