Skip to content

Commit

Permalink
[Keyboard Manager] Fix crash in Keyboard Manager by ignoring MapToSam…
Browse files Browse the repository at this point in the history
…eKey warnings on loading (#6714)

* Add extra argument to ignore MapToSameKey error for Key To Shortcut mapping

* added comment
  • Loading branch information
arjunbalgovind committed Sep 22, 2020
1 parent c1e9f2d commit 1ef4c43
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
28 changes: 25 additions & 3 deletions src/modules/keyboardmanager/ui/KeyDropDownControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ std::pair<KeyboardManagerHelper::ErrorType, int> KeyDropDownControl::ValidateSho
parent.UpdateLayout();
}

// If ignore key to shortcut warning flag is true and it is a hybrid control in SingleKeyRemapControl, then skip MapToSameKey error
if (isHybridControl && isSingleKeyWindow && ignoreKeyToShortcutWarning && (validationResult.first == KeyboardManagerHelper::ErrorType::MapToSameKey))
{
validationResult.first = KeyboardManagerHelper::ErrorType::NoError;
}

// If the remapping is invalid display an error message
if (validationResult.first != KeyboardManagerHelper::ErrorType::NoError)
{
SetDropDownError(currentDropDown, KeyboardManagerHelper::GetErrorMessage(validationResult.first));
Expand All @@ -170,6 +177,12 @@ std::pair<KeyboardManagerHelper::ErrorType, int> KeyDropDownControl::ValidateSho
}
}

// Reset ignoreKeyToShortcutWarning
if (ignoreKeyToShortcutWarning)
{
ignoreKeyToShortcutWarning = false;
}

return std::make_pair(validationResult.first, rowIndex);
}

Expand Down Expand Up @@ -268,9 +281,9 @@ ComboBox KeyDropDownControl::GetComboBox()
}

// Function to add a drop down to the shortcut stack panel
void KeyDropDownControl::AddDropDown(Grid table, StackPanel shortcutControl, StackPanel parent, const int colIndex, RemapBuffer& shortcutRemapBuffer, std::vector<std::unique_ptr<KeyDropDownControl>>& keyDropDownControlObjects, TextBox targetApp, bool isHybridControl, bool isSingleKeyWindow)
void KeyDropDownControl::AddDropDown(Grid table, StackPanel shortcutControl, StackPanel parent, const int colIndex, RemapBuffer& shortcutRemapBuffer, std::vector<std::unique_ptr<KeyDropDownControl>>& keyDropDownControlObjects, TextBox targetApp, bool isHybridControl, bool isSingleKeyWindow, bool ignoreWarning)
{
keyDropDownControlObjects.push_back(std::move(std::unique_ptr<KeyDropDownControl>(new KeyDropDownControl(true))));
keyDropDownControlObjects.push_back(std::move(std::unique_ptr<KeyDropDownControl>(new KeyDropDownControl(true, ignoreWarning))));
parent.Children().Append(keyDropDownControlObjects[keyDropDownControlObjects.size() - 1]->GetComboBox());
keyDropDownControlObjects[keyDropDownControlObjects.size() - 1]->SetSelectionHandler(table, shortcutControl, parent, colIndex, shortcutRemapBuffer, keyDropDownControlObjects, targetApp, isHybridControl, isSingleKeyWindow);
parent.UpdateLayout();
Expand Down Expand Up @@ -342,7 +355,16 @@ void KeyDropDownControl::AddShortcutToControl(Shortcut shortcut, Grid table, Sta
std::vector<DWORD> keyCodeList = keyboardManagerState.keyboardMap.GetKeyCodeList(true);
if (shortcutKeyCodes.size() != 0)
{
KeyDropDownControl::AddDropDown(table, controlLayout, parent, colIndex, remapBuffer, keyDropDownControlObjects, targetApp, isHybridControl, isSingleKeyWindow);
bool ignoreWarning = false;

// If more than one key is to be added, ignore a shortcut to key warning on partially entering the remapping
if (shortcutKeyCodes.size() > 1)
{
ignoreWarning = true;
}

KeyDropDownControl::AddDropDown(table, controlLayout, parent, colIndex, remapBuffer, keyDropDownControlObjects, targetApp, isHybridControl, isSingleKeyWindow, ignoreWarning);

for (int i = 0; i < shortcutKeyCodes.size(); i++)
{
// New drop down gets added automatically when the SelectedIndex is set
Expand Down
7 changes: 5 additions & 2 deletions src/modules/keyboardmanager/ui/KeyDropDownControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class KeyDropDownControl
winrt::Windows::Foundation::IInspectable warningMessage;
// Stores the flyout attached to the current drop down
winrt::Windows::Foundation::IInspectable warningFlyout;
// Stores whether a key to shortcut warning has to be ignored
bool ignoreKeyToShortcutWarning;

// Function to set properties apart from the SelectionChanged event handler
void SetDefaultProperties(bool isShortcut);
Expand All @@ -53,7 +55,8 @@ class KeyDropDownControl
static KeyboardManagerState* keyboardManagerState;

// Constructor - the last default parameter should be passed as false only if it originates from Type shortcut or when an old shortcut is reloaded
KeyDropDownControl(bool isShortcut)
KeyDropDownControl(bool isShortcut, bool fromAddShortcutToControl = false) :
ignoreKeyToShortcutWarning(fromAddShortcutToControl)
{
SetDefaultProperties(isShortcut);
}
Expand All @@ -74,7 +77,7 @@ class KeyDropDownControl
ComboBox GetComboBox();

// Function to add a drop down to the shortcut stack panel
static void AddDropDown(winrt::Windows::UI::Xaml::Controls::Grid table, winrt::Windows::UI::Xaml::Controls::StackPanel shortcutControl, winrt::Windows::UI::Xaml::Controls::StackPanel parent, const int colIndex, RemapBuffer& shortcutRemapBuffer, std::vector<std::unique_ptr<KeyDropDownControl>>& keyDropDownControlObjects, winrt::Windows::UI::Xaml::Controls::TextBox targetApp, bool isHybridControl, bool isSingleKeyWindow);
static void AddDropDown(winrt::Windows::UI::Xaml::Controls::Grid table, winrt::Windows::UI::Xaml::Controls::StackPanel shortcutControl, winrt::Windows::UI::Xaml::Controls::StackPanel parent, const int colIndex, RemapBuffer& shortcutRemapBuffer, std::vector<std::unique_ptr<KeyDropDownControl>>& keyDropDownControlObjects, winrt::Windows::UI::Xaml::Controls::TextBox targetApp, bool isHybridControl, bool isSingleKeyWindow, bool ignoreWarning = false);

// Function to get the list of key codes from the shortcut combo box stack panel
static std::vector<int32_t> GetSelectedIndicesFromStackPanel(StackPanel parent);
Expand Down

0 comments on commit 1ef4c43

Please sign in to comment.