Refactor Shortcut remaps to distinguish modifiers and action keys (#1927)

* Added unique lock mutexes for thread safety

* Fixed a bug in detect key logic

* Changed dword to word

* Added early unlock statements to fix issue with shortcut guide

* Fixed type conversion warnings

* Migrated detect shortcut window to use Shortcut class

* made changes in Apply button logic

* Revert thread safety changes

* refactored code works on the UI side

* Refactored remapping code to use new Shortcut class

* Refactored to SetKeyEvent function

* Moved function to cpp file and added more comments

* Refactored map variable and handled common Win key

* Remove debug code

* Change arguments to const references
This commit is contained in:
Arjun Balgovind
2020-04-08 09:11:58 -07:00
committed by Udit Singh
parent cf54a4919b
commit 52c12731cb
14 changed files with 1261 additions and 304 deletions

View File

@@ -133,17 +133,17 @@ void createEditShortcutsWindow(HINSTANCE hInst, KeyboardManagerState& keyboardMa
for (unsigned int i = 1; i < shortcutTable.Children().Size(); i++)
{
StackPanel currentRow = shortcutTable.Children().GetAt(i).as<StackPanel>();
hstring originalShortcut = currentRow.Children().GetAt(0).as<StackPanel>().Children().GetAt(1).as<TextBlock>().Text();
hstring newShortcut = currentRow.Children().GetAt(1).as<StackPanel>().Children().GetAt(1).as<TextBlock>().Text();
if (!originalShortcut.empty() && !newShortcut.empty())
hstring originalShortcutText = currentRow.Children().GetAt(0).as<StackPanel>().Children().GetAt(1).as<TextBlock>().Text();
hstring newShortcutText = currentRow.Children().GetAt(1).as<StackPanel>().Children().GetAt(1).as<TextBlock>().Text();
if (!originalShortcutText.empty() && !newShortcutText.empty())
{
std::vector<DWORD> originalKeys = convertWStringVectorToIntegerVector<DWORD>(splitwstring(originalShortcut.c_str(), L' '));
std::vector<WORD> newKeys = convertWStringVectorToIntegerVector<WORD>(splitwstring(newShortcut.c_str(), L' '));
Shortcut originalShortcut = Shortcut::CreateShortcut(originalShortcutText);
Shortcut newShortcut = Shortcut::CreateShortcut(newShortcutText);
// Shortcut should consist of atleast two keys
if (originalKeys.size() > 1 && newKeys.size() > 1)
// Shortcut should be valid
if (originalShortcut.IsValidShortcut() && originalShortcut.IsValidShortcut())
{
bool result = keyboardManagerState.AddOSLevelShortcut(originalKeys, newKeys);
bool result = keyboardManagerState.AddOSLevelShortcut(originalShortcut, newShortcut);
if (!result)
{
isSuccess = false;
@@ -186,7 +186,7 @@ void createEditShortcutsWindow(HINSTANCE hInst, KeyboardManagerState& keyboardMa
std::unique_lock<std::mutex> lock(keyboardManagerState.osLevelShortcutReMap_mutex);
for (const auto& it : keyboardManagerState.osLevelShortcutReMap)
{
ShortcutControl::AddNewShortcutControlRow(shortcutTable, it.first, it.second.first);
ShortcutControl::AddNewShortcutControlRow(shortcutTable, it.first, it.second.targetShortcut);
}
lock.unlock();

View File

@@ -6,7 +6,7 @@ HWND ShortcutControl::EditShortcutsWindowHandle = nullptr;
KeyboardManagerState* ShortcutControl::keyboardManagerState = nullptr;
// Function to add a new row to the shortcut table. If the originalKeys and newKeys args are provided, then the displayed shortcuts are set to those values.
void ShortcutControl::AddNewShortcutControlRow(StackPanel& parent, const std::vector<DWORD>& originalKeys, const std::vector<WORD>& newKeys)
void ShortcutControl::AddNewShortcutControlRow(StackPanel& parent, const Shortcut& originalKeys, const Shortcut& newKeys)
{
// Parent element for the row
Windows::UI::Xaml::Controls::StackPanel tableRow;
@@ -23,10 +23,10 @@ void ShortcutControl::AddNewShortcutControlRow(StackPanel& parent, const std::ve
tableRow.Children().Append(newSC.getShortcutControl());
// Set the shortcut text if the two vectors are not empty (i.e. default args)
if (!originalKeys.empty() && !newKeys.empty())
if (!originalKeys.IsEmpty() && !newKeys.IsEmpty())
{
originalSC.shortcutText.Text(convertVectorToHstring<DWORD>(originalKeys));
newSC.shortcutText.Text(convertVectorToHstring<WORD>(newKeys));
originalSC.shortcutText.Text(originalKeys.ToHstring());
newSC.shortcutText.Text(newKeys.ToHstring());
}
// Delete row button
@@ -71,8 +71,8 @@ void ShortcutControl::createDetectShortcutWindow(IInspectable const& sender, Xam
// OK button
detectShortcutBox.PrimaryButtonClick([=, &keyboardManagerState](Windows::UI::Xaml::Controls::ContentDialog const& sender, ContentDialogButtonClickEventArgs const&) {
// Save the detected shortcut in the linked text block
std::vector<DWORD> detectedShortcutKeys = keyboardManagerState.GetDetectedShortcut();
linkedShortcutText.Text(convertVectorToHstring<DWORD>(detectedShortcutKeys));
Shortcut detectedShortcutKeys = keyboardManagerState.GetDetectedShortcut();
linkedShortcutText.Text(detectedShortcutKeys.ToHstring());
// Reset the keyboard manager UI state
keyboardManagerState.ResetUIState();

View File

@@ -1,6 +1,7 @@
#pragma once
#include <keyboardmanager/common/KeyboardManagerState.h>
#include <keyboardManager/common/Helpers.h>
#include <keyboardmanager/common/Shortcut.h>
class ShortcutControl
{
@@ -38,7 +39,7 @@ public:
}
// Function to add a new row to the shortcut table. If the originalKeys and newKeys args are provided, then the displayed shortcuts are set to those values.
static void AddNewShortcutControlRow(StackPanel& parent, const std::vector<DWORD>& originalKeys = std::vector<DWORD>(), const std::vector<WORD>& newKeys = std::vector<WORD>());
static void AddNewShortcutControlRow(StackPanel& parent, const Shortcut& originalKeys = Shortcut(), const Shortcut& newKeys = Shortcut());
// Function to return the stack panel element of the ShortcutControl. This is the externally visible UI element which can be used to add it to other layouts
StackPanel getShortcutControl();

View File

@@ -6,7 +6,7 @@ HWND SingleKeyRemapControl::EditKeyboardWindowHandle = nullptr;
KeyboardManagerState* SingleKeyRemapControl::keyboardManagerState = nullptr;
// Function to add a new row to the remap keys table. If the originalKey and newKey args are provided, then the displayed remap keys are set to those values.
void SingleKeyRemapControl::AddNewControlKeyRemapRow(StackPanel& parent, const DWORD& originalKey, const WORD& newKey)
void SingleKeyRemapControl::AddNewControlKeyRemapRow(StackPanel& parent, const DWORD& originalKey, const DWORD& newKey)
{
// Parent element for the row
Windows::UI::Xaml::Controls::StackPanel tableRow;

View File

@@ -39,7 +39,7 @@ public:
}
// Function to add a new row to the remap keys table. If the originalKey and newKey args are provided, then the displayed remap keys are set to those values.
static void AddNewControlKeyRemapRow(StackPanel& parent, const DWORD& originalKey = NULL, const WORD& newKey = NULL);
static void AddNewControlKeyRemapRow(StackPanel& parent, const DWORD& originalKey = NULL, const DWORD& newKey = NULL);
// Function to return the stack panel element of the SingleKeyRemapControl. This is the externally visible UI element which can be used to add it to other layouts
StackPanel getSingleKeyRemapControl();