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 10 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
2 changes: 1 addition & 1 deletion src/cascadia/LocalTests_SettingsModel/KeyBindingsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ namespace SettingsModelLocalTests
// Verify the args have the expected value
VERIFY_IS_NOT_NULL(realArgs.TabColor());
// Remember that COLORREFs are actually BBGGRR order, while the string is in #RRGGBB order
VERIFY_ARE_EQUAL(static_cast<uint32_t>(til::color(0x563412)), realArgs.TabColor().Value());
VERIFY_ARE_EQUAL(til::color(0x563412), til::color(realArgs.TabColor().Value()));
Don-Vito marked this conversation as resolved.
Show resolved Hide resolved
}
{
KeyChord kc{ true, false, false, static_cast<int32_t>('F') };
Expand Down
98 changes: 73 additions & 25 deletions src/cascadia/LocalTests_TerminalApp/CommandlineTest.cpp

Large diffs are not rendered by default.

11 changes: 4 additions & 7 deletions src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,23 +350,20 @@ namespace winrt::TerminalApp::implementation
void TerminalPage::_HandleSetTabColor(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
std::optional<til::color> tabColor;
Windows::Foundation::IReference<Windows::UI::Color> tabColor;

if (const auto& realArgs = args.ActionArgs().try_as<SetTabColorArgs>())
{
if (realArgs.TabColor() != nullptr)
{
tabColor = realArgs.TabColor().Value();
}
tabColor = realArgs.TabColor();
}

if (auto focusedTab = _GetFocusedTab())
{
if (auto activeTab = _GetTerminalTabImpl(focusedTab))
{
if (tabColor.has_value())
if (tabColor)
{
activeTab->SetRuntimeTabColor(tabColor.value());
activeTab->SetRuntimeTabColor(tabColor.Value());
}
else
{
Expand Down
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)
{
const auto tabColor = Microsoft::Console::Utils::ColorFromHexString(_startingTabColor);
args.TabColor(static_cast<winrt::Windows::UI::Color>(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 @@ -295,6 +295,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, in #rrggbb format</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
11 changes: 9 additions & 2 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,13 @@ 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
_GetTerminalTabImpl(newTab)->SetRuntimeTabColor(newTerminalArgs.TabColor().Value());
}

const uint32_t tabCount = _tabs.Size();
const bool usedManualProfile = (newTerminalArgs != nullptr) &&
Expand Down Expand Up @@ -645,7 +651,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::TerminalTab TerminalPage::_CreateNewTabFromSettings(GUID profileGuid, TerminalApp::TerminalSettings settings)
{
// Initialize the new tab

Expand Down Expand Up @@ -733,6 +739,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 @@ -135,7 +135,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::TerminalTab _CreateNewTabFromSettings(GUID profileGuid, TerminalApp::TerminalSettings settings);
winrt::Microsoft::Terminal::TerminalConnection::ITerminalConnection _CreateConnectionFromSettings(GUID profileGuid, TerminalApp::TerminalSettings settings);

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

if (_TabColor)
{
const til::color tabColor{ _TabColor.Value() };
ss << fmt::format(L"tabColor: {}, ", tabColor.ToHexString(true));
}

auto s = ss.str();
if (s.empty())
{
Expand Down Expand Up @@ -300,10 +307,10 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// "Reset tab color"
if (_TabColor)
{
til::color c{ _TabColor.Value() };
til::color tabColor{ _TabColor.Value() };
return winrt::hstring{
fmt::format(std::wstring_view(RS_(L"SetTabColorCommandKey")),
c.ToHexString(true))
tabColor.ToHexString(true))
};
}

Expand Down
12 changes: 7 additions & 5 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<Windows::UI::Color>, 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,7 @@ 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);
JsonUtils::GetValueForKey(json, TabColorKey, args->_TabColor);
return *args;
}
Model::NewTerminalArgs Copy() const
Expand All @@ -97,6 +101,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 Expand Up @@ -493,7 +498,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
struct SetTabColorArgs : public SetTabColorArgsT<SetTabColorArgs>
{
SetTabColorArgs() = default;
GETSET_PROPERTY(Windows::Foundation::IReference<uint32_t>, TabColor, nullptr);
GETSET_PROPERTY(Windows::Foundation::IReference<Windows::UI::Color>, TabColor, nullptr);

static constexpr std::string_view ColorKey{ "color" };

Expand All @@ -513,10 +518,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
// LOAD BEARING: Not using make_self here _will_ break you in the future!
auto args = winrt::make_self<SetTabColorArgs>();
if (const auto temp{ JsonUtils::GetValueForKey<std::optional<til::color>>(json, ColorKey) })
{
args->_TabColor = static_cast<uint32_t>(*temp);
}
JsonUtils::GetValueForKey(json, ColorKey, args->_TabColor);
Don-Vito marked this conversation as resolved.
Show resolved Hide resolved
return { *args, {} };
}
IActionArgs Copy() const
Expand Down
3 changes: 2 additions & 1 deletion 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<Windows.UI.Color> 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 Expand Up @@ -130,7 +131,7 @@ namespace Microsoft.Terminal.Settings.Model

[default_interface] runtimeclass SetTabColorArgs : IActionArgs
{
Windows.Foundation.IReference<UInt32> TabColor { get; };
Windows.Foundation.IReference<Windows.UI.Color> TabColor { get; };
};

[default_interface] runtimeclass RenameTabArgs : IActionArgs
Expand Down