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

Refactor ActionMap and Command to use ActionIDs #17162

Merged
merged 84 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
90627b3
add origin tag
PankajBhojwani Mar 5, 2024
9dff28f
update calls in tests
PankajBhojwani Mar 5, 2024
8bcbd0b
fix tests
PankajBhojwani Mar 5, 2024
052d063
ah one of the tests uses this
PankajBhojwani Mar 5, 2024
8cc82de
generated
PankajBhojwani Mar 5, 2024
642d0ab
inbox makes more sense
PankajBhojwani Mar 5, 2024
66fe08f
default ids
PankajBhojwani Mar 7, 2024
2bb1b6c
conflict
PankajBhojwani Mar 19, 2024
be193b2
merge origin
PankajBhojwani Mar 19, 2024
db528c9
generate IDs for user commands
PankajBhojwani Mar 26, 2024
7c907fe
nits
PankajBhojwani Mar 26, 2024
b43191d
spacing
PankajBhojwani Mar 26, 2024
2093660
line
PankajBhojwani Mar 26, 2024
6c32539
string of numbers is unsightly but it works
PankajBhojwani Mar 27, 2024
eccd87f
update comment
PankajBhojwani Mar 27, 2024
44510dc
move id generation to fixupusersettings
PankajBhojwani Mar 28, 2024
10d1fc8
this way is better
PankajBhojwani Mar 28, 2024
71bf90f
even better, also get the ID from json
PankajBhojwani Mar 28, 2024
d57c7a1
move this
PankajBhojwani Mar 28, 2024
5c2307c
fix test
PankajBhojwani Mar 28, 2024
9fc6972
add tests
PankajBhojwani Mar 29, 2024
dca7df5
excess line
PankajBhojwani Mar 29, 2024
dd25ed7
change tests
PankajBhojwani Apr 1, 2024
6e293a5
Everytime
PankajBhojwani Apr 1, 2024
af2d22f
defaults conflict
PankajBhojwani Apr 11, 2024
bdf42c2
first round of nits
PankajBhojwani Apr 11, 2024
12f3aa9
truncate and hex, debug assert
PankajBhojwani Apr 12, 2024
aa49212
null check
PankajBhojwani Apr 12, 2024
5ee630e
fmt is smart
PankajBhojwani Apr 12, 2024
360b92e
fmt_compile, fix test
PankajBhojwani Apr 17, 2024
5e70911
remove 0
PankajBhojwani Apr 17, 2024
ca3eb87
rename and comment
PankajBhojwani Apr 17, 2024
85933e2
midpoint
PankajBhojwani Apr 23, 2024
c134402
about to test stage 1
PankajBhojwani Apr 24, 2024
22ab936
works??
PankajBhojwani Apr 24, 2024
0a3e17e
edge cases
PankajBhojwani Apr 24, 2024
e28d478
some todos for later
PankajBhojwani Apr 24, 2024
f425746
remove keysmap
PankajBhojwani Apr 24, 2024
d0938e2
ugly way to make sure we fixup
PankajBhojwani Apr 25, 2024
12a61c5
shows up in sui and all keybindings work
PankajBhojwani Apr 26, 2024
f1633e0
overwritten IDs and overwritten keychords show up properly in the SUI
PankajBhojwani Apr 26, 2024
ddfac90
Merge branch 'main' of https://github.com/microsoft/terminal into dev…
PankajBhojwani Apr 26, 2024
3e7ab38
sui works?
PankajBhojwani Apr 26, 2024
ae16a5e
started stage 3
PankajBhojwani Apr 26, 2024
dc874c3
rename to special/standard
PankajBhojwani Apr 27, 2024
936afd6
_getactionbyid no longer returns optional
PankajBhojwani Apr 27, 2024
b3e9c26
remove check for invalid
PankajBhojwani Apr 27, 2024
5a1b822
reimplement populating all known keybindings
PankajBhojwani Apr 30, 2024
c51558f
unmark these
PankajBhojwani Apr 30, 2024
754bf04
mark gh todo
PankajBhojwani Apr 30, 2024
2f1d8d2
update defaults
PankajBhojwani Apr 30, 2024
2b4aeb2
don't check for special in standard
PankajBhojwani Apr 30, 2024
e62dfa2
some comments
PankajBhojwani Apr 30, 2024
e725f1e
resolve conflict
PankajBhojwani Apr 30, 2024
db00b90
spelling things
PankajBhojwani Apr 30, 2024
3d92f27
format
PankajBhojwani Apr 30, 2024
428821b
remove _idwasgenerated
PankajBhojwani Apr 30, 2024
6437b9f
fix user defaults file
PankajBhojwani Apr 30, 2024
4c744e6
misc
PankajBhojwani Apr 30, 2024
ca4015f
only one tojson
PankajBhojwani Apr 30, 2024
45cfcd6
just add duplicate pane auto to defaults
PankajBhojwani Apr 30, 2024
3c6015d
remove _getcumulativeactions
PankajBhojwani Apr 30, 2024
c2c75c8
bandaid temporary fix for name
PankajBhojwani May 1, 2024
3e601f5
better if
PankajBhojwani May 1, 2024
cdb907d
mark todo
PankajBhojwani May 1, 2024
f35bf20
Merge branch 'main' of https://github.com/microsoft/terminal into dev…
PankajBhojwani May 1, 2024
2b16acd
check for name, fix some tests
PankajBhojwani May 1, 2024
193e573
fix remaining tests
PankajBhojwani May 2, 2024
0480d65
this is better
PankajBhojwani May 2, 2024
02a1e37
correct GH todo
PankajBhojwani May 2, 2024
80fc299
some new tests
PankajBhojwani May 3, 2024
ebc03e9
another test
PankajBhojwani May 3, 2024
ccf1cc9
nits
PankajBhojwani May 3, 2024
7793c5c
schema
PankajBhojwani May 3, 2024
abef25d
move this to header
PankajBhojwani May 3, 2024
4d35c14
schema conflict
PankajBhojwani May 3, 2024
3e31bda
generate here instead
PankajBhojwani May 6, 2024
14d83b5
delete user actions that are identical to inbox actions
PankajBhojwani May 6, 2024
6c6dd46
leonard comments
PankajBhojwani May 20, 2024
b88a8c5
eraseif
PankajBhojwani May 30, 2024
9703815
nits n fixes
PankajBhojwani May 31, 2024
a80316d
1 more test
PankajBhojwani May 31, 2024
625753c
x86 hash
PankajBhojwani Jun 1, 2024
406312f
fix loops
PankajBhojwani Jun 1, 2024
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
Prev Previous commit
Next Next commit
nits n fixes
  • Loading branch information
PankajBhojwani committed May 31, 2024
commit 9703815f59731f96b82c5ffd154b59b45ce116d4
51 changes: 31 additions & 20 deletions src/cascadia/TerminalSettingsModel/ActionMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,13 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
// only one parent contains all the inbox actions and that parent contains only inbox actions,
// so if we found a non-inbox action we can just skip to the next parent
continue;
break;
}
foundParent = parent;
}

if (foundParent)
{
break;
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand All @@ -91,13 +95,15 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
}
}

std::unordered_map<KeyChord, winrt::hstring, KeyChordHash, KeyChordEquality> keysToReassign;

// now, look through our _ActionMap for commands that
// - had an ID generated for them
// - do not have a name/icon path
// - have a hash that matches a command in the inbox actions
std::erase_if(_ActionMap, [&](const auto& pair) {
const auto userCmdImpl{ get_self<Command>(pair.second) };
if (userCmdImpl->IdWasGenerated() && !userCmdImpl->HasName() && userCmdImpl->IconPath().empty())
if (userCmdImpl->IDWasGenerated() && !userCmdImpl->HasName() && userCmdImpl->IconPath().empty())
{
const auto userActionHash = Hash(userCmdImpl->ActionAndArgs());
if (const auto inboxCmd = inboxActions.find(userActionHash); inboxCmd != inboxActions.end())
Expand All @@ -107,7 +113,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// for any of our keys that point to the user action, point them to the inbox action instead
if (cmdID == pair.first)
{
_KeyMap.insert_or_assign(key, inboxCmd->second.ID());
keysToReassign.insert_or_assign(key, inboxCmd->second.ID());

// register the keys with the inbox action
inboxCmd->second.RegisterKey(key);
Expand All @@ -120,11 +126,16 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
}
return false;
});

for (const auto [key, cmdID] : keysToReassign)
{
_KeyMap.insert_or_assign(key, cmdID);
}
}

bool ActionMap::FixUpsAppliedDuringLoad() const
bool ActionMap::FixupsAppliedDuringLoad() const
{
return _fixUpsAppliedDuringLoad;
return _fixupsAppliedDuringLoad;
}

// Method Description:
Expand All @@ -134,7 +145,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// - actionID: the internal ID associated with a Command
// Return Value:
// - The command if it exists in this layer, otherwise nullptr
Model::Command ActionMap::_GetActionByID(const winrt::hstring actionID) const
Model::Command ActionMap::_GetActionByID(const winrt::hstring& actionID) const
{
// Check current layer
const auto actionMapPair{ _ActionMap.find(actionID) };
Expand Down Expand Up @@ -204,9 +215,9 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
// Only populate AvailableActions with actions that haven't been visited already.
const auto actionID = Hash(cmd.ActionAndArgs());
Copy link
Contributor Author

@PankajBhojwani PankajBhojwani Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately we still have to use InternalActionID to populate the available action dropdown in the SUI since we don't have a Terminal.<> id for every configuration of each shortcut action and its args

if (visitedActionIDs.find(actionID) == visitedActionIDs.end())
if (!visitedActionIDs.contains(actionID))
{
const auto& name{ cmd.Name() };
const auto name{ cmd.Name() };
if (!name.empty())
{
// Update AvailableActions.
Expand Down Expand Up @@ -314,7 +325,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

// Method Description:
// - Recursively populate keyBindingsMap with ours and our parents' key -> id pairs
// - This is a bottom-up approach, ensuring that the keybindings of the parents are overridden by the children
// - This is a bottom-up approach
// - Keybindings of the parents are overridden by the children
void ActionMap::_PopulateCumulativeKeyMap(std::unordered_map<Control::KeyChord, winrt::hstring, KeyChordHash, KeyChordEquality>& keyBindingsMap)
{
for (const auto& [keys, cmdID] : _KeyMap)
Expand All @@ -333,7 +345,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

// Method Description:
// - Recursively populate actionMap with ours and our parents' id -> command pairs
// - This is a bottom-up approach, ensuring that the actions of the parents are overridden by the children
// - This is a bottom-up approach
// - Actions of the parents are overridden by the children
void ActionMap::_PopulateCumulativeActionMap(std::unordered_map<hstring, Model::Command>& actionMap)
{
for (const auto& [cmdID, cmd] : _ActionMap)
Expand Down Expand Up @@ -499,7 +512,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
}

// only add to the _ActionMap if there is an ID
if (auto cmdID = cmd.ID(); !cmdID.empty() && cmd.ActionAndArgs().Action() != ShortcutAction::Invalid)
if (auto cmdID = cmd.ID(); !cmdID.empty())
{
// in the legacy scenario, a user might have several of the same action but only one of them has defined an icon or a name
// eg. { "command": "paste", "name": "myPaste", "keys":"ctrl+a" }
Expand All @@ -514,7 +527,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// if so, we check if there already exists a command with that generated ID, and if there is we port over any name/icon there might be
// (this may cause us to overwrite in scenarios where the user has an existing command that has the same generated ID but
// performs a different action or has different args, but that falls under "play stupid games")
if (cmdImpl->IdWasGenerated())
if (cmdImpl->IDWasGenerated())
{
if (const auto foundCmd{ _GetActionByID(cmdID) })
{
Expand Down Expand Up @@ -611,7 +624,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
if (const auto keyIDPair = _KeyMap.find(keys); keyIDPair != _KeyMap.end())
{
// the keychord is defined in this layer, return the ID (ID be empty, in which case this key is explicitly unbound)
// the keychord is defined in this layer, return the ID
return keyIDPair->second;
}

Expand All @@ -624,7 +637,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
}
}

// we did not find the keychord anywhere, its not bound and not explicity unbound either
// we did not find the keychord anywhere, it's not bound and not explicitly unbound either
return std::nullopt;
}

Expand All @@ -641,18 +654,16 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
if (const auto actionIDOptional = _GetActionIdByKeyChordInternal(keys))
{
if (!(*actionIDOptional).empty())
if (!actionIDOptional->empty())
{
// there is an ID associated with these keys, find the command
if (const auto foundCmd = _GetActionByID(*actionIDOptional))
{
return foundCmd;
}
}
{
// the ID is an empty string, these keys are explicitly unbound
return nullptr;
}
// the ID is an empty string, these keys are explicitly unbound
return nullptr;
}

return std::nullopt;
Expand All @@ -665,7 +676,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// Return Value:
// - the key chord that executes the given action
// - nullptr if the action is not bound to a key chord
Control::KeyChord ActionMap::GetKeyBindingForAction(winrt::hstring cmdID) const
Control::KeyChord ActionMap::GetKeyBindingForAction(const winrt::hstring& cmdID) const
{
// Check our internal state.
if (const auto cmd{ _GetActionByID(cmdID) })
Expand Down
8 changes: 4 additions & 4 deletions src/cascadia/TerminalSettingsModel/ActionMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// queries
Model::Command GetActionByKeyChord(const Control::KeyChord& keys) const;
bool IsKeyChordExplicitlyUnbound(const Control::KeyChord& keys) const;
Control::KeyChord GetKeyBindingForAction(winrt::hstring cmdID) const;
Control::KeyChord GetKeyBindingForAction(const winrt::hstring& cmdID) const;

// population
void AddAction(const Model::Command& cmd);
Expand All @@ -71,7 +71,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
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;
bool FixupsAppliedDuringLoad() const;

// modification
bool RebindKeys(const Control::KeyChord& oldKeys, const Control::KeyChord& newKeys);
Expand All @@ -85,7 +85,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
winrt::Windows::Foundation::Collections::IVector<Model::Command> FilterToSendInput(winrt::hstring currentCommandline);

private:
Model::Command _GetActionByID(const winrt::hstring actionID) const;
Model::Command _GetActionByID(const winrt::hstring& actionID) const;
std::optional<winrt::hstring> _GetActionIdByKeyChordInternal(const Control::KeyChord& keys) const;
std::optional<Model::Command> _GetActionByKeyChordInternal(const Control::KeyChord& keys) const;

Expand All @@ -109,7 +109,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
std::unordered_map<winrt::hstring, Model::Command> _NestedCommands;
std::vector<Model::Command> _IterableCommands;

bool _fixUpsAppliedDuringLoad;
bool _fixupsAppliedDuringLoad{ false };

void _AddKeyBindingHelper(const Json::Value& json, std::vector<SettingsLoadWarnings>& warnings);

Expand Down
6 changes: 3 additions & 3 deletions src/cascadia/TerminalSettingsModel/ActionMapSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
if (jsonBlock.isMember(JsonKey(KeysKey)))
{
// there are keys in this command block - its the legacy style
_fixUpsAppliedDuringLoad = true;
// there are keys in this command block - it's the legacy style
_fixupsAppliedDuringLoad = true;
}

if (origin == OriginTag::User && !jsonBlock.isMember(JsonKey(IDKey)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 do we enforce ID's present for fragment actions now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep! This is as specced (docs will need to be updated as well, I've got a todo item for that once these have been reviewed so I can do it all at once)

{
// there's no ID in this command block - we will generate one for the user
// inform the loader that the ID needs to be written into the json
_fixUpsAppliedDuringLoad = true;
_fixupsAppliedDuringLoad = true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ bool SettingsLoader::FixupUserSettings()
};

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

fixedUp = RemapColorSchemeForProfile(userSettings.baseLayerProfile) || fixedUp;
for (const auto& profile : userSettings.profiles)
Expand Down
6 changes: 3 additions & 3 deletions src/cascadia/TerminalSettingsModel/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
if (const auto generatedID = actionAndArgsImpl->GenerateID(); !generatedID.empty())
{
_ID = generatedID;
_IdWasGenerated = true;
_IDWasGenerated = true;
}
}
}

bool Command::IdWasGenerated()
bool Command::IDWasGenerated()
{
return _IdWasGenerated;
return _IDWasGenerated;
}

void Command::Name(const hstring& value)
Expand Down
4 changes: 2 additions & 2 deletions src/cascadia/TerminalSettingsModel/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

hstring ID() const noexcept;
void GenerateID();
bool IdWasGenerated();
bool IDWasGenerated();

Control::KeyChord Keys() const noexcept;
hstring KeyChordText() const noexcept;
Expand All @@ -97,7 +97,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
std::vector<Control::KeyChord> _keyMappings;
std::optional<std::wstring> _name;
std::wstring _ID;
bool _IdWasGenerated{ false };
bool _IDWasGenerated{ false };
std::optional<std::wstring> _iconPath;
bool _nestedCommand{ false };

Expand Down
10 changes: 5 additions & 5 deletions src/cascadia/TerminalSettingsModel/GlobalAppSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ using namespace winrt::Windows::UI::Xaml;
using namespace ::Microsoft::Console;
using namespace winrt::Microsoft::UI::Xaml::Controls;

static constexpr std::string_view LegacyKeybindingsKey{ "keybindings" };
static constexpr std::string_view KeybindingsKey{ "keybindings" };
static constexpr std::string_view ActionsKey{ "actions" };
static constexpr std::string_view ThemeKey{ "theme" };
static constexpr std::string_view DefaultProfileKey{ "defaultProfile" };
Expand Down Expand Up @@ -158,7 +158,7 @@ void GlobalAppSettings::LayerActionsFrom(const Json::Value& json, const OriginTa
{
// we want to do the keybindings map after the actions map so that we overwrite any leftover keybindings
// that might have existed in the first pass, in case the user did a partial update from legacy to modern
static constexpr std::array bindingsKeys{ ActionsKey, LegacyKeybindingsKey };
static constexpr std::array bindingsKeys{ ActionsKey, KeybindingsKey };
for (const auto& jsonKey : bindingsKeys)
{
if (auto bindings{ json[JsonKey(jsonKey)] })
Expand Down Expand Up @@ -262,14 +262,14 @@ Json::Value GlobalAppSettings::ToJson()
#undef GLOBAL_SETTINGS_TO_JSON

json[JsonKey(ActionsKey)] = _actionMap->ToJson();
json[JsonKey(LegacyKeybindingsKey)] = _actionMap->KeyBindingsToJson();
json[JsonKey(KeybindingsKey)] = _actionMap->KeyBindingsToJson();

return json;
}

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

winrt::Microsoft::Terminal::Settings::Model::Theme GlobalAppSettings::CurrentTheme() noexcept
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalSettingsModel/GlobalAppSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,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();
bool FixupsAppliedDuringLoad();

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

Expand Down
Loading
Loading