Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for setting tabColor on the command line #8102

Merged
17 commits merged into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/cascadia/profiles.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@
"index": {
"type": "integer",
"description": "The index of the profile in the new tab dropdown (starting at 0)"
},
"tabColor": {
"$ref": "#/definitions/Color",
"default": null,
"description": "If provided, will set the tab's color to the given value"
}
},
"type": "object"
Expand Down
98 changes: 73 additions & 25 deletions src/cascadia/LocalTests_TerminalApp/CommandlineTest.cpp

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/cascadia/TerminalApp/AppCommandlineArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "pch.h"
#include "AppLogic.h"
#include "AppCommandlineArgs.h"
#include "../types/inc/utils.hpp"
#include <LibraryResources.h>

using namespace winrt::TerminalApp;
Expand Down Expand Up @@ -367,6 +368,10 @@ void AppCommandlineArgs::_addNewTerminalArgs(AppCommandlineArgs::NewTerminalSubc
_startingTitle,
RS_A(L"CmdTitleArgDesc"));

subcommand.tabColorOption = subcommand.subcommand->add_option("--tabColor",
_startingTabColor,
RS_A(L"CmdTabColorArgDesc"));

// Using positionals_at_end allows us to support "wt new-tab -d wsl -d Ubuntu"
// without CLI11 thinking that we've specified -d twice.
// There's an alternate construction where we make all subcommands "prefix commands",
Expand Down Expand Up @@ -428,6 +433,12 @@ NewTerminalArgs AppCommandlineArgs::_getNewTerminalArgs(AppCommandlineArgs::NewT
args.TabTitle(winrt::to_hstring(_startingTitle));
}

if (*subcommand.tabColorOption)
{
til::color tabColor = ::Microsoft::Console::Utils::ColorFromHexString(_startingTabColor);
args.TabColor(static_cast<uint32_t>(tabColor));
}

return args;
}

Expand Down Expand Up @@ -462,6 +473,7 @@ void AppCommandlineArgs::_resetStateToDefault()
_profileName.clear();
_startingDirectory.clear();
_startingTitle.clear();
_startingTabColor.clear();
_commandline.clear();

_splitVertical = false;
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/AppCommandlineArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class TerminalApp::AppCommandlineArgs final
CLI::Option* profileNameOption;
CLI::Option* startingDirectoryOption;
CLI::Option* titleOption;
CLI::Option* tabColorOption;
};

struct NewPaneSubcommand : public NewTerminalSubcommand
Expand All @@ -74,6 +75,7 @@ class TerminalApp::AppCommandlineArgs final
std::string _profileName;
std::string _startingDirectory;
std::string _startingTitle;
std::string _startingTabColor;

// _commandline will contain the command line with which we'll be spawning a new terminal
std::vector<std::string> _commandline;
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalApp/Resources/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@
<value>Open the terminal with the provided title instead of the profile's set "title"</value>
<comment>{Locked="\"title\""}</comment>
</data>
<data name="CmdTabColorArgDesc" xml:space="preserve">
<value>Open the terminal with the tab color</value>
Don-Vito marked this conversation as resolved.
Show resolved Hide resolved
</data>
<data name="CmdVersionDesc" xml:space="preserve">
<value>Display the application version</value>
</data>
Expand Down
12 changes: 10 additions & 2 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,14 @@ namespace winrt::TerminalApp::implementation
{
auto [profileGuid, settings] = TerminalSettings::BuildSettings(_settings, newTerminalArgs, *_bindings);

_CreateNewTabFromSettings(profileGuid, settings);
const auto newTab = _CreateNewTabFromSettings(profileGuid, settings);
if (newTerminalArgs && newTerminalArgs.TabColor())
{
// We set the tab color we got from terminal arguments
// as runtime color so it won't be reset upon settings reload
til::color tabColor{ newTerminalArgs.TabColor().Value() };
_GetStrongTabImpl(newTab)->SetRuntimeTabColor(tabColor);
}

const uint32_t tabCount = _tabs.Size();
const bool usedManualProfile = (newTerminalArgs != nullptr) &&
Expand Down Expand Up @@ -643,7 +650,7 @@ namespace winrt::TerminalApp::implementation
// currently displayed, it will be shown.
// Arguments:
// - settings: the TerminalSettings object to use to create the TerminalControl with.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: update the comments up here for this function to say that you're returning a tab now

void TerminalPage::_CreateNewTabFromSettings(GUID profileGuid, TerminalApp::TerminalSettings settings)
TerminalApp::Tab TerminalPage::_CreateNewTabFromSettings(GUID profileGuid, TerminalApp::TerminalSettings settings)
{
// Initialize the new tab

Expand Down Expand Up @@ -740,6 +747,7 @@ namespace winrt::TerminalApp::implementation
// This kicks off TabView::SelectionChanged, in response to which
// we'll attach the terminal's Xaml control to the Xaml root.
_tabView.SelectedItem(tabViewItem);
return *newTabImpl;
}

// Method Description:
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/TerminalPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ namespace winrt::TerminalApp::implementation
void _CreateNewTabFlyout();
void _OpenNewTabDropdown();
void _OpenNewTab(const Microsoft::Terminal::Settings::Model::NewTerminalArgs& newTerminalArgs);
void _CreateNewTabFromSettings(GUID profileGuid, TerminalApp::TerminalSettings settings);
TerminalApp::Tab _CreateNewTabFromSettings(GUID profileGuid, TerminalApp::TerminalSettings settings);
winrt::Microsoft::Terminal::TerminalConnection::ITerminalConnection _CreateConnectionFromSettings(GUID profileGuid, TerminalApp::TerminalSettings settings);

bool _displayingCloseDialog{ false };
Expand Down
6 changes: 6 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
ss << fmt::format(L"title: {}, ", _TabTitle);
}

if (_TabColor)
{
ss << fmt::format(L"tabColor: {}, ", _TabColor.Value());
}

auto s = ss.str();
if (s.empty())
{
Expand Down
10 changes: 10 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
GETSET_PROPERTY(winrt::hstring, Commandline, L"");
GETSET_PROPERTY(winrt::hstring, StartingDirectory, L"");
GETSET_PROPERTY(winrt::hstring, TabTitle, L"");
GETSET_PROPERTY(Windows::Foundation::IReference<uint32_t>, TabColor, nullptr);
GETSET_PROPERTY(Windows::Foundation::IReference<int32_t>, ProfileIndex, nullptr);
GETSET_PROPERTY(winrt::hstring, Profile, L"");

static constexpr std::string_view CommandlineKey{ "commandline" };
static constexpr std::string_view StartingDirectoryKey{ "startingDirectory" };
static constexpr std::string_view TabTitleKey{ "tabTitle" };
static constexpr std::string_view TabColorKey{ "tabColor" };
static constexpr std::string_view ProfileIndexKey{ "index" };
static constexpr std::string_view ProfileKey{ "profile" };

Expand All @@ -77,6 +79,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return other.Commandline() == _Commandline &&
other.StartingDirectory() == _StartingDirectory &&
other.TabTitle() == _TabTitle &&
other.TabColor() == _TabColor &&
other.ProfileIndex() == _ProfileIndex &&
other.Profile() == _Profile;
};
Expand All @@ -89,6 +92,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
JsonUtils::GetValueForKey(json, TabTitleKey, args->_TabTitle);
JsonUtils::GetValueForKey(json, ProfileIndexKey, args->_ProfileIndex);
JsonUtils::GetValueForKey(json, ProfileKey, args->_Profile);

if (const auto tabColor{ JsonUtils::GetValueForKey<std::optional<til::color>>(json, TabColorKey) })
Don-Vito marked this conversation as resolved.
Show resolved Hide resolved
{
args->_TabColor = static_cast<uint32_t>(*tabColor);
}

return *args;
}
Model::NewTerminalArgs Copy() const
Expand All @@ -97,6 +106,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
copy->_Commandline = _Commandline;
copy->_StartingDirectory = _StartingDirectory;
copy->_TabTitle = _TabTitle;
copy->_TabColor = _TabColor;
copy->_ProfileIndex = _ProfileIndex;
copy->_Profile = _Profile;
return *copy;
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.idl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ namespace Microsoft.Terminal.Settings.Model
String Commandline;
String StartingDirectory;
String TabTitle;
Windows.Foundation.IReference<UInt32> TabColor;
String Profile; // Either a GUID or a profile's name if the GUID isn't a match
// ProfileIndex can be null (for "use the default"), so this needs to be
// a IReference, so it's nullable
Expand Down