Skip to content

Commit aee803e

Browse files
authored
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
1 parent 20a2880 commit aee803e

19 files changed

+146
-2
lines changed

doc/cascadia/profiles.schema.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"toggleAlwaysOnTop",
6060
"toggleRetroEffect",
6161
"find",
62+
"setColorScheme",
6263
"setTabColor",
6364
"openTabColorPicker",
6465
"renameTab",
@@ -283,6 +284,23 @@
283284
}
284285
]
285286
},
287+
"SetColorSchemeAction": {
288+
"description": "Arguments corresponding to a Set Color Scheme Action",
289+
"allOf": [
290+
{ "$ref": "#/definitions/ShortcutAction" },
291+
{
292+
"properties": {
293+
"action": { "type": "string", "pattern": "setColorScheme" },
294+
"colorScheme": {
295+
"type": "string",
296+
"default": "",
297+
"description": "the name of the scheme to apply to the active pane"
298+
}
299+
}
300+
}
301+
],
302+
"required": [ "name" ]
303+
},
286304
"WtAction": {
287305
"description": "Arguments corresponding to a wt Action",
288306
"allOf": [
@@ -350,6 +368,7 @@
350368
{ "$ref": "#/definitions/SplitPaneAction" },
351369
{ "$ref": "#/definitions/OpenSettingsAction" },
352370
{ "$ref": "#/definitions/SetTabColorAction" },
371+
{ "$ref": "#/definitions/SetColorSchemeAction" },
353372
{ "$ref": "#/definitions/WtAction" },
354373
{ "$ref": "#/definitions/CloseOtherTabsAction" },
355374
{ "$ref": "#/definitions/CloseTabsAfterAction" },

src/cascadia/TerminalApp/ActionAndArgs.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ static constexpr std::string_view ToggleRetroEffectKey{ "toggleRetroEffect" };
3636
static constexpr std::string_view ToggleFocusModeKey{ "toggleFocusMode" };
3737
static constexpr std::string_view ToggleFullscreenKey{ "toggleFullscreen" };
3838
static constexpr std::string_view ToggleAlwaysOnTopKey{ "toggleAlwaysOnTop" };
39+
static constexpr std::string_view SetColorSchemeKey{ "setColorScheme" };
3940
static constexpr std::string_view SetTabColorKey{ "setTabColor" };
4041
static constexpr std::string_view OpenTabColorPickerKey{ "openTabColorPicker" };
4142
static constexpr std::string_view RenameTabKey{ "renameTab" };
@@ -83,6 +84,7 @@ namespace winrt::TerminalApp::implementation
8384
{ ResizePaneKey, ShortcutAction::ResizePane },
8485
{ MoveFocusKey, ShortcutAction::MoveFocus },
8586
{ OpenSettingsKey, ShortcutAction::OpenSettings },
87+
{ SetColorSchemeKey, ShortcutAction::SetColorScheme },
8688
{ ToggleRetroEffectKey, ShortcutAction::ToggleRetroEffect },
8789
{ ToggleFocusModeKey, ShortcutAction::ToggleFocusMode },
8890
{ ToggleFullscreenKey, ShortcutAction::ToggleFullscreen },
@@ -125,6 +127,8 @@ namespace winrt::TerminalApp::implementation
125127

126128
{ ShortcutAction::OpenSettings, winrt::TerminalApp::implementation::OpenSettingsArgs::FromJson },
127129

130+
{ ShortcutAction::SetColorScheme, winrt::TerminalApp::implementation::SetColorSchemeArgs::FromJson },
131+
128132
{ ShortcutAction::SetTabColor, winrt::TerminalApp::implementation::SetTabColorArgs::FromJson },
129133

130134
{ ShortcutAction::RenameTab, winrt::TerminalApp::implementation::RenameTabArgs::FromJson },
@@ -280,6 +284,7 @@ namespace winrt::TerminalApp::implementation
280284
{ ShortcutAction::TogglePaneZoom, RS_(L"TogglePaneZoomCommandKey") },
281285
{ ShortcutAction::Invalid, L"" },
282286
{ ShortcutAction::Find, RS_(L"FindCommandKey") },
287+
{ ShortcutAction::SetColorScheme, L"" },
283288
{ ShortcutAction::SetTabColor, RS_(L"ResetTabColorCommandKey") },
284289
{ ShortcutAction::OpenTabColorPicker, RS_(L"OpenTabColorPickerCommandKey") },
285290
{ ShortcutAction::RenameTab, RS_(L"ResetTabNameCommandKey") },

src/cascadia/TerminalApp/ActionArgs.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "AdjustFontSizeArgs.g.cpp"
1616
#include "SplitPaneArgs.g.cpp"
1717
#include "OpenSettingsArgs.g.cpp"
18+
#include "SetColorSchemeArgs.g.cpp"
1819
#include "SetTabColorArgs.g.cpp"
1920
#include "RenameTabArgs.g.cpp"
2021
#include "ExecuteCommandlineArgs.g.cpp"
@@ -229,6 +230,19 @@ namespace winrt::TerminalApp::implementation
229230
}
230231
}
231232

233+
winrt::hstring SetColorSchemeArgs::GenerateName() const
234+
{
235+
// "Set color scheme to "{_SchemeName}""
236+
if (!_SchemeName.empty())
237+
{
238+
return winrt::hstring{
239+
fmt::format(std::wstring_view(RS_(L"SetColorSchemeCommandKey")),
240+
_SchemeName.c_str())
241+
};
242+
}
243+
return L"";
244+
}
245+
232246
winrt::hstring SetTabColorArgs::GenerateName() const
233247
{
234248
// "Set tab color to #RRGGBB"

src/cascadia/TerminalApp/ActionArgs.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "AdjustFontSizeArgs.g.h"
1616
#include "SplitPaneArgs.g.h"
1717
#include "OpenSettingsArgs.g.h"
18+
#include "SetColorSchemeArgs.g.h"
1819
#include "SetTabColorArgs.g.h"
1920
#include "RenameTabArgs.g.h"
2021
#include "ExecuteCommandlineArgs.g.h"
@@ -332,6 +333,38 @@ namespace winrt::TerminalApp::implementation
332333
}
333334
};
334335

336+
struct SetColorSchemeArgs : public SetColorSchemeArgsT<SetColorSchemeArgs>
337+
{
338+
SetColorSchemeArgs() = default;
339+
GETSET_PROPERTY(winrt::hstring, SchemeName, L"");
340+
341+
static constexpr std::string_view NameKey{ "colorScheme" };
342+
343+
public:
344+
hstring GenerateName() const;
345+
346+
bool Equals(const IActionArgs& other)
347+
{
348+
auto otherAsUs = other.try_as<SetColorSchemeArgs>();
349+
if (otherAsUs)
350+
{
351+
return otherAsUs->_SchemeName == _SchemeName;
352+
}
353+
return false;
354+
};
355+
static FromJsonResult FromJson(const Json::Value& json)
356+
{
357+
// LOAD BEARING: Not using make_self here _will_ break you in the future!
358+
auto args = winrt::make_self<SetColorSchemeArgs>();
359+
JsonUtils::GetValueForKey(json, NameKey, args->_SchemeName);
360+
if (args->_SchemeName.empty())
361+
{
362+
return { nullptr, { ::TerminalApp::SettingsLoadWarnings::MissingRequiredParameter } };
363+
}
364+
return { *args, {} };
365+
}
366+
};
367+
335368
struct SetTabColorArgs : public SetTabColorArgsT<SetTabColorArgs>
336369
{
337370
SetTabColorArgs() = default;

src/cascadia/TerminalApp/ActionArgs.idl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ namespace TerminalApp
106106
SettingsTarget Target { get; };
107107
};
108108

109+
[default_interface] runtimeclass SetColorSchemeArgs : IActionArgs
110+
{
111+
String SchemeName { get; };
112+
};
113+
109114
[default_interface] runtimeclass SetTabColorArgs : IActionArgs
110115
{
111116
Windows.Foundation.IReference<UInt32> TabColor { get; };

src/cascadia/TerminalApp/AppActionHandlers.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,27 @@ namespace winrt::TerminalApp::implementation
291291
args.Handled(true);
292292
}
293293

294+
void TerminalPage::_HandleSetColorScheme(const IInspectable& /*sender*/,
295+
const TerminalApp::ActionEventArgs& args)
296+
{
297+
args.Handled(false);
298+
if (const auto& realArgs = args.ActionArgs().try_as<TerminalApp::SetColorSchemeArgs>())
299+
{
300+
if (auto activeTab = _GetFocusedTab())
301+
{
302+
if (auto activeControl = activeTab->GetActiveTerminalControl())
303+
{
304+
auto controlSettings = activeControl.Settings();
305+
if (_settings->ApplyColorScheme(controlSettings, realArgs.SchemeName()))
306+
{
307+
activeControl.UpdateSettings(controlSettings);
308+
args.Handled(true);
309+
}
310+
}
311+
}
312+
}
313+
}
314+
294315
void TerminalPage::_HandleSetTabColor(const IInspectable& /*sender*/,
295316
const TerminalApp::ActionEventArgs& args)
296317
{

src/cascadia/TerminalApp/CascadiaSettings.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,3 +744,26 @@ const ColorScheme* CascadiaSettings::GetColorSchemeForProfile(const GUID profile
744744
return nullptr;
745745
}
746746
}
747+
748+
// Method Description:
749+
// - Apply the color scheme (provided by name) to the given IControlSettings.
750+
// The settings are modified in-place.
751+
// - If the name doesn't correspond to any of our schemes, this does nothing.
752+
// Arguments:
753+
// - settings: the IControlSettings object to modify
754+
// - name: the name of the scheme to apply
755+
// Return Value:
756+
// - true iff we found a matching scheme for the name schemeName
757+
bool CascadiaSettings::ApplyColorScheme(winrt::Microsoft::Terminal::TerminalControl::IControlSettings& settings,
758+
std::wstring_view schemeName)
759+
{
760+
std::wstring name{ schemeName };
761+
auto schemeAndName = _globals.GetColorSchemes().find(name);
762+
if (schemeAndName != _globals.GetColorSchemes().end())
763+
{
764+
const auto& scheme = schemeAndName->second;
765+
scheme.ApplyScheme(settings);
766+
return true;
767+
}
768+
return false;
769+
}

src/cascadia/TerminalApp/CascadiaSettings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class TerminalApp::CascadiaSettings final
7474

7575
std::vector<TerminalApp::SettingsLoadWarnings>& GetWarnings();
7676

77+
bool ApplyColorScheme(winrt::Microsoft::Terminal::TerminalControl::IControlSettings& settings, std::wstring_view schemeName);
78+
7779
private:
7880
GlobalAppSettings _globals;
7981
std::vector<Profile> _profiles;

src/cascadia/TerminalApp/ColorScheme.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ ColorScheme::~ColorScheme()
6767
// - terminalSettings: the object to apply our settings to.
6868
// Return Value:
6969
// - <none>
70-
void ColorScheme::ApplyScheme(TerminalSettings terminalSettings) const
70+
void ColorScheme::ApplyScheme(const winrt::Microsoft::Terminal::TerminalControl::IControlSettings& terminalSettings) const
7171
{
7272
terminalSettings.DefaultForeground(static_cast<COLORREF>(_defaultForeground));
7373
terminalSettings.DefaultBackground(static_cast<COLORREF>(_defaultBackground));

src/cascadia/TerminalApp/ColorScheme.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class TerminalApp::ColorScheme
3737
ColorScheme(std::wstring name, til::color defaultFg, til::color defaultBg, til::color cursorColor);
3838
~ColorScheme();
3939

40-
void ApplyScheme(winrt::TerminalApp::TerminalSettings terminalSettings) const;
40+
void ApplyScheme(const winrt::Microsoft::Terminal::TerminalControl::IControlSettings& terminalSettings) const;
4141

4242
static ColorScheme FromJson(const Json::Value& json);
4343
bool ShouldBeLayered(const Json::Value& json) const;

0 commit comments

Comments
 (0)