[KBM] decoupling editor and engine (#11133)

This commit is contained in:
Mykhailo Pylyp
2021-05-07 11:16:31 +03:00
committed by GitHub
parent 9461909321
commit 8785fca309
77 changed files with 2509 additions and 2775 deletions

View File

@@ -2,17 +2,20 @@
#include "BufferValidationHelpers.h"
#include <common/interop/shared_constants.h>
#include <keyboardmanager/common/KeyboardManagerConstants.h>
#include <KeyboardManagerEditorStrings.h>
#include <KeyboardManagerConstants.h>
#include <KeyDropDownControl.h>
#include "KeyboardManagerEditorStrings.h"
#include "KeyDropDownControl.h"
#include "UIHelpers.h"
#include "EditorHelpers.h"
#include "EditorConstants.h"
namespace BufferValidationHelpers
{
// Function to validate and update an element of the key remap buffer when the selection has changed
KeyboardManagerHelper::ErrorType ValidateAndUpdateKeyBufferElement(int rowIndex, int colIndex, int selectedKeyCode, RemapBuffer& remapBuffer)
ShortcutErrorType ValidateAndUpdateKeyBufferElement(int rowIndex, int colIndex, int selectedKeyCode, RemapBuffer& remapBuffer)
{
KeyboardManagerHelper::ErrorType errorType = KeyboardManagerHelper::ErrorType::NoError;
ShortcutErrorType errorType = ShortcutErrorType::NoError;
// Check if the element was not found or the index exceeds the known keys
if (selectedKeyCode != -1)
@@ -22,13 +25,13 @@ namespace BufferValidationHelpers
{
if (std::get<DWORD>(remapBuffer[rowIndex].first[std::abs(int(colIndex) - 1)]) == selectedKeyCode)
{
errorType = KeyboardManagerHelper::ErrorType::MapToSameKey;
errorType = ShortcutErrorType::MapToSameKey;
}
}
// If one column is shortcut and other is key no warning required
if (errorType == KeyboardManagerHelper::ErrorType::NoError && colIndex == 0)
if (errorType == ShortcutErrorType::NoError && colIndex == 0)
{
// Check if the key is already remapped to something else
for (int i = 0; i < remapBuffer.size(); i++)
@@ -37,8 +40,8 @@ namespace BufferValidationHelpers
{
if (remapBuffer[i].first[colIndex].index() == 0)
{
KeyboardManagerHelper::ErrorType result = KeyboardManagerHelper::DoKeysOverlap(std::get<DWORD>(remapBuffer[i].first[colIndex]), selectedKeyCode);
if (result != KeyboardManagerHelper::ErrorType::NoError)
ShortcutErrorType result = EditorHelpers::DoKeysOverlap(std::get<DWORD>(remapBuffer[i].first[colIndex]), selectedKeyCode);
if (result != ShortcutErrorType::NoError)
{
errorType = result;
break;
@@ -51,7 +54,7 @@ namespace BufferValidationHelpers
}
// If there is no error, set the buffer
if (errorType == KeyboardManagerHelper::ErrorType::NoError)
if (errorType == ShortcutErrorType::NoError)
{
remapBuffer[rowIndex].first[colIndex] = selectedKeyCode;
}
@@ -70,32 +73,32 @@ namespace BufferValidationHelpers
}
// Function to validate an element of the shortcut remap buffer when the selection has changed
std::pair<KeyboardManagerHelper::ErrorType, DropDownAction> ValidateShortcutBufferElement(int rowIndex, int colIndex, uint32_t dropDownIndex, const std::vector<int32_t>& selectedCodes, std::wstring appName, bool isHybridControl, const RemapBuffer& remapBuffer, bool dropDownFound)
std::pair<ShortcutErrorType, DropDownAction> ValidateShortcutBufferElement(int rowIndex, int colIndex, uint32_t dropDownIndex, const std::vector<int32_t>& selectedCodes, std::wstring appName, bool isHybridControl, const RemapBuffer& remapBuffer, bool dropDownFound)
{
BufferValidationHelpers::DropDownAction dropDownAction = BufferValidationHelpers::DropDownAction::NoAction;
KeyboardManagerHelper::ErrorType errorType = KeyboardManagerHelper::ErrorType::NoError;
ShortcutErrorType errorType = ShortcutErrorType::NoError;
size_t dropDownCount = selectedCodes.size();
DWORD selectedKeyCode = dropDownFound ? selectedCodes[dropDownIndex] : -1;
if (selectedKeyCode != -1 && dropDownFound)
{
// If only 1 drop down and action key is chosen: Warn that a modifier must be chosen (if the drop down is not for a hybrid scenario)
if (dropDownCount == 1 && !KeyboardManagerHelper::IsModifierKey(selectedKeyCode) && !isHybridControl)
if (dropDownCount == 1 && !Helpers::IsModifierKey(selectedKeyCode) && !isHybridControl)
{
// warn and reset the drop down
errorType = KeyboardManagerHelper::ErrorType::ShortcutStartWithModifier;
errorType = ShortcutErrorType::ShortcutStartWithModifier;
}
else if (dropDownIndex == dropDownCount - 1)
{
// If it is the last drop down
// If last drop down and a modifier is selected: add a new drop down (max drop down count should be enforced)
if (KeyboardManagerHelper::IsModifierKey(selectedKeyCode) && dropDownCount < KeyboardManagerConstants::MaxShortcutSize)
if (Helpers::IsModifierKey(selectedKeyCode) && dropDownCount < EditorConstants::MaxShortcutSize)
{
// If it matched any of the previous modifiers then reset that drop down
if (KeyboardManagerHelper::CheckRepeatedModifier(selectedCodes, selectedKeyCode))
if (EditorHelpers::CheckRepeatedModifier(selectedCodes, selectedKeyCode))
{
// warn and reset the drop down
errorType = KeyboardManagerHelper::ErrorType::ShortcutCannotHaveRepeatedModifier;
errorType = ShortcutErrorType::ShortcutCannotHaveRepeatedModifier;
}
else
{
@@ -103,17 +106,17 @@ namespace BufferValidationHelpers
dropDownAction = BufferValidationHelpers::DropDownAction::AddDropDown;
}
}
else if (KeyboardManagerHelper::IsModifierKey(selectedKeyCode) && dropDownCount >= KeyboardManagerConstants::MaxShortcutSize)
else if (Helpers::IsModifierKey(selectedKeyCode) && dropDownCount >= EditorConstants::MaxShortcutSize)
{
// If last drop down and a modifier is selected but there are already max drop downs: warn the user
// warn and reset the drop down
errorType = KeyboardManagerHelper::ErrorType::ShortcutMaxShortcutSizeOneActionKey;
errorType = ShortcutErrorType::ShortcutMaxShortcutSizeOneActionKey;
}
else if (selectedKeyCode == 0)
{
// If None is selected but it's the last index: warn
// If it is a hybrid control and there are 2 drop downs then deletion is allowed
if (isHybridControl && dropDownCount == KeyboardManagerConstants::MinShortcutSize)
if (isHybridControl && dropDownCount == EditorConstants::MinShortcutSize)
{
// set delete drop down flag
dropDownAction = BufferValidationHelpers::DropDownAction::DeleteDropDown;
@@ -122,40 +125,40 @@ namespace BufferValidationHelpers
else
{
// warn and reset the drop down
errorType = KeyboardManagerHelper::ErrorType::ShortcutOneActionKey;
errorType = ShortcutErrorType::ShortcutOneActionKey;
}
}
else if (selectedKeyCode == CommonSharedConstants::VK_DISABLED && dropDownIndex)
{
// Disable can not be selected if one modifier key has already been selected
errorType = KeyboardManagerHelper::ErrorType::ShortcutDisableAsActionKey;
errorType = ShortcutErrorType::ShortcutDisableAsActionKey;
}
// If none of the above, then the action key will be set
}
else
{
// If it is not the last drop down
if (KeyboardManagerHelper::IsModifierKey(selectedKeyCode))
if (Helpers::IsModifierKey(selectedKeyCode))
{
// If it matched any of the previous modifiers then reset that drop down
if (KeyboardManagerHelper::CheckRepeatedModifier(selectedCodes, selectedKeyCode))
if (EditorHelpers::CheckRepeatedModifier(selectedCodes, selectedKeyCode))
{
// warn and reset the drop down
errorType = KeyboardManagerHelper::ErrorType::ShortcutCannotHaveRepeatedModifier;
errorType = ShortcutErrorType::ShortcutCannotHaveRepeatedModifier;
}
// If not, the modifier key will be set
}
else if (selectedKeyCode == 0 && dropDownCount > KeyboardManagerConstants::MinShortcutSize)
else if (selectedKeyCode == 0 && dropDownCount > EditorConstants::MinShortcutSize)
{
// If None is selected and there are more than 2 drop downs
// set delete drop down flag
dropDownAction = BufferValidationHelpers::DropDownAction::DeleteDropDown;
// do not delete the drop down now since there may be some other error which would cause the drop down to be invalid after removal
}
else if (selectedKeyCode == 0 && dropDownCount <= KeyboardManagerConstants::MinShortcutSize)
else if (selectedKeyCode == 0 && dropDownCount <= EditorConstants::MinShortcutSize)
{
// If it is a hybrid control and there are 2 drop downs then deletion is allowed
if (isHybridControl && dropDownCount == KeyboardManagerConstants::MinShortcutSize)
if (isHybridControl && dropDownCount == EditorConstants::MinShortcutSize)
{
// set delete drop down flag
dropDownAction = BufferValidationHelpers::DropDownAction::DeleteDropDown;
@@ -164,13 +167,13 @@ namespace BufferValidationHelpers
else
{
// warn and reset the drop down
errorType = KeyboardManagerHelper::ErrorType::ShortcutAtleast2Keys;
errorType = ShortcutErrorType::ShortcutAtleast2Keys;
}
}
else if (selectedKeyCode == CommonSharedConstants::VK_DISABLED && dropDownIndex)
{
// Allow selection of VK_DISABLE only in first dropdown
errorType = KeyboardManagerHelper::ErrorType::ShortcutDisableAsActionKey;
errorType = ShortcutErrorType::ShortcutDisableAsActionKey;
}
else if (dropDownIndex != 0 || isHybridControl)
{
@@ -193,20 +196,20 @@ namespace BufferValidationHelpers
else
{
// warn and reset the drop down
errorType = KeyboardManagerHelper::ErrorType::ShortcutNotMoreThanOneActionKey;
errorType = ShortcutErrorType::ShortcutNotMoreThanOneActionKey;
}
}
else
{
// If there an action key is chosen on the first drop down and there are more than one drop down menus
// warn and reset the drop down
errorType = KeyboardManagerHelper::ErrorType::ShortcutStartWithModifier;
errorType = ShortcutErrorType::ShortcutStartWithModifier;
}
}
}
// After validating the shortcut, now for errors like remap to same shortcut, remap shortcut more than once, Win L and Ctrl Alt Del
if (errorType == KeyboardManagerHelper::ErrorType::NoError)
if (errorType == ShortcutErrorType::NoError)
{
KeyShortcutUnion tempShortcut;
if (isHybridControl && KeyDropDownControl::GetNumberOfSelectedKeys(selectedCodes) == 1)
@@ -234,9 +237,10 @@ namespace BufferValidationHelpers
// If shortcut to shortcut
if (remapBuffer[rowIndex].first[std::abs(int(colIndex) - 1)].index() == 1)
{
if (std::get<Shortcut>(remapBuffer[rowIndex].first[std::abs(int(colIndex) - 1)]) == std::get<Shortcut>(tempShortcut) && std::get<Shortcut>(remapBuffer[rowIndex].first[std::abs(int(colIndex) - 1)]).IsValidShortcut() && std::get<Shortcut>(tempShortcut).IsValidShortcut())
auto& shortcut = std::get<Shortcut>(remapBuffer[rowIndex].first[std::abs(int(colIndex) - 1)]);
if (shortcut == std::get<Shortcut>(tempShortcut) && EditorHelpers::IsValidShortcut(shortcut) && EditorHelpers::IsValidShortcut(std::get<Shortcut>(tempShortcut)))
{
errorType = KeyboardManagerHelper::ErrorType::MapToSameShortcut;
errorType = ShortcutErrorType::MapToSameShortcut;
}
}
@@ -249,14 +253,14 @@ namespace BufferValidationHelpers
{
if (std::get<DWORD>(remapBuffer[rowIndex].first[std::abs(int(colIndex) - 1)]) == std::get<DWORD>(tempShortcut) && std::get<DWORD>(remapBuffer[rowIndex].first[std::abs(int(colIndex) - 1)]) != NULL && std::get<DWORD>(tempShortcut) != NULL)
{
errorType = KeyboardManagerHelper::ErrorType::MapToSameKey;
errorType = ShortcutErrorType::MapToSameKey;
}
}
// If one column is shortcut and other is key no warning required
}
if (errorType == KeyboardManagerHelper::ErrorType::NoError && colIndex == 0)
if (errorType == ShortcutErrorType::NoError && colIndex == 0)
{
// Check if the key is already remapped to something else for the same target app
for (int i = 0; i < remapBuffer.size(); i++)
@@ -266,10 +270,10 @@ namespace BufferValidationHelpers
if (i != rowIndex && currAppName == appName)
{
KeyboardManagerHelper::ErrorType result = KeyboardManagerHelper::ErrorType::NoError;
ShortcutErrorType result = ShortcutErrorType::NoError;
if (!isHybridControl)
{
result = Shortcut::DoKeysOverlap(std::get<Shortcut>(remapBuffer[i].first[colIndex]), std::get<Shortcut>(tempShortcut));
result = EditorHelpers::DoShortcutsOverlap(std::get<Shortcut>(remapBuffer[i].first[colIndex]), std::get<Shortcut>(tempShortcut));
}
else
{
@@ -277,19 +281,20 @@ namespace BufferValidationHelpers
{
if (std::get<DWORD>(tempShortcut) != NULL && std::get<DWORD>(remapBuffer[i].first[colIndex]) != NULL)
{
result = KeyboardManagerHelper::DoKeysOverlap(std::get<DWORD>(remapBuffer[i].first[colIndex]), std::get<DWORD>(tempShortcut));
result = EditorHelpers::DoKeysOverlap(std::get<DWORD>(remapBuffer[i].first[colIndex]), std::get<DWORD>(tempShortcut));
}
}
else if (tempShortcut.index() == 1 && remapBuffer[i].first[colIndex].index() == 1)
{
if (std::get<Shortcut>(tempShortcut).IsValidShortcut() && std::get<Shortcut>(remapBuffer[i].first[colIndex]).IsValidShortcut())
auto& shortcut = std::get<Shortcut>(remapBuffer[i].first[colIndex]);
if (EditorHelpers::IsValidShortcut(std::get<Shortcut>(tempShortcut)) && EditorHelpers::IsValidShortcut(shortcut))
{
result = Shortcut::DoKeysOverlap(std::get<Shortcut>(remapBuffer[i].first[colIndex]), std::get<Shortcut>(tempShortcut));
result = EditorHelpers::DoShortcutsOverlap(std::get<Shortcut>(remapBuffer[i].first[colIndex]), std::get<Shortcut>(tempShortcut));
}
}
// Other scenarios not possible since key to shortcut is with key to key, and shortcut to key is with shortcut to shortcut
}
if (result != KeyboardManagerHelper::ErrorType::NoError)
if (result != ShortcutErrorType::NoError)
{
errorType = result;
break;
@@ -298,9 +303,9 @@ namespace BufferValidationHelpers
}
}
if (errorType == KeyboardManagerHelper::ErrorType::NoError && tempShortcut.index() == 1)
if (errorType == ShortcutErrorType::NoError && tempShortcut.index() == 1)
{
errorType = std::get<Shortcut>(tempShortcut).IsShortcutIllegal();
errorType = EditorHelpers::IsShortcutIllegal(std::get<Shortcut>(tempShortcut));
}
}