Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs introduced in #18623 #18635

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 23 additions & 26 deletions src/cascadia/WindowsTerminal/WindowEmperor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ void WindowEmperor::CreateNewWindow(winrt::TerminalApp::WindowRequestedArgs args

auto host = std::make_shared<AppHost>(this, _app.Logic(), std::move(args));
host->Initialize();

_windowCount += 1;
_windows.emplace_back(std::move(host));
}

Expand Down Expand Up @@ -354,10 +356,7 @@ void WindowEmperor::HandleCommandlineArgs(int nCmdShow)
}

// If we created no windows, e.g. because the args are "/?" we can just exit now.
if (_windows.empty())
{
_postQuitMessageIfNeeded();
}
_postQuitMessageIfNeeded();
}

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

// Counterpart to _postQuitMessageIfNeeded:
// If it returns true, don't close that last window, if any.
// This ensures we persist the last window.
bool WindowEmperor::_shouldSkipClosingWindows() const
{
const auto globalSettings = _app.Logic().Settings().GlobalSettings();
const size_t windowLimit = globalSettings.ShouldUsePersistedLayout() ? 1 : 0;
return _windows.size() <= windowLimit;
}

// Posts a WM_QUIT as soon as we have no reason to exist anymore.
// That basically means no windows [^1] and no message boxes.
//
// [^1] Unless:
// * We've been asked to persist the last remaining window
// in which case we exit with 1 remaining window.
// * We're allowed to be headless
// in which case we never exit.
// That basically means no windows and no message boxes.
void WindowEmperor::_postQuitMessageIfNeeded() const
{
const auto globalSettings = _app.Logic().Settings().GlobalSettings();
const size_t windowLimit = globalSettings.ShouldUsePersistedLayout() ? 1 : 0;

if (_messageBoxCount <= 0 && _windows.size() <= windowLimit && !globalSettings.AllowHeadless())
if (
_messageBoxCount <= 0 &&
_windowCount <= 0 &&
!_app.Logic().Settings().GlobalSettings().AllowHeadless())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

between the lines... does this mean that wt --help when headless mode is enabled will leave one running?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I think that's an edge case worth solving later (or not at all?).

{
PostQuitMessage(0);
}
Expand Down Expand Up @@ -795,8 +778,20 @@ LRESULT WindowEmperor::_messageHandler(HWND window, UINT const message, WPARAM c
{
case WM_CLOSE_TERMINAL_WINDOW:
{
if (!_shouldSkipClosingWindows())
const auto globalSettings = _app.Logic().Settings().GlobalSettings();
// Keep the last window in the array so that we can persist it on exit.
// We check for AllowHeadless(), as that being true prevents us from ever quitting in the first place.
// (= If we avoided closing the last window you wouldn't be able to reach a headless state.)
const auto shouldKeepWindow =
_windows.size() == 1 &&
globalSettings.ShouldUsePersistedLayout() &&
!globalSettings.AllowHeadless();

if (!shouldKeepWindow)
{
// Did the window counter get out of sync? It shouldn't.
assert(_windowCount == gsl::narrow_cast<int32_t>(_windows.size()));

const auto host = reinterpret_cast<AppHost*>(lParam);
auto it = _windows.begin();
const auto end = _windows.end();
Expand All @@ -812,6 +807,8 @@ LRESULT WindowEmperor::_messageHandler(HWND window, UINT const message, WPARAM c
}
}

// Counterpart specific to CreateNewWindow().
_windowCount -= 1;
_postQuitMessageIfNeeded();
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/WindowsTerminal/WindowEmperor.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class WindowEmperor
bool _forcePersistence = false;
bool _needsPersistenceCleanup = false;
std::optional<bool> _currentSystemThemeIsDark;
int32_t _windowCount = 0;
int32_t _messageBoxCount = 0;

#ifdef NDEBUG
Expand Down