Skip to content

Commit 96d1407

Browse files
authored
Fix bugs introduced in #18623 (#18635)
The logic didn't work when persistence was enabled and you had 2 windows and closed the 2nd one, or when dragging the last tab out of the only window. ## Validation Steps Performed * 2 windows, close the 2nd one, app doesn't exit ✅ * 1 window, 1 tab, drag the tab out of the window, app doesn't exit ✅
1 parent e1be2f4 commit 96d1407

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

src/cascadia/WindowsTerminal/WindowEmperor.cpp

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ void WindowEmperor::CreateNewWindow(winrt::TerminalApp::WindowRequestedArgs args
246246

247247
auto host = std::make_shared<AppHost>(this, _app.Logic(), std::move(args));
248248
host->Initialize();
249+
250+
_windowCount += 1;
249251
_windows.emplace_back(std::move(host));
250252
}
251253

@@ -354,10 +356,7 @@ void WindowEmperor::HandleCommandlineArgs(int nCmdShow)
354356
}
355357

356358
// 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();
361360
}
362361

363362
// ALWAYS change the _real_ CWD of the Terminal to system32,
@@ -738,30 +737,14 @@ void WindowEmperor::_createMessageWindow(const wchar_t* className)
738737
StringCchCopy(_notificationIcon.szTip, ARRAYSIZE(_notificationIcon.szTip), appNameLoc.c_str());
739738
}
740739

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-
751740
// 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.
759742
void WindowEmperor::_postQuitMessageIfNeeded() const
760743
{
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())
765748
{
766749
PostQuitMessage(0);
767750
}
@@ -795,8 +778,20 @@ LRESULT WindowEmperor::_messageHandler(HWND window, UINT const message, WPARAM c
795778
{
796779
case WM_CLOSE_TERMINAL_WINDOW:
797780
{
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)
799791
{
792+
// Did the window counter get out of sync? It shouldn't.
793+
assert(_windowCount == gsl::narrow_cast<int32_t>(_windows.size()));
794+
800795
const auto host = reinterpret_cast<AppHost*>(lParam);
801796
auto it = _windows.begin();
802797
const auto end = _windows.end();
@@ -812,6 +807,8 @@ LRESULT WindowEmperor::_messageHandler(HWND window, UINT const message, WPARAM c
812807
}
813808
}
814809

810+
// Counterpart specific to CreateNewWindow().
811+
_windowCount -= 1;
815812
_postQuitMessageIfNeeded();
816813
return 0;
817814
}

src/cascadia/WindowsTerminal/WindowEmperor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class WindowEmperor
7878
bool _forcePersistence = false;
7979
bool _needsPersistenceCleanup = false;
8080
std::optional<bool> _currentSystemThemeIsDark;
81+
int32_t _windowCount = 0;
8182
int32_t _messageBoxCount = 0;
8283

8384
#ifdef NDEBUG

0 commit comments

Comments
 (0)