From fda3323386c38400de1a00ba4542c6fb766d8d8d Mon Sep 17 00:00:00 2001 From: ironpark Date: Sat, 4 Dec 2021 04:55:36 +0900 Subject: [PATCH 1/2] basic implement --- v2/internal/frontend/desktop/darwin/frontend.go | 3 +++ v2/internal/frontend/desktop/linux/frontend.go | 3 +++ v2/internal/frontend/desktop/windows/frontend.go | 3 +++ v2/pkg/options/options.go | 7 ++++--- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/v2/internal/frontend/desktop/darwin/frontend.go b/v2/internal/frontend/desktop/darwin/frontend.go index 2d169da1..7e3b4bab 100644 --- a/v2/internal/frontend/desktop/darwin/frontend.go +++ b/v2/internal/frontend/desktop/darwin/frontend.go @@ -206,6 +206,9 @@ func (f *Frontend) WindowSetRGBA(col *options.RGBA) { } func (f *Frontend) Quit() { + if f.frontendOptions.OnBeforeClose != nil && f.frontendOptions.OnBeforeClose(f.ctx) { + return + } f.mainWindow.Quit() } diff --git a/v2/internal/frontend/desktop/linux/frontend.go b/v2/internal/frontend/desktop/linux/frontend.go index 8a94a82f..abcfcd44 100644 --- a/v2/internal/frontend/desktop/linux/frontend.go +++ b/v2/internal/frontend/desktop/linux/frontend.go @@ -208,6 +208,9 @@ func (f *Frontend) WindowSetRGBA(col *options.RGBA) { } func (f *Frontend) Quit() { + if f.frontendOptions.OnBeforeClose != nil && f.frontendOptions.OnBeforeClose(f.ctx) { + return + } f.mainWindow.Quit() } diff --git a/v2/internal/frontend/desktop/windows/frontend.go b/v2/internal/frontend/desktop/windows/frontend.go index 32245c12..eed02b39 100644 --- a/v2/internal/frontend/desktop/windows/frontend.go +++ b/v2/internal/frontend/desktop/windows/frontend.go @@ -261,6 +261,9 @@ func (f *Frontend) WindowSetRGBA(col *options.RGBA) { } func (f *Frontend) Quit() { + if f.frontendOptions.OnBeforeClose != nil && f.frontendOptions.OnBeforeClose(f.ctx) { + return + } // Exit must be called on the Main-Thread. It calls PostQuitMessage which sends the WM_QUIT message to the thread's // message queue and our message queue runs on the Main-Thread. f.mainWindow.Invoke(winc.Exit) diff --git a/v2/pkg/options/options.go b/v2/pkg/options/options.go index 91c1b329..717fd93d 100644 --- a/v2/pkg/options/options.go +++ b/v2/pkg/options/options.go @@ -45,9 +45,10 @@ type App struct { Menu *menu.Menu Logger logger.Logger `json:"-"` LogLevel logger.LogLevel - OnStartup func(ctx context.Context) `json:"-"` - OnDomReady func(ctx context.Context) `json:"-"` - OnShutdown func(ctx context.Context) `json:"-"` + OnStartup func(ctx context.Context) `json:"-"` + OnDomReady func(ctx context.Context) `json:"-"` + OnShutdown func(ctx context.Context) `json:"-"` + OnBeforeClose func(ctx context.Context) (prevent bool) `json:"-"` Bind []interface{} WindowStartState WindowStartState From 92b63d9fad7a988aa81f08fbfb78f3cc265e5d2d Mon Sep 17 00:00:00 2001 From: ironpark Date: Sat, 4 Dec 2021 07:52:34 +0900 Subject: [PATCH 2/2] window delegate modify for onBeforeClose hook --- v2/internal/frontend/desktop/darwin/WindowDelegate.m | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/v2/internal/frontend/desktop/darwin/WindowDelegate.m b/v2/internal/frontend/desktop/darwin/WindowDelegate.m index aca68688..cf22e0f5 100644 --- a/v2/internal/frontend/desktop/darwin/WindowDelegate.m +++ b/v2/internal/frontend/desktop/darwin/WindowDelegate.m @@ -12,16 +12,15 @@ #import "WailsContext.h" @implementation WindowDelegate - - (BOOL)windowShouldClose:(WailsWindow *)sender { - [sender orderOut:nil]; - if( self.hideOnClose == false ) { - processMessage("Q"); + if( self.hideOnClose ) { + [NSApp hide:nil]; + return false; } - return !self.hideOnClose; + processMessage("Q"); + return false; } - - (void)windowDidExitFullScreen:(NSNotification *)notification { [self.ctx.mainWindow applyWindowConstraints]; }