Skip to content

Commit

Permalink
about to test stage 1
Browse files Browse the repository at this point in the history
  • Loading branch information
PankajBhojwani committed Apr 24, 2024
1 parent 85933e2 commit c134402
Show file tree
Hide file tree
Showing 9 changed files with 337 additions and 45 deletions.
268 changes: 229 additions & 39 deletions src/cascadia/TerminalSettingsModel/ActionMap.cpp

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions src/cascadia/TerminalSettingsModel/ActionMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
bool IsKeyChordExplicitlyUnbound(const Control::KeyChord& keys) const;
Control::KeyChord GetKeyBindingForAction(const ShortcutAction& action) const;
Control::KeyChord GetKeyBindingForAction(const ShortcutAction& action, const IActionArgs& actionArgs) const;
Control::KeyChord GetKeyBindingForAction2(winrt::hstring cmdID) const;

// population
void AddAction(const Model::Command& cmd);
Expand All @@ -54,6 +55,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
static com_ptr<ActionMap> FromJson(const Json::Value& json, const OriginTag origin = OriginTag::None);
std::vector<SettingsLoadWarnings> LayerJson(const Json::Value& json, const OriginTag origin, const bool withKeybindings = true);
Json::Value ToJson() const;
Json::Value KeyBindingsToJson() const;
bool FixUpsAppliedDuringLoad() const;

// modification
bool GenerateIDsForActions();
Expand All @@ -74,20 +77,21 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
std::optional<Model::Command> _GetActionByKeyChordInternal2(const Control::KeyChord& keys) const;

void _RefreshKeyBindingCaches();
void _RefreshKeyBindingCaches2();
void _PopulateAvailableActionsWithStandardCommands(std::unordered_map<hstring, Model::ActionAndArgs>& availableActions, std::unordered_set<InternalActionID>& visitedActionIDs) const;
void _PopulateAvailableActionsWithStandardCommands2(std::unordered_map<hstring, Model::ActionAndArgs>& availableActions, std::unordered_set<winrt::hstring>& visitedActionIDs) const;
void _PopulateNameMapWithSpecialCommands(std::unordered_map<hstring, Model::Command>& nameMap) const;
void _PopulateNameMapWithStandardCommands(std::unordered_map<hstring, Model::Command>& nameMap) const;
void _PopulateKeyBindingMapWithStandardCommands(std::unordered_map<Control::KeyChord, Model::Command, KeyChordHash, KeyChordEquality>& keyBindingsMap, std::unordered_set<Control::KeyChord, KeyChordHash, KeyChordEquality>& unboundKeys) const;
void _PopulateKeyBindingMapWithStandardCommands2(std::unordered_map<Control::KeyChord, Model::Command, KeyChordHash, KeyChordEquality>& keyBindingsMap, std::unordered_set<Control::KeyChord, KeyChordHash, KeyChordEquality>& unboundKeys) const;

std::vector<Model::Command> _GetCumulativeActions() const noexcept;
std::vector<Model::Command> _GetCumulativeActions2() const noexcept;

void _TryUpdateActionMap(const Model::Command& cmd, Model::Command& oldCmd, Model::Command& consolidatedCmd);
void _TryUpdateName(const Model::Command& cmd, const Model::Command& oldCmd, const Model::Command& consolidatedCmd);
void _TryUpdateKeyChord(const Model::Command& cmd, const Model::Command& oldCmd, const Model::Command& consolidatedCmd);

void _recursiveUpdateCommandKeybindingLabels();

Windows::Foundation::Collections::IMap<hstring, Model::ActionAndArgs> _AvailableActionsCache{ nullptr };
Windows::Foundation::Collections::IMap<hstring, Model::Command> _NameMapCache{ nullptr };
Windows::Foundation::Collections::IMap<Control::KeyChord, Model::Command> _GlobalHotkeysCache{ nullptr };
Expand All @@ -113,12 +117,21 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// than is necessary to be serialized.
std::unordered_map<InternalActionID, Model::Command> _MaskingActions;

bool _fixUpsAppliedDuringLoad;

void _AddKeyBindingHelper(const Json::Value& json, std::vector<SettingsLoadWarnings>& warnings);
void _TryUpdateActionMap2(const Model::Command& cmd);
void _TryUpdateKeyChord2(const Model::Command& cmd);
std::unordered_map<Control::KeyChord, winrt::hstring, KeyChordHash, KeyChordEquality> _KeyMap2;
std::unordered_map<winrt::hstring, Model::Command> _ActionMap2;

void _PopulateNameMapWithSpecialCommands2(std::unordered_map<hstring, Model::Command>& nameMap) const;
void _PopulateNameMapWithStandardCommands2(std::unordered_map<hstring, Model::Command>& nameMap) const;

Windows::Foundation::Collections::IMap<hstring, Model::Command> _NameMapCache2{ nullptr };
Windows::Foundation::Collections::IMap<Control::KeyChord, Model::Command> _GlobalHotkeysCache2{ nullptr };
Windows::Foundation::Collections::IMap<Control::KeyChord, Model::Command> _KeyBindingMapCache2{ nullptr };

friend class SettingsModelUnitTests::KeyBindingsTests;
friend class SettingsModelUnitTests::DeserializationTests;
friend class SettingsModelUnitTests::TerminalSettingsTests;
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/ActionMap.idl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Microsoft.Terminal.Settings.Model

Microsoft.Terminal.Control.KeyChord GetKeyBindingForAction(ShortcutAction action);
[method_name("GetKeyBindingForActionWithArgs")] Microsoft.Terminal.Control.KeyChord GetKeyBindingForAction(ShortcutAction action, IActionArgs actionArgs);
Microsoft.Terminal.Control.KeyChord GetKeyBindingForAction2(String cmdID);

Windows.Foundation.Collections.IMapView<String, ActionAndArgs> AvailableActions { get; };

Expand Down
44 changes: 42 additions & 2 deletions src/cascadia/TerminalSettingsModel/ActionMapSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,26 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
if (jsonBlock.isMember(JsonKey("commands")) || jsonBlock.isMember(JsonKey("command")))
{
AddAction(*Command::FromJson(jsonBlock, warnings, origin, withKeybindings));

// if we're in a 'command' block and there are keys, this is the legacy style
// let the parse know that fixups are needed
if (jsonBlock.isMember(JsonKey("keys")))
{
_fixUpsAppliedDuringLoad = true;
}
}
else
{
_AddKeyBindingHelper(jsonBlock, warnings);
}
// todo: need to have a flag for fixups applied during load
}

return warnings;
}

Json::Value ActionMap::ToJson() const
{
// todo: stage 1 (done, need to uncomment)
Json::Value actionList{ Json::ValueType::arrayValue };

// Command serializes to an array of JSON objects.
Expand All @@ -95,13 +102,26 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
}
};

//auto toJson2 = [&actionList](const Model::Command& cmd) {
// const auto cmdImpl{ winrt::get_self<implementation::Command>(cmd) };
// const auto& cmdJsonArray{ cmdImpl->ToJson2() };
// for (const auto& cmdJson : cmdJsonArray)
// {
// actionList.append(cmdJson);
// }
//};

// Serialize all standard Command objects in the current layer
// todo: change to _NewActionMap
for (const auto& [_, cmd] : _ActionMap)
{
toJson(cmd);
}

//for (const auto& [_, cmd] : _ActionMap2)
//{
// toJson2(cmd);
//}

// Serialize all nested Command objects added in the current layer
for (const auto& [_, cmd] : _NestedCommands)
{
Expand All @@ -117,6 +137,26 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return actionList;
}

Json::Value ActionMap::KeyBindingsToJson() const
{
Json::Value keybindingsList{ Json::ValueType::arrayValue };

auto toJson = [&keybindingsList](const KeyChord kc, const winrt::hstring cmdID) {
Json::Value keyIDPair{ Json::ValueType::objectValue };
JsonUtils::SetValueForKey(keyIDPair, "keys", kc);
JsonUtils::SetValueForKey(keyIDPair, "id", cmdID);
keybindingsList.append(keyIDPair);
};

// Serialize all standard Command objects in the current layer
for (const auto& [keys, cmdID] : _KeyMap2)
{
toJson(keys, cmdID);
}

return keybindingsList;
}

void ActionMap::_AddKeyBindingHelper(const Json::Value& json, std::vector<SettingsLoadWarnings>& warnings)
{
// There should always be a "keys" field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ bool SettingsLoader::FixupUserSettings()
};

auto fixedUp = userSettings.fixupsAppliedDuringLoad;
fixedUp = userSettings.globals->FixUpsAppliedDuringLoad() || fixedUp;

fixedUp = RemapColorSchemeForProfile(userSettings.baseLayerProfile) || fixedUp;
for (const auto& profile : userSettings.profiles)
Expand Down
39 changes: 39 additions & 0 deletions src/cascadia/TerminalSettingsModel/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,45 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return cmdList;
}

// Function Description:
// - Serialize the Command into an array of json actions
// Arguments:
// - <none>
// Return Value:
// - an array of serialized actions
Json::Value Command::ToJson2() const
{
Json::Value cmdList{ Json::ValueType::arrayValue };

if (_nestedCommand || _IterateOn != ExpandCommandType::None)
{
// handle special commands
// For these, we can trust _originalJson to be correct.
// In fact, we _need_ to use it here because we don't actually deserialize `iterateOn`
// until we expand the command.
cmdList.append(_originalJson);
}
else
{
Json::Value cmdJson{ Json::ValueType::objectValue };
JsonUtils::SetValueForKey(cmdJson, IconKey, _iconPath);
JsonUtils::SetValueForKey(cmdJson, NameKey, _name);
if (!_ID.empty())
{
JsonUtils::SetValueForKey(cmdJson, IDKey, _ID);
}

if (_ActionAndArgs)
{
cmdJson[JsonKey(ActionKey)] = ActionAndArgs::ToJson(_ActionAndArgs);
}

cmdList.append(cmdJson);
}

return cmdList;
}

// Function Description:
// - Helper to escape a string as a json string. This function will also
// trim off the leading and trailing double-quotes, so the output string
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
const Json::Value& json,
const OriginTag origin);
Json::Value ToJson() const;
Json::Value ToJson2() const;

bool HasNestedCommands() const;
bool IsNestedCommand() const noexcept;
Expand Down
10 changes: 8 additions & 2 deletions src/cascadia/TerminalSettingsModel/GlobalAppSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,17 @@ Json::Value GlobalAppSettings::ToJson()
#undef GLOBAL_SETTINGS_TO_JSON

json[JsonKey(ActionsKey)] = _actionMap->ToJson();
// todo: complete this
//json[JsonKey(KeysMapKey)] = _keysMap->ToJson();
json[JsonKey(LegacyKeybindingsKey)] = _actionMap->KeyBindingsToJson();
// todo: stage 3 - fix tests, they're surely broke with this

return json;
}

bool GlobalAppSettings::FixUpsAppliedDuringLoad()
{
return _actionMap->FixUpsAppliedDuringLoad();
}

winrt::Microsoft::Terminal::Settings::Model::Theme GlobalAppSettings::CurrentTheme() noexcept
{
auto requestedTheme = Model::Theme::IsSystemInDarkTheme() ?
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/GlobalAppSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
void LayerActionsFrom(const Json::Value& json, const OriginTag origin, const bool withKeybindings = true);

Json::Value ToJson();
bool FixUpsAppliedDuringLoad();

const std::vector<SettingsLoadWarnings>& KeybindingsWarnings() const;

Expand Down

0 comments on commit c134402

Please sign in to comment.