Add support for changing the active color scheme with an action (#6993)

## Summary of the Pull Request

Adds the `setColorScheme` action, to change the color scheme of the active control to one given by the `name` parameter. `name` is required. If `name` is not the name of a color scheme, the action does nothing.

## References

* Being done as a stepping stone to #6689 

## PR Checklist
* [x] Closes #5401
* [x] I work here
* [ ] Tests added/passed
* [n/a] Requires documentation to be updated

## Detailed Description of the Pull Request / Additional comments

Technically, the action is being done by changing the settings of the current `TerminalSettings` of the `TermControl`. Frankly, it should be operating on a copy of the `TermControl`'s `IControlSettings`, then updating the control's settings, or the Control should just listen for changes to it's setting's properties, and update in real time (without a manual call to `UpdateSettings`. However, both those paths are somewhere unknowable beyond #6904, so we'll just do this for now.

## Validation Steps Performed

* tested manually with a scheme that exists
* tested manually with a scheme that doesn't exist
This commit is contained in:
Mike Griese 2020-08-10 11:21:56 -05:00 committed by GitHub
parent 20a288020e
commit aee803e694
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 146 additions and 2 deletions

View file

@ -59,6 +59,7 @@
"toggleAlwaysOnTop",
"toggleRetroEffect",
"find",
"setColorScheme",
"setTabColor",
"openTabColorPicker",
"renameTab",
@ -283,6 +284,23 @@
}
]
},
"SetColorSchemeAction": {
"description": "Arguments corresponding to a Set Color Scheme Action",
"allOf": [
{ "$ref": "#/definitions/ShortcutAction" },
{
"properties": {
"action": { "type": "string", "pattern": "setColorScheme" },
"colorScheme": {
"type": "string",
"default": "",
"description": "the name of the scheme to apply to the active pane"
}
}
}
],
"required": [ "name" ]
},
"WtAction": {
"description": "Arguments corresponding to a wt Action",
"allOf": [
@ -350,6 +368,7 @@
{ "$ref": "#/definitions/SplitPaneAction" },
{ "$ref": "#/definitions/OpenSettingsAction" },
{ "$ref": "#/definitions/SetTabColorAction" },
{ "$ref": "#/definitions/SetColorSchemeAction" },
{ "$ref": "#/definitions/WtAction" },
{ "$ref": "#/definitions/CloseOtherTabsAction" },
{ "$ref": "#/definitions/CloseTabsAfterAction" },

View file

@ -36,6 +36,7 @@ static constexpr std::string_view ToggleRetroEffectKey{ "toggleRetroEffect" };
static constexpr std::string_view ToggleFocusModeKey{ "toggleFocusMode" };
static constexpr std::string_view ToggleFullscreenKey{ "toggleFullscreen" };
static constexpr std::string_view ToggleAlwaysOnTopKey{ "toggleAlwaysOnTop" };
static constexpr std::string_view SetColorSchemeKey{ "setColorScheme" };
static constexpr std::string_view SetTabColorKey{ "setTabColor" };
static constexpr std::string_view OpenTabColorPickerKey{ "openTabColorPicker" };
static constexpr std::string_view RenameTabKey{ "renameTab" };
@ -83,6 +84,7 @@ namespace winrt::TerminalApp::implementation
{ ResizePaneKey, ShortcutAction::ResizePane },
{ MoveFocusKey, ShortcutAction::MoveFocus },
{ OpenSettingsKey, ShortcutAction::OpenSettings },
{ SetColorSchemeKey, ShortcutAction::SetColorScheme },
{ ToggleRetroEffectKey, ShortcutAction::ToggleRetroEffect },
{ ToggleFocusModeKey, ShortcutAction::ToggleFocusMode },
{ ToggleFullscreenKey, ShortcutAction::ToggleFullscreen },
@ -125,6 +127,8 @@ namespace winrt::TerminalApp::implementation
{ ShortcutAction::OpenSettings, winrt::TerminalApp::implementation::OpenSettingsArgs::FromJson },
{ ShortcutAction::SetColorScheme, winrt::TerminalApp::implementation::SetColorSchemeArgs::FromJson },
{ ShortcutAction::SetTabColor, winrt::TerminalApp::implementation::SetTabColorArgs::FromJson },
{ ShortcutAction::RenameTab, winrt::TerminalApp::implementation::RenameTabArgs::FromJson },
@ -280,6 +284,7 @@ namespace winrt::TerminalApp::implementation
{ ShortcutAction::TogglePaneZoom, RS_(L"TogglePaneZoomCommandKey") },
{ ShortcutAction::Invalid, L"" },
{ ShortcutAction::Find, RS_(L"FindCommandKey") },
{ ShortcutAction::SetColorScheme, L"" },
{ ShortcutAction::SetTabColor, RS_(L"ResetTabColorCommandKey") },
{ ShortcutAction::OpenTabColorPicker, RS_(L"OpenTabColorPickerCommandKey") },
{ ShortcutAction::RenameTab, RS_(L"ResetTabNameCommandKey") },

View file

@ -15,6 +15,7 @@
#include "AdjustFontSizeArgs.g.cpp"
#include "SplitPaneArgs.g.cpp"
#include "OpenSettingsArgs.g.cpp"
#include "SetColorSchemeArgs.g.cpp"
#include "SetTabColorArgs.g.cpp"
#include "RenameTabArgs.g.cpp"
#include "ExecuteCommandlineArgs.g.cpp"
@ -229,6 +230,19 @@ namespace winrt::TerminalApp::implementation
}
}
winrt::hstring SetColorSchemeArgs::GenerateName() const
{
// "Set color scheme to "{_SchemeName}""
if (!_SchemeName.empty())
{
return winrt::hstring{
fmt::format(std::wstring_view(RS_(L"SetColorSchemeCommandKey")),
_SchemeName.c_str())
};
}
return L"";
}
winrt::hstring SetTabColorArgs::GenerateName() const
{
// "Set tab color to #RRGGBB"

View file

@ -15,6 +15,7 @@
#include "AdjustFontSizeArgs.g.h"
#include "SplitPaneArgs.g.h"
#include "OpenSettingsArgs.g.h"
#include "SetColorSchemeArgs.g.h"
#include "SetTabColorArgs.g.h"
#include "RenameTabArgs.g.h"
#include "ExecuteCommandlineArgs.g.h"
@ -332,6 +333,38 @@ namespace winrt::TerminalApp::implementation
}
};
struct SetColorSchemeArgs : public SetColorSchemeArgsT<SetColorSchemeArgs>
{
SetColorSchemeArgs() = default;
GETSET_PROPERTY(winrt::hstring, SchemeName, L"");
static constexpr std::string_view NameKey{ "colorScheme" };
public:
hstring GenerateName() const;
bool Equals(const IActionArgs& other)
{
auto otherAsUs = other.try_as<SetColorSchemeArgs>();
if (otherAsUs)
{
return otherAsUs->_SchemeName == _SchemeName;
}
return false;
};
static FromJsonResult FromJson(const Json::Value& json)
{
// LOAD BEARING: Not using make_self here _will_ break you in the future!
auto args = winrt::make_self<SetColorSchemeArgs>();
JsonUtils::GetValueForKey(json, NameKey, args->_SchemeName);
if (args->_SchemeName.empty())
{
return { nullptr, { ::TerminalApp::SettingsLoadWarnings::MissingRequiredParameter } };
}
return { *args, {} };
}
};
struct SetTabColorArgs : public SetTabColorArgsT<SetTabColorArgs>
{
SetTabColorArgs() = default;

View file

@ -106,6 +106,11 @@ namespace TerminalApp
SettingsTarget Target { get; };
};
[default_interface] runtimeclass SetColorSchemeArgs : IActionArgs
{
String SchemeName { get; };
};
[default_interface] runtimeclass SetTabColorArgs : IActionArgs
{
Windows.Foundation.IReference<UInt32> TabColor { get; };

View file

@ -291,6 +291,27 @@ namespace winrt::TerminalApp::implementation
args.Handled(true);
}
void TerminalPage::_HandleSetColorScheme(const IInspectable& /*sender*/,
const TerminalApp::ActionEventArgs& args)
{
args.Handled(false);
if (const auto& realArgs = args.ActionArgs().try_as<TerminalApp::SetColorSchemeArgs>())
{
if (auto activeTab = _GetFocusedTab())
{
if (auto activeControl = activeTab->GetActiveTerminalControl())
{
auto controlSettings = activeControl.Settings();
if (_settings->ApplyColorScheme(controlSettings, realArgs.SchemeName()))
{
activeControl.UpdateSettings(controlSettings);
args.Handled(true);
}
}
}
}
}
void TerminalPage::_HandleSetTabColor(const IInspectable& /*sender*/,
const TerminalApp::ActionEventArgs& args)
{

View file

@ -744,3 +744,26 @@ const ColorScheme* CascadiaSettings::GetColorSchemeForProfile(const GUID profile
return nullptr;
}
}
// Method Description:
// - Apply the color scheme (provided by name) to the given IControlSettings.
// The settings are modified in-place.
// - If the name doesn't correspond to any of our schemes, this does nothing.
// Arguments:
// - settings: the IControlSettings object to modify
// - name: the name of the scheme to apply
// Return Value:
// - true iff we found a matching scheme for the name schemeName
bool CascadiaSettings::ApplyColorScheme(winrt::Microsoft::Terminal::TerminalControl::IControlSettings& settings,
std::wstring_view schemeName)
{
std::wstring name{ schemeName };
auto schemeAndName = _globals.GetColorSchemes().find(name);
if (schemeAndName != _globals.GetColorSchemes().end())
{
const auto& scheme = schemeAndName->second;
scheme.ApplyScheme(settings);
return true;
}
return false;
}

View file

@ -74,6 +74,8 @@ public:
std::vector<TerminalApp::SettingsLoadWarnings>& GetWarnings();
bool ApplyColorScheme(winrt::Microsoft::Terminal::TerminalControl::IControlSettings& settings, std::wstring_view schemeName);
private:
GlobalAppSettings _globals;
std::vector<Profile> _profiles;

View file

@ -67,7 +67,7 @@ ColorScheme::~ColorScheme()
// - terminalSettings: the object to apply our settings to.
// Return Value:
// - <none>
void ColorScheme::ApplyScheme(TerminalSettings terminalSettings) const
void ColorScheme::ApplyScheme(const winrt::Microsoft::Terminal::TerminalControl::IControlSettings& terminalSettings) const
{
terminalSettings.DefaultForeground(static_cast<COLORREF>(_defaultForeground));
terminalSettings.DefaultBackground(static_cast<COLORREF>(_defaultBackground));

View file

@ -37,7 +37,7 @@ public:
ColorScheme(std::wstring name, til::color defaultFg, til::color defaultBg, til::color cursorColor);
~ColorScheme();
void ApplyScheme(winrt::TerminalApp::TerminalSettings terminalSettings) const;
void ApplyScheme(const winrt::Microsoft::Terminal::TerminalControl::IControlSettings& terminalSettings) const;
static ColorScheme FromJson(const Json::Value& json);
bool ShouldBeLayered(const Json::Value& json) const;

View file

@ -529,6 +529,10 @@
<data name="ToggleCommandPaletteCommandKey" xml:space="preserve">
<value>Toggle command palette</value>
</data>
<data name="SetColorSchemeCommandKey" xml:space="preserve">
<value>Set color scheme to {0}</value>
<comment>{0} will be replaced with the name of a color scheme as defined by the user.</comment>
</data>
<data name="SetTabColorCommandKey" xml:space="preserve">
<value>Set tab color to {0}</value>
<comment>{0} will be replaced with a color, displayed in hexadecimal (#RRGGBB) notation.</comment>

View file

@ -185,6 +185,11 @@ namespace winrt::TerminalApp::implementation
_ToggleCommandPaletteHandlers(*this, *eventArgs);
break;
}
case ShortcutAction::SetColorScheme:
{
_SetColorSchemeHandlers(*this, *eventArgs);
break;
}
case ShortcutAction::SetTabColor:
{
_SetTabColorHandlers(*this, *eventArgs);

View file

@ -52,6 +52,7 @@ namespace winrt::TerminalApp::implementation
TYPED_EVENT(ToggleFullscreen, TerminalApp::ShortcutActionDispatch, TerminalApp::ActionEventArgs);
TYPED_EVENT(ToggleAlwaysOnTop, TerminalApp::ShortcutActionDispatch, TerminalApp::ActionEventArgs);
TYPED_EVENT(ToggleCommandPalette, TerminalApp::ShortcutActionDispatch, TerminalApp::ActionEventArgs);
TYPED_EVENT(SetColorScheme, TerminalApp::ShortcutActionDispatch, TerminalApp::ActionEventArgs);
TYPED_EVENT(SetTabColor, TerminalApp::ShortcutActionDispatch, TerminalApp::ActionEventArgs);
TYPED_EVENT(OpenTabColorPicker, TerminalApp::ShortcutActionDispatch, TerminalApp::ActionEventArgs);
TYPED_EVENT(RenameTab, TerminalApp::ShortcutActionDispatch, TerminalApp::ActionEventArgs);

View file

@ -37,6 +37,7 @@ namespace TerminalApp
ToggleFullscreen,
ToggleAlwaysOnTop,
OpenSettings,
SetColorScheme,
SetTabColor,
OpenTabColorPicker,
RenameTab,
@ -86,6 +87,7 @@ namespace TerminalApp
event Windows.Foundation.TypedEventHandler<ShortcutActionDispatch, ActionEventArgs> ToggleFullscreen;
event Windows.Foundation.TypedEventHandler<ShortcutActionDispatch, ActionEventArgs> ToggleAlwaysOnTop;
event Windows.Foundation.TypedEventHandler<ShortcutActionDispatch, ActionEventArgs> ToggleCommandPalette;
event Windows.Foundation.TypedEventHandler<ShortcutActionDispatch, ActionEventArgs> SetColorScheme;
event Windows.Foundation.TypedEventHandler<ShortcutActionDispatch, ActionEventArgs> SetTabColor;
event Windows.Foundation.TypedEventHandler<ShortcutActionDispatch, ActionEventArgs> OpenTabColorPicker;
event Windows.Foundation.TypedEventHandler<ShortcutActionDispatch, ActionEventArgs> RenameTab;

View file

@ -890,6 +890,7 @@ namespace winrt::TerminalApp::implementation
_actionDispatch->ToggleFullscreen({ this, &TerminalPage::_HandleToggleFullscreen });
_actionDispatch->ToggleAlwaysOnTop({ this, &TerminalPage::_HandleToggleAlwaysOnTop });
_actionDispatch->ToggleCommandPalette({ this, &TerminalPage::_HandleToggleCommandPalette });
_actionDispatch->SetColorScheme({ this, &TerminalPage::_HandleSetColorScheme });
_actionDispatch->SetTabColor({ this, &TerminalPage::_HandleSetTabColor });
_actionDispatch->OpenTabColorPicker({ this, &TerminalPage::_HandleOpenTabColorPicker });
_actionDispatch->RenameTab({ this, &TerminalPage::_HandleRenameTab });

View file

@ -222,6 +222,7 @@ namespace winrt::TerminalApp::implementation
void _HandleToggleFocusMode(const IInspectable& sender, const TerminalApp::ActionEventArgs& args);
void _HandleToggleFullscreen(const IInspectable& sender, const TerminalApp::ActionEventArgs& args);
void _HandleToggleAlwaysOnTop(const IInspectable& sender, const TerminalApp::ActionEventArgs& args);
void _HandleSetColorScheme(const IInspectable& sender, const TerminalApp::ActionEventArgs& args);
void _HandleSetTabColor(const IInspectable& sender, const TerminalApp::ActionEventArgs& args);
void _HandleOpenTabColorPicker(const IInspectable& sender, const TerminalApp::ActionEventArgs& args);
void _HandleRenameTab(const IInspectable& sender, const TerminalApp::ActionEventArgs& args);

View file

@ -2838,6 +2838,11 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
_renderer->ResetErrorStateAndResume();
}
IControlSettings TermControl::Settings() const
{
return _settings;
}
Windows::Foundation::IReference<winrt::Windows::UI::Color> TermControl::TabColor() noexcept
{
auto coreColor = _terminal->GetTabColor();

View file

@ -102,6 +102,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
const Windows::UI::Xaml::Thickness GetPadding();
TerminalConnection::ConnectionState ConnectionState() const;
IControlSettings Settings() const;
static Windows::Foundation::Size GetProposedDimensions(IControlSettings const& settings, const uint32_t dpi);
static Windows::Foundation::Size GetProposedDimensions(const winrt::Windows::Foundation::Size& initialSizeInChars,

View file

@ -39,6 +39,8 @@ namespace Microsoft.Terminal.TerminalControl
void UpdateSettings(Microsoft.Terminal.TerminalControl.IControlSettings newSettings);
Microsoft.Terminal.TerminalControl.IControlSettings Settings { get; };
event TitleChangedEventArgs TitleChanged;
event FontSizeChangedEventArgs FontSizeChanged;
event Windows.Foundation.TypedEventHandler<TermControl, CopyToClipboardEventArgs> CopyToClipboard;