Skip to content

Commit

Permalink
PRE-MERGE #14944 Add support for running the Terminal without _any_ w…
Browse files Browse the repository at this point in the history
…indows
  • Loading branch information
zadjii-msft committed Mar 24, 2023
2 parents 601fec9 + 9942d55 commit 3f50e69
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/cascadia/TerminalApp/AppLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,16 @@ namespace winrt::TerminalApp::implementation
globals.MinimizeToNotificationArea();
}

bool AppLogic::AllowHeadless()
{
if (!_loadedInitialSettings)
{
// Load settings if we haven't already
ReloadSettings();
}
return _settings.GlobalSettings().AllowHeadless();
}

TerminalApp::TerminalWindow AppLogic::CreateNewWindow()
{
if (_settings == nullptr)
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/AppLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace winrt::TerminalApp::implementation

Microsoft::Terminal::Settings::Model::Theme Theme();
bool IsolatedMode();
bool AllowHeadless();
bool RequestsTrayIcon();

TerminalApp::TerminalWindow CreateNewWindow();
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/AppLogic.idl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace TerminalApp
// Selected settings to expose
Microsoft.Terminal.Settings.Model.Theme Theme { get; };
Boolean IsolatedMode { get; };
Boolean AllowHeadless { get; };
Boolean RequestsTrayIcon { get; };

FindTargetWindowResult FindTargetWindow(String[] args);
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/GlobalAppSettings.idl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ namespace Microsoft.Terminal.Settings.Model
INHERITABLE_SETTING(IVector<NewTabMenuEntry>, NewTabMenu);
INHERITABLE_SETTING(Boolean, EnableColorSelection);
INHERITABLE_SETTING(Boolean, IsolatedMode);
INHERITABLE_SETTING(Boolean, AllowHeadless);

Windows.Foundation.Collections.IMapView<String, ColorScheme> ColorSchemes();
void AddColorScheme(ColorScheme scheme);
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/MTSMSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Author(s):
X(bool, TrimPaste, "trimPaste", true) \
X(bool, EnableColorSelection, "experimental.enableColorSelection", false) \
X(winrt::Windows::Foundation::Collections::IVector<Model::NewTabMenuEntry>, NewTabMenu, "newTabMenu", winrt::single_threaded_vector<Model::NewTabMenuEntry>({ Model::RemainingProfilesEntry{} })) \
X(bool, AllowHeadless, "compatibility.allowHeadless", false) \
X(bool, IsolatedMode, "compatibility.isolatedMode", false)

#define MTSM_PROFILE_SETTINGS(X) \
Expand Down
15 changes: 12 additions & 3 deletions src/cascadia/WindowsTerminal/WindowEmperor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ void WindowEmperor::_createNewWindowThread(const Remoting::WindowRequestedArgs&
_windowStartedHandlerPreXAML();

std::thread t([weakThis, window]() {
try {
try
{
const auto cleanup = wil::scope_exit([&]() {
if (auto self{ weakThis.lock() })
{
Expand All @@ -133,7 +134,8 @@ void WindowEmperor::_createNewWindowThread(const Remoting::WindowRequestedArgs&
}

window->RunMessagePump();
} CATCH_LOG()
}
CATCH_LOG()
});
LOG_IF_FAILED(SetThreadDescription(t.native_handle(), L"Window Thread"));

Expand Down Expand Up @@ -192,7 +194,12 @@ void WindowEmperor::_windowExitedHandler(uint64_t senderID)
return w->Peasant().GetID() == senderID;
});

if (_windowThreadInstances.fetch_sub(1, std::memory_order_relaxed) == 1)
// When we run out of windows, exit our process if and only if:
// * We're not allowed to run headless OR
// * we've explicitly been told to "quit", which should fully exit the Terminal.
const bool quitWhenLastWindowExits{ !_app.Logic().AllowHeadless() };
if ((_windowThreadInstances.fetch_sub(1, std::memory_order_relaxed) == 1) && // no more windows
(_quitting || quitWhenLastWindowExits))
{
_close();
}
Expand Down Expand Up @@ -287,6 +294,8 @@ void WindowEmperor::_numberOfWindowsChanged(const winrt::Windows::Foundation::II
void WindowEmperor::_quitAllRequested(const winrt::Windows::Foundation::IInspectable&,
const winrt::Microsoft::Terminal::Remoting::QuitAllRequestedArgs& args)
{
_quitting = true;

// Make sure that the current timer is destroyed so that it doesn't attempt
// to run while we are in the middle of quitting.
if (_getWindowLayoutThrottler.has_value())
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/WindowsTerminal/WindowEmperor.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ class WindowEmperor : public std::enable_shared_from_this<WindowEmperor>

std::unique_ptr<NotificationIcon> _notificationIcon;

bool _quitting{ false };

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

void _windowExitedHandler(uint64_t senderID);

void _becomeMonarch();
Expand Down

0 comments on commit 3f50e69

Please sign in to comment.