mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
[KBM] Prevent keyboard manager crash when index is not found (#6488)
* Added Sleep(5000) for repro * Added check to prevent kbm crash for index not found * Add more safeguards
This commit is contained in:
@@ -104,11 +104,24 @@ void ShortcutControl::AddNewShortcutControlRow(Grid& parent, std::vector<std::ve
|
|||||||
// Get index of targetAppTextBox button
|
// Get index of targetAppTextBox button
|
||||||
UIElementCollection children = parent.Children();
|
UIElementCollection children = parent.Children();
|
||||||
uint32_t index;
|
uint32_t index;
|
||||||
children.IndexOf(targetAppTextBox, index);
|
bool indexFound = children.IndexOf(targetAppTextBox, index);
|
||||||
|
|
||||||
|
// IndexOf could fail if the the row got deleted after LostFocus handler was invoked. In this case it should return
|
||||||
|
if (!indexFound)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t lastIndexInRow = index + ((KeyboardManagerConstants::ShortcutTableColCount - 1) - KeyboardManagerConstants::ShortcutTableTargetAppColIndex);
|
uint32_t lastIndexInRow = index + ((KeyboardManagerConstants::ShortcutTableColCount - 1) - KeyboardManagerConstants::ShortcutTableTargetAppColIndex);
|
||||||
// Calculate row index in the buffer from the grid child index (first set of children are header elements and then three children in each row)
|
// Calculate row index in the buffer from the grid child index (first set of children are header elements and then three children in each row)
|
||||||
int rowIndex = (lastIndexInRow - KeyboardManagerConstants::ShortcutTableHeaderCount) / KeyboardManagerConstants::ShortcutTableColCount;
|
int rowIndex = (lastIndexInRow - KeyboardManagerConstants::ShortcutTableHeaderCount) / KeyboardManagerConstants::ShortcutTableColCount;
|
||||||
|
|
||||||
|
// rowIndex could be out of bounds if the the row got deleted after LostFocus handler was invoked. In this case it should return
|
||||||
|
if (rowIndex >= keyboardRemapControlObjects.size())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Validate both set of drop downs
|
// Validate both set of drop downs
|
||||||
KeyDropDownControl::ValidateShortcutFromDropDownList(parent, keyboardRemapControlObjects[rowIndex][0]->getShortcutControl(), keyboardRemapControlObjects[rowIndex][0]->shortcutDropDownStackPanel.as<StackPanel>(), 0, ShortcutControl::shortcutRemapBuffer, keyboardRemapControlObjects[rowIndex][0]->keyDropDownControlObjects, targetAppTextBox, false, false);
|
KeyDropDownControl::ValidateShortcutFromDropDownList(parent, keyboardRemapControlObjects[rowIndex][0]->getShortcutControl(), keyboardRemapControlObjects[rowIndex][0]->shortcutDropDownStackPanel.as<StackPanel>(), 0, ShortcutControl::shortcutRemapBuffer, keyboardRemapControlObjects[rowIndex][0]->keyDropDownControlObjects, targetAppTextBox, false, false);
|
||||||
KeyDropDownControl::ValidateShortcutFromDropDownList(parent, keyboardRemapControlObjects[rowIndex][1]->getShortcutControl(), keyboardRemapControlObjects[rowIndex][1]->shortcutDropDownStackPanel.as<StackPanel>(), 1, ShortcutControl::shortcutRemapBuffer, keyboardRemapControlObjects[rowIndex][1]->keyDropDownControlObjects, targetAppTextBox, true, false);
|
KeyDropDownControl::ValidateShortcutFromDropDownList(parent, keyboardRemapControlObjects[rowIndex][1]->getShortcutControl(), keyboardRemapControlObjects[rowIndex][1]->shortcutDropDownStackPanel.as<StackPanel>(), 1, ShortcutControl::shortcutRemapBuffer, keyboardRemapControlObjects[rowIndex][1]->keyDropDownControlObjects, targetAppTextBox, true, false);
|
||||||
@@ -164,7 +177,14 @@ void ShortcutControl::AddNewShortcutControlRow(Grid& parent, std::vector<std::ve
|
|||||||
uint32_t index;
|
uint32_t index;
|
||||||
// Get index of delete button
|
// Get index of delete button
|
||||||
UIElementCollection children = parent.Children();
|
UIElementCollection children = parent.Children();
|
||||||
children.IndexOf(currentButton, index);
|
bool indexFound = children.IndexOf(currentButton, index);
|
||||||
|
|
||||||
|
// IndexOf could fail if the the row got deleted and the button handler was invoked twice. In this case it should return
|
||||||
|
if (!indexFound)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t lastIndexInRow = index + ((KeyboardManagerConstants::ShortcutTableColCount - 1) - KeyboardManagerConstants::ShortcutTableRemoveColIndex);
|
uint32_t lastIndexInRow = index + ((KeyboardManagerConstants::ShortcutTableColCount - 1) - KeyboardManagerConstants::ShortcutTableRemoveColIndex);
|
||||||
// Change the row index of elements appearing after the current row, as we will delete the row definition
|
// Change the row index of elements appearing after the current row, as we will delete the row definition
|
||||||
for (uint32_t i = lastIndexInRow + 1; i < children.Size(); i++)
|
for (uint32_t i = lastIndexInRow + 1; i < children.Size(); i++)
|
||||||
|
|||||||
@@ -138,7 +138,14 @@ void SingleKeyRemapControl::AddNewControlKeyRemapRow(Grid& parent, std::vector<s
|
|||||||
uint32_t index;
|
uint32_t index;
|
||||||
// Get index of delete button
|
// Get index of delete button
|
||||||
UIElementCollection children = parent.Children();
|
UIElementCollection children = parent.Children();
|
||||||
children.IndexOf(currentButton, index);
|
bool indexFound = children.IndexOf(currentButton, index);
|
||||||
|
|
||||||
|
// IndexOf could fail if the the row got deleted and the button handler was invoked twice. In this case it should return
|
||||||
|
if (!indexFound)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t lastIndexInRow = index + ((KeyboardManagerConstants::RemapTableColCount - 1) - KeyboardManagerConstants::RemapTableRemoveColIndex);
|
uint32_t lastIndexInRow = index + ((KeyboardManagerConstants::RemapTableColCount - 1) - KeyboardManagerConstants::RemapTableRemoveColIndex);
|
||||||
// Change the row index of elements appearing after the current row, as we will delete the row definition
|
// Change the row index of elements appearing after the current row, as we will delete the row definition
|
||||||
for (uint32_t i = lastIndexInRow + 1; i < children.Size(); i++)
|
for (uint32_t i = lastIndexInRow + 1; i < children.Size(); i++)
|
||||||
|
|||||||
Reference in New Issue
Block a user