Skip to content

Commit 4d1b543

Browse files
lheckerDHowett
authored andcommitted
Fix session being persisted even when disabled (#17211)
This fixes 2 bugs: * `PersistState` being called when the window is closed (as opposed to closing the tab). The settings check was missing. * Session cleanup running depending on whether the feature is currently enabled as opposed to whether it was enabled on launch. Closes #17206 Closes #17207 ## Validation Steps Performed * Create a bunch of leftover buffer_*.txt files by running the current Dev version off of main * Build this branch, then open and close a window * All buffer_*.txt are gone and state.json is cleaned up ✅ (cherry picked from commit dbac3a1) Service-Card-Id: 92515454 Service-Version: 1.21
1 parent d838ce5 commit 4d1b543

File tree

6 files changed

+19
-7
lines changed

6 files changed

+19
-7
lines changed

src/cascadia/TerminalApp/TerminalWindow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ namespace winrt::TerminalApp::implementation
263263
AppLogic::Current()->NotifyRootInitialized();
264264
}
265265

266-
void TerminalWindow::Quit()
266+
void TerminalWindow::PersistState()
267267
{
268268
if (_root)
269269
{

src/cascadia/TerminalApp/TerminalWindow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ namespace winrt::TerminalApp::implementation
7373

7474
void Create();
7575

76-
void Quit();
76+
void PersistState();
7777

7878
winrt::fire_and_forget UpdateSettings(winrt::TerminalApp::SettingsLoadEventArgs args);
7979

src/cascadia/TerminalApp/TerminalWindow.idl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ namespace TerminalApp
6161
Boolean ShouldImmediatelyHandoffToElevated();
6262
void HandoffToElevated();
6363

64-
void Quit();
64+
void PersistState();
6565

6666
Windows.UI.Xaml.UIElement GetRoot();
6767

src/cascadia/WindowsTerminal/AppHost.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,13 +1184,16 @@ winrt::fire_and_forget AppHost::_QuitRequested(const winrt::Windows::Foundation:
11841184
co_await wil::resume_foreground(_windowLogic.GetRoot().Dispatcher());
11851185

11861186
const auto strongThis = weakThis.lock();
1187-
// GH #16235: If we don't have a window logic, we're already refrigerating, and won't have our _window either.
1188-
if (!strongThis || _windowLogic == nullptr)
1187+
if (!strongThis)
11891188
{
11901189
co_return;
11911190
}
11921191

1193-
_windowLogic.Quit();
1192+
if (_appLogic && _windowLogic && _appLogic.ShouldUsePersistedLayout())
1193+
{
1194+
_windowLogic.PersistState();
1195+
}
1196+
11941197
PostQuitMessage(0);
11951198
}
11961199

src/cascadia/WindowsTerminal/WindowEmperor.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,14 @@ void WindowEmperor::_becomeMonarch()
348348

349349
_revokers.WindowCreated = _manager.WindowCreated(winrt::auto_revoke, { this, &WindowEmperor::_numberOfWindowsChanged });
350350
_revokers.WindowClosed = _manager.WindowClosed(winrt::auto_revoke, { this, &WindowEmperor::_numberOfWindowsChanged });
351+
352+
// If a previous session of Windows Terminal stored buffer_*.txt files, then we need to clean all those up on exit
353+
// that aren't needed anymore, even if the user disabled the ShouldUsePersistedLayout() setting in the meantime.
354+
{
355+
const auto state = ApplicationState::SharedInstance();
356+
const auto layouts = state.PersistedWindowLayouts();
357+
_requiresPersistenceCleanupOnExit = layouts && layouts.Size() > 0;
358+
}
351359
}
352360

353361
// sender and args are always nullptr
@@ -486,7 +494,7 @@ void WindowEmperor::_finalizeSessionPersistence() const
486494
// Ensure to write the state.json before we TerminateProcess()
487495
state.Flush();
488496

489-
if (!_app.Logic().ShouldUsePersistedLayout())
497+
if (!_requiresPersistenceCleanupOnExit)
490498
{
491499
return;
492500
}

src/cascadia/WindowsTerminal/WindowEmperor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class WindowEmperor : public std::enable_shared_from_this<WindowEmperor>
5151

5252
std::unique_ptr<NotificationIcon> _notificationIcon;
5353

54+
bool _requiresPersistenceCleanupOnExit = false;
5455
bool _quitting{ false };
5556

5657
void _windowStartedHandlerPostXAML(const std::shared_ptr<WindowThread>& sender);

0 commit comments

Comments
 (0)