Backport session persistence fixes in #17211 and #17268 (#17269)

As the title says, this backports the changes in #17211 and #17268:
* `PersistState` being called when the window is closed
  (as opposed to closing the tab). The settings check was missing.
* Avoid persisting windows with 0 tabs (= last tab gets closed).
This commit is contained in:
Leonard Hecker 2024-05-16 02:10:53 +02:00 committed by GitHub
parent c6c078834c
commit 488bef7bca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 16 additions and 10 deletions

View File

@ -1898,7 +1898,10 @@ namespace winrt::TerminalApp::implementation
void TerminalPage::PersistState()
{
if (_startupState != StartupState::Initialized)
// This method may be called for a window even if it hasn't had a tab yet or lost all of them.
// We shouldn't persist such windows.
const auto tabCount = _tabs.Size();
if (_startupState != StartupState::Initialized || tabCount == 0)
{
return;
}
@ -1914,7 +1917,7 @@ namespace winrt::TerminalApp::implementation
// if the focused tab was not the last tab, restore that
auto idx = _GetFocusedTabIndex();
if (idx && idx != _tabs.Size() - 1)
if (idx && idx != tabCount - 1)
{
ActionAndArgs action;
action.Action(ShortcutAction::SwitchToTab);

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

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

View File

@ -186,7 +186,7 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
const HANDLE hRef,
const HANDLE hServerProcess,
const HANDLE hClientProcess,
TERMINAL_STARTUP_INFO startupInfo) :
const TERMINAL_STARTUP_INFO& startupInfo) :
_rows{ 25 },
_cols{ 80 },
_guid{ Utils::CreateGuid() },

View File

@ -19,7 +19,7 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
const HANDLE hRef,
const HANDLE hServerProcess,
const HANDLE hClientProcess,
TERMINAL_STARTUP_INFO startupInfo);
const TERMINAL_STARTUP_INFO& startupInfo);
ConptyConnection() noexcept = default;
void Initialize(const Windows::Foundation::Collections::ValueSet& settings);

View File

@ -1190,13 +1190,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);
}