Skip to content

Commit

Permalink
Implement CascadiaSettings::Copy() (#7877)
Browse files Browse the repository at this point in the history
## Summary of the Pull Request
This implements the `Copy` function for `CascadiaSettings`. Copy performs a deep copy of a `CascadiaSettings` object. This is needed for data binding in the Terminal Settings Editor.

The `Copy` function was basically implemented in every settings model object. This was mostly just repetitive work.

## References
#7667 - TSM
#1564 - Settings UI

## PR Checklist
* [X] Tests added/passed
  • Loading branch information
carlos-zamora authored Oct 16, 2020
1 parent df7c3cc commit 9045266
Show file tree
Hide file tree
Showing 18 changed files with 374 additions and 6 deletions.
90 changes: 90 additions & 0 deletions src/cascadia/LocalTests_SettingsModel/DeserializationTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ namespace SettingsModelLocalTests
TEST_METHOD(TestUnbindNestedCommand);
TEST_METHOD(TestRebindNestedCommand);

TEST_METHOD(TestCopy);

TEST_CLASS_SETUP(ClassSetup)
{
InitializeJsonReader();
Expand Down Expand Up @@ -2370,4 +2372,92 @@ namespace SettingsModelLocalTests
}
}

void DeserializationTests::TestCopy()
{
const std::string settingsJson{ R"(
{
"defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"initialCols": 50,
"profiles":
[
{
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"name": "Custom Profile",
"fontFace": "Cascadia Code"
}
],
"schemes":
[
{
"name": "Campbell, but for a test",
"foreground": "#CCCCCC",
"background": "#0C0C0C",
"cursorColor": "#FFFFFF",
"black": "#0C0C0C",
"red": "#C50F1F",
"green": "#13A10E",
"yellow": "#C19C00",
"blue": "#0037DA",
"purple": "#881798",
"cyan": "#3A96DD",
"white": "#CCCCCC",
"brightBlack": "#767676",
"brightRed": "#E74856",
"brightGreen": "#16C60C",
"brightYellow": "#F9F1A5",
"brightBlue": "#3B78FF",
"brightPurple": "#B4009E",
"brightCyan": "#61D6D6",
"brightWhite": "#F2F2F2"
}
],
"actions":
[
{ "command": "openSettings", "keys": "ctrl+," },
{ "command": { "action": "openSettings", "target": "defaultsFile" }, "keys": "ctrl+alt+," },
{
"name": { "key": "SetColorSchemeParentCommandName" },
"commands": [
{
"iterateOn": "schemes",
"name": "${scheme.name}",
"command": { "action": "setColorScheme", "colorScheme": "${scheme.name}" }
}
]
}
]
})" };

VerifyParseSucceeded(settingsJson);

auto settings{ winrt::make_self<implementation::CascadiaSettings>() };
settings->_ParseJsonString(settingsJson, false);
settings->LayerJson(settings->_userSettings);
settings->_ValidateSettings();

const auto copy{ settings->Copy() };
const auto copyImpl{ winrt::get_self<implementation::CascadiaSettings>(copy) };

// test globals
VERIFY_ARE_EQUAL(settings->_globals->DefaultProfile(), copyImpl->_globals->DefaultProfile());

// test profiles
VERIFY_ARE_EQUAL(settings->_profiles.Size(), copyImpl->_profiles.Size());
VERIFY_ARE_EQUAL(settings->_profiles.GetAt(0).Name(), copyImpl->_profiles.GetAt(0).Name());

// test schemes
const auto schemeName{ L"Campbell, but for a test" };
VERIFY_ARE_EQUAL(settings->_globals->_colorSchemes.Size(), copyImpl->_globals->_colorSchemes.Size());
VERIFY_ARE_EQUAL(settings->_globals->_colorSchemes.HasKey(schemeName), copyImpl->_globals->_colorSchemes.HasKey(schemeName));

// test actions
VERIFY_ARE_EQUAL(settings->_globals->_keymap->_keyShortcuts.size(), copyImpl->_globals->_keymap->_keyShortcuts.size());
VERIFY_ARE_EQUAL(settings->_globals->_commands.Size(), copyImpl->_globals->_commands.Size());

// Test that changing the copy should not change the original
VERIFY_ARE_EQUAL(settings->_globals->WordDelimiters(), copyImpl->_globals->WordDelimiters());
copyImpl->_globals->WordDelimiters(L"changed value");
VERIFY_ARE_NOT_EQUAL(settings->_globals->WordDelimiters(), copyImpl->_globals->WordDelimiters());
}
}
8 changes: 8 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionAndArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
}
}

com_ptr<ActionAndArgs> ActionAndArgs::Copy() const
{
auto copy{ winrt::make_self<ActionAndArgs>() };
copy->_Action = _Action;
copy->_Args = _Args.Copy();
return copy;
}

winrt::hstring ActionAndArgs::GenerateName() const
{
// Use a magic static to initialize this map, because we won't be able
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/ActionAndArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
ActionAndArgs(ShortcutAction action, IActionArgs args) :
_Action{ action },
_Args{ args } {};
com_ptr<ActionAndArgs> Copy() const;

hstring GenerateName() const;

Expand Down
104 changes: 104 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
JsonUtils::GetValueForKey(json, ProfileKey, args->_Profile);
return *args;
}
Model::NewTerminalArgs Copy() const
{
auto copy{ winrt::make_self<NewTerminalArgs>() };
copy->_Commandline = _Commandline;
copy->_StartingDirectory = _StartingDirectory;
copy->_TabTitle = _TabTitle;
copy->_ProfileIndex = _ProfileIndex;
copy->_Profile = _Profile;
return *copy;
}
};

struct CopyTextArgs : public CopyTextArgsT<CopyTextArgs>
Expand Down Expand Up @@ -121,6 +131,14 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
JsonUtils::GetValueForKey(json, CopyFormattingKey, args->_CopyFormatting);
return { *args, {} };
}

IActionArgs Copy() const
{
auto copy{ winrt::make_self<CopyTextArgs>() };
copy->_SingleLine = _SingleLine;
copy->_CopyFormatting = _CopyFormatting;
return *copy;
}
};

struct NewTabArgs : public NewTabArgsT<NewTabArgs>
Expand Down Expand Up @@ -149,6 +167,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
args->_TerminalArgs = NewTerminalArgs::FromJson(json);
return { *args, {} };
}
IActionArgs Copy() const
{
auto copy{ winrt::make_self<NewTabArgs>() };
copy->_TerminalArgs = _TerminalArgs.Copy();
return *copy;
}
};

struct SwitchToTabArgs : public SwitchToTabArgsT<SwitchToTabArgs>
Expand Down Expand Up @@ -179,6 +203,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
JsonUtils::GetValueForKey(json, TabIndexKey, args->_TabIndex);
return { *args, {} };
}
IActionArgs Copy() const
{
auto copy{ winrt::make_self<SwitchToTabArgs>() };
copy->_TabIndex = _TabIndex;
return *copy;
}
};

struct ResizePaneArgs : public ResizePaneArgsT<ResizePaneArgs>
Expand Down Expand Up @@ -214,6 +244,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return { *args, {} };
}
}
IActionArgs Copy() const
{
auto copy{ winrt::make_self<ResizePaneArgs>() };
copy->_Direction = _Direction;
return *copy;
}
};

struct MoveFocusArgs : public MoveFocusArgsT<MoveFocusArgs>
Expand Down Expand Up @@ -249,6 +285,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return { *args, {} };
}
}
IActionArgs Copy() const
{
auto copy{ winrt::make_self<MoveFocusArgs>() };
copy->_Direction = _Direction;
return *copy;
}
};

struct AdjustFontSizeArgs : public AdjustFontSizeArgsT<AdjustFontSizeArgs>
Expand Down Expand Up @@ -277,6 +319,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
JsonUtils::GetValueForKey(json, AdjustFontSizeDelta, args->_Delta);
return { *args, {} };
}
IActionArgs Copy() const
{
auto copy{ winrt::make_self<AdjustFontSizeArgs>() };
copy->_Delta = _Delta;
return *copy;
}
};

struct SendInputArgs : public SendInputArgsT<SendInputArgs>
Expand Down Expand Up @@ -308,6 +356,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
}
return { *args, {} };
}
IActionArgs Copy() const
{
auto copy{ winrt::make_self<SendInputArgs>() };
copy->_Input = _Input;
return *copy;
}
};

struct SplitPaneArgs : public SplitPaneArgsT<SplitPaneArgs>
Expand Down Expand Up @@ -347,6 +401,14 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
JsonUtils::GetValueForKey(json, SplitModeKey, args->_SplitMode);
return { *args, {} };
}
IActionArgs Copy() const
{
auto copy{ winrt::make_self<SplitPaneArgs>() };
copy->_SplitStyle = _SplitStyle;
copy->_TerminalArgs = _TerminalArgs.Copy();
copy->_SplitMode = _SplitMode;
return *copy;
}
};

struct OpenSettingsArgs : public OpenSettingsArgsT<OpenSettingsArgs>
Expand Down Expand Up @@ -375,6 +437,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
JsonUtils::GetValueForKey(json, TargetKey, args->_Target);
return { *args, {} };
}
IActionArgs Copy() const
{
auto copy{ winrt::make_self<OpenSettingsArgs>() };
copy->_Target = _Target;
return *copy;
}
};

struct SetColorSchemeArgs : public SetColorSchemeArgsT<SetColorSchemeArgs>
Expand Down Expand Up @@ -407,6 +475,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
}
return { *args, {} };
}
IActionArgs Copy() const
{
auto copy{ winrt::make_self<SetColorSchemeArgs>() };
copy->_SchemeName = _SchemeName;
return *copy;
}
};

struct SetTabColorArgs : public SetTabColorArgsT<SetTabColorArgs>
Expand Down Expand Up @@ -438,6 +512,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
}
return { *args, {} };
}
IActionArgs Copy() const
{
auto copy{ winrt::make_self<SetTabColorArgs>() };
copy->_TabColor = _TabColor;
return *copy;
}
};

struct RenameTabArgs : public RenameTabArgsT<RenameTabArgs>
Expand Down Expand Up @@ -466,6 +546,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
JsonUtils::GetValueForKey(json, TitleKey, args->_Title);
return { *args, {} };
}
IActionArgs Copy() const
{
auto copy{ winrt::make_self<RenameTabArgs>() };
copy->_Title = _Title;
return *copy;
}
};

struct ExecuteCommandlineArgs : public ExecuteCommandlineArgsT<ExecuteCommandlineArgs>
Expand Down Expand Up @@ -500,6 +586,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
}
return { *args, {} };
}
IActionArgs Copy() const
{
auto copy{ winrt::make_self<ExecuteCommandlineArgs>() };
copy->_Commandline = _Commandline;
return *copy;
}
};

struct CloseOtherTabsArgs : public CloseOtherTabsArgsT<CloseOtherTabsArgs>
Expand Down Expand Up @@ -530,6 +622,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
JsonUtils::GetValueForKey(json, IndexKey, args->_Index);
return { *args, {} };
}
IActionArgs Copy() const
{
auto copy{ winrt::make_self<CloseOtherTabsArgs>() };
copy->_Index = _Index;
return *copy;
}
};

struct CloseTabsAfterArgs : public CloseTabsAfterArgsT<CloseTabsAfterArgs>
Expand Down Expand Up @@ -560,6 +658,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
JsonUtils::GetValueForKey(json, IndexKey, args->_Index);
return { *args, {} };
}
IActionArgs Copy() const
{
auto copy{ winrt::make_self<CloseTabsAfterArgs>() };
copy->_Index = _Index;
return *copy;
}
};
}

Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.idl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Microsoft.Terminal.Settings.Model
{
Boolean Equals(IActionArgs other);
String GenerateName();
IActionArgs Copy();
};

interface IActionEventArgs
Expand Down Expand Up @@ -48,6 +49,7 @@ namespace Microsoft.Terminal.Settings.Model
[default_interface] runtimeclass NewTerminalArgs {
NewTerminalArgs();
NewTerminalArgs(Int32 profileIndex);
NewTerminalArgs Copy();

String Commandline;
String StartingDirectory;
Expand Down
23 changes: 23 additions & 0 deletions src/cascadia/TerminalSettingsModel/CascadiaSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ CascadiaSettings::CascadiaSettings(winrt::hstring json) :
_ValidateSettings();
}

winrt::Microsoft::Terminal::Settings::Model::CascadiaSettings CascadiaSettings::Copy() const
{
// dynamic profile generators added by default
auto settings{ winrt::make_self<CascadiaSettings>() };
settings->_globals = _globals->Copy();
for (const auto profile : _profiles)
{
auto profImpl{ winrt::get_self<Profile>(profile) };
settings->_profiles.Append(*profImpl->Copy());
}
for (auto warning : _warnings)
{
settings->_warnings.Append(warning);
}
settings->_loadError = _loadError;
settings->_deserializationErrorMessage = _deserializationErrorMessage;
settings->_userSettingsString = _userSettingsString;
settings->_userSettings = _userSettings;
settings->_defaultSettings = _defaultSettings;
settings->_userDefaultProfileSettings = _userDefaultProfileSettings;
return *settings;
}

// Method Description:
// - Finds a profile that matches the given GUID. If there is no profile in this
// settings object that matches, returns nullptr.
Expand Down
Loading

0 comments on commit 9045266

Please sign in to comment.