mirror of
https://github.com/taigrr/wails.git
synced 2026-04-02 05:08:54 -07:00
Develop (#265)
* Patch for file dialog on OSX * Update CONTRIBUTORS.md * 262 add wails shutdown method (#264) * feat: support close in bridge mode * feat: WailsShutdown callback Now handles proper shutdown through: * runtime.Window.Close() * Killing the main window * Ctrl-C * chore: version bump
This commit is contained in:
@@ -16,6 +16,7 @@ type Manager struct {
|
||||
functions map[string]*boundFunction
|
||||
internalMethods *internalMethods
|
||||
initMethods []*boundMethod
|
||||
shutdownMethods []*boundMethod
|
||||
log *logger.CustomLogger
|
||||
renderer interfaces.Renderer
|
||||
runtime interfaces.Runtime // The runtime object to pass to bound structs
|
||||
@@ -127,6 +128,9 @@ func (b *Manager) bindMethod(object interface{}) error {
|
||||
if newMethod.isWailsInit {
|
||||
b.log.Debugf("Detected WailsInit function: %s", fullMethodName)
|
||||
b.initMethods = append(b.initMethods, newMethod)
|
||||
} else if newMethod.isWailsShutdown {
|
||||
b.log.Debugf("Detected WailsShutdown function: %s", fullMethodName)
|
||||
b.shutdownMethods = append(b.shutdownMethods, newMethod)
|
||||
} else {
|
||||
// Save boundMethod
|
||||
b.log.Infof("Bound Method: %s()", fullMethodName)
|
||||
@@ -292,3 +296,13 @@ func (b *Manager) callWailsInitMethods() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Shutdown the binding manager
|
||||
func (b *Manager) Shutdown() {
|
||||
b.log.Debug("Shutdown called")
|
||||
for _, method := range b.shutdownMethods {
|
||||
b.log.Debugf("Calling Shutdown for method: %s", method.fullName)
|
||||
method.call("[]")
|
||||
}
|
||||
b.log.Debug("Shutdown complete")
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ type boundMethod struct {
|
||||
log *logger.CustomLogger
|
||||
hasErrorReturnType bool // Indicates if there is an error return type
|
||||
isWailsInit bool
|
||||
isWailsShutdown bool
|
||||
}
|
||||
|
||||
// Creates a new bound method based on the given method + type
|
||||
@@ -39,6 +40,11 @@ func newBoundMethod(name string, fullName string, method reflect.Value, objectTy
|
||||
err = result.processWailsInit()
|
||||
}
|
||||
|
||||
// Are we a WailsShutdown method?
|
||||
if result.Name == "WailsShutdown" {
|
||||
err = result.processWailsShutdown()
|
||||
}
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
@@ -211,3 +217,20 @@ func (b *boundMethod) processWailsInit() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *boundMethod) processWailsShutdown() error {
|
||||
// We must have only 1 input, it must be *wails.Runtime
|
||||
if len(b.inputs) != 0 {
|
||||
return fmt.Errorf("Invalid WailsShutdown() definition. Expected 0 inputs, but got %d", len(b.inputs))
|
||||
}
|
||||
|
||||
// We must have only 1 output, it must be error
|
||||
if len(b.returnTypes) != 0 {
|
||||
return fmt.Errorf("Invalid WailsShutdown() definition. Expected 0 return types, but got %d", len(b.returnTypes))
|
||||
}
|
||||
|
||||
// We are indeed a wails Shutdown method
|
||||
b.isWailsShutdown = true
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,18 +3,21 @@ package event
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/wailsapp/wails/lib/interfaces"
|
||||
"github.com/wailsapp/wails/lib/logger"
|
||||
"github.com/wailsapp/wails/lib/messages"
|
||||
"github.com/wailsapp/wails/lib/interfaces"
|
||||
)
|
||||
|
||||
// Manager handles and processes events
|
||||
type Manager struct {
|
||||
incomingEvents chan *messages.EventData
|
||||
listeners map[string][]*eventListener
|
||||
exit bool
|
||||
running bool
|
||||
log *logger.CustomLogger
|
||||
renderer interfaces.Renderer // Messages will be dispatched to the frontend
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
// NewManager creates a new event manager with a 100 event buffer
|
||||
@@ -22,7 +25,7 @@ func NewManager() interfaces.EventManager {
|
||||
return &Manager{
|
||||
incomingEvents: make(chan *messages.EventData, 100),
|
||||
listeners: make(map[string][]*eventListener),
|
||||
exit: false,
|
||||
running: false,
|
||||
log: logger.NewCustomLogger("Events"),
|
||||
}
|
||||
}
|
||||
@@ -87,15 +90,14 @@ func (e *Manager) Start(renderer interfaces.Renderer) {
|
||||
// Store renderer
|
||||
e.renderer = renderer
|
||||
|
||||
// Set up waitgroup so we can wait for goroutine to start
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
// Set up waitgroup so we can wait for goroutine to quit
|
||||
e.running = true
|
||||
e.wg.Add(1)
|
||||
|
||||
// Run main loop in separate goroutine
|
||||
go func() {
|
||||
wg.Done()
|
||||
e.log.Info("Listening")
|
||||
for e.exit == false {
|
||||
for e.running {
|
||||
// TODO: Listen for application exit
|
||||
select {
|
||||
case event := <-e.incomingEvents:
|
||||
@@ -139,14 +141,18 @@ func (e *Manager) Start(renderer interfaces.Renderer) {
|
||||
}
|
||||
}
|
||||
}
|
||||
default:
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
e.wg.Done()
|
||||
}()
|
||||
|
||||
// Wait for goroutine to start
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func (e *Manager) stop() {
|
||||
e.exit = true
|
||||
// Shutdown is called when exiting the Application
|
||||
func (e *Manager) Shutdown() {
|
||||
e.log.Debug("Shutting Down")
|
||||
e.running = false
|
||||
e.log.Debug("Waiting for main loop to exit")
|
||||
e.wg.Wait()
|
||||
}
|
||||
|
||||
@@ -7,4 +7,5 @@ type BindingManager interface {
|
||||
Bind(object interface{})
|
||||
Start(renderer Renderer, runtime Runtime) error
|
||||
ProcessCall(callData *messages.CallData) (result interface{}, err error)
|
||||
Shutdown()
|
||||
}
|
||||
|
||||
@@ -8,4 +8,5 @@ type EventManager interface {
|
||||
Emit(eventName string, optionalData ...interface{})
|
||||
On(eventName string, callback func(...interface{}))
|
||||
Start(Renderer)
|
||||
Shutdown()
|
||||
}
|
||||
|
||||
@@ -5,4 +5,5 @@ type IPCManager interface {
|
||||
BindRenderer(Renderer)
|
||||
Dispatch(message string)
|
||||
Start(eventManager EventManager, bindingManager BindingManager)
|
||||
Shutdown()
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package ipc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/wailsapp/wails/lib/interfaces"
|
||||
"github.com/wailsapp/wails/lib/logger"
|
||||
@@ -12,18 +14,20 @@ import (
|
||||
type Manager struct {
|
||||
renderer interfaces.Renderer // The renderer
|
||||
messageQueue chan *ipcMessage
|
||||
// quitChannel chan struct{}
|
||||
quitChannel chan struct{}
|
||||
// signals chan os.Signal
|
||||
log *logger.CustomLogger
|
||||
eventManager interfaces.EventManager
|
||||
bindingManager interfaces.BindingManager
|
||||
running bool
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
// NewManager creates a new IPC Manager
|
||||
func NewManager() interfaces.IPCManager {
|
||||
result := &Manager{
|
||||
messageQueue: make(chan *ipcMessage, 100),
|
||||
// quitChannel: make(chan struct{}),
|
||||
quitChannel: make(chan struct{}),
|
||||
// signals: make(chan os.Signal, 1),
|
||||
log: logger.NewCustomLogger("IPC"),
|
||||
}
|
||||
@@ -44,9 +48,12 @@ func (i *Manager) Start(eventManager interfaces.EventManager, bindingManager int
|
||||
|
||||
i.log.Info("Starting")
|
||||
// signal.Notify(manager.signals, os.Interrupt)
|
||||
i.running = true
|
||||
|
||||
// Keep track of this goroutine
|
||||
i.wg.Add(1)
|
||||
go func() {
|
||||
running := true
|
||||
for running {
|
||||
for i.running {
|
||||
select {
|
||||
case incomingMessage := <-i.messageQueue:
|
||||
i.log.DebugFields("Processing message", logger.Fields{
|
||||
@@ -117,15 +124,12 @@ func (i *Manager) Start(eventManager interfaces.EventManager, bindingManager int
|
||||
i.log.DebugFields("Finished processing message", logger.Fields{
|
||||
"1D": &incomingMessage,
|
||||
})
|
||||
// case <-manager.quitChannel:
|
||||
// Debug("[MessageQueue] Quit caught")
|
||||
// running = false
|
||||
// case <-manager.signals:
|
||||
// Debug("[MessageQueue] Signal caught")
|
||||
// running = false
|
||||
default:
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
i.log.Debug("Stopping")
|
||||
i.wg.Done()
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -167,3 +171,11 @@ func (i *Manager) SendResponse(response *ipcResponse) error {
|
||||
// Call back to the front end
|
||||
return i.renderer.Callback(data)
|
||||
}
|
||||
|
||||
// Shutdown is called when exiting the Application
|
||||
func (i *Manager) Shutdown() {
|
||||
i.log.Debug("Shutdown called")
|
||||
i.running = false
|
||||
i.log.Debug("Waiting of main loop shutdown")
|
||||
i.wg.Wait()
|
||||
}
|
||||
|
||||
@@ -156,7 +156,7 @@ func (h *Bridge) Run() error {
|
||||
h.log.Info("The frontend will connect automatically.")
|
||||
|
||||
err := h.server.ListenAndServe()
|
||||
if err != nil {
|
||||
if err != nil && err != http.ErrServerClosed {
|
||||
h.log.Fatal(err.Error())
|
||||
}
|
||||
return err
|
||||
@@ -250,5 +250,9 @@ func (h *Bridge) SetTitle(title string) {
|
||||
// Close is unsupported for Bridge but required
|
||||
// for the Renderer interface
|
||||
func (h *Bridge) Close() {
|
||||
h.log.Warn("Close() unsupported in bridge mode")
|
||||
h.log.Debug("Shutting down")
|
||||
err := h.server.Close()
|
||||
if err != nil {
|
||||
h.log.Errorf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
package webview
|
||||
|
||||
/*
|
||||
#cgo linux openbsd freebsd CFLAGS: -DWEBVIEW_GTK=1
|
||||
#cgo linux openbsd freebsd CFLAGS: -DWEBVIEW_GTK=1 -Wno-deprecated-declarations
|
||||
#cgo linux openbsd freebsd pkg-config: gtk+-3.0 webkit2gtk-4.0
|
||||
|
||||
#cgo windows CFLAGS: -DWEBVIEW_WINAPI=1
|
||||
|
||||
@@ -86,7 +86,7 @@ struct webview_priv
|
||||
NSAutoreleasePool *pool;
|
||||
NSWindow *window;
|
||||
WebView *webview;
|
||||
id windowDelegate;
|
||||
id delegate;
|
||||
int should_exit;
|
||||
};
|
||||
#else
|
||||
@@ -1894,6 +1894,22 @@ struct webview_priv
|
||||
[script setValue:self forKey:@"external"];
|
||||
}
|
||||
|
||||
static void webview_run_input_open_panel(id self, SEL cmd, id webview,
|
||||
id listener, BOOL allowMultiple) {
|
||||
char filename[256] = "";
|
||||
struct webview *w =
|
||||
(struct webview *)objc_getAssociatedObject(self, "webview");
|
||||
|
||||
webview_dialog(w, WEBVIEW_DIALOG_TYPE_OPEN, WEBVIEW_DIALOG_FLAG_FILE, "", "",
|
||||
filename, 255);
|
||||
if (strlen(filename)) {
|
||||
[listener chooseFilename:[NSString stringWithUTF8String:filename]];
|
||||
} else {
|
||||
[listener cancel];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void webview_external_invoke(id self, SEL cmd, id arg)
|
||||
{
|
||||
struct webview *w =
|
||||
@@ -1927,12 +1943,17 @@ struct webview_priv
|
||||
class_addMethod(webViewDelegateClass,
|
||||
sel_registerName("webView:didClearWindowObject:forFrame:"),
|
||||
(IMP)webview_did_clear_window_object, "v@:@@@");
|
||||
class_addMethod(
|
||||
webViewDelegateClass,
|
||||
sel_registerName("webView:runOpenPanelForFileButtonWithResultListener:"
|
||||
"allowMultipleFiles:"),
|
||||
(IMP)webview_run_input_open_panel, "v@:@@c");
|
||||
class_addMethod(webViewDelegateClass, sel_registerName("invoke:"),
|
||||
(IMP)webview_external_invoke, "v@:@");
|
||||
objc_registerClassPair(webViewDelegateClass);
|
||||
|
||||
w->priv.windowDelegate = [[webViewDelegateClass alloc] init];
|
||||
objc_setAssociatedObject(w->priv.windowDelegate, "webview", (id)(w),
|
||||
w->priv.delegate = [[webViewDelegateClass alloc] init];
|
||||
objc_setAssociatedObject(w->priv.delegate, "webview", (id)(w),
|
||||
OBJC_ASSOCIATION_ASSIGN);
|
||||
|
||||
NSRect r = NSMakeRect(0, 0, w->width, w->height);
|
||||
@@ -1960,7 +1981,7 @@ struct webview_priv
|
||||
NSString *nsTitle = [NSString stringWithUTF8String:w->title];
|
||||
[w->priv.window setTitle:nsTitle];
|
||||
|
||||
[w->priv.window setDelegate:w->priv.windowDelegate];
|
||||
[w->priv.window setDelegate:w->priv.delegate];
|
||||
[w->priv.window center];
|
||||
|
||||
// NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier:@"wat"];
|
||||
@@ -1989,7 +2010,8 @@ struct webview_priv
|
||||
[w->priv.webview setAutoresizesSubviews:YES];
|
||||
[w->priv.webview
|
||||
setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
|
||||
w->priv.webview.frameLoadDelegate = w->priv.windowDelegate;
|
||||
w->priv.webview.frameLoadDelegate = w->priv.delegate;
|
||||
w->priv.webview.UIDelegate = w->priv.delegate;
|
||||
[[w->priv.window contentView] addSubview:w->priv.webview];
|
||||
[w->priv.window orderFrontRegardless];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user