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 
This commit is contained in:
Leonard Hecker 2024-05-08 22:52:52 +02:00 committed by GitHub
parent 0b76c51ba1
commit dbac3a1fa3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 19 additions and 7 deletions

View File

@ -263,7 +263,7 @@ namespace winrt::TerminalApp::implementation
AppLogic::Current()->NotifyRootInitialized();
}
void TerminalWindow::Quit()
void TerminalWindow::PersistState()
{
if (_root)
{

View File

@ -73,7 +73,7 @@ namespace winrt::TerminalApp::implementation
void Create();
void Quit();
void PersistState();
winrt::fire_and_forget UpdateSettings(winrt::TerminalApp::SettingsLoadEventArgs args);

View File

@ -61,7 +61,7 @@ namespace TerminalApp
Boolean ShouldImmediatelyHandoffToElevated();
void HandoffToElevated();
void Quit();
void PersistState();
Windows.UI.Xaml.UIElement GetRoot();

View File

@ -1184,13 +1184,16 @@ winrt::fire_and_forget AppHost::_QuitRequested(const winrt::Windows::Foundation:
co_await wil::resume_foreground(_windowLogic.GetRoot().Dispatcher());
const auto strongThis = weakThis.lock();
// GH #16235: If we don't have a window logic, we're already refrigerating, and won't have our _window either.
if (!strongThis || _windowLogic == nullptr)
if (!strongThis)
{
co_return;
}
_windowLogic.Quit();
if (_appLogic && _windowLogic && _appLogic.ShouldUsePersistedLayout())
{
_windowLogic.PersistState();
}
PostQuitMessage(0);
}

View File

@ -348,6 +348,14 @@ void WindowEmperor::_becomeMonarch()
_revokers.WindowCreated = _manager.WindowCreated(winrt::auto_revoke, { this, &WindowEmperor::_numberOfWindowsChanged });
_revokers.WindowClosed = _manager.WindowClosed(winrt::auto_revoke, { this, &WindowEmperor::_numberOfWindowsChanged });
// If a previous session of Windows Terminal stored buffer_*.txt files, then we need to clean all those up on exit
// that aren't needed anymore, even if the user disabled the ShouldUsePersistedLayout() setting in the meantime.
{
const auto state = ApplicationState::SharedInstance();
const auto layouts = state.PersistedWindowLayouts();
_requiresPersistenceCleanupOnExit = layouts && layouts.Size() > 0;
}
}
// sender and args are always nullptr
@ -486,7 +494,7 @@ void WindowEmperor::_finalizeSessionPersistence() const
// Ensure to write the state.json before we TerminateProcess()
state.Flush();
if (!_app.Logic().ShouldUsePersistedLayout())
if (!_requiresPersistenceCleanupOnExit)
{
return;
}

View File

@ -51,6 +51,7 @@ private:
std::unique_ptr<NotificationIcon> _notificationIcon;
bool _requiresPersistenceCleanupOnExit = false;
bool _quitting{ false };
void _windowStartedHandlerPostXAML(const std::shared_ptr<WindowThread>& sender);