From cabb4957faf4e620c21cb5bbf6306a5f4a4a54d6 Mon Sep 17 00:00:00 2001 From: Yee Cheng Chin Date: Fri, 4 Nov 2022 18:47:18 -0700 Subject: [PATCH] Allow never opening window + terminate on last window, with guards Previously we disabled this combo in f6ba7dd40be20 because when implemented naively, it causes an issue where just opening an About MacVim or Settings window could immediately cause MacVim to exit (because macOS determined that there were no non-auxillary window open). This was awkward and potentially made it hard to change the setting back, and exact behavior depended on OS behavior. However, it seems like there are legit use case for this combo of settings. Change it so that we allow setting both of them again, but add checks so that `applicationShouldTerminateAfterLastWindowClosed:` will only return `YES` if we have opened at least one Vim window before. This gives the user a chance to open a window first, so using Settings etc wouldn't immediately terminate the app. Fix #1338 --- src/MacVim/MMAppController.h | 2 ++ src/MacVim/MMAppController.m | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/MacVim/MMAppController.h b/src/MacVim/MMAppController.h index d3e2fae121..a8acd9c219 100644 --- a/src/MacVim/MMAppController.h +++ b/src/MacVim/MMAppController.h @@ -49,6 +49,8 @@ int numChildProcesses; NSMutableDictionary *inputQueues; int processingFlag; + + BOOL hasShownWindowBefore; #if !DISABLE_SPARKLE #if USE_SPARKLE_1 diff --git a/src/MacVim/MMAppController.m b/src/MacVim/MMAppController.m index eab4df0202..8e8af1c87e 100644 --- a/src/MacVim/MMAppController.m +++ b/src/MacVim/MMAppController.m @@ -548,6 +548,15 @@ fsEventCallback(ConstFSEventStreamRef streamRef, - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender { + if (!hasShownWindowBefore) { + // If we have not opened a window before, never return YES. This can + // happen when MacVim is not configured to open window at launch. We + // want to give the user a chance to open a window first. Otherwise + // just opening the About MacVim or Settings windows could immediately + // terminate the app (since those are not proper app windows), + // depending if the OS feels like invoking this method. + return NO; + } return (MMTerminateWhenLastWindowClosed == [[NSUserDefaults standardUserDefaults] integerForKey:MMLastWindowClosedBehaviorKey]); @@ -888,6 +897,8 @@ fsEventCallback(ConstFSEventStreamRef streamRef, [NSApp activateIgnoringOtherApps:YES]; shouldActivateWhenNextWindowOpens = NO; } + + hasShownWindowBefore = YES; } - (void)setMainMenu:(NSMenu *)mainMenu