Improve warnings for KBM (dev/build-features) (#2386)

* Handled invalid input for single key remaps

* Added some functions

* Added better error message functions and finished warnings for edit keyboard

* Updated dropdown order of keys

* Added new warning template for shortcuts

* Added show warning on Apply code

* Removed old flyout code

* Fixed issue where tooltips were replaced on Apply

* Simplified == operator
This commit is contained in:
Arjun Balgovind
2020-04-26 15:09:40 -07:00
committed by GitHub
parent 7ec8d02c1f
commit 38ecc82e97
15 changed files with 439 additions and 125 deletions

View File

@@ -65,7 +65,7 @@ namespace KeyboardManagerHelper
}
// Function to return if the key is an extended key which requires the use of the extended key flag
bool isExtendedKey(DWORD key)
bool IsExtendedKey(DWORD key)
{
switch (key)
{
@@ -79,6 +79,7 @@ namespace KeyboardManagerHelper
return false;
}
}
Collections::IVector<IInspectable> ToBoxValue(const std::vector<std::wstring>& list)
{
Collections::IVector<IInspectable> boxList = single_threaded_vector<IInspectable>();
@@ -89,4 +90,73 @@ namespace KeyboardManagerHelper
return boxList;
}
// Function to check if two keys are equal or cover the same set of keys. Return value depends on type of overlap
ErrorType DoKeysOverlap(DWORD first, DWORD second)
{
// If the keys are same
if (first == second)
{
return ErrorType::SameKeyPreviouslyMapped;
}
else if ((GetKeyType(first) == GetKeyType(second)) && GetKeyType(first) != KeyType::Action)
{
// If the keys are of the same modifier type and overlapping, i.e. one is L/R and other is common
if ((first == VK_LWIN && second == VK_RWIN) || (first == VK_LCONTROL && second == VK_RCONTROL) || (first == VK_LMENU && second == VK_RMENU) || (first == VK_LSHIFT && second == VK_RSHIFT))
{
return ErrorType::NoError;
}
else
{
return ErrorType::ConflictingModifierKey;
}
}
// If no overlap
else
{
return ErrorType::NoError;
}
}
// Function to return the error message
winrt::hstring GetErrorMessage(ErrorType errorType)
{
switch (errorType)
{
case ErrorType::NoError:
return L"Remapping successful";
case ErrorType::SameKeyPreviouslyMapped:
return L"Cannot remap a key more than once";
case ErrorType::MapToSameKey:
return L"Cannot remap a key to itself";
case ErrorType::ConflictingModifierKey:
return L"Cannot remap this key as it conflicts with another remapped key";
case ErrorType::SameShortcutPreviouslyMapped:
return L"Cannot remap a shortcut more than once";
case ErrorType::MapToSameShortcut:
return L"Cannot remap a shortcut to itself";
case ErrorType::ConflictingModifierShortcut:
return L"Cannot remap this shortcut as it conflicts with another remapped shortcut";
case ErrorType::WinL:
return L"Cannot remap from/to Win L";
case ErrorType::CtrlAltDel:
return L"Cannot remap from/to Ctrl Alt Del";
case ErrorType::RemapUnsuccessful:
return L"Some remappings were not applied";
case ErrorType::SaveFailed:
return L"Failed to save the remappings";
case ErrorType::MissingKey:
return L"Incomplete remapping";
case ErrorType::ShortcutStartWithModifier:
return L"Shortcut must start with a modifier key";
case ErrorType::ShortcutCannotHaveRepeatedModifier:
return L"Shortcut cannot contain a repeated modifier";
case ErrorType::ShortcutAtleast2Keys:
return L"Shortcut must have atleast 2 keys";
case ErrorType::ShortcutOneActionKey:
return L"Shortcut must contain an action key";
case ErrorType::ShortcutNotMoreThanOneActionKey:
return L"Shortcut cannot have more than one action key";
}
}
}

View File

@@ -15,6 +15,28 @@ namespace KeyboardManagerHelper
Action
};
// Type to store codes for different errors
enum class ErrorType
{
NoError,
SameKeyPreviouslyMapped,
MapToSameKey,
ConflictingModifierKey,
SameShortcutPreviouslyMapped,
MapToSameShortcut,
ConflictingModifierShortcut,
WinL,
CtrlAltDel,
RemapUnsuccessful,
SaveFailed,
MissingKey,
ShortcutStartWithModifier,
ShortcutCannotHaveRepeatedModifier,
ShortcutAtleast2Keys,
ShortcutOneActionKey,
ShortcutNotMoreThanOneActionKey
};
// Function to split a wstring based on a delimiter and return a vector of split strings
std::vector<std::wstring> splitwstring(const std::wstring& input, wchar_t delimiter);
@@ -22,7 +44,7 @@ namespace KeyboardManagerHelper
winrt::Windows::Foundation::IInspectable getSiblingElement(winrt::Windows::Foundation::IInspectable const& element);
// Function to return if the key is an extended key which requires the use of the extended key flag
bool isExtendedKey(DWORD key);
bool IsExtendedKey(DWORD key);
// Function to check if the key is a modifier key
bool IsModifierKey(DWORD key);
@@ -30,8 +52,11 @@ namespace KeyboardManagerHelper
// Function to get the type of the key
KeyType GetKeyType(DWORD key);
// Function to return if the key is an extended key which requires the use of the extended key flag
bool isExtendedKey(DWORD key);
// Function to check if two keys are equal or cover the same set of keys. Return value depends on type of overlap
ErrorType DoKeysOverlap(DWORD first, DWORD second);
// Function to return the error message
winrt::hstring GetErrorMessage(ErrorType errorType);
// Function to return the list of key name in the order for the drop down based on the key codes
winrt::Windows::Foundation::Collections::IVector<winrt::Windows::Foundation::IInspectable> ToBoxValue(const std::vector<std::wstring>& list);

View File

@@ -35,4 +35,7 @@ namespace KeyboardManagerConstants
// Name of the dummy update file.
inline const std::wstring DummyUpdateFileName = L"settings-updated.json";
// Initial value for tooltip
inline const winrt::hstring ToolTipInitialContent = L"Initialised";
}

View File

@@ -764,3 +764,56 @@ int Shortcut::GetCommonModifiersCount(const Shortcut& input) const
return commonElements;
}
// Function to check if the two shortcuts are equal or cover the same set of keys. Return value depends on type of overlap
KeyboardManagerHelper::ErrorType Shortcut::DoKeysOverlap(const Shortcut& first, const Shortcut& second)
{
if (first.IsValidShortcut() && second.IsValidShortcut())
{
// If the shortcuts are equal
if (first == second)
{
return KeyboardManagerHelper::ErrorType::SameShortcutPreviouslyMapped;
}
// If both have win key modifiers and one is the both version then there will be an overlap
else if (first.winKey != ModifierKey::Disabled && second.winKey != ModifierKey::Disabled && (first.winKey == ModifierKey::Both || second.winKey == ModifierKey::Both))
{
return KeyboardManagerHelper::ErrorType::ConflictingModifierShortcut;
}
// If both have ctrl key modifiers and one is the both version then there will be an overlap
else if (first.ctrlKey != ModifierKey::Disabled && second.ctrlKey != ModifierKey::Disabled && (first.ctrlKey == ModifierKey::Both || second.ctrlKey == ModifierKey::Both))
{
return KeyboardManagerHelper::ErrorType::ConflictingModifierShortcut;
}
// If both have alt key modifiers and one is the both version then there will be an overlap
else if (first.altKey != ModifierKey::Disabled && second.altKey != ModifierKey::Disabled && (first.altKey == ModifierKey::Both || second.altKey == ModifierKey::Both))
{
return KeyboardManagerHelper::ErrorType::ConflictingModifierShortcut;
}
// If both have shift key modifiers and one is the both version then there will be an overlap
else if (first.shiftKey != ModifierKey::Disabled && second.shiftKey != ModifierKey::Disabled && (first.shiftKey == ModifierKey::Both || second.shiftKey == ModifierKey::Both))
{
return KeyboardManagerHelper::ErrorType::ConflictingModifierShortcut;
}
}
return KeyboardManagerHelper::ErrorType::NoError;
}
// Function to check if the shortcut is illegal (i.e. Win+L or Ctrl+Alt+Del)
KeyboardManagerHelper::ErrorType Shortcut::IsShortcutIllegal() const
{
// Win+L
if (winKey != ModifierKey::Disabled && ctrlKey == ModifierKey::Disabled && altKey == ModifierKey::Disabled && shiftKey == ModifierKey::Disabled && actionKey == 0x4C)
{
return KeyboardManagerHelper::ErrorType::WinL;
}
// Ctrl+Alt+Del
if (winKey == ModifierKey::Disabled && ctrlKey != ModifierKey::Disabled && altKey != ModifierKey::Disabled && shiftKey == ModifierKey::Disabled && actionKey == VK_DELETE)
{
return KeyboardManagerHelper::ErrorType::CtrlAltDel;
}
return KeyboardManagerHelper::ErrorType::NoError;
}

View File

@@ -41,6 +41,12 @@ public:
}
}
// == operator
inline bool operator==(const Shortcut& sc) const
{
return (winKey == sc.winKey && ctrlKey == sc.ctrlKey && altKey == sc.altKey && shiftKey == sc.shiftKey && actionKey == sc.actionKey);
}
// Less than operator must be defined to use with std::map.
inline bool operator<(const Shortcut& sc) const
{
@@ -168,4 +174,10 @@ public:
// Function to get the number of modifiers that are common between the current shortcut and the shortcut in the argument
int GetCommonModifiersCount(const Shortcut& input) const;
// Function to check if the two shortcuts are equal or cover the same set of keys. Return value depends on type of overlap
static KeyboardManagerHelper::ErrorType DoKeysOverlap(const Shortcut& first, const Shortcut& second);
// Function to check if the shortcut is illegal (i.e. Win+L or Ctrl+Alt+Del)
KeyboardManagerHelper::ErrorType IsShortcutIllegal() const;
};