@@ -246,6 +246,8 @@ void WindowEmperor::CreateNewWindow(winrt::TerminalApp::WindowRequestedArgs args
246
246
247
247
auto host = std::make_shared<AppHost>(this , _app.Logic (), std::move (args));
248
248
host->Initialize ();
249
+
250
+ _windowCount += 1 ;
249
251
_windows.emplace_back (std::move (host));
250
252
}
251
253
@@ -354,10 +356,7 @@ void WindowEmperor::HandleCommandlineArgs(int nCmdShow)
354
356
}
355
357
356
358
// If we created no windows, e.g. because the args are "/?" we can just exit now.
357
- if (_windows.empty ())
358
- {
359
- _postQuitMessageIfNeeded ();
360
- }
359
+ _postQuitMessageIfNeeded ();
361
360
}
362
361
363
362
// ALWAYS change the _real_ CWD of the Terminal to system32,
@@ -738,30 +737,14 @@ void WindowEmperor::_createMessageWindow(const wchar_t* className)
738
737
StringCchCopy (_notificationIcon.szTip , ARRAYSIZE (_notificationIcon.szTip ), appNameLoc.c_str ());
739
738
}
740
739
741
- // Counterpart to _postQuitMessageIfNeeded:
742
- // If it returns true, don't close that last window, if any.
743
- // This ensures we persist the last window.
744
- bool WindowEmperor::_shouldSkipClosingWindows () const
745
- {
746
- const auto globalSettings = _app.Logic ().Settings ().GlobalSettings ();
747
- const size_t windowLimit = globalSettings.ShouldUsePersistedLayout () ? 1 : 0 ;
748
- return _windows.size () <= windowLimit;
749
- }
750
-
751
740
// Posts a WM_QUIT as soon as we have no reason to exist anymore.
752
- // That basically means no windows [^1] and no message boxes.
753
- //
754
- // [^1] Unless:
755
- // * We've been asked to persist the last remaining window
756
- // in which case we exit with 1 remaining window.
757
- // * We're allowed to be headless
758
- // in which case we never exit.
741
+ // That basically means no windows and no message boxes.
759
742
void WindowEmperor::_postQuitMessageIfNeeded () const
760
743
{
761
- const auto globalSettings = _app. Logic (). Settings (). GlobalSettings ();
762
- const size_t windowLimit = globalSettings. ShouldUsePersistedLayout () ? 1 : 0 ;
763
-
764
- if (_messageBoxCount <= 0 && _windows. size () <= windowLimit && !globalSettings .AllowHeadless ())
744
+ if (
745
+ _messageBoxCount <= 0 &&
746
+ _windowCount <= 0 &&
747
+ !_app. Logic (). Settings (). GlobalSettings () .AllowHeadless ())
765
748
{
766
749
PostQuitMessage (0 );
767
750
}
@@ -795,8 +778,20 @@ LRESULT WindowEmperor::_messageHandler(HWND window, UINT const message, WPARAM c
795
778
{
796
779
case WM_CLOSE_TERMINAL_WINDOW:
797
780
{
798
- if (!_shouldSkipClosingWindows ())
781
+ const auto globalSettings = _app.Logic ().Settings ().GlobalSettings ();
782
+ // Keep the last window in the array so that we can persist it on exit.
783
+ // We check for AllowHeadless(), as that being true prevents us from ever quitting in the first place.
784
+ // (= If we avoided closing the last window you wouldn't be able to reach a headless state.)
785
+ const auto shouldKeepWindow =
786
+ _windows.size () == 1 &&
787
+ globalSettings.ShouldUsePersistedLayout () &&
788
+ !globalSettings.AllowHeadless ();
789
+
790
+ if (!shouldKeepWindow)
799
791
{
792
+ // Did the window counter get out of sync? It shouldn't.
793
+ assert (_windowCount == gsl::narrow_cast<int32_t >(_windows.size ()));
794
+
800
795
const auto host = reinterpret_cast <AppHost*>(lParam);
801
796
auto it = _windows.begin ();
802
797
const auto end = _windows.end ();
@@ -812,6 +807,8 @@ LRESULT WindowEmperor::_messageHandler(HWND window, UINT const message, WPARAM c
812
807
}
813
808
}
814
809
810
+ // Counterpart specific to CreateNewWindow().
811
+ _windowCount -= 1 ;
815
812
_postQuitMessageIfNeeded ();
816
813
return 0 ;
817
814
}
0 commit comments