Skip to content

Commit 6a9254e

Browse files
committed
PRE-MERGE #14944 Add support for running the Terminal without _any_ windows
2 parents dbdc672 + 4182742 commit 6a9254e

File tree

11 files changed

+35
-23
lines changed

11 files changed

+35
-23
lines changed

src/cascadia/TerminalApp/AppLogic.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,16 @@ namespace winrt::TerminalApp::implementation
674674
return _settings.GlobalSettings().IsolatedMode();
675675
}
676676

677+
bool AppLogic::AllowHeadless()
678+
{
679+
if (!_loadedInitialSettings)
680+
{
681+
// Load settings if we haven't already
682+
ReloadSettings();
683+
}
684+
return _settings.GlobalSettings().AllowHeadless();
685+
}
686+
677687
TerminalApp::TerminalWindow AppLogic::CreateNewWindow()
678688
{
679689
if (_settings == nullptr)

src/cascadia/TerminalApp/AppLogic.h

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ namespace winrt::TerminalApp::implementation
6666

6767
Microsoft::Terminal::Settings::Model::Theme Theme();
6868
bool IsolatedMode();
69+
bool AllowHeadless();
6970

7071
TerminalApp::TerminalWindow CreateNewWindow();
7172

src/cascadia/TerminalApp/AppLogic.idl

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ namespace TerminalApp
4545

4646
Microsoft.Terminal.Settings.Model.Theme Theme { get; };
4747
Boolean IsolatedMode { get; };
48+
Boolean AllowHeadless { get; };
4849

4950
FindTargetWindowResult FindTargetWindow(String[] args);
5051

src/cascadia/TerminalSettingsModel/ActionMap.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -898,8 +898,6 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
898898
// WHILE also making sure that upon re-saving the commands, we don't
899899
// actually serialize the results of the expansion. I don't think it is.
900900

901-
auto warnings{ winrt::single_threaded_vector<SettingsLoadWarnings>() };
902-
903901
std::vector<Model::ColorScheme> sortedSchemes;
904902
sortedSchemes.reserve(schemes.Size());
905903

@@ -921,8 +919,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
921919

922920
Command::ExpandCommands(copyOfCommands,
923921
profiles,
924-
winrt::param::vector_view<Model::ColorScheme>{ sortedSchemes },
925-
warnings);
922+
winrt::param::vector_view<Model::ColorScheme>{ sortedSchemes });
926923

927924
_ExpandedMapCache = copyOfCommands;
928925
}

src/cascadia/TerminalSettingsModel/Command.cpp

+9-12
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
480480
// - <none>
481481
void Command::ExpandCommands(IMap<winrt::hstring, Model::Command> commands,
482482
IVectorView<Model::Profile> profiles,
483-
IVectorView<Model::ColorScheme> schemes,
484-
IVector<SettingsLoadWarnings> warnings)
483+
IVectorView<Model::ColorScheme> schemes)
485484
{
486485
std::vector<winrt::hstring> commandsToRemove;
487486
std::vector<Model::Command> commandsToAdd;
@@ -491,7 +490,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
491490
{
492491
auto cmd{ get_self<implementation::Command>(nameAndCmd.Value()) };
493492

494-
auto newCommands = _expandCommand(cmd, profiles, schemes, warnings);
493+
auto newCommands = _expandCommand(cmd, profiles, schemes);
495494
if (newCommands.size() > 0)
496495
{
497496
commandsToRemove.push_back(nameAndCmd.Key());
@@ -529,21 +528,18 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
529528
// Arguments:
530529
// - expandable: the Command to potentially turn into more commands
531530
// - profiles: A list of all the profiles that this command should be expanded on.
532-
// - warnings: If there were any warnings during parsing, they'll be
533-
// appended to this vector.
534531
// Return Value:
535532
// - and empty vector if the command wasn't expandable, otherwise a list of
536533
// the newly-created commands.
537534
std::vector<Model::Command> Command::_expandCommand(Command* const expandable,
538535
IVectorView<Model::Profile> profiles,
539-
IVectorView<Model::ColorScheme> schemes,
540-
IVector<SettingsLoadWarnings>& warnings)
536+
IVectorView<Model::ColorScheme> schemes)
541537
{
542538
std::vector<Model::Command> newCommands;
543539

544540
if (expandable->HasNestedCommands())
545541
{
546-
ExpandCommands(expandable->_subcommands, profiles, schemes, warnings);
542+
ExpandCommands(expandable->_subcommands, profiles, schemes);
547543
}
548544

549545
if (expandable->_IterateOn == ExpandCommandType::None)
@@ -564,18 +560,19 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
564560
const auto actualDataEnd = newJsonString.data() + newJsonString.size();
565561
if (!reader->parse(actualDataStart, actualDataEnd, &newJsonValue, &errs))
566562
{
567-
warnings.Append(SettingsLoadWarnings::FailedToParseCommandJson);
568563
// If we encounter a re-parsing error, just stop processing the rest of the commands.
569564
return false;
570565
}
571566

572567
// Pass the new json back though FromJson, to get the new expanded value.
573-
std::vector<SettingsLoadWarnings> newWarnings;
574-
if (auto newCmd{ Command::FromJson(newJsonValue, newWarnings) })
568+
// FromJson requires that we pass in a vector to hang on to the
569+
// warnings, but ultimately, we don't care about warnings during
570+
// expansion.
571+
std::vector<SettingsLoadWarnings> unused;
572+
if (auto newCmd{ Command::FromJson(newJsonValue, unused) })
575573
{
576574
newCommands.push_back(*newCmd);
577575
}
578-
std::for_each(newWarnings.begin(), newWarnings.end(), [warnings](auto& warn) { warnings.Append(warn); });
579576
return true;
580577
};
581578

src/cascadia/TerminalSettingsModel/Command.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
4343

4444
static void ExpandCommands(Windows::Foundation::Collections::IMap<winrt::hstring, Model::Command> commands,
4545
Windows::Foundation::Collections::IVectorView<Model::Profile> profiles,
46-
Windows::Foundation::Collections::IVectorView<Model::ColorScheme> schemes,
47-
Windows::Foundation::Collections::IVector<SettingsLoadWarnings> warnings);
46+
Windows::Foundation::Collections::IVectorView<Model::ColorScheme> schemes);
4847

4948
static std::vector<SettingsLoadWarnings> LayerJson(Windows::Foundation::Collections::IMap<winrt::hstring, Model::Command>& commands,
5049
const Json::Value& json);
@@ -80,8 +79,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
8079

8180
static std::vector<Model::Command> _expandCommand(Command* const expandable,
8281
Windows::Foundation::Collections::IVectorView<Model::Profile> profiles,
83-
Windows::Foundation::Collections::IVectorView<Model::ColorScheme> schemes,
84-
Windows::Foundation::Collections::IVector<SettingsLoadWarnings>& warnings);
82+
Windows::Foundation::Collections::IVectorView<Model::ColorScheme> schemes);
8583
friend class SettingsModelLocalTests::DeserializationTests;
8684
friend class SettingsModelLocalTests::CommandTests;
8785
};

src/cascadia/TerminalSettingsModel/Command.idl

+1-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ namespace Microsoft.Terminal.Settings.Model
4848

4949
static void ExpandCommands(Windows.Foundation.Collections.IMap<String, Command> commands,
5050
Windows.Foundation.Collections.IVectorView<Profile> profiles,
51-
Windows.Foundation.Collections.IVectorView<ColorScheme> schemes,
52-
Windows.Foundation.Collections.IVector<SettingsLoadWarnings> warnings);
51+
Windows.Foundation.Collections.IVectorView<ColorScheme> schemes);
5352
}
5453
}

src/cascadia/TerminalSettingsModel/GlobalAppSettings.idl

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ namespace Microsoft.Terminal.Settings.Model
9898
INHERITABLE_SETTING(IVector<NewTabMenuEntry>, NewTabMenu);
9999
INHERITABLE_SETTING(Boolean, EnableColorSelection);
100100
INHERITABLE_SETTING(Boolean, IsolatedMode);
101+
INHERITABLE_SETTING(Boolean, AllowHeadless);
101102

102103
Windows.Foundation.Collections.IMapView<String, ColorScheme> ColorSchemes();
103104
void AddColorScheme(ColorScheme scheme);

src/cascadia/TerminalSettingsModel/MTSMSettings.h

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Author(s):
6363
X(bool, TrimPaste, "trimPaste", true) \
6464
X(bool, EnableColorSelection, "experimental.enableColorSelection", false) \
6565
X(winrt::Windows::Foundation::Collections::IVector<Model::NewTabMenuEntry>, NewTabMenu, "newTabMenu", winrt::single_threaded_vector<Model::NewTabMenuEntry>({ Model::RemainingProfilesEntry{} })) \
66+
X(bool, AllowHeadless, "compatibility.allowHeadless", true) \
6667
X(bool, IsolatedMode, "compatibility.isolatedMode", false)
6768

6869
#define MTSM_PROFILE_SETTINGS(X) \

src/cascadia/WindowsTerminal/WindowEmperor.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,11 @@ void WindowEmperor::CreateNewWindowThread(Remoting::WindowRequestedArgs args, co
151151
}),
152152
_windows.end());
153153

154-
if (_windows.size() == 0)
154+
// When we run out of windows, exit our process if and only if:
155+
// * We're not allowed to run headless OR
156+
// * we've explicitly been told to "quit", which should fully exit the Terminal.
157+
if (_windows.size() == 0 &&
158+
(_quitting || !_app.Logic().AllowHeadless()))
155159
{
156160
_close();
157161
}
@@ -232,6 +236,8 @@ void WindowEmperor::_numberOfWindowsChanged(const winrt::Windows::Foundation::II
232236
void WindowEmperor::_quitAllRequested(const winrt::Windows::Foundation::IInspectable&,
233237
const winrt::Microsoft::Terminal::Remoting::QuitAllRequestedArgs& args)
234238
{
239+
_quitting = true;
240+
235241
// Make sure that the current timer is destroyed so that it doesn't attempt
236242
// to run while we are in the middle of quitting.
237243
if (_getWindowLayoutThrottler.has_value())

src/cascadia/WindowsTerminal/WindowEmperor.h

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class WindowEmperor
4949
std::vector<winrt::Microsoft::Terminal::Settings::Model::GlobalSummonArgs> _hotkeys;
5050

5151
std::unique_ptr<NotificationIcon> _notificationIcon;
52+
bool _quitting{ false };
5253

5354
void _becomeMonarch();
5455
void _numberOfWindowsChanged(const winrt::Windows::Foundation::IInspectable&, const winrt::Windows::Foundation::IInspectable&);

0 commit comments

Comments
 (0)