Refactor app (#1909)

* Application refactor

* [windows] refactor out main loop. Create new application struct. Refactor assethandler/assetserver signatures.

* Refactor darwin app

* Refactor app for linux

* Update v2/internal/frontend/assetserver/assethandler.go

Co-authored-by: stffabi <stffabi@users.noreply.github.com>

* Update v2/internal/frontend/assetserver/assethandler.go

Co-authored-by: stffabi <stffabi@users.noreply.github.com>

* Update v2/internal/frontend/assetserver/assetserver.go

Co-authored-by: stffabi <stffabi@users.noreply.github.com>

* Update v2/internal/frontend/assetserver/assetserver.go

Co-authored-by: stffabi <stffabi@users.noreply.github.com>

Co-authored-by: stffabi <stffabi@users.noreply.github.com>
This commit is contained in:
Lea Anthony
2022-09-29 18:43:35 +10:00
committed by GitHub
co-authored by stffabi
parent e22cfc18c9
commit b2069c871d
31 changed files with 205 additions and 198 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
module github.com/wailsapp/wails/v2
go 1.19
go 1.18
require (
github.com/Masterminds/semver v1.5.0
+41
View File
@@ -0,0 +1,41 @@
package app
import (
"context"
"github.com/wailsapp/wails/v2/internal/frontend"
"github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/internal/menumanager"
"github.com/wailsapp/wails/v2/pkg/menu"
"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
}
// Shutdown the application
func (a *App) Shutdown() {
if a.frontend != nil {
a.frontend.Quit()
}
}
// SetApplicationMenu sets the application menu
func (a *App) SetApplicationMenu(menu *menu.Menu) {
if a.frontend != nil {
a.frontend.MenuSetApplicationMenu(menu)
}
}
+7 -16
View File
@@ -1,5 +1,4 @@
//go:build bindings
// +build bindings
package app
@@ -16,23 +15,17 @@ import (
"github.com/wailsapp/wails/v2/pkg/options"
)
// App defines a Wails application structure
type App struct {
logger *logger.Logger
appoptions *options.App
}
func (a *App) Run() error {
// Create binding exemptions - Ugly hack. There must be a better way
bindingExemptions := []interface{}{
a.appoptions.OnStartup,
a.appoptions.OnShutdown,
a.appoptions.OnDomReady,
a.appoptions.OnBeforeClose,
a.options.OnStartup,
a.options.OnShutdown,
a.options.OnDomReady,
a.options.OnBeforeClose,
}
appBindings := binding.NewBindings(a.logger, a.appoptions.Bind, bindingExemptions, IsObfuscated())
appBindings := binding.NewBindings(a.logger, a.options.Bind, bindingExemptions, IsObfuscated())
err := generateBindings(appBindings)
if err != nil {
@@ -41,8 +34,6 @@ func (a *App) Run() error {
return nil
}
func (a *App) Shutdown() {}
// CreateApp creates the app!
func CreateApp(appoptions *options.App) (*App, error) {
// Set up logger
@@ -50,8 +41,8 @@ func CreateApp(appoptions *options.App) (*App, error) {
myLogger.SetLogLevel(appoptions.LogLevel)
result := &App{
logger: myLogger,
appoptions: appoptions,
logger: myLogger,
options: appoptions,
}
return result, nil
-16
View File
@@ -1,16 +0,0 @@
//go:build darwin && !bindings
// +build darwin,!bindings
package app
import (
"github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/pkg/options"
)
func PreflightChecks(options *options.App, logger *logger.Logger) error {
_ = options
return nil
}
-1
View File
@@ -1,5 +1,4 @@
//go:build debug
// +build debug
package app
@@ -1,5 +1,4 @@
//go:build !debug
// +build !debug
package app
-24
View File
@@ -1,24 +0,0 @@
//go:build !dev && !production && !bindings && darwin
// +build !dev,!production,!bindings,darwin
package app
import (
"fmt"
"github.com/wailsapp/wails/v2/pkg/options"
)
// App defines a Wails application structure
type App struct{}
func (a *App) Run() error {
return nil
}
func (a *App) Shutdown() {}
// CreateApp creates the app!
func CreateApp(_ *options.App) (*App, error) {
return nil, fmt.Errorf(`Wails applications will not build without the correct build tags.`)
}
@@ -1,5 +1,4 @@
//go:build !dev && !production && !bindings && linux
// +build !dev,!production,!bindings,linux
//go:build !dev && !production && !bindings && (linux || darwin)
package app
@@ -9,15 +8,10 @@ import (
"github.com/wailsapp/wails/v2/pkg/options"
)
// App defines a Wails application structure
type App struct{}
func (a *App) Run() error {
return nil
}
func (a *App) Shutdown() {}
// CreateApp creates the app!
func CreateApp(_ *options.App) (*App, error) {
return nil, fmt.Errorf(`Wails applications will not build without the correct build tags.`)
-6
View File
@@ -1,5 +1,4 @@
//go:build !dev && !production && !bindings && windows
// +build !dev,!production,!bindings,windows
package app
@@ -10,15 +9,10 @@ import (
"github.com/wailsapp/wails/v2/pkg/options"
)
// App defines a Wails application structure
type App struct{}
func (a *App) Run() error {
return nil
}
func (a *App) Shutdown() {}
// CreateApp creates the app!
func CreateApp(_ *options.App) (*App, error) {
result := w32.MessageBox(0,
+2 -23
View File
@@ -1,5 +1,4 @@
//go:build dev
// +build dev
package app
@@ -14,7 +13,6 @@ import (
"path/filepath"
"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/devserver"
"github.com/wailsapp/wails/v2/internal/frontend/dispatcher"
@@ -26,29 +24,10 @@ import (
"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)
a.frontend.RunMainLoop()
a.frontend.WindowClose()
if a.shutdownCallback != nil {
a.shutdownCallback(a.ctx)
}
-16
View File
@@ -1,16 +0,0 @@
//go:build linux && !bindings
// +build linux,!bindings
package app
import (
"github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/pkg/options"
)
func PreflightChecks(options *options.App, logger *logger.Logger) error {
_ = options
return nil
}
+12
View File
@@ -0,0 +1,12 @@
//go:build (linux || darwin) && !bindings
package app
import (
"github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/pkg/options"
)
func PreflightChecks(_ *options.App, _ *logger.Logger) error {
return nil
}
@@ -1,5 +1,4 @@
//go:build windows && !bindings
// +build windows,!bindings
package app
+2 -22
View File
@@ -6,7 +6,6 @@ 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"
@@ -15,29 +14,10 @@ import (
"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)
a.frontend.RunMainLoop()
a.frontend.WindowClose()
if a.shutdownCallback != nil {
a.shutdownCallback(a.ctx)
}
@@ -14,7 +14,6 @@ import (
"github.com/wailsapp/wails/v2/internal/fs"
"github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/pkg/options"
)
//go:embed defaultindex.html
@@ -33,8 +32,7 @@ type assetHandler struct {
retryMissingFiles bool
}
func NewAssetHandler(ctx context.Context, options *options.App) (http.Handler, error) {
vfs := options.Assets
func NewAssetHandler(ctx context.Context, vfs iofs.FS, assetsHandler http.Handler) (http.Handler, error) {
if vfs != nil {
if _, err := vfs.Open("."); err != nil {
return nil, err
@@ -53,7 +51,7 @@ func NewAssetHandler(ctx context.Context, options *options.App) (http.Handler, e
result := &assetHandler{
fs: vfs,
handler: options.AssetsHandler,
handler: assetsHandler,
}
if _logger := ctx.Value("logger"); _logger != nil {
@@ -4,13 +4,13 @@ import (
"bytes"
"context"
"fmt"
iofs "io/fs"
"net/http"
"net/http/httptest"
"strconv"
"github.com/wailsapp/wails/v2/internal/frontend/runtime"
"github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/pkg/options"
"golang.org/x/net/html"
)
@@ -31,8 +31,8 @@ type AssetServer struct {
appendSpinnerToBody bool
}
func NewAssetServer(ctx context.Context, options *options.App, bindingsJSON string) (*AssetServer, error) {
handler, err := NewAssetHandler(ctx, options)
func NewAssetServer(ctx context.Context, vfs iofs.FS, assetsHandler http.Handler, bindingsJSON string) (*AssetServer, error) {
handler, err := NewAssetHandler(ctx, vfs, assetsHandler)
if err != nil {
return nil, err
}
@@ -67,6 +67,8 @@ void SetAbout(void *inctx, const char* title, const char* description, void* ima
void* AppendMenuItem(void* inctx, void* nsmenu, const char* label, const char* shortcutKey, int modifiers, int disabled, int checked, int menuItemID);
void AppendSeparator(void* inMenu);
void UpdateMenuItem(void* nsmenuitem, int checked);
void RunMainLoop(void);
void ReleaseContext(void *inctx);
NSString* safeInit(const char* input);
@@ -381,6 +381,14 @@ void Run(void *inctx, const char* url) {
[_url release];
[app setMainMenu:ctx.applicationMenu];
}
void RunMainLoop(void) {
NSApplication *app = [NSApplication sharedApplication];
[app run];
}
void ReleaseContext(void *inctx) {
WailsContext *ctx = (__bridge WailsContext*) inctx;
[ctx release];
}
@@ -59,6 +59,14 @@ type Frontend struct {
dispatcher frontend.Dispatcher
}
func (f *Frontend) RunMainLoop() {
C.RunMainLoop()
}
func (f *Frontend) WindowClose() {
C.ReleaseContext(f.mainWindow.context)
}
func NewFrontend(ctx context.Context, appoptions *options.App, myLogger *logger.Logger, appBindings *binding.Bindings, dispatcher frontend.Dispatcher) *Frontend {
result := &Frontend{
frontendOptions: appoptions,
@@ -82,7 +90,7 @@ func NewFrontend(ctx context.Context, appoptions *options.App, myLogger *logger.
} else {
appBindings.DB().UpdateObfuscatedCallMap()
}
assets, err := assetserver.NewAssetServer(ctx, appoptions, bindings)
assets, err := assetserver.NewAssetServer(ctx, appoptions.Assets, appoptions.AssetsHandler, bindings)
if err != nil {
log.Fatal(err)
}

Some files were not shown because too many files have changed in this diff Show More