From c678ab7d0103f38e6d7345f9709a00a47d3fd182 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Tue, 16 Nov 2021 18:20:41 +1100 Subject: [PATCH] [mac] Potential file dialog fix. Fix for starthidden. Partial WindowStartState --- .../frontend/desktop/darwin/AppDelegate.h | 1 + .../frontend/desktop/darwin/AppDelegate.m | 8 +++++++- .../frontend/desktop/darwin/Application.h | 7 ++++++- .../frontend/desktop/darwin/Application.m | 20 ++++++++++++++++++- .../frontend/desktop/darwin/WailsContext.h | 1 + .../frontend/desktop/darwin/WailsContext.m | 14 +++++++------ .../frontend/desktop/darwin/WailsMenu.m | 4 ++-- v2/internal/frontend/desktop/darwin/window.go | 4 +++- 8 files changed, 47 insertions(+), 12 deletions(-) diff --git a/v2/internal/frontend/desktop/darwin/AppDelegate.h b/v2/internal/frontend/desktop/darwin/AppDelegate.h index 7760b09a3..990c4e8a9 100644 --- a/v2/internal/frontend/desktop/darwin/AppDelegate.h +++ b/v2/internal/frontend/desktop/darwin/AppDelegate.h @@ -14,6 +14,7 @@ @interface AppDelegate : NSResponder @property bool alwaysOnTop; +@property bool startHidden; @property (retain) WailsWindow* mainWindow; @end diff --git a/v2/internal/frontend/desktop/darwin/AppDelegate.m b/v2/internal/frontend/desktop/darwin/AppDelegate.m index 0d468bbfb..a605022fa 100644 --- a/v2/internal/frontend/desktop/darwin/AppDelegate.m +++ b/v2/internal/frontend/desktop/darwin/AppDelegate.m @@ -16,16 +16,22 @@ } - (void)applicationWillFinishLaunching:(NSNotification *)aNotification { [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; - [self.mainWindow makeKeyAndOrderFront:self]; if (self.alwaysOnTop) { [self.mainWindow setLevel:NSStatusWindowLevel]; } + if ( !self.startHidden ) { + [self.mainWindow makeKeyAndOrderFront:self]; + } } - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { [NSApp activateIgnoringOtherApps:YES]; } +- (void)dealloc { + [super dealloc]; +} + @synthesize touchBar; @end diff --git a/v2/internal/frontend/desktop/darwin/Application.h b/v2/internal/frontend/desktop/darwin/Application.h index 3cfbb853e..e42aab7bc 100644 --- a/v2/internal/frontend/desktop/darwin/Application.h +++ b/v2/internal/frontend/desktop/darwin/Application.h @@ -12,7 +12,12 @@ #import #import "WailsContext.h" -WailsContext* Create(const char* title, int width, int height, int frameless, int resizable, int fullscreen, int fullSizeContent, int hideTitleBar, int titlebarAppearsTransparent, int hideTitle, int useToolbar, int hideToolbarSeparator, int webviewIsTransparent, int alwaysOnTop, int hideWindowOnClose, const char *appearance, int windowIsTranslucent, int debug); +#define WindowStartsNormal 0 +#define WindowStartsMaximised 1 +#define WindowStartsMinimised 2 +#define WindowStartsFullscreen 3 + +WailsContext* Create(const char* title, int width, int height, int frameless, int resizable, int fullscreen, int fullSizeContent, int hideTitleBar, int titlebarAppearsTransparent, int hideTitle, int useToolbar, int hideToolbarSeparator, int webviewIsTransparent, int alwaysOnTop, int hideWindowOnClose, const char *appearance, int windowIsTranslucent, int debug, int windowStartState, int startsHidden); void Run(void*); void SetTitle(void* ctx, const char *title); diff --git a/v2/internal/frontend/desktop/darwin/Application.m b/v2/internal/frontend/desktop/darwin/Application.m index 816fe225a..f93a3e52c 100644 --- a/v2/internal/frontend/desktop/darwin/Application.m +++ b/v2/internal/frontend/desktop/darwin/Application.m @@ -12,7 +12,7 @@ #import "WailsMenu.h" #import "WailsMenuItem.h" -WailsContext* Create(const char* title, int width, int height, int frameless, int resizable, int fullscreen, int fullSizeContent, int hideTitleBar, int titlebarAppearsTransparent, int hideTitle, int useToolbar, int hideToolbarSeparator, int webviewIsTransparent, int alwaysOnTop, int hideWindowOnClose, const char *appearance, int windowIsTranslucent, int debug) { +WailsContext* Create(const char* title, int width, int height, int frameless, int resizable, int fullscreen, int fullSizeContent, int hideTitleBar, int titlebarAppearsTransparent, int hideTitle, int useToolbar, int hideToolbarSeparator, int webviewIsTransparent, int alwaysOnTop, int hideWindowOnClose, const char *appearance, int windowIsTranslucent, int debug, int windowStartState, int startsHidden) { [NSApplication sharedApplication]; @@ -20,10 +20,27 @@ WailsContext* Create(const char* title, int width, int height, int frameless, in result.debug = debug; + if ( windowStartState == WindowStartsFullscreen ) { + fullscreen = 1; + } + [result CreateWindow:width :height :frameless :resizable :fullscreen :fullSizeContent :hideTitleBar :titlebarAppearsTransparent :hideTitle :useToolbar :hideToolbarSeparator :webviewIsTransparent :hideWindowOnClose :safeInit(appearance) :windowIsTranslucent]; [result SetTitle:safeInit(title)]; [result Center]; + switch( windowStartState ) { + case WindowStartsMaximised: + [result.mainWindow zoom:nil]; + break; + case WindowStartsMinimised: + //TODO: Can you start a mac app minimised? + break; + } + + if ( startsHidden == 1 ) { + result.startHidden = true; + } + result.alwaysOnTop = alwaysOnTop; result.hideOnClose = hideWindowOnClose; @@ -306,6 +323,7 @@ void Run(void *inctx) { ctx.appdelegate = delegate; delegate.mainWindow = ctx.mainWindow; delegate.alwaysOnTop = ctx.alwaysOnTop; + delegate.startHidden = ctx.startHidden; [ctx loadRequest:@"wails://wails/"]; [app setMainMenu:ctx.applicationMenu]; diff --git a/v2/internal/frontend/desktop/darwin/WailsContext.h b/v2/internal/frontend/desktop/darwin/WailsContext.h index c39687e96..022338c59 100644 --- a/v2/internal/frontend/desktop/darwin/WailsContext.h +++ b/v2/internal/frontend/desktop/darwin/WailsContext.h @@ -26,6 +26,7 @@ @property bool hideOnClose; @property bool shuttingDown; +@property bool startHidden; @property NSSize maxSize; @property NSSize minSize; diff --git a/v2/internal/frontend/desktop/darwin/WailsContext.m b/v2/internal/frontend/desktop/darwin/WailsContext.m index 85da9009e..9a82a9b1c 100644 --- a/v2/internal/frontend/desktop/darwin/WailsContext.m +++ b/v2/internal/frontend/desktop/darwin/WailsContext.m @@ -533,10 +533,10 @@ NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:0 error:nil]; NSString *nsjson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; processOpenFileDialogResponse([nsjson UTF8String]); + [nsjson release]; + [arr release]; }]; - ON_MAIN_THREAD([dialog runModal];) - } @@ -579,11 +579,13 @@ // Setup callback handler [dialog beginSheetModalForWindow:self.mainWindow completionHandler:^(NSModalResponse returnCode) { NSURL *url = [dialog URL]; - processSaveFileDialogResponse([url.path UTF8String]); + if ( url != nil ) { + processSaveFileDialogResponse([url.path UTF8String]); + return; + } + processSaveFileDialogResponse(""); }]; - - ON_MAIN_THREAD([dialog runModal];) - + } - (void) SetAbout :(NSString*)title :(NSString*)description :(void*)imagedata :(int)datalen { diff --git a/v2/internal/frontend/desktop/darwin/WailsMenu.m b/v2/internal/frontend/desktop/darwin/WailsMenu.m index cebb31c49..7b20eb23c 100644 --- a/v2/internal/frontend/desktop/darwin/WailsMenu.m +++ b/v2/internal/frontend/desktop/darwin/WailsMenu.m @@ -19,7 +19,7 @@ } - (NSMenuItem*) newMenuItemWithContext :(WailsContext*)ctx :(NSString*)title :(SEL)selector :(NSString*)key :(NSEventModifierFlags)flags { - NSMenuItem *result = [[NSMenuItem new] autorelease]; + NSMenuItem *result = [NSMenuItem new]; if ( title != nil ) { [result setTitle:title]; } @@ -45,7 +45,7 @@ [super initWithTitle:title]; } [self setAutoenablesItems:NO]; - return self; + return [self init]; } - (void) appendSubmenu :(WailsMenu*)child { diff --git a/v2/internal/frontend/desktop/darwin/window.go b/v2/internal/frontend/desktop/darwin/window.go index 5cf9e465d..2ae07be36 100644 --- a/v2/internal/frontend/desktop/darwin/window.go +++ b/v2/internal/frontend/desktop/darwin/window.go @@ -47,6 +47,7 @@ func NewWindow(frontendOptions *options.App, debugMode bool) *Window { fullscreen := bool2Cint(frontendOptions.Fullscreen) alwaysOnTop := bool2Cint(frontendOptions.AlwaysOnTop) hideWindowOnClose := bool2Cint(frontendOptions.HideWindowOnClose) + startsHidden := bool2Cint(frontendOptions.StartHidden) debug := bool2Cint(debugMode) var fullSizeContent, hideTitleBar, hideTitle, useToolbar, webviewIsTransparent C.int @@ -55,6 +56,7 @@ func NewWindow(frontendOptions *options.App, debugMode bool) *Window { width := C.int(frontendOptions.Width) height := C.int(frontendOptions.Height) + windowStartState := C.int(int(frontendOptions.WindowStartState)) title = c.String(frontendOptions.Title) @@ -75,7 +77,7 @@ func NewWindow(frontendOptions *options.App, debugMode bool) *Window { } var context *C.WailsContext = C.Create(title, width, height, frameless, resizable, fullscreen, fullSizeContent, hideTitleBar, titlebarAppearsTransparent, hideTitle, useToolbar, hideToolbarSeparator, webviewIsTransparent, - alwaysOnTop, hideWindowOnClose, appearance, windowIsTranslucent, debug) + alwaysOnTop, hideWindowOnClose, appearance, windowIsTranslucent, debug, windowStartState, startsHidden) // Create menu result := &Window{