Skip to content

Commit

Permalink
Working UI update with just runProgram Path and isRunProgram
Browse files Browse the repository at this point in the history
  • Loading branch information
jefflord committed Dec 2, 2023
1 parent 5f36a39 commit 38841e2
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@
<Manifest Include="KeyboardManagerEditor.exe.manifest" />
</ItemGroup>
<ItemGroup>
<None Include="Resources.resx" />
<None Include="Resources.resx">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<Image Include="Keyboard.ico" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@
<value>Key/Shortcut</value>
<comment>Key on a keyboard</comment>
</data>
<data name="Mapping_Type_DropDown_Run_Program" xml:space="preserve">
<value>Run Program</value>
<comment>Run Program</comment>
</data>
<data name="Add_Key_Remap_Button" xml:space="preserve">
<value>Add key remapping</value>
<comment>Key on a keyboard</comment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ inline void CreateEditShortcutsWindowImpl(HINSTANCE hInst, KBMEditor::KeyboardMa
// Create copy of the remaps to avoid concurrent access
ShortcutRemapTable osLevelShortcutReMapCopy = mappingConfiguration.osLevelShortcutReMap;

while (!true)
{
// debugger wait
Sleep(1000);
}

for (const auto& it : osLevelShortcutReMapCopy)
{
ShortcutControl::AddNewShortcutControlRow(shortcutTable, keyboardRemapControlObjects, it.first, it.second.targetShortcut);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ namespace EditorHelpers
// Function to return true if the shortcut is valid. A valid shortcut has atleast one modifier, as well as an action key
bool IsValidShortcut(Shortcut shortcut)
{
if (shortcut.isRunProgram)
{
return true;
}

if (shortcut.actionKey != NULL)
{
if (shortcut.winKey != ModifierKey::Disabled || shortcut.ctrlKey != ModifierKey::Disabled || shortcut.altKey != ModifierKey::Disabled || shortcut.shiftKey != ModifierKey::Disabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ void KeyDropDownControl::SetDefaultProperties(bool isShortcut, bool renderDisabl
auto child0 = Media::VisualTreeHelper::GetChild(combo, 0);
if (!child0)
return;

auto grid = child0.as<Grid>();
if (!grid)
return;

auto& gridChildren = grid.Children();
if (!gridChildren)
return;

gridChildren.Append(warningTip);
});

Expand Down Expand Up @@ -432,7 +432,7 @@ void KeyDropDownControl::AddShortcutToControl(Shortcut shortcut, StackPanel tabl
// Remove references to the old drop down objects to destroy them
keyDropDownControlObjects.clear();
std::vector<DWORD> shortcutKeyCodes = shortcut.GetKeyCodes();
if (shortcutKeyCodes.size() != 0)
if (shortcutKeyCodes.size() != 0 || shortcut.isRunProgram)
{
bool ignoreWarning = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ namespace KeyboardManagerEditorStrings
return GET_RESOURCE_STRING(IDS_MAPPING_TYPE_DROPDOWN_KEY_SHORTCUT);
}

inline std::wstring MappingTypeRunProgram()
{
return GET_RESOURCE_STRING(IDS_MAPPING_TYPE_DROPDOWN_RUN_PROGRAM);
}

// Function to return the error message
winrt::hstring GetErrorMessage(ShortcutErrorType errorType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,17 @@ void ShortcutControl::AddNewShortcutControlRow(StackPanel& parent, std::vector<s
auto target = keyboardRemapControlObjects.back()[1]->GetShortcutControl();
target.Width(EditorConstants::ShortcutTargetColumnWidth);

// add shortcut type choice
auto typeCombo = ComboBox();
typeCombo.Width(EditorConstants::RemapTableDropDownWidth);
typeCombo.Items().Append(winrt::box_value(KeyboardManagerEditorStrings::MappingTypeKeyShortcut()));
typeCombo.Items().Append(winrt::box_value(KeyboardManagerEditorStrings::MappingTypeText()));
typeCombo.Items().Append(winrt::box_value(KeyboardManagerEditorStrings::MappingTypeRunProgram()));
auto controlStackPanel = keyboardRemapControlObjects.back()[1]->shortcutControlLayout.as<StackPanel>();
auto firstLineStackPanel = keyboardRemapControlObjects.back()[1]->keyComboAndSelectStackPanel.as<StackPanel>();
firstLineStackPanel.Children().InsertAt(0, typeCombo);

// add textbox for when it's a text input
auto textInput = TextBox();
auto textInputMargin = Windows::UI::Xaml::Thickness();
textInputMargin.Top = -EditorConstants::ShortcutTableDropDownSpacing;
Expand All @@ -141,6 +144,7 @@ void ShortcutControl::AddNewShortcutControlRow(StackPanel& parent, std::vector<s
textInput.Width(EditorConstants::TableDropDownHeight);
controlStackPanel.Children().Append(textInput);
textInput.HorizontalAlignment(HorizontalAlignment::Left);

textInput.TextChanged([parent, row](winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Controls::TextChangedEventArgs const& e) mutable {
auto textbox = sender.as<TextBox>();
auto text = textbox.Text();
Expand All @@ -154,26 +158,100 @@ void ShortcutControl::AddNewShortcutControlRow(StackPanel& parent, std::vector<s
shortcutRemapBuffer[rowIndex].first[1] = text.c_str();
});

auto grid = keyboardRemapControlObjects.back()[1]->shortcutDropDownVariableSizedWrapGrid.as<VariableSizedWrapGrid>();
auto gridMargin = Windows::UI::Xaml::Thickness();
gridMargin.Bottom = -EditorConstants::ShortcutTableDropDownSpacing; // compensate for a collapsed textInput
grid.Margin(gridMargin);
auto button = keyboardRemapControlObjects.back()[1]->typeShortcut.as<Button>();
// add textbox for when it's a runProgram
auto runProgramPathInput = TextBox();
auto runProgramPathInputMargin = Windows::UI::Xaml::Thickness();
runProgramPathInputMargin.Top = -EditorConstants::ShortcutTableDropDownSpacing;
runProgramPathInputMargin.Bottom = EditorConstants::ShortcutTableDropDownSpacing; // compensate for a collapsed UIElement
runProgramPathInput.Margin(runProgramPathInputMargin);

typeCombo.SelectionChanged([typeCombo, grid, button, textInput](winrt::Windows::Foundation::IInspectable const&, SelectionChangedEventArgs const&) {
const bool textSelected = typeCombo.SelectedIndex() == 1;
runProgramPathInput.AcceptsReturn(false);
runProgramPathInput.Visibility(Visibility::Collapsed);
runProgramPathInput.Width(EditorConstants::TableDropDownHeight);
controlStackPanel.Children().Append(runProgramPathInput);
runProgramPathInput.HorizontalAlignment(HorizontalAlignment::Left);

const auto shortcutInputVisibility = textSelected ? Visibility::Collapsed : Visibility::Visible;
runProgramPathInput.TextChanged([parent, row](winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Controls::TextChangedEventArgs const& e) mutable {
auto textbox = sender.as<TextBox>();
auto text = textbox.Text();
uint32_t rowIndex = -1;

grid.Visibility(shortcutInputVisibility);
button.Visibility(shortcutInputVisibility);
if (!parent.Children().IndexOf(row, rowIndex))
{
return;
}

const auto textInputVisibility = textSelected ? Visibility::Visible : Visibility::Collapsed;
textInput.Visibility(textInputVisibility);
Shortcut tempShortcut;
tempShortcut.isRunProgram = true;
tempShortcut.runProgramPath = text.c_str();
// Assign instead of setting the value in the buffer since the previous value may not be a Shortcut
shortcutRemapBuffer[rowIndex].first[1] = tempShortcut;
});

// add grid for when it's a key/shortcut
auto shortcutGrid = keyboardRemapControlObjects.back()[1]->shortcutDropDownVariableSizedWrapGrid.as<VariableSizedWrapGrid>();
auto gridMargin = Windows::UI::Xaml::Thickness();
gridMargin.Bottom = -EditorConstants::ShortcutTableDropDownSpacing; // compensate for a collapsed textInput
shortcutGrid.Margin(gridMargin);
auto shortcutButton = keyboardRemapControlObjects.back()[1]->typeShortcut.as<Button>();

// event code for when type changes
typeCombo.SelectionChanged([typeCombo, shortcutGrid, shortcutButton, textInput, runProgramPathInput, targetAppTextBox](winrt::Windows::Foundation::IInspectable const&, SelectionChangedEventArgs const&) {
const auto shortcutType = ShortcutControl::GetShortcutType(typeCombo);

if (shortcutType == ShortcutControl::ShortcutType::Shortcut)
{
//targetAppTextBox.Visibility(Visibility::Visible);
targetAppTextBox.IsEnabled(true);
shortcutGrid.Visibility(Visibility::Visible);
shortcutButton.Visibility(Visibility::Visible);
textInput.Visibility(Visibility::Collapsed);
runProgramPathInput.Visibility(Visibility::Collapsed);
}
else if (shortcutType == ShortcutControl::ShortcutType::Text)
{
//targetAppTextBox.Visibility(Visibility::Visible);
targetAppTextBox.IsEnabled(true);
shortcutGrid.Visibility(Visibility::Collapsed);
shortcutButton.Visibility(Visibility::Collapsed);
textInput.Visibility(Visibility::Visible);
runProgramPathInput.Visibility(Visibility::Collapsed);
}
else
{
//targetAppTextBox.Visibility(Visibility::Collapsed);
targetAppTextBox.IsEnabled(false);
shortcutGrid.Visibility(Visibility::Collapsed);
shortcutButton.Visibility(Visibility::Collapsed);
textInput.Visibility(Visibility::Collapsed);
runProgramPathInput.Visibility(Visibility::Visible);
}
});

while (!true)
{
// debugger wait
Sleep(1000);
}

const bool textSelected = newKeys.index() == 2;
typeCombo.SelectedIndex(textSelected);
if (textSelected)
{
typeCombo.SelectedIndex(1);
}
else
{
auto shortCut = std::get<Shortcut>(newKeys);
if (shortCut.isRunProgram)
{
runProgramPathInput.Text(shortCut.runProgramPath);
typeCombo.SelectedIndex(2);
}
else
{
typeCombo.SelectedIndex(0);
}
}

row.Children().Append(target);

Expand Down Expand Up @@ -205,18 +283,23 @@ void ShortcutControl::AddNewShortcutControlRow(StackPanel& parent, std::vector<s

// Validate both set of drop downs
KeyDropDownControl::ValidateShortcutFromDropDownList(parent, row, keyboardRemapControlObjects[rowIndex][0]->shortcutDropDownVariableSizedWrapGrid.as<VariableSizedWrapGrid>(), 0, ShortcutControl::shortcutRemapBuffer, keyboardRemapControlObjects[rowIndex][0]->keyDropDownControlObjects, targetAppTextBox, false, false);
KeyDropDownControl::ValidateShortcutFromDropDownList(parent, row, keyboardRemapControlObjects[rowIndex][1]->shortcutDropDownVariableSizedWrapGrid.as<VariableSizedWrapGrid>(), 1, ShortcutControl::shortcutRemapBuffer, keyboardRemapControlObjects[rowIndex][1]->keyDropDownControlObjects, targetAppTextBox, true, false);

auto shortcutType = ShortcutControl::GetShortcutType(typeCombo);

if (shortcutType == ShortcutControl::ShortcutType::Text || shortcutType == ShortcutControl::ShortcutType::Shortcut)
{
KeyDropDownControl::ValidateShortcutFromDropDownList(parent, row, keyboardRemapControlObjects[rowIndex][1]->shortcutDropDownVariableSizedWrapGrid.as<VariableSizedWrapGrid>(), 1, ShortcutControl::shortcutRemapBuffer, keyboardRemapControlObjects[rowIndex][1]->keyDropDownControlObjects, targetAppTextBox, true, false);
}

// Reset the buffer based on the selected drop down items
std::get<Shortcut>(shortcutRemapBuffer[rowIndex].first[0]).SetKeyCodes(KeyDropDownControl::GetSelectedCodesFromStackPanel(keyboardRemapControlObjects[rowIndex][0]->shortcutDropDownVariableSizedWrapGrid.as<VariableSizedWrapGrid>()));
// second column is a hybrid column

const bool textSelected = typeCombo.SelectedIndex() == 1;
if (textSelected)
if (shortcutType == ShortcutControl::ShortcutType::Text)
{
shortcutRemapBuffer[rowIndex].first[1] = textInput.Text().c_str();
}
else
else if (shortcutType == ShortcutControl::ShortcutType::Shortcut)
{
std::vector<int32_t> selectedKeyCodes = KeyDropDownControl::GetSelectedCodesFromStackPanel(keyboardRemapControlObjects[rowIndex][1]->shortcutDropDownVariableSizedWrapGrid.as<VariableSizedWrapGrid>());

Expand All @@ -228,11 +311,22 @@ void ShortcutControl::AddNewShortcutControlRow(StackPanel& parent, std::vector<s
else
{
Shortcut tempShortcut;
tempShortcut.isRunProgram = false;
tempShortcut.SetKeyCodes(selectedKeyCodes);
// Assign instead of setting the value in the buffer since the previous value may not be a Shortcut
shortcutRemapBuffer[rowIndex].first[1] = tempShortcut;
}
}
else if (shortcutType == ShortcutControl::ShortcutType::RunProgram)
{
Shortcut tempShortcut;
tempShortcut.isRunProgram = true;
// Assign instead of setting the value in the buffer since the previous value may not be a Shortcut
shortcutRemapBuffer[rowIndex].first[1] = tempShortcut;

//shortcutRemapBuffer[rowIndex].first[1] = textInput.Text().c_str();
}

std::wstring newText = targetAppTextBox.Text().c_str();
std::wstring lowercaseDefAppName = KeyboardManagerEditorStrings::DefaultAppName();
std::transform(newText.begin(), newText.end(), newText.begin(), towlower);
Expand Down Expand Up @@ -320,6 +414,12 @@ void ShortcutControl::AddNewShortcutControlRow(StackPanel& parent, std::vector<s
// Set accessible names
UpdateAccessibleNames(keyboardRemapControlObjects[keyboardRemapControlObjects.size() - 1][0]->GetShortcutControl(), keyboardRemapControlObjects[keyboardRemapControlObjects.size() - 1][1]->GetShortcutControl(), targetAppTextBox, deleteShortcut, static_cast<int>(keyboardRemapControlObjects.size()));

while (!true)
{
// debugger wait
Sleep(1000);
}

// Set the shortcut text if the two vectors are not empty (i.e. default args)
if (EditorHelpers::IsValidShortcut(originalKeys) && !(newKeys.index() == 0 && std::get<DWORD>(newKeys) == NULL) && !(newKeys.index() == 1 && !EditorHelpers::IsValidShortcut(std::get<Shortcut>(newKeys))))
{
Expand Down Expand Up @@ -352,6 +452,22 @@ void ShortcutControl::AddNewShortcutControlRow(StackPanel& parent, std::vector<s
}
}

ShortcutControl::ShortcutType ShortcutControl::GetShortcutType(const winrt::Windows::UI::Xaml::Controls::ComboBox& typeCombo)
{
if (typeCombo.SelectedIndex() == 0)
{
return ShortcutControl::ShortcutType::Shortcut;
}
else if (typeCombo.SelectedIndex() == 1)
{
return ShortcutControl::ShortcutType::Text;
}
else
{
return ShortcutControl::ShortcutType::RunProgram;
}
}

// 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 ShortcutControl::GetShortcutControl()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ class ShortcutControl
// Function to set the accessible names for all the controls in a row
static void UpdateAccessibleNames(StackPanel sourceColumn, StackPanel mappedToColumn, TextBox targetAppTextBox, Button deleteButton, int rowIndex);

enum class ShortcutType
{
Shortcut,
Text,
RunProgram
};

public:
// Handle to the current Edit Shortcuts Window
static HWND editShortcutsWindowHandle;
Expand All @@ -50,6 +57,9 @@ class ShortcutControl
// Stores the current list of remappings
static RemapBuffer shortcutRemapBuffer;

// Stores the current list of runProgram... things?
static RemapBuffer runProgramRemapBuffer;

// Vector to store dynamically allocated KeyDropDownControl objects to avoid early destruction
std::vector<std::unique_ptr<KeyDropDownControl>> keyDropDownControlObjects;

Expand All @@ -59,6 +69,8 @@ class ShortcutControl
// 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, std::vector<std::vector<std::unique_ptr<ShortcutControl>>>& keyboardRemapControlObjects, const Shortcut& originalKeys = Shortcut(), const KeyShortcutTextUnion& newKeys = Shortcut(), const std::wstring& targetAppName = L"");

static ShortcutType GetShortcutType(const winrt::Windows::UI::Xaml::Controls::ComboBox& typeCombo);

// 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();

Expand Down
9 changes: 9 additions & 0 deletions src/modules/keyboardmanager/common/KeyboardManagerConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ namespace KeyboardManagerConstants

// Name of the property use to store shortcut to text remaps.
inline const std::wstring RemapShortcutsToTextSettingName = L"remapShortcutsToText";

// Name of the property use to store shortcut to run-program remaps.
inline const std::wstring RemapShortcutsToRunProgramSettingName = L"remapShortcutsToRunProgram";

// Name of the property use to store global shortcut remaps array.
inline const std::wstring GlobalRemapShortcutsSettingName = L"global";
Expand All @@ -43,6 +46,12 @@ namespace KeyboardManagerConstants
// Name of the property use to store new remapped string.
inline const std::wstring NewTextSettingName = L"unicodeText";

// Name of the property use to store runProgramFilePath.
inline const std::wstring RunProgramSettingName = L"runProgram";

// Name of the property use to store runProgramFilePath.
inline const std::wstring IsRunProgramSettingName = L"isRunProgram";

// Name of the property use to store the target application.
inline const std::wstring TargetAppSettingName = L"targetApp";

Expand Down
Loading

0 comments on commit 38841e2

Please sign in to comment.