@@ -354,7 +354,10 @@ void WindowEmperor::HandleCommandlineArgs(int nCmdShow)
354
354
}
355
355
356
356
// If we created no windows, e.g. because the args are "/?" we can just exit now.
357
- _postQuitMessageIfNeeded ();
357
+ if (_windows.empty ())
358
+ {
359
+ _postQuitMessageIfNeeded ();
360
+ }
358
361
}
359
362
360
363
// ALWAYS change the _real_ CWD of the Terminal to system32,
@@ -735,9 +738,30 @@ void WindowEmperor::_createMessageWindow(const wchar_t* className)
735
738
StringCchCopy (_notificationIcon.szTip , ARRAYSIZE (_notificationIcon.szTip ), appNameLoc.c_str ());
736
739
}
737
740
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
+
751
+ // 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.
738
759
void WindowEmperor::_postQuitMessageIfNeeded () const
739
760
{
740
- if (_messageBoxCount <= 0 && _windows.empty () && !_app.Logic ().Settings ().GlobalSettings ().AllowHeadless ())
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 ())
741
765
{
742
766
PostQuitMessage (0 );
743
767
}
@@ -771,17 +795,20 @@ LRESULT WindowEmperor::_messageHandler(HWND window, UINT const message, WPARAM c
771
795
{
772
796
case WM_CLOSE_TERMINAL_WINDOW:
773
797
{
774
- const auto host = reinterpret_cast <AppHost*>(lParam);
775
- auto it = _windows.begin ();
776
- const auto end = _windows.end ();
777
-
778
- for (; it != end; ++it)
798
+ if (!_shouldSkipClosingWindows ())
779
799
{
780
- if (host == it->get ())
800
+ const auto host = reinterpret_cast <AppHost*>(lParam);
801
+ auto it = _windows.begin ();
802
+ const auto end = _windows.end ();
803
+
804
+ for (; it != end; ++it)
781
805
{
782
- host->Close ();
783
- _windows.erase (it);
784
- break ;
806
+ if (host == it->get ())
807
+ {
808
+ host->Close ();
809
+ _windows.erase (it);
810
+ break ;
811
+ }
785
812
}
786
813
}
787
814
0 commit comments