Compare commits

...

11 Commits

Author SHA1 Message Date
Lea Anthony
2d1447d558 darwin bridge js 2021-02-08 06:37:57 +11:00
Lea Anthony
7c22cbcf38 Bridge working for calls. Notify TBD 2021-02-08 06:36:13 +11:00
Lea Anthony
e4b03f510b First step to bridge support 2021-02-06 21:50:21 +11:00
Lea Anthony
8dfd206aa9 Updates for using the bridge 2021-02-06 13:36:56 +11:00
Lea Anthony
6dabab1d2e v2.0.0-alpha.21 2021-02-03 07:20:08 +11:00
Lea Anthony
c3bd8b1a85 Remove debug info 2021-02-03 07:15:52 +11:00
Lea Anthony
e1b7332c47 Graceful shutdown 2021-02-03 07:14:44 +11:00
Lea Anthony
5cd08e45f0 v2.0.0-alpha.20 2021-01-31 22:50:01 +11:00
Lea Anthony
c2d03f0e6e Update NSFontWeightRegular to float const 2021-01-31 22:49:29 +11:00
Lea Anthony
d3501f4cb7 v2.0.0-alpha.19 2021-01-31 21:59:16 +11:00
Lea Anthony
ee82cd25b7 Menu Items default to 12 pt on Mac 2021-01-31 21:58:41 +11:00
42 changed files with 1334 additions and 340 deletions

View File

@@ -3,12 +3,12 @@
"browser": true,
"es6": true,
"amd": true,
"node": true,
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 2016,
"sourceType": "module",
"sourceType": "module"
},
"rules": {
"indent": [

View File

@@ -10,9 +10,10 @@ import (
"syscall"
"time"
"github.com/pkg/errors"
"github.com/fsnotify/fsnotify"
"github.com/leaanthony/clir"
"github.com/leaanthony/slicer"
"github.com/wailsapp/wails/v2/internal/fs"
"github.com/wailsapp/wails/v2/internal/process"
"github.com/wailsapp/wails/v2/pkg/clilogger"
@@ -24,14 +25,6 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
command := app.NewSubCommand("dev", "Development mode")
outputType := "desktop"
validTargetTypes := slicer.String([]string{"desktop", "hybrid", "server"})
// Setup target type flag
description := "Type of application to develop. Valid types: " + validTargetTypes.Join(",")
command.StringFlag("t", description, &outputType)
// Passthrough ldflags
ldflags := ""
command.StringFlag("ldflags", "optional ldflags", &ldflags)
@@ -42,15 +35,10 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
// extensions to trigger rebuilds
extensions := "go"
command.StringFlag("m", "Extensions to trigger rebuilds (comma separated) eg go,js,css,html", &extensions)
command.StringFlag("e", "Extensions to trigger rebuilds (comma separated) eg go,js,css,html", &extensions)
command.Action(func() error {
// Validate inputs
if !validTargetTypes.Contains(outputType) {
return fmt.Errorf("output type '%s' is not valid", outputType)
}
// Create logger
logger := clilogger.New(w)
app.PrintBanner()
@@ -64,7 +52,7 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
defer watcher.Close()
var debugBinaryProcess *process.Process = nil
var buildFrontend bool = true
var buildFrontend bool = false
var extensionsThatTriggerARebuild = strings.Split(extensions, ",")
// Setup signal handler
@@ -75,8 +63,10 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
// Do initial build
logger.Println("Building application for development...")
debugBinaryProcess = restartApp(logger, outputType, ldflags, compilerCommand, buildFrontend, debugBinaryProcess)
debugBinaryProcess, err = restartApp(logger, "dev", ldflags, compilerCommand, debugBinaryProcess)
if err != nil {
return err
}
go debounce(100*time.Millisecond, watcher.Events, debounceQuit, func(event fsnotify.Event) {
// logger.Println("event: %+v", event)
@@ -98,7 +88,7 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
// Check for file writes
if event.Op&fsnotify.Write == fsnotify.Write {
// logger.Println("modified file: %s", event.Name)
logger.Println("modified file: %s", event.Name)
var rebuild bool = false
// Iterate all file patterns
@@ -119,17 +109,16 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
return
}
if buildFrontend {
logger.Println("Full rebuild triggered: %s updated", event.Name)
} else {
logger.Println("Partial build triggered: %s updated", event.Name)
}
logger.Println("Partial build triggered: %s updated", event.Name)
// Do a rebuild
// Try and build the app
newBinaryProcess := restartApp(logger, outputType, ldflags, compilerCommand, buildFrontend, debugBinaryProcess)
newBinaryProcess, err := restartApp(logger, "dev", ldflags, compilerCommand, debugBinaryProcess)
if err != nil {
fmt.Printf("Error during build: %s", err.Error())
return
}
// If we have a new process, save it
if newBinaryProcess != nil {
debugBinaryProcess = newBinaryProcess
@@ -167,7 +156,7 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
for quit == false {
select {
case <-quitChannel:
println()
println("Caught quit")
// Notify debouncer to quit
debounceQuit <- true
quit = true
@@ -209,13 +198,13 @@ exit:
}
}
func restartApp(logger *clilogger.CLILogger, outputType string, ldflags string, compilerCommand string, buildFrontend bool, debugBinaryProcess *process.Process) *process.Process {
func restartApp(logger *clilogger.CLILogger, outputType string, ldflags string, compilerCommand string, debugBinaryProcess *process.Process) (*process.Process, error) {
appBinary, err := buildApp(logger, outputType, ldflags, compilerCommand, buildFrontend)
appBinary, err := buildApp(logger, outputType, ldflags, compilerCommand)
println()
if err != nil {
logger.Println("[ERROR] Build Failed: %s", err.Error())
return nil
logger.Fatal(err.Error())
return nil, errors.Wrap(err, "Build Failed:")
}
logger.Println("Build new binary: %s", appBinary)
@@ -244,10 +233,10 @@ func restartApp(logger *clilogger.CLILogger, outputType string, ldflags string,
logger.Fatal("Unable to start application: %s", err.Error())
}
return newProcess
return newProcess, nil
}
func buildApp(logger *clilogger.CLILogger, outputType string, ldflags string, compilerCommand string, buildFrontend bool) (string, error) {
func buildApp(logger *clilogger.CLILogger, outputType string, ldflags string, compilerCommand string) (string, error) {
// Create random output file
outputFile := fmt.Sprintf("debug-%d", time.Now().Unix())
@@ -262,7 +251,7 @@ func buildApp(logger *clilogger.CLILogger, outputType string, ldflags string, co
LDFlags: ldflags,
Compiler: compilerCommand,
OutputFile: outputFile,
IgnoreFrontend: !buildFrontend,
IgnoreFrontend: true,
}
return build.Build(buildOptions)

View File

@@ -1,3 +1,3 @@
package main
var version = "v2.0.0-alpha.18"
var version = "v2.0.0-alpha.21"

View File

@@ -7,6 +7,7 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/fatih/structtag v1.2.0
github.com/fsnotify/fsnotify v1.4.9
github.com/gorilla/websocket v1.4.1
github.com/imdario/mergo v0.3.11
github.com/jackmordaunt/icns v1.0.0
github.com/leaanthony/clir v1.0.4

View File

@@ -1,4 +1,4 @@
// +build !desktop,!hybrid,!server
// +build !desktop,!hybrid,!server,!dev
package app

View File

@@ -3,6 +3,9 @@
package app
import (
"context"
"sync"
"github.com/wailsapp/wails/v2/internal/binding"
"github.com/wailsapp/wails/v2/internal/ffenestri"
"github.com/wailsapp/wails/v2/internal/logger"
@@ -26,10 +29,10 @@ type App struct {
options *options.App
// Subsystems
log *subsystem.Log
runtime *subsystem.Runtime
event *subsystem.Event
binding *subsystem.Binding
log *subsystem.Log
runtime *subsystem.Runtime
event *subsystem.Event
//binding *subsystem.Binding
call *subsystem.Call
menu *subsystem.Menu
dispatcher *messagedispatcher.Dispatcher
@@ -109,8 +112,13 @@ func (a *App) Run() error {
var err error
// Setup a context
var subsystemWaitGroup sync.WaitGroup
parentContext := context.WithValue(context.Background(), "waitgroup", &subsystemWaitGroup)
ctx, cancel := context.WithCancel(parentContext)
// Setup signal handler
signalsubsystem, err := signal.NewManager(a.servicebus, a.logger)
signalsubsystem, err := signal.NewManager(ctx, cancel, a.servicebus, a.logger, a.shutdownCallback)
if err != nil {
return err
}
@@ -124,7 +132,7 @@ func (a *App) Run() error {
return err
}
runtimesubsystem, err := subsystem.NewRuntime(a.servicebus, a.logger, a.startupCallback, a.shutdownCallback)
runtimesubsystem, err := subsystem.NewRuntime(ctx, a.servicebus, a.logger, a.startupCallback, a.shutdownCallback)
if err != nil {
return err
}
@@ -138,17 +146,6 @@ func (a *App) Run() error {
a.loglevelStore = a.runtime.GoRuntime().Store.New("wails:loglevel", a.options.LogLevel)
a.appconfigStore = a.runtime.GoRuntime().Store.New("wails:appconfig", a.options)
// Start the binding subsystem
bindingsubsystem, err := subsystem.NewBinding(a.servicebus, a.logger, a.bindings, a.runtime.GoRuntime())
if err != nil {
return err
}
a.binding = bindingsubsystem
err = a.binding.Start()
if err != nil {
return err
}
// Start the logging subsystem
log, err := subsystem.NewLog(a.servicebus, a.logger, a.loglevelStore)
if err != nil {
@@ -172,18 +169,18 @@ func (a *App) Run() error {
}
// Start the eventing subsystem
event, err := subsystem.NewEvent(a.servicebus, a.logger)
eventsubsystem, err := subsystem.NewEvent(ctx, a.servicebus, a.logger)
if err != nil {
return err
}
a.event = event
a.event = eventsubsystem
err = a.event.Start()
if err != nil {
return err
}
// Start the menu subsystem
menusubsystem, err := subsystem.NewMenu(a.servicebus, a.logger, a.menuManager)
menusubsystem, err := subsystem.NewMenu(ctx, a.servicebus, a.logger, a.menuManager)
if err != nil {
return err
}
@@ -194,11 +191,11 @@ func (a *App) Run() error {
}
// Start the call subsystem
call, err := subsystem.NewCall(a.servicebus, a.logger, a.bindings.DB(), a.runtime.GoRuntime())
callSubsystem, err := subsystem.NewCall(ctx, a.servicebus, a.logger, a.bindings.DB(), a.runtime.GoRuntime())
if err != nil {
return err
}
a.call = call
a.call = callSubsystem
err = a.call.Start()
if err != nil {
return err
@@ -210,12 +207,29 @@ func (a *App) Run() error {
return err
}
result := a.window.Run(dispatcher, bindingDump, a.debug)
err = a.window.Run(dispatcher, bindingDump, a.debug)
a.logger.Trace("Ffenestri.Run() exited")
if err != nil {
return err
}
// Close down all the subsystems
a.logger.Trace("Cancelling subsystems")
cancel()
subsystemWaitGroup.Wait()
a.logger.Trace("Cancelling dispatcher")
dispatcher.Close()
// Close log
a.logger.Trace("Stopping log")
log.Close()
a.logger.Trace("Stopping Service bus")
err = a.servicebus.Stop()
if err != nil {
return err
}
return result
return nil
}

237
v2/internal/app/dev.go Normal file
View File

@@ -0,0 +1,237 @@
// +build dev
package app
import (
"context"
"sync"
"github.com/wailsapp/wails/v2/internal/bridge"
"github.com/wailsapp/wails/v2/internal/menumanager"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/internal/binding"
"github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/internal/messagedispatcher"
"github.com/wailsapp/wails/v2/internal/runtime"
"github.com/wailsapp/wails/v2/internal/servicebus"
"github.com/wailsapp/wails/v2/internal/signal"
"github.com/wailsapp/wails/v2/internal/subsystem"
)
// App defines a Wails application structure
type App struct {
appType string
servicebus *servicebus.ServiceBus
logger *logger.Logger
signal *signal.Manager
options *options.App
// Subsystems
log *subsystem.Log
runtime *subsystem.Runtime
event *subsystem.Event
//binding *subsystem.Binding
call *subsystem.Call
menu *subsystem.Menu
dispatcher *messagedispatcher.Dispatcher
menuManager *menumanager.Manager
// Indicates if the app is in debug mode
debug bool
// This is our binding DB
bindings *binding.Bindings
// Application Stores
loglevelStore *runtime.Store
appconfigStore *runtime.Store
// Startup/Shutdown
startupCallback func(*runtime.Runtime)
shutdownCallback func()
// Bridge
bridge *bridge.Bridge
}
// Create App
func CreateApp(appoptions *options.App) (*App, error) {
// Merge default options
options.MergeDefaults(appoptions)
// Set up logger
myLogger := logger.New(appoptions.Logger)
myLogger.SetLogLevel(appoptions.LogLevel)
// Create the menu manager
menuManager := menumanager.NewManager()
// Process the application menu
menuManager.SetApplicationMenu(options.GetApplicationMenu(appoptions))
// Process context menus
contextMenus := options.GetContextMenus(appoptions)
for _, contextMenu := range contextMenus {
menuManager.AddContextMenu(contextMenu)
}
// Process tray menus
trayMenus := options.GetTrayMenus(appoptions)
for _, trayMenu := range trayMenus {
menuManager.AddTrayMenu(trayMenu)
}
// Create binding exemptions - Ugly hack. There must be a better way
bindingExemptions := []interface{}{appoptions.Startup, appoptions.Shutdown}
result := &App{
appType: "dev",
bindings: binding.NewBindings(myLogger, appoptions.Bind, bindingExemptions),
logger: myLogger,
servicebus: servicebus.New(myLogger),
startupCallback: appoptions.Startup,
shutdownCallback: appoptions.Shutdown,
bridge: bridge.NewBridge(myLogger),
}
result.options = appoptions
// Initialise the app
err := result.Init()
return result, err
}
// Run the application
func (a *App) Run() error {
var err error
// Setup a context
var subsystemWaitGroup sync.WaitGroup
parentContext := context.WithValue(context.Background(), "waitgroup", &subsystemWaitGroup)
ctx, cancel := context.WithCancel(parentContext)
// Setup signal handler
signalsubsystem, err := signal.NewManager(ctx, cancel, a.servicebus, a.logger, a.shutdownCallback)
if err != nil {
return err
}
a.signal = signalsubsystem
a.signal.Start()
// Start the service bus
a.servicebus.Debug()
err = a.servicebus.Start()
if err != nil {
return err
}
runtimesubsystem, err := subsystem.NewRuntime(ctx, a.servicebus, a.logger, a.startupCallback, a.shutdownCallback)
if err != nil {
return err
}
a.runtime = runtimesubsystem
err = a.runtime.Start()
if err != nil {
return err
}
// Application Stores
a.loglevelStore = a.runtime.GoRuntime().Store.New("wails:loglevel", a.options.LogLevel)
a.appconfigStore = a.runtime.GoRuntime().Store.New("wails:appconfig", a.options)
// Start the logging subsystem
log, err := subsystem.NewLog(a.servicebus, a.logger, a.loglevelStore)
if err != nil {
return err
}
a.log = log
err = a.log.Start()
if err != nil {
return err
}
// create the dispatcher
dispatcher, err := messagedispatcher.New(a.servicebus, a.logger)
if err != nil {
return err
}
a.dispatcher = dispatcher
err = dispatcher.Start()
if err != nil {
return err
}
// Start the eventing subsystem
eventsubsystem, err := subsystem.NewEvent(ctx, a.servicebus, a.logger)
if err != nil {
return err
}
a.event = eventsubsystem
err = a.event.Start()
if err != nil {
return err
}
// Start the menu subsystem
menusubsystem, err := subsystem.NewMenu(ctx, a.servicebus, a.logger, a.menuManager)
if err != nil {
return err
}
a.menu = menusubsystem
err = a.menu.Start()
if err != nil {
return err
}
// Start the call subsystem
callSubsystem, err := subsystem.NewCall(ctx, a.servicebus, a.logger, a.bindings.DB(), a.runtime.GoRuntime())
if err != nil {
return err
}
a.call = callSubsystem
err = a.call.Start()
if err != nil {
return err
}
// Dump bindings as a debug
bindingDump, err := a.bindings.ToJSON()
if err != nil {
return err
}
err = a.bridge.Run(dispatcher, bindingDump, a.debug)
a.logger.Trace("Bridge.Run() exited")
if err != nil {
return err
}
// Close down all the subsystems
a.logger.Trace("Cancelling subsystems")
cancel()
subsystemWaitGroup.Wait()
a.logger.Trace("Cancelling dispatcher")
dispatcher.Close()
// Close log
a.logger.Trace("Stopping log")
log.Close()
a.logger.Trace("Stopping Service bus")
err = a.servicebus.Stop()
if err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,99 @@
package bridge
import (
"context"
"log"
"net/http"
"sync"
"github.com/wailsapp/wails/v2/internal/messagedispatcher"
"github.com/gorilla/websocket"
"github.com/wailsapp/wails/v2/internal/logger"
)
type Bridge struct {
upgrader websocket.Upgrader
server *http.Server
myLogger *logger.Logger
bindings string
dispatcher *messagedispatcher.Dispatcher
mu sync.Mutex
sessions map[string]*session
ctx context.Context
cancel context.CancelFunc
}
func NewBridge(myLogger *logger.Logger) *Bridge {
result := &Bridge{
myLogger: myLogger,
upgrader: websocket.Upgrader{CheckOrigin: func(r *http.Request) bool { return true }},
sessions: make(map[string]*session),
}
myLogger.SetLogLevel(1)
ctx, cancel := context.WithCancel(context.Background())
result.ctx = ctx
result.cancel = cancel
result.server = &http.Server{Addr: ":34115"}
http.HandleFunc("/bridge", result.wsBridgeHandler)
return result
}
func (b *Bridge) Run(dispatcher *messagedispatcher.Dispatcher, bindings string, debug bool) error {
// Ensure we cancel the context when we shutdown
defer b.cancel()
b.bindings = bindings
b.dispatcher = dispatcher
b.myLogger.Info("Bridge mode started.")
err := b.server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
return err
}
return nil
}
func (b *Bridge) wsBridgeHandler(w http.ResponseWriter, r *http.Request) {
c, err := b.upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
if err != nil {
http.Error(w, "Could not open websocket connection", http.StatusBadRequest)
}
b.myLogger.Info("Connection from frontend accepted [%s].", c.RemoteAddr().String())
b.startSession(c)
}
func (b *Bridge) startSession(conn *websocket.Conn) {
// Create a new session for this connection
s := newSession(conn, b.bindings, b.dispatcher, b.myLogger, b.ctx)
// Setup the close handler
conn.SetCloseHandler(func(int, string) error {
b.myLogger.Info("Connection dropped [%s].", s.Identifier())
b.mu.Lock()
delete(b.sessions, s.Identifier())
b.mu.Unlock()
return nil
})
b.mu.Lock()
go s.start(len(b.sessions) == 0)
b.sessions[s.Identifier()] = s
b.mu.Unlock()
}

View File

@@ -0,0 +1,121 @@
package bridge
import (
"github.com/wailsapp/wails/v2/pkg/options/dialog"
)
type BridgeClient struct {
session *session
}
func (b BridgeClient) Quit() {
b.session.log.Info("Quit unsupported in Bridge mode")
}
func (b BridgeClient) NotifyEvent(message string) {
//b.session.sendMessage("n" + message)
b.session.log.Info("NotifyEvent: %s", message)
b.session.log.Info("NotifyEvent unsupported in Bridge mode")
}
func (b BridgeClient) CallResult(message string) {
b.session.sendMessage("c" + message)
}
func (b BridgeClient) OpenDialog(dialogOptions *dialog.OpenDialog, callbackID string) {
b.session.log.Info("OpenDialog unsupported in Bridge mode")
}
func (b BridgeClient) SaveDialog(dialogOptions *dialog.SaveDialog, callbackID string) {
b.session.log.Info("SaveDialog unsupported in Bridge mode")
}
func (b BridgeClient) MessageDialog(dialogOptions *dialog.MessageDialog, callbackID string) {
b.session.log.Info("MessageDialog unsupported in Bridge mode")
}
func (b BridgeClient) WindowSetTitle(title string) {
b.session.log.Info("WindowSetTitle unsupported in Bridge mode")
}
func (b BridgeClient) WindowShow() {
b.session.log.Info("WindowShow unsupported in Bridge mode")
}
func (b BridgeClient) WindowHide() {
b.session.log.Info("WindowHide unsupported in Bridge mode")
}
func (b BridgeClient) WindowCenter() {
b.session.log.Info("WindowCenter unsupported in Bridge mode")
}
func (b BridgeClient) WindowMaximise() {
b.session.log.Info("WindowMaximise unsupported in Bridge mode")
}
func (b BridgeClient) WindowUnmaximise() {
b.session.log.Info("WindowUnmaximise unsupported in Bridge mode")
}
func (b BridgeClient) WindowMinimise() {
b.session.log.Info("WindowMinimise unsupported in Bridge mode")
}
func (b BridgeClient) WindowUnminimise() {
b.session.log.Info("WindowUnminimise unsupported in Bridge mode")
}
func (b BridgeClient) WindowPosition(x int, y int) {
b.session.log.Info("WindowPosition unsupported in Bridge mode")
}
func (b BridgeClient) WindowSize(width int, height int) {
b.session.log.Info("WindowSize unsupported in Bridge mode")
}
func (b BridgeClient) WindowSetMinSize(width int, height int) {
b.session.log.Info("WindowSetMinSize unsupported in Bridge mode")
}
func (b BridgeClient) WindowSetMaxSize(width int, height int) {
b.session.log.Info("WindowSetMaxSize unsupported in Bridge mode")
}
func (b BridgeClient) WindowFullscreen() {
b.session.log.Info("WindowFullscreen unsupported in Bridge mode")
}
func (b BridgeClient) WindowUnFullscreen() {
b.session.log.Info("WindowUnFullscreen unsupported in Bridge mode")
}
func (b BridgeClient) WindowSetColour(colour int) {
b.session.log.Info("WindowSetColour unsupported in Bridge mode")
}
func (b BridgeClient) DarkModeEnabled(callbackID string) {
b.session.log.Info("DarkModeEnabled unsupported in Bridge mode")
}
func (b BridgeClient) SetApplicationMenu(menuJSON string) {
b.session.log.Info("SetApplicationMenu unsupported in Bridge mode")
}
func (b BridgeClient) SetTrayMenu(trayMenuJSON string) {
b.session.log.Info("SetTrayMenu unsupported in Bridge mode")
}
func (b BridgeClient) UpdateTrayMenuLabel(JSON string) {
b.session.log.Info("UpdateTrayMenuLabel unsupported in Bridge mode")
}
func (b BridgeClient) UpdateContextMenu(contextMenuJSON string) {
b.session.log.Info("UpdateContextMenu unsupported in Bridge mode")
}
func newBridgeClient(session *session) *BridgeClient {
return &BridgeClient{
session: session,
}
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,145 @@
package bridge
import (
"context"
_ "embed"
"log"
"runtime"
"time"
"github.com/wailsapp/wails/v2/internal/messagedispatcher"
"github.com/gorilla/websocket"
"github.com/wailsapp/wails/v2/internal/logger"
)
//go:embed darwin.js
var darwinRuntime string
// session represents a single websocket session
type session struct {
bindings string
conn *websocket.Conn
//eventManager interfaces.EventManager
log *logger.Logger
//ipc interfaces.IPCManager
// Mutex for writing to the socket
shutdown chan bool
writeChan chan []byte
done bool
// context
ctx context.Context
// client
client *messagedispatcher.DispatchClient
}
func newSession(conn *websocket.Conn, bindings string, dispatcher *messagedispatcher.Dispatcher, logger *logger.Logger, ctx context.Context) *session {
result := &session{
conn: conn,
bindings: bindings,
log: logger,
shutdown: make(chan bool),
writeChan: make(chan []byte, 100),
ctx: ctx,
}
result.client = dispatcher.RegisterClient(newBridgeClient(result))
return result
}
// Identifier returns a string identifier for the remote connection.
// Taking the form of the client's <ip address>:<port>.
func (s *session) Identifier() string {
if s.conn != nil {
return s.conn.RemoteAddr().String()
}
return ""
}
func (s *session) sendMessage(msg string) error {
if !s.done {
s.writeChan <- []byte(msg)
}
return nil
}
func (s *session) start(firstSession bool) {
s.log.SetLogLevel(1)
s.log.Info("Connected to frontend.")
go s.writePump()
var wailsRuntime string
switch runtime.GOOS {
case "darwin":
wailsRuntime = darwinRuntime
default:
log.Fatal("platform not supported")
}
bindingsMessage := "window.wailsbindings = `" + s.bindings + "`;"
s.log.Info(bindingsMessage)
bootstrapMessage := bindingsMessage + wailsRuntime
s.sendMessage("b" + bootstrapMessage)
for {
messageType, buffer, err := s.conn.ReadMessage()
if messageType == -1 {
return
}
if err != nil {
s.log.Error("Error reading message: %v", err)
continue
}
message := string(buffer)
s.log.Debug("Got message: %#v\n", message)
// Dispatch message as normal
s.client.DispatchMessage(message)
if s.done {
break
}
}
}
// Shutdown
func (s *session) Shutdown() {
s.conn.Close()
s.done = true
s.log.Info("session %v exit", s.Identifier())
}
// writePump pulls messages from the writeChan and sends them to the client
// since it uses a channel to read the messages the socket is protected without locks
func (s *session) writePump() {
s.log.Debug("Session %v - writePump start", s.Identifier())
defer s.log.Debug("Session %v - writePump shutdown", s.Identifier())
for {
select {
case <-s.ctx.Done():
s.Shutdown()
return
case msg, ok := <-s.writeChan:
s.conn.SetWriteDeadline(time.Now().Add(1 * time.Second))
if !ok {
s.log.Debug("writeChan was closed!")
s.conn.WriteMessage(websocket.CloseMessage, []byte{})
return
}
if err := s.conn.WriteMessage(websocket.TextMessage, msg); err != nil {
s.log.Debug(err.Error())
return
}
}
}
}

View File

@@ -1,11 +1,12 @@
package ffenestri
import (
"github.com/wailsapp/wails/v2/internal/menumanager"
"runtime"
"strings"
"unsafe"
"github.com/wailsapp/wails/v2/internal/menumanager"
"github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/internal/messagedispatcher"
"github.com/wailsapp/wails/v2/pkg/options"
@@ -118,7 +119,8 @@ func (a *Application) Run(incomingDispatcher Dispatcher, bindings string, debug
fullscreen := a.bool2Cint(a.config.Fullscreen)
startHidden := a.bool2Cint(a.config.StartHidden)
logLevel := C.int(a.config.LogLevel)
app := C.NewApplication(title, width, height, resizable, devtools, fullscreen, startHidden, logLevel)
hideWindowOnClose := a.bool2Cint(a.config.HideWindowOnClose)
app := C.NewApplication(title, width, height, resizable, devtools, fullscreen, startHidden, logLevel, hideWindowOnClose)
// Save app reference
a.app = (*C.struct_Application)(app)

View File

@@ -4,7 +4,7 @@
#include <stdio.h>
struct Application;
extern struct Application *NewApplication(const char *title, int width, int height, int resizable, int devtools, int fullscreen, int startHidden, int logLevel);
extern struct Application *NewApplication(const char *title, int width, int height, int resizable, int devtools, int fullscreen, int startHidden, int logLevel, int hideWindowOnClose);
extern void SetMinWindowSize(struct Application*, int minWidth, int minHeight);
extern void SetMaxWindowSize(struct Application*, int maxWidth, int maxHeight);
extern void Run(struct Application*, int argc, char **argv);

View File

@@ -34,6 +34,12 @@ BOOL yes(id self, SEL cmd)
return YES;
}
// no command simply returns NO!
BOOL no(id self, SEL cmd)
{
return NO;
}
// Prints a hashmap entry
int hashmap_log(void *const context, struct hashmap_element_s *const e) {
printf("%s: %p ", (char*)e->key, e->data);
@@ -65,6 +71,7 @@ struct Application {
// Cocoa data
id application;
id delegate;
id windowDelegate;
id mainWindow;
id wkwebview;
id manager;
@@ -92,6 +99,7 @@ struct Application {
const char *appearance;
int decorations;
int logLevel;
int hideWindowOnClose;
// Features
int frame;
@@ -120,6 +128,9 @@ struct Application {
// Bindings
const char *bindings;
// shutting down flag
bool shuttingDown;
};
// Debug works like sprintf but mutes if the global debug flag is true
@@ -222,6 +233,9 @@ void applyWindowColour(struct Application *app) {
}
void SetColour(struct Application *app, int red, int green, int blue, int alpha) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
app->red = red;
app->green = green;
app->blue = blue;
@@ -235,12 +249,18 @@ void FullSizeContent(struct Application *app) {
}
void Hide(struct Application *app) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
ON_MAIN_THREAD(
msg(app->application, s("hide:"))
);
}
void Show(struct Application *app) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
ON_MAIN_THREAD(
msg(app->mainWindow, s("makeKeyAndOrderFront:"), NULL);
msg(app->application, s("activateIgnoringOtherApps:"), YES);
@@ -422,6 +442,7 @@ void freeDialogIconCache(struct Application *app) {
}
void DestroyApplication(struct Application *app) {
app->shuttingDown = true;
Debug(app, "Destroying Application");
// Free the bindings
@@ -466,10 +487,14 @@ void DestroyApplication(struct Application *app) {
msg(app->manager, s("removeScriptMessageHandlerForName:"), str("error"));
// Close main window
msg(app->mainWindow, s("close"));
if( app->windowDelegate != NULL ) {
msg(app->windowDelegate, s("release"));
msg(app->mainWindow, s("setDelegate:"), NULL);
}
// msg(app->mainWindow, s("close"));
// Terminate app
msg(c("NSApp"), s("terminate:"), NULL);
Debug(app, "Finished Destroying Application");
}
@@ -477,11 +502,32 @@ void DestroyApplication(struct Application *app) {
// used by the application
void Quit(struct Application *app) {
Debug(app, "Quit Called");
DestroyApplication(app);
ON_MAIN_THREAD (
// Terminate app
msg(app->application, s("stop:"), NULL);
id fakeevent = msg(c("NSEvent"),
s("otherEventWithType:location:modifierFlags:timestamp:windowNumber:context:subtype:data1:data2:"),
15, // Type
msg(c("CGPoint"), s("init:x:y:"), 0, 0), // location
0, // flags
0, // timestamp
0, // window
NULL, // context
0, // subtype
0, // data1
0 // data2
);
msg(c("NSApp"), s("postEvent:atStart:"), fakeevent, true);
// msg(c(app->mainWindow), s("performClose:"))
);
}
// SetTitle sets the main window title to the given string
void SetTitle(struct Application *app, const char *title) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
Debug(app, "SetTitle Called");
ON_MAIN_THREAD(
msg(app->mainWindow, s("setTitle:"), str(title));
@@ -503,6 +549,9 @@ bool isFullScreen(struct Application *app) {
// Fullscreen sets the main window to be fullscreen
void Fullscreen(struct Application *app) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
Debug(app, "Fullscreen Called");
if( ! isFullScreen(app) ) {
ToggleFullscreen(app);
@@ -511,6 +560,9 @@ void Fullscreen(struct Application *app) {
// UnFullscreen resets the main window after a fullscreen
void UnFullscreen(struct Application *app) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
Debug(app, "UnFullscreen Called");
if( isFullScreen(app) ) {
ToggleFullscreen(app);
@@ -518,6 +570,9 @@ void UnFullscreen(struct Application *app) {
}
void Center(struct Application *app) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
Debug(app, "Center Called");
ON_MAIN_THREAD(
MAIN_WINDOW_CALL("center");
@@ -532,23 +587,35 @@ void ToggleMaximise(struct Application *app) {
}
void Maximise(struct Application *app) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
if( app->maximised == 0) {
ToggleMaximise(app);
}
}
void Unmaximise(struct Application *app) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
if( app->maximised == 1) {
ToggleMaximise(app);
}
}
void Minimise(struct Application *app) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
ON_MAIN_THREAD(
MAIN_WINDOW_CALL("miniaturize:");
);
}
void Unminimise(struct Application *app) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
ON_MAIN_THREAD(
MAIN_WINDOW_CALL("deminiaturize:");
);
@@ -572,6 +639,9 @@ void dumpFrame(struct Application *app, const char *message, CGRect frame) {
}
void SetSize(struct Application *app, int width, int height) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
ON_MAIN_THREAD(
id screen = getCurrentScreen(app);
@@ -588,6 +658,9 @@ void SetSize(struct Application *app, int width, int height) {
}
void SetPosition(struct Application *app, int x, int y) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
ON_MAIN_THREAD(
id screen = getCurrentScreen(app);
CGRect screenFrame = GET_FRAME(screen);
@@ -613,6 +686,9 @@ void processDialogButton(id alert, char *buttonTitle, char *cancelButton, char *
}
extern void MessageDialog(struct Application *app, char *callbackID, char *type, char *title, char *message, char *icon, char *button1, char *button2, char *button3, char *button4, char *defaultButton, char *cancelButton) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
ON_MAIN_THREAD(
id alert = ALLOC_INIT("NSAlert");
char *dialogType = type;
@@ -726,6 +802,9 @@ extern void MessageDialog(struct Application *app, char *callbackID, char *type,
// OpenDialog opens a dialog to select files/directories
void OpenDialog(struct Application *app, char *callbackID, char *title, char *filters, char *defaultFilename, char *defaultDir, int allowFiles, int allowDirs, int allowMultiple, int showHiddenFiles, int canCreateDirectories, int resolvesAliases, int treatPackagesAsDirectories) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
Debug(app, "OpenDialog Called with callback id: %s", callbackID);
// Create an open panel
@@ -814,6 +893,9 @@ void OpenDialog(struct Application *app, char *callbackID, char *title, char *fi
// SaveDialog opens a dialog to select files/directories
void SaveDialog(struct Application *app, char *callbackID, char *title, char *filters, char *defaultFilename, char *defaultDir, int showHiddenFiles, int canCreateDirectories, int treatPackagesAsDirectories) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
Debug(app, "SaveDialog Called with callback id: %s", callbackID);
// Create an open panel
@@ -891,6 +973,9 @@ void DisableFrame(struct Application *app)
void setMinMaxSize(struct Application *app)
{
// Guard against calling during shutdown
if( app->shuttingDown ) return;
if (app->maxHeight > 0 && app->maxWidth > 0)
{
msg(app->mainWindow, s("setMaxSize:"), CGSizeMake(app->maxWidth, app->maxHeight));
@@ -917,6 +1002,9 @@ void setMinMaxSize(struct Application *app)
void SetMinWindowSize(struct Application *app, int minWidth, int minHeight)
{
// Guard against calling during shutdown
if( app->shuttingDown ) return;
app->minWidth = minWidth;
app->minHeight = minHeight;
@@ -930,6 +1018,9 @@ void SetMinWindowSize(struct Application *app, int minWidth, int minHeight)
void SetMaxWindowSize(struct Application *app, int maxWidth, int maxHeight)
{
// Guard against calling during shutdown
if( app->shuttingDown ) return;
app->maxWidth = maxWidth;
app->maxHeight = maxHeight;
@@ -950,24 +1041,40 @@ void SetDebug(void *applicationPointer, int flag) {
// AddContextMenu sets the context menu map for this application
void AddContextMenu(struct Application *app, const char *contextMenuJSON) {
AddContextMenuToStore(app->contextMenuStore, contextMenuJSON);
// Guard against calling during shutdown
if( app->shuttingDown ) return;
AddContextMenuToStore(app->contextMenuStore, contextMenuJSON);
}
void UpdateContextMenu(struct Application *app, const char* contextMenuJSON) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
UpdateContextMenuInStore(app->contextMenuStore, contextMenuJSON);
}
void AddTrayMenu(struct Application *app, const char *trayMenuJSON) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
AddTrayMenuToStore(app->trayMenuStore, trayMenuJSON);
}
void SetTrayMenu(struct Application *app, const char* trayMenuJSON) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
ON_MAIN_THREAD(
UpdateTrayMenuInStore(app->trayMenuStore, trayMenuJSON);
);
}
void UpdateTrayMenuLabel(struct Application* app, const char* JSON) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
ON_MAIN_THREAD(
UpdateTrayMenuLabelInStore(app->trayMenuStore, JSON);
);
@@ -1034,6 +1141,9 @@ void createApplication(struct Application *app) {
}
void DarkModeEnabled(struct Application *app, const char *callbackID) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
ON_MAIN_THREAD(
const char *result = isDarkMode(app) ? "T" : "F";
@@ -1054,9 +1164,9 @@ void DarkModeEnabled(struct Application *app, const char *callbackID) {
void createDelegate(struct Application *app) {
// Define delegate
Class delegateClass = objc_allocateClassPair((Class) c("NSObject"), "AppDelegate", 0);
bool resultAddProtoc = class_addProtocol(delegateClass, objc_getProtocol("NSApplicationDelegate"));
class_addMethod(delegateClass, s("applicationShouldTerminateAfterLastWindowClosed:"), (IMP) yes, "c@:@");
class_addMethod(delegateClass, s("applicationWillTerminate:"), (IMP) closeWindow, "v@:@");
bool resultAddProtoc = class_addProtocol(delegateClass, objc_getProtocol("NSApplicationDelegate"));
class_addMethod(delegateClass, s("applicationShouldTerminateAfterLastWindowClosed:"), (IMP) no, "c@:@");
// class_addMethod(delegateClass, s("applicationWillTerminate:"), (IMP) closeWindow, "v@:@");
class_addMethod(delegateClass, s("applicationWillFinishLaunching:"), (IMP) willFinishLaunching, "v@:@");
// All Menu Items use a common callback
@@ -1082,6 +1192,11 @@ void createDelegate(struct Application *app) {
msg(app->application, s("setDelegate:"), delegate);
}
bool windowShouldClose(id self, SEL cmd, id sender) {
msg(sender, s("orderBack:"));
return false;
}
void createMainWindow(struct Application *app) {
// Create main window
id mainWindow = ALLOC("NSWindow");
@@ -1100,6 +1215,15 @@ void createMainWindow(struct Application *app) {
msg(mainWindow, s("setTitlebarAppearsTransparent:"), app->titlebarAppearsTransparent ? YES : NO);
msg(mainWindow, s("setTitleVisibility:"), app->hideTitle);
if( app->hideWindowOnClose ) {
// Create window delegate to override windowShouldClose
Class delegateClass = objc_allocateClassPair((Class) c("NSObject"), "WindowDelegate", 0);
bool resultAddProtoc = class_addProtocol(delegateClass, objc_getProtocol("NSWindowDelegate"));
class_replaceMethod(delegateClass, s("windowShouldClose:"), (IMP) windowShouldClose, "v@:@");
app->windowDelegate = msg((id)delegateClass, s("new"));
msg(mainWindow, s("setDelegate:"), app->windowDelegate);
}
app->mainWindow = mainWindow;
}
@@ -1186,7 +1310,7 @@ void parseMenuRole(struct Application *app, id parentMenu, JsonNode *item) {
return;
}
if ( STREQ(roleName, "quit")) {
addMenuItem(parentMenu, "Quit (More work TBD)", "terminate:", "q", FALSE);
addMenuItem(parentMenu, "Quit", "terminate:", "q", FALSE);
return;
}
if ( STREQ(roleName, "togglefullscreen")) {
@@ -1464,6 +1588,9 @@ void updateMenu(struct Application *app, const char *menuAsJSON) {
// SetApplicationMenu sets the initial menu for the application
void SetApplicationMenu(struct Application *app, const char *menuAsJSON) {
// Guard against calling during shutdown
if( app->shuttingDown ) return;
if ( app->applicationMenu == NULL ) {
app->applicationMenu = NewApplicationMenu(menuAsJSON);
return;
@@ -1705,11 +1832,13 @@ void Run(struct Application *app, int argc, char **argv) {
Debug(app, "Run called");
msg(app->application, s("run"));
DestroyApplication(app);
MEMFREE(internalCode);
}
void* NewApplication(const char *title, int width, int height, int resizable, int devtools, int fullscreen, int startHidden, int logLevel) {
void* NewApplication(const char *title, int width, int height, int resizable, int devtools, int fullscreen, int startHidden, int logLevel, int hideWindowOnClose) {
// Load the tray icons
LoadTrayIcons();
@@ -1730,6 +1859,7 @@ void* NewApplication(const char *title, int width, int height, int resizable, in
result->startHidden = startHidden;
result->decorations = 0;
result->logLevel = logLevel;
result->hideWindowOnClose = hideWindowOnClose;
result->mainWindow = NULL;
result->mouseEvent = NULL;
@@ -1758,12 +1888,17 @@ void* NewApplication(const char *title, int width, int height, int resizable, in
// Context Menus
result->contextMenuStore = NewContextMenuStore();
// Window delegate
result->windowDelegate = NULL;
// Window Appearance
result->titlebarAppearsTransparent = 0;
result->webviewIsTranparent = 0;
result->sendMessageToBackend = (ffenestriCallback) messageFromWindowCallback;
result->shuttingDown = false;
return (void*) result;
}

View File

@@ -740,7 +740,7 @@ void processMenuItem(Menu *menu, id parentMenu, JsonNode *item) {
const char *image = getJSONString(item, "Image");
const char *fontName = getJSONString(item, "FontName");
const char *RGBA = getJSONString(item, "RGBA");
int fontSize = 0;
int fontSize = 12;
getJSONInt(item, "FontSize", &fontSize);
// If we have an accelerator

View File

@@ -22,7 +22,7 @@ typedef struct _NSRange {
#define NSFontWeightUltraLight -0.8
#define NSFontWeightThin -0.6
#define NSFontWeightLight -0.4
#define NSFontWeightRegular 0
#define NSFontWeightRegular 0.0
#define NSFontWeightMedium 0.23
#define NSFontWeightSemibold 0.3
#define NSFontWeightBold 0.4

File diff suppressed because one or more lines are too long

View File

@@ -1,6 +1,7 @@
package messagedispatcher
import (
"context"
"encoding/json"
"strconv"
"strings"
@@ -24,7 +25,6 @@ type Dispatcher struct {
dialogChannel <-chan *servicebus.Message
systemChannel <-chan *servicebus.Message
menuChannel <-chan *servicebus.Message
running bool
servicebus *servicebus.ServiceBus
logger logger.CustomLogger
@@ -32,6 +32,13 @@ type Dispatcher struct {
// Clients
clients map[string]*DispatchClient
lock sync.RWMutex
// Context for cancellation
ctx context.Context
cancel context.CancelFunc
// internal wait group
wg sync.WaitGroup
}
// New dispatcher. Needs a service bus to send to.
@@ -76,6 +83,9 @@ func New(servicebus *servicebus.ServiceBus, logger *logger.Logger) (*Dispatcher,
return nil, err
}
// Create context
ctx, cancel := context.WithCancel(context.Background())
result := &Dispatcher{
servicebus: servicebus,
eventChannel: eventChannel,
@@ -87,6 +97,8 @@ func New(servicebus *servicebus.ServiceBus, logger *logger.Logger) (*Dispatcher,
dialogChannel: dialogChannel,
systemChannel: systemChannel,
menuChannel: menuChannel,
ctx: ctx,
cancel: cancel,
}
return result, nil
@@ -97,15 +109,18 @@ func (d *Dispatcher) Start() error {
d.logger.Trace("Starting")
d.running = true
d.wg.Add(1)
// Spin off a go routine
go func() {
for d.running {
defer d.logger.Trace("Shutdown")
for {
select {
case <-d.ctx.Done():
d.wg.Done()
return
case <-d.quitChannel:
d.processQuit()
d.running = false
case resultMessage := <-d.resultChannel:
d.processCallResult(resultMessage)
case eventMessage := <-d.eventChannel:
@@ -120,9 +135,6 @@ func (d *Dispatcher) Start() error {
d.processMenuMessage(menuMessage)
}
}
// Call shutdown
d.shutdown()
}()
return nil
@@ -136,10 +148,6 @@ func (d *Dispatcher) processQuit() {
}
}
func (d *Dispatcher) shutdown() {
d.logger.Trace("Shutdown")
}
// RegisterClient will register the given callback with the dispatcher
// and return a DispatchClient that the caller can use to send messages
func (d *Dispatcher) RegisterClient(client Client) *DispatchClient {
@@ -524,3 +532,8 @@ func (d *Dispatcher) processMenuMessage(result *servicebus.Message) {
d.logger.Error("Unknown menufrontend command: %s", command)
}
}
func (d *Dispatcher) Close() {
d.cancel()
d.wg.Wait()
}

View File

@@ -1,6 +1,7 @@
package process
import (
"os"
"os/exec"
"github.com/wailsapp/wails/v2/pkg/clilogger"
@@ -16,11 +17,14 @@ type Process struct {
// NewProcess creates a new process struct
func NewProcess(logger *clilogger.CLILogger, cmd string, args ...string) *Process {
return &Process{
result := &Process{
logger: logger,
cmd: exec.Command(cmd, args...),
exitChannel: make(chan bool, 1),
}
result.cmd.Stdout = os.Stdout
result.cmd.Stderr = os.Stderr
return result
}
// Start the process

View File

@@ -65,7 +65,7 @@ export function SetBindings(bindingsMap) {
window.backend[packageName][structName][methodName] = function () {
// No timeout by default
var timeout = 0;
let timeout = 0;
// Actual function
function dynamic() {
@@ -89,19 +89,3 @@ export function SetBindings(bindingsMap) {
});
});
}
// /**
// * Determines if the given identifier is valid Javascript
// *
// * @param {boolean} name
// * @returns
// */
// function isValidIdentifier(name) {
// // Don't xss yourself :-)
// try {
// new Function('var ' + name);
// return true;
// } catch (e) {
// return false;
// }
// }

View File

@@ -13,6 +13,7 @@ import { Error } from './log';
import { SendMessage } from 'ipc';
// Defines a single listener with a maximum number of times to callback
/**
* The Listener class defines a listener! :-)
*
@@ -43,7 +44,7 @@ class Listener {
}
}
var eventListeners = {};
let eventListeners = {};
/**
* Registers an event listener that will be invoked `maxCallbacks` times before being destroyed
@@ -96,7 +97,7 @@ export function Once(eventName, callback) {
function notifyListeners(eventData) {
// Get the event name
var eventName = eventData.name;
let eventName = eventData.name;
// Check if we have any listeners for this event
if (eventListeners[eventName]) {
@@ -110,7 +111,7 @@ function notifyListeners(eventData) {
// Get next listener
const listener = eventListeners[eventName][count];
var data = eventData.data;
let data = eventData.data;
// Do the callback
const destroy = listener.Callback(data);
@@ -120,7 +121,7 @@ function notifyListeners(eventData) {
}
}
// Update callbacks with new list of listners
// Update callbacks with new list of listeners
eventListeners[eventName] = newEventListenerList;
}
}

View File

@@ -62,6 +62,4 @@ export function Init() {
// Do platform specific Init
Platform.Init();
window.wailsloader.runtime = true;
}

View File

@@ -27,7 +27,7 @@ export function Init() {
// Setup drag handler
// Based on code from: https://github.com/patr0nus/DeskGap
window.addEventListener('mousedown', function (e) {
var currentElement = e.target;
let currentElement = e.target;
while (currentElement != null) {
if (currentElement.hasAttribute('data-wails-no-drag')) {
break;

View File

@@ -1 +0,0 @@
bridge.js

View File

@@ -0,0 +1,225 @@
/*
_ __ _ __
| | / /___ _(_) /____
| | /| / / __ `/ / / ___/
| |/ |/ / /_/ / / (__ )
|__/|__/\__,_/_/_/____/
The lightweight framework for web-like apps
(c) Lea Anthony 2019-present
*/
/* jshint esversion: 6 */
function init() {
// Bridge object
window.wailsbridge = {
reconnectOverlay: null,
reconnectTimer: 300,
wsURL: 'ws://' + window.location.hostname + ':34115/bridge',
connectionState: null,
config: {},
websocket: null,
callback: null,
overlayHTML:
'<div class="wails-reconnect-overlay"><div class="wails-reconnect-overlay-content"><div class="wails-reconnect-overlay-loadingspinner"></div></div></div>',
overlayCSS:
'.wails-reconnect-overlay{position:fixed;top:0;left:0;width:100%;height:100%;backdrop-filter: blur(20px) saturate(160%) contrast(45%) brightness(140%);display:none;z-index:999999}.wails-reconnect-overlay-content{position:relative;top:50%;transform:translateY(-50%);margin: 0;background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAC8AAAAuCAMAAACPpbA7AAAAflBMVEUAAAAAAAAAAAAAAAAAAAAAAAAAAAAEBAQAAAAAAAAAAAABAQEEBAQAAAAAAAAEBAQAAAADAwMAAAABAQEAAAAAAAAAAAAAAAAAAAACAgICAgIBAQEAAAAAAAAAAAAAAAAAAAACAgIAAAAAAAAAAAAAAAAAAAAAAAAAAAAFBQWCC3waAAAAKXRSTlMALgUMIBk0+xEqJs70Xhb3lu3EjX2EZTlv5eHXvbarQj3cdmpXSqOeUDwaqNAAAAKCSURBVEjHjZTntqsgEIUPVVCwtxg1vfD+L3hHRe8K6snZf+KKn8OewvzsSSeXLruLnz+KHs0gr6DkT3xsRkU6VVn4Ha/UxLe1Z4y64i847sykPBh/AvQ7ry3eFN70oKrfcBJYvm/tQ1qxP4T3emXPeXAkvodPUvtdjbhk+Ft4c0hslTiXVOzxOJ15NWUblQhRsdu3E1AfCjj3Gdm18zSOsiH8Lk4TB480ksy62fiqNo4OpyU8O21l6+hyRtS6z8r1pHlmle5sR1/WXS6Mq2Nl+YeKt3vr+vdH/q4O68tzXuwkiZmngYb4R8Co1jh0+Ww2UTyWxBvtyxLO7QVjO3YOD/lWZpbXDGellFG2Mws58mMnjVZSn7p+XvZ6IF4nn02OJZV0aTO22arp/DgLPtrgpVoi6TPbZm4XQBjY159w02uO0BDdYsfrOEi0M2ulRXlCIPAOuN1NOVhi+riBR3dgwQplYsZRZJLXq23Mlo5njkbY0rZFu3oiNIYG2kqsbVz67OlNuZZIOlfxHDl0UpyRX86z/OYC/3qf1A1xTrMp/PWWM4ePzf8DDp1nesQRpcFk7BlwdzN08ZIALJpCaciQXO0f6k4dnuT/Ewg4l7qSTNzm2SykdHn6GJ12mWc6aCNj/g1cTXpB8YFfr0uVc96aFkkqiIiX4nO+salKwGtIkvfB+Ja8DxMeD3hIXP5mTOYPB4eVT0+32I5ykvPZjesnkGgIREgYnmLrPb0PdV3hoLup2TjcGBPM4mgsfF5BrawZR4/GpzYQzQfrUZCf0TCWYo2DqhdhTJBQ6j4xqmmLN5LjdRIY8LWExiFUsSrza/nmFBqw3I9tEZB9h0lIQSO9if8DkISDAj8CDawAAAAASUVORK5CYII=);background-repeat:no-repeat;background-position:center}.wails-reconnect-overlay-loadingspinner{pointer-events:none;width:2.5em;height:2.5em;border:.4em solid transparent;border-color:#f00 #eee0 #f00 #eee0;border-radius:50%;animation:loadingspin 1s linear infinite;margin:auto;padding:2.5em}@keyframes loadingspin{100%{transform:rotate(360deg); opacity}}',
log: function (message) {
// eslint-disable-next-line
console.log(
'%c wails bridge %c ' + message + ' ',
'background: #aa0000; color: #fff; border-radius: 3px 0px 0px 3px; padding: 1px; font-size: 0.7rem',
'background: #009900; color: #fff; border-radius: 0px 3px 3px 0px; padding: 1px; font-size: 0.7rem'
);
}
};
}
function setupIPCBridge() {
// darwin
window.webkit = {
messageHandlers: {
external: {
postMessage: (message) => {
window.wailsbridge.websocket.send(message);
}
}
}
};
}
// Adapted from webview - thanks zserge!
function injectCSS(css) {
const elem = document.createElement('style');
elem.setAttribute('type', 'text/css');
elem.appendChild(document.createTextNode(css));
const head = document.head || document.getElementsByTagName('head')[0];
head.appendChild(elem);
}
// Creates a node in the Dom
/**
* @param {HTMLElement} parent
* @param {string} elementType
* @param {string} id
* @param {string|null} className
* @param {string|null} content
*/
function createNode(parent, elementType, id, className, content) {
const d = document.createElement(elementType);
if (id) {
d.id = id;
}
if (className) {
d.className = className;
}
if (content) {
d.innerHTML = content;
}
parent.appendChild(d);
return d;
}
// Sets up the overlay
function setupOverlay() {
const body = document.body;
const wailsBridgeNode = createNode(body, 'div', 'wails-bridge', null, null);
wailsBridgeNode.innerHTML = window.wailsbridge.overlayHTML;
// Inject the overlay CSS
injectCSS(window.wailsbridge.overlayCSS);
}
// Start the Wails Bridge
function startBridge() {
// Setup the overlay
setupOverlay();
window.wailsbridge.websocket = null;
window.wailsbridge.connectTimer = null;
window.wailsbridge.reconnectOverlay = document.querySelector(
'.wails-reconnect-overlay'
);
window.wailsbridge.connectionState = 'disconnected';
// Shows the overlay
function showReconnectOverlay() {
window.wailsbridge.reconnectOverlay.style.display = 'block';
}
// Hides the overlay
const hideReconnectOverlay = function () {
window.wailsbridge.reconnectOverlay.style.display = 'none';
}
window.wailsbridge.hideReconnectOverlay = hideReconnectOverlay;
// Adds a script to the Dom.
// Removes it if second parameter is true.
function addScript(script, remove) {
const s = document.createElement('script');
s.setAttribute('type', 'text/javascript');
s.textContent = script;
document.head.appendChild(s);
// Remove internal messages from the DOM
if (remove) {
s.parentNode.removeChild(s);
}
}
// Handles incoming websocket connections
function handleConnect() {
window.wailsbridge.log('Connected to backend');
setupIPCBridge();
hideReconnectOverlay();
clearInterval(window.wailsbridge.connectTimer);
window.wailsbridge.websocket.onclose = handleDisconnect;
window.wailsbridge.websocket.onmessage = handleMessage;
window.wailsbridge.connectionState = 'connected';
}
// Handles websocket disconnects
function handleDisconnect() {
window.wailsbridge.log('Disconnected from backend');
window.wailsbridge.websocket = null;
window.wailsbridge.connectionState = 'disconnected';
showReconnectOverlay();
connect();
}
// Try to connect to the backend every 1s (default value).
// Change this value in the main wailsbridge object.
function connect() {
window.wailsbridge.connectTimer = setInterval(function () {
if (window.wailsbridge.websocket == null) {
window.wailsbridge.websocket = new WebSocket(window.wailsbridge.wsURL);
window.wailsbridge.websocket.onopen = handleConnect;
window.wailsbridge.websocket.onerror = function (e) {
e.stopImmediatePropagation();
e.stopPropagation();
e.preventDefault();
window.wailsbridge.websocket = null;
return false;
};
}
}, window.wailsbridge.reconnectTimer);
}
function handleMessage(message) {
// As a bridge we ignore js and css injections
switch (message.data[0]) {
// Wails library - inject!
case 'b':
message = message.data.slice(1)
addScript(message);
window.wailsbridge.log('Loaded Wails Runtime');
// Now wails runtime is loaded, wails for the ready event
// and callback to the main app
// window.wails.Events.On('wails:loaded', function () {
if (window.wailsbridge.callback) {
window.wailsbridge.log('Notifying application');
window.wailsbridge.callback(window.wails);
}
// });
break;
// // Notifications
// case 'n':
// addScript(message.data.slice(1), true);
// break;
// // Binding
// case 'b':
// const binding = message.data.slice(1);
// //log("Binding: " + binding)
// window.wails._.NewBinding(binding);
// break;
// // Call back
case 'c':
const callbackData = message.data.slice(1);
window.wails._.Callback(callbackData);
break;
default:
window.wailsbridge.log('Unknown message: ' + message.data);
}
}
// Start by showing the overlay...
showReconnectOverlay();
// ...and attempt to connect
connect();
}
function InitBridge(callback) {
// Set up the bridge
init();
// Save the callback
window.wailsbridge.callback = callback;
// Start Bridge
startBridge();
}
module.exports = {
InitBridge: InitBridge,
}

View File

@@ -9,13 +9,25 @@ The lightweight framework for web-like apps
*/
/* jshint esversion: 6 */
import { InitBridge } from './bridge';
/**
* Initialises the Wails runtime
* ready will execute the callback when Wails has loaded
* and initialised.
*
* @param {function} callback
*/
function Init(callback) {
window.wails._.Init(callback);
function ready(callback) {
// If window.wails exists, we are ready
if( window.wails ) {
return callback();
}
// If not we need to setup the bridge
InitBridge(callback);
}
module.exports = Init;
module.exports = {
ready: ready,
};

View File

@@ -23,7 +23,7 @@ module.exports = {
Browser: Browser,
Dialog: Dialog,
Events: Events,
Init: Init,
ready: Init.ready,
Log: Log,
System: System,
Store: Store,

View File

@@ -1,6 +1,6 @@
{
"name": "@wails/runtime",
"version": "1.2.13",
"version": "1.2.24",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@@ -1,6 +1,6 @@
{
"name": "@wails/runtime",
"version": "1.2.22",
"version": "1.3.6",
"description": "Wails V2 Javascript runtime library",
"main": "main.js",
"types": "runtime.d.ts",

View File

@@ -13,7 +13,7 @@ const Events = require('./events');
/**
* Registers an event listener that will be invoked when the user changes the
* desktop theme (light mode / dark mode). The callback receives a boolean which
* desktop theme (light mode / dark mode). The callback receives a booleanean which
* indicates if dark mode is enabled.
*
* @export
@@ -43,12 +43,12 @@ function DarkModeEnabled() {
* Mac Title Bar Config
* Check out https://github.com/lukakerr/NSWindowStyles for some examples of these settings
* @typedef {Object} MacTitleBar
* @param {bool} TitleBarAppearsTransparent - NSWindow.titleBarAppearsTransparent
* @param {bool} HideTitle - NSWindow.hideTitle
* @param {bool} HideTitleBar - NSWindow.hideTitleBar
* @param {bool} FullSizeContent - Makes the webview portion of the window the full size of the window, even over the titlebar
* @param {bool} UseToolbar - Set true to add a blank toolbar to the window (makes the title bar larger)
* @param {bool} HideToolbarSeparator - Set true to remove the separator between the toolbar and the main content area
* @param {boolean} TitleBarAppearsTransparent - NSWindow.titleBarAppearsTransparent
* @param {boolean} HideTitle - NSWindow.hideTitle
* @param {boolean} HideTitleBar - NSWindow.hideTitleBar
* @param {boolean} FullSizeContent - Makes the webview portion of the window the full size of the window, even over the titlebar
* @param {boolean} UseToolbar - Set true to add a blank toolbar to the window (makes the title bar larger)
* @param {boolean} HideToolbarSeparator - Set true to remove the separator between the toolbar and the main content area
*
*/
@@ -65,8 +65,8 @@ function DarkModeEnabled() {
* @param {number} MinHeight - Window Minimum Height
* @param {number} MaxWidth - Window Maximum Width
* @param {number} MaxHeight - Window Maximum Height
* @param {bool} StartHidden - Start with window hidden
* @param {bool} DevTools - Enables the window devtools
* @param {boolean} StartHidden - Start with window hidden
* @param {boolean} DevTools - Enables the window devtools
* @param {number} RBGA - The initial window colour. Convert to hex then it'll mean 0xRRGGBBAA
* @param {MacAppConfig} [Mac] - Configuration when running on Mac
* @param {LinuxAppConfig} [Linux] - Configuration when running on Linux
@@ -88,11 +88,23 @@ function AppConfig() {
return window.wails.System.AppConfig.get();
}
function LogLevel() {
return window.wails.System.LogLevel();
}
function Platform() {
return window.wails.System.Platform();
}
function AppType() {
return window.wails.System.AppType();
}
module.exports = {
OnThemeChange: OnThemeChange,
DarkModeEnabled: DarkModeEnabled,
LogLevel: window.wails.System.LogLevel,
Platform: window.wails.System.Platform,
AppType: window.wails.System.AppType,
LogLevel: LogLevel,
Platform: Platform,
AppType: AppType,
AppConfig: AppConfig,
};

View File

@@ -6,19 +6,20 @@ import (
// Runtime is a means for the user to interact with the application at runtime
type Runtime struct {
Browser Browser
Events Events
Window Window
Dialog Dialog
System System
Menu Menu
Store *StoreProvider
Log Log
bus *servicebus.ServiceBus
Browser Browser
Events Events
Window Window
Dialog Dialog
System System
Menu Menu
Store *StoreProvider
Log Log
bus *servicebus.ServiceBus
shutdownCallback func()
}
// New creates a new runtime
func New(serviceBus *servicebus.ServiceBus) *Runtime {
func New(serviceBus *servicebus.ServiceBus, shutdownCallback func()) *Runtime {
result := &Runtime{
Browser: newBrowser(),
Events: newEvents(serviceBus),
@@ -35,5 +36,11 @@ func New(serviceBus *servicebus.ServiceBus) *Runtime {
// Quit the application
func (r *Runtime) Quit() {
// Call back to user's shutdown method if defined
if r.shutdownCallback != nil {
r.shutdownCallback()
}
// Start shutdown of Wails
r.bus.Publish("quit", "runtime.Quit()")
}

View File

@@ -50,12 +50,20 @@ func main() {
log.Fatal(err)
}
wailsJS := fs.RelativePath("../../../internal/runtime/assets/desktop_" + platform + ".js")
wailsJS := fs.RelativePath("../assets/desktop_" + platform + ".js")
runtimeData, err := ioutil.ReadFile(wailsJS)
if err != nil {
log.Fatal(err)
}
// Copy this file to bridge directory for embedding
bridgeDir := fs.RelativePath("../../bridge/" + platform + ".js")
println("Copying", wailsJS, "to", bridgeDir)
err = fs.CopyFile(wailsJS, bridgeDir)
if err != nil {
log.Fatal(err)
}
// Convert to C structure
runtimeC := `
// runtime.c (c) 2019-Present Lea Anthony.

View File

@@ -1,6 +1,7 @@
package servicebus
import (
"context"
"fmt"
"strings"
"sync"
@@ -12,23 +13,26 @@ import (
type ServiceBus struct {
listeners map[string][]chan *Message
messageQueue chan *Message
quitChannel chan struct{}
wg sync.WaitGroup
lock sync.RWMutex
closed bool
debug bool
logger logger.CustomLogger
ctx context.Context
cancel context.CancelFunc
}
// New creates a new ServiceBus
// The internal message queue is set to 100 messages
// Listener queues are set to 10
func New(logger *logger.Logger) *ServiceBus {
ctx, cancel := context.WithCancel(context.Background())
return &ServiceBus{
listeners: make(map[string][]chan *Message),
messageQueue: make(chan *Message, 100),
quitChannel: make(chan struct{}, 1),
logger: logger.CustomLogger("Service Bus"),
ctx: ctx,
cancel: cancel,
}
}
@@ -63,24 +67,22 @@ func (s *ServiceBus) Debug() {
// Start the service bus
func (s *ServiceBus) Start() error {
s.logger.Trace("Starting")
// Prevent starting when closed
if s.closed {
return fmt.Errorf("cannot call start on closed servicebus")
}
// We run in a different thread
s.wg.Add(1)
s.logger.Trace("Starting")
go func() {
quit := false
defer s.logger.Trace("Stopped")
// Loop until we get a quit message
for !quit {
for {
select {
case <-s.ctx.Done():
return
// Listen for messages
case message := <-s.messageQueue:
@@ -91,16 +93,9 @@ func (s *ServiceBus) Start() error {
}
// Dispatch message
s.dispatchMessage(message)
// Listen for quit messages
case <-s.quitChannel:
quit = true
}
}
// Indicate we have shut down
s.wg.Done()
}()
return nil
@@ -117,10 +112,7 @@ func (s *ServiceBus) Stop() error {
s.closed = true
// Send quit message
s.quitChannel <- struct{}{}
// Wait for dispatcher to stop
s.wg.Wait()
s.cancel()
// Close down subscriber channels
s.lock.Lock()
@@ -135,7 +127,6 @@ func (s *ServiceBus) Stop() error {
// Close message queue
close(s.messageQueue)
s.logger.Trace("Stopped")
return nil
}
@@ -172,7 +163,6 @@ func (s *ServiceBus) Subscribe(topic string) (<-chan *Message, error) {
func (s *ServiceBus) Publish(topic string, data interface{}) {
// Prevent publish when closed
if s.closed {
s.logger.Fatal("cannot call publish on closed servicebus")
return
}
@@ -184,7 +174,6 @@ func (s *ServiceBus) Publish(topic string, data interface{}) {
func (s *ServiceBus) PublishForTarget(topic string, data interface{}, target string) {
// Prevent publish when closed
if s.closed {
s.logger.Fatal("cannot call publish on closed servicebus")
return
}
message := NewMessageForTarget(topic, data, target)

View File

@@ -1,8 +1,10 @@
package signal
import (
"context"
"os"
gosignal "os/signal"
"sync"
"syscall"
"github.com/wailsapp/wails/v2/internal/logger"
@@ -20,24 +22,29 @@ type Manager struct {
// signalChannel
signalchannel chan os.Signal
// Quit channel
quitChannel <-chan *servicebus.Message
// ctx
ctx context.Context
cancel context.CancelFunc
// The shutdown callback to notify the user's app that a shutdown
// has started
shutdownCallback func()
// Parent waitgroup
wg *sync.WaitGroup
}
// NewManager creates a new signal manager
func NewManager(bus *servicebus.ServiceBus, logger *logger.Logger) (*Manager, error) {
// Register quit channel
quitChannel, err := bus.Subscribe("quit")
if err != nil {
return nil, err
}
func NewManager(ctx context.Context, cancel context.CancelFunc, bus *servicebus.ServiceBus, logger *logger.Logger, shutdownCallback func()) (*Manager, error) {
result := &Manager{
bus: bus,
logger: logger.CustomLogger("Event Manager"),
signalchannel: make(chan os.Signal, 2),
quitChannel: quitChannel,
bus: bus,
logger: logger.CustomLogger("Event Manager"),
signalchannel: make(chan os.Signal, 2),
ctx: ctx,
cancel: cancel,
shutdownCallback: shutdownCallback,
wg: ctx.Value("waitgroup").(*sync.WaitGroup),
}
return result, nil
@@ -49,20 +56,28 @@ func (m *Manager) Start() {
// Hook into interrupts
gosignal.Notify(m.signalchannel, os.Interrupt, syscall.SIGTERM)
// Spin off signal listener
m.wg.Add(1)
// Spin off signal listener and wait for either a cancellation
// or signal
go func() {
running := true
for running {
select {
case <-m.signalchannel:
println()
m.logger.Trace("Ctrl+C detected. Shutting down...")
m.bus.Publish("quit", "ctrl-c pressed")
case <-m.quitChannel:
running = false
break
select {
case <-m.signalchannel:
println()
m.logger.Trace("Ctrl+C detected. Shutting down...")
m.bus.Publish("quit", "ctrl-c pressed")
// Shutdown app first
if m.shutdownCallback != nil {
m.shutdownCallback()
}
// Start shutdown of Wails
m.cancel()
case <-m.ctx.Done():
}
m.logger.Trace("Shutdown")
m.wg.Done()
}()
}

View File

@@ -10,9 +10,9 @@ import (
// Binding is the Binding subsystem. It manages all service bus messages
// starting with "binding".
type Binding struct {
quitChannel <-chan *servicebus.Message
bindingChannel <-chan *servicebus.Message
running bool
running bool
// Binding db
bindings *binding.Bindings
@@ -27,12 +27,6 @@ type Binding struct {
// NewBinding creates a new binding subsystem. Uses the given bindings db for reference.
func NewBinding(bus *servicebus.ServiceBus, logger *logger.Logger, bindings *binding.Bindings, runtime *runtime.Runtime) (*Binding, error) {
// Register quit channel
quitChannel, err := bus.Subscribe("quit")
if err != nil {
return nil, err
}
// Subscribe to event messages
bindingChannel, err := bus.Subscribe("binding")
if err != nil {
@@ -40,7 +34,6 @@ func NewBinding(bus *servicebus.ServiceBus, logger *logger.Logger, bindings *bin
}
result := &Binding{
quitChannel: quitChannel,
bindingChannel: bindingChannel,
logger: logger.CustomLogger("Binding Subsystem"),
bindings: bindings,
@@ -61,20 +54,16 @@ func (b *Binding) Start() error {
go func() {
for b.running {
select {
case <-b.quitChannel:
b.running = false
case bindingMessage := <-b.bindingChannel:
b.logger.Trace("Got binding message: %+v", bindingMessage)
}
}
// Call shutdown
b.shutdown()
b.logger.Trace("Shutdown")
}()
return nil
}
func (b *Binding) shutdown() {
b.logger.Trace("Shutdown")
func (b *Binding) Close() {
b.running = false
}

View File

@@ -1,10 +1,13 @@
package subsystem
import (
"context"
"encoding/json"
"fmt"
"github.com/wailsapp/wails/v2/pkg/options/dialog"
"strings"
"sync"
"github.com/wailsapp/wails/v2/pkg/options/dialog"
"github.com/wailsapp/wails/v2/internal/binding"
"github.com/wailsapp/wails/v2/internal/logger"
@@ -16,9 +19,10 @@ import (
// Call is the Call subsystem. It manages all service bus messages
// starting with "call".
type Call struct {
quitChannel <-chan *servicebus.Message
callChannel <-chan *servicebus.Message
running bool
// quit flag
shouldQuit bool
// bindings DB
DB *binding.DB
@@ -31,16 +35,16 @@ type Call struct {
// runtime
runtime *runtime.Runtime
// context
ctx context.Context
// parent waitgroup
wg *sync.WaitGroup
}
// NewCall creates a new call subsystem
func NewCall(bus *servicebus.ServiceBus, logger *logger.Logger, DB *binding.DB, runtime *runtime.Runtime) (*Call, error) {
// Register quit channel
quitChannel, err := bus.Subscribe("quit")
if err != nil {
return nil, err
}
func NewCall(ctx context.Context, bus *servicebus.ServiceBus, logger *logger.Logger, DB *binding.DB, runtime *runtime.Runtime) (*Call, error) {
// Subscribe to event messages
callChannel, err := bus.Subscribe("call:invoke")
@@ -49,12 +53,13 @@ func NewCall(bus *servicebus.ServiceBus, logger *logger.Logger, DB *binding.DB,
}
result := &Call{
quitChannel: quitChannel,
callChannel: callChannel,
logger: logger.CustomLogger("Call Subsystem"),
DB: DB,
bus: bus,
runtime: runtime,
ctx: ctx,
wg: ctx.Value("waitgroup").(*sync.WaitGroup),
}
return result, nil
@@ -63,22 +68,21 @@ func NewCall(bus *servicebus.ServiceBus, logger *logger.Logger, DB *binding.DB,
// Start the subsystem
func (c *Call) Start() error {
c.running = true
c.wg.Add(1)
// Spin off a go routine
go func() {
for c.running {
defer c.logger.Trace("Shutdown")
for {
select {
case <-c.quitChannel:
c.running = false
case <-c.ctx.Done():
c.wg.Done()
return
case callMessage := <-c.callChannel:
// TODO: Check if this works ok in a goroutine
c.processCall(callMessage)
}
}
// Call shutdown
c.shutdown()
}()
return nil
@@ -190,10 +194,6 @@ func (c *Call) sendError(err error, payload *message.CallMessage, clientID strin
c.bus.PublishForTarget("call:result", string(messageData), clientID)
}
func (c *Call) shutdown() {
c.logger.Trace("Shutdown")
}
// CallbackMessage defines a message that contains the result of a call
type CallbackMessage struct {
Result interface{} `json:"result"`

View File

@@ -1,6 +1,7 @@
package subsystem
import (
"context"
"strings"
"sync"
@@ -22,9 +23,7 @@ type eventListener struct {
// Event is the Eventing subsystem. It manages all service bus messages
// starting with "event".
type Event struct {
quitChannel <-chan *servicebus.Message
eventChannel <-chan *servicebus.Message
running bool
// Event listeners
listeners map[string][]*eventListener
@@ -32,16 +31,16 @@ type Event struct {
// logger
logger logger.CustomLogger
// ctx
ctx context.Context
// parent waitgroup
wg *sync.WaitGroup
}
// NewEvent creates a new log subsystem
func NewEvent(bus *servicebus.ServiceBus, logger *logger.Logger) (*Event, error) {
// Register quit channel
quitChannel, err := bus.Subscribe("quit")
if err != nil {
return nil, err
}
func NewEvent(ctx context.Context, bus *servicebus.ServiceBus, logger *logger.Logger) (*Event, error) {
// Subscribe to event messages
eventChannel, err := bus.Subscribe("event")
@@ -50,10 +49,11 @@ func NewEvent(bus *servicebus.ServiceBus, logger *logger.Logger) (*Event, error)
}
result := &Event{
quitChannel: quitChannel,
eventChannel: eventChannel,
logger: logger.CustomLogger("Event Subsystem"),
listeners: make(map[string][]*eventListener),
ctx: ctx,
wg: ctx.Value("waitgroup").(*sync.WaitGroup),
}
return result, nil
@@ -80,15 +80,16 @@ func (e *Event) Start() error {
e.logger.Trace("Starting")
e.running = true
e.wg.Add(1)
// Spin off a go routine
go func() {
for e.running {
defer e.logger.Trace("Shutdown")
for {
select {
case <-e.quitChannel:
e.running = false
break
case <-e.ctx.Done():
e.wg.Done()
return
case eventMessage := <-e.eventChannel:
splitTopic := strings.Split(eventMessage.Topic(), ":")
eventType := splitTopic[1]
@@ -128,8 +129,6 @@ func (e *Event) Start() error {
}
}
// Call shutdown
e.shutdown()
}()
return nil
@@ -190,7 +189,3 @@ func (e *Event) notifyListeners(eventName string, message *message.EventMessage)
// Unlock
e.notifyLock.Unlock()
}
func (e *Event) shutdown() {
e.logger.Trace("Shutdown")
}

View File

@@ -1,8 +1,10 @@
package subsystem
import (
"context"
"strconv"
"strings"
"sync"
"github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/internal/runtime"
@@ -12,15 +14,23 @@ import (
// Log is the Logging subsystem. It handles messages with topics starting
// with "log:"
type Log struct {
logChannel <-chan *servicebus.Message
quitChannel <-chan *servicebus.Message
running bool
logChannel <-chan *servicebus.Message
// quit flag
shouldQuit bool
// Logger!
logger *logger.Logger
// Loglevel store
logLevelStore *runtime.Store
// Context for shutdown
ctx context.Context
cancel context.CancelFunc
// internal waitgroup
wg sync.WaitGroup
}
// NewLog creates a new log subsystem
@@ -32,17 +42,14 @@ func NewLog(bus *servicebus.ServiceBus, logger *logger.Logger, logLevelStore *ru
return nil, err
}
// Subscribe to quit messages
quitChannel, err := bus.Subscribe("quit")
if err != nil {
return nil, err
}
ctx, cancel := context.WithCancel(context.Background())
result := &Log{
logChannel: logChannel,
quitChannel: quitChannel,
logger: logger,
logLevelStore: logLevelStore,
ctx: ctx,
cancel: cancel,
}
return result, nil
@@ -51,15 +58,17 @@ func NewLog(bus *servicebus.ServiceBus, logger *logger.Logger, logLevelStore *ru
// Start the subsystem
func (l *Log) Start() error {
l.running = true
l.wg.Add(1)
// Spin off a go routine
go func() {
for l.running {
defer l.logger.Trace("Logger Shutdown")
for l.shouldQuit == false {
select {
case <-l.quitChannel:
l.running = false
break
case <-l.ctx.Done():
l.wg.Done()
return
case logMessage := <-l.logChannel:
logType := strings.TrimPrefix(logMessage.Topic(), "log:")
switch logType {
@@ -98,8 +107,12 @@ func (l *Log) Start() error {
}
}
}
l.logger.Trace("Logger Shutdown")
}()
return nil
}
func (l *Log) Close() {
l.cancel()
l.wg.Wait()
}

View File

@@ -1,9 +1,12 @@
package subsystem
import (
"context"
"encoding/json"
"github.com/wailsapp/wails/v2/pkg/menu"
"strings"
"sync"
"github.com/wailsapp/wails/v2/pkg/menu"
"github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/internal/menumanager"
@@ -13,9 +16,10 @@ import (
// Menu is the subsystem that handles the operation of menus. It manages all service bus messages
// starting with "menu".
type Menu struct {
quitChannel <-chan *servicebus.Message
menuChannel <-chan *servicebus.Message
running bool
// shutdown flag
shouldQuit bool
// logger
logger logger.CustomLogger
@@ -25,16 +29,16 @@ type Menu struct {
// Menu Manager
menuManager *menumanager.Manager
// ctx
ctx context.Context
// parent waitgroup
wg *sync.WaitGroup
}
// NewMenu creates a new menu subsystem
func NewMenu(bus *servicebus.ServiceBus, logger *logger.Logger, menuManager *menumanager.Manager) (*Menu, error) {
// Register quit channel
quitChannel, err := bus.Subscribe("quit")
if err != nil {
return nil, err
}
func NewMenu(ctx context.Context, bus *servicebus.ServiceBus, logger *logger.Logger, menuManager *menumanager.Manager) (*Menu, error) {
// Subscribe to menu messages
menuChannel, err := bus.Subscribe("menu:")
@@ -43,11 +47,12 @@ func NewMenu(bus *servicebus.ServiceBus, logger *logger.Logger, menuManager *men
}
result := &Menu{
quitChannel: quitChannel,
menuChannel: menuChannel,
logger: logger.CustomLogger("Menu Subsystem"),
bus: bus,
menuManager: menuManager,
ctx: ctx,
wg: ctx.Value("waitgroup").(*sync.WaitGroup),
}
return result, nil
@@ -58,15 +63,16 @@ func (m *Menu) Start() error {
m.logger.Trace("Starting")
m.running = true
m.wg.Add(1)
// Spin off a go routine
go func() {
for m.running {
defer m.logger.Trace("Shutdown")
for {
select {
case <-m.quitChannel:
m.running = false
break
case <-m.ctx.Done():
m.wg.Done()
return
case menuMessage := <-m.menuChannel:
splitTopic := strings.Split(menuMessage.Topic(), ":")
menuMessageType := splitTopic[1]
@@ -147,14 +153,7 @@ func (m *Menu) Start() error {
}
}
}
// Call shutdown
m.shutdown()
}()
return nil
}
func (m *Menu) shutdown() {
m.logger.Trace("Shutdown")
}

View File

@@ -1,6 +1,7 @@
package subsystem
import (
"context"
"fmt"
"strings"
@@ -12,7 +13,6 @@ import (
// Runtime is the Runtime subsystem. It handles messages with topics starting
// with "runtime:"
type Runtime struct {
quitChannel <-chan *servicebus.Message
runtimeChannel <-chan *servicebus.Message
// The hooks channel allows us to hook into frontend startup
@@ -20,22 +20,20 @@ type Runtime struct {
startupCallback func(*runtime.Runtime)
shutdownCallback func()
running bool
// quit flag
shouldQuit bool
logger logger.CustomLogger
// Runtime library
runtime *runtime.Runtime
//ctx
ctx context.Context
}
// NewRuntime creates a new runtime subsystem
func NewRuntime(bus *servicebus.ServiceBus, logger *logger.Logger, startupCallback func(*runtime.Runtime), shutdownCallback func()) (*Runtime, error) {
// Register quit channel
quitChannel, err := bus.Subscribe("quit")
if err != nil {
return nil, err
}
func NewRuntime(ctx context.Context, bus *servicebus.ServiceBus, logger *logger.Logger, startupCallback func(*runtime.Runtime), shutdownCallback func()) (*Runtime, error) {
// Subscribe to log messages
runtimeChannel, err := bus.Subscribe("runtime:")
@@ -50,13 +48,13 @@ func NewRuntime(bus *servicebus.ServiceBus, logger *logger.Logger, startupCallba
}
result := &Runtime{
quitChannel: quitChannel,
runtimeChannel: runtimeChannel,
hooksChannel: hooksChannel,
logger: logger.CustomLogger("Runtime Subsystem"),
runtime: runtime.New(bus),
runtime: runtime.New(bus, shutdownCallback),
startupCallback: startupCallback,
shutdownCallback: shutdownCallback,
ctx: ctx,
}
return result, nil
@@ -65,15 +63,11 @@ func NewRuntime(bus *servicebus.ServiceBus, logger *logger.Logger, startupCallba
// Start the subsystem
func (r *Runtime) Start() error {
r.running = true
// Spin off a go routine
go func() {
for r.running {
defer r.logger.Trace("Shutdown")
for {
select {
case <-r.quitChannel:
r.running = false
break
case hooksMessage := <-r.hooksChannel:
r.logger.Trace(fmt.Sprintf("Received hooksmessage: %+v", hooksMessage))
messageSlice := strings.Split(hooksMessage.Topic(), ":")
@@ -113,11 +107,10 @@ func (r *Runtime) Start() error {
if err != nil {
r.logger.Error(err.Error())
}
case <-r.ctx.Done():
return
}
}
// Call shutdown
r.shutdown()
}()
return nil
@@ -128,15 +121,6 @@ func (r *Runtime) GoRuntime() *runtime.Runtime {
return r.runtime
}
func (r *Runtime) shutdown() {
if r.shutdownCallback != nil {
go r.shutdownCallback()
} else {
r.logger.Warning("no shutdown callback registered!")
}
r.logger.Trace("Shutdown")
}
func (r *Runtime) processBrowserMessage(method string, data interface{}) error {
switch method {
case "open":

View File

@@ -86,6 +86,8 @@ func Build(options *Options) (string, error) {
builder = newHybridBuilder(options)
case "server":
builder = newServerBuilder(options)
case "dev":
builder = newDesktopBuilder(options)
default:
return "", fmt.Errorf("cannot build assets for output type %s", projectData.OutputType)
}

View File

@@ -14,27 +14,28 @@ import (
// App contains options for creating the App
type App struct {
Title string
Width int
Height int
DisableResize bool
Fullscreen bool
MinWidth int
MinHeight int
MaxWidth int
MaxHeight int
StartHidden bool
DevTools bool
RGBA int
ContextMenus []*menu.ContextMenu
TrayMenus []*menu.TrayMenu
Menu *menu.Menu
Mac *mac.Options
Logger logger.Logger `json:"-"`
LogLevel logger.LogLevel
Startup func(*wailsruntime.Runtime) `json:"-"`
Shutdown func() `json:"-"`
Bind []interface{}
Title string
Width int
Height int
DisableResize bool
Fullscreen bool
MinWidth int
MinHeight int
MaxWidth int
MaxHeight int
StartHidden bool
HideWindowOnClose bool
DevTools bool
RGBA int
ContextMenus []*menu.ContextMenu
TrayMenus []*menu.TrayMenu
Menu *menu.Menu
Mac *mac.Options
Logger logger.Logger `json:"-"`
LogLevel logger.LogLevel
Startup func(*wailsruntime.Runtime) `json:"-"`
Shutdown func() `json:"-"`
Bind []interface{}
}
// MergeDefaults will set the minimum default values for an application