Skip to content

Commit

Permalink
Code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
jefflord committed Dec 3, 2023
1 parent 92aee5f commit 354a620
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,6 @@ 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 Expand Up @@ -459,7 +453,8 @@ LRESULT CALLBACK EditShortcutsWindowProc(HWND hWnd, UINT messageCode, WPARAM wPa
rect->top,
rect->right - rect->left,
rect->bottom - rect->top,
SWP_NOZORDER | SWP_NOACTIVATE);
SWP_NOZORDER | SWP_NOACTIVATE
);

Logger::trace(L"WM_DPICHANGED: new dpi {} rect {} {} ", newDPI, rect->right - rect->left, rect->bottom - rect->top);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ 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)
if (shortcut.isRunProgram && shortcut.runProgramFilePath.length() > 0)
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,7 @@ void ShortcutControl::AddNewShortcutControlRow(StackPanel& parent, std::vector<s
shortcutRemapBuffer[rowIndex].first[1] = text.c_str();
});

// add textbox for when it's a runProgramPath

//auto runProgramPathInputMargin = Windows::UI::Xaml::Thickness();
//runProgramPathInputMargin.Top = -EditorConstants::ShortcutTableDropDownSpacing;
//runProgramPathInputMargin.Bottom = EditorConstants::ShortcutTableDropDownSpacing; // compensate for a collapsed UIElement

// add textboxes for when it's a runProgram fields
auto runProgramPathInput = TextBox();
runProgramPathInput.PlaceholderText(KeyboardManagerEditorStrings::EditShortcutsPathToProgram());
runProgramPathInput.Margin(textInputMargin);
Expand Down Expand Up @@ -197,6 +192,7 @@ void ShortcutControl::AddNewShortcutControlRow(StackPanel& parent, std::vector<s
controlStackPanel.Children().Append(runProgramStartInDirInput);
runProgramStartInDirInput.HorizontalAlignment(HorizontalAlignment::Left);

// add events to textboxes for runProgram fields
runProgramPathInput.TextChanged([parent, row, runProgramPathInput, runProgramArgsForProgramInput, runProgramStartInDirInput](winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Controls::TextChangedEventArgs const& e) mutable {
uint32_t rowIndex = -1;
if (!parent.Children().IndexOf(row, rowIndex))
Expand Down Expand Up @@ -249,21 +245,21 @@ void ShortcutControl::AddNewShortcutControlRow(StackPanel& parent, std::vector<s
});

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

// event code for when type changes
typeCombo.SelectionChanged([typeCombo, shortcutGrid, shortcutButton, textInput, runProgramPathInput, runProgramArgsForProgramInput, runProgramStartInDirInput, targetAppTextBox](winrt::Windows::Foundation::IInspectable const&, SelectionChangedEventArgs const&) {
typeCombo.SelectionChanged([typeCombo, grid, shortcutButton, textInput, runProgramPathInput, runProgramArgsForProgramInput, runProgramStartInDirInput, 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);
grid.Visibility(Visibility::Visible);
shortcutButton.Visibility(Visibility::Visible);
textInput.Visibility(Visibility::Collapsed);
runProgramPathInput.Visibility(Visibility::Collapsed);
Expand All @@ -274,7 +270,7 @@ void ShortcutControl::AddNewShortcutControlRow(StackPanel& parent, std::vector<s
{
//targetAppTextBox.Visibility(Visibility::Visible);
targetAppTextBox.IsEnabled(true);
shortcutGrid.Visibility(Visibility::Collapsed);
grid.Visibility(Visibility::Collapsed);
shortcutButton.Visibility(Visibility::Collapsed);
textInput.Visibility(Visibility::Visible);
runProgramPathInput.Visibility(Visibility::Collapsed);
Expand All @@ -285,7 +281,7 @@ void ShortcutControl::AddNewShortcutControlRow(StackPanel& parent, std::vector<s
{
//targetAppTextBox.Visibility(Visibility::Collapsed);
targetAppTextBox.IsEnabled(false);
shortcutGrid.Visibility(Visibility::Collapsed);
grid.Visibility(Visibility::Collapsed);
shortcutButton.Visibility(Visibility::Collapsed);
textInput.Visibility(Visibility::Collapsed);
runProgramPathInput.Visibility(Visibility::Visible);
Expand All @@ -294,12 +290,6 @@ void ShortcutControl::AddNewShortcutControlRow(StackPanel& parent, std::vector<s
}
});

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

const bool textSelected = newKeys.index() == 2;
bool isRunProgram = false;
if (textSelected)
Expand Down Expand Up @@ -353,23 +343,18 @@ 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);

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);
}
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

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

Expand All @@ -381,21 +366,56 @@ 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();
}
//// 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);

//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

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

// // If exactly one key is selected consider it to be a key remap
// if (selectedKeyCodes.size() == 1)
// {
// shortcutRemapBuffer[rowIndex].first[1] = (DWORD)selectedKeyCodes[0];
// }
// 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();
Expand Down Expand Up @@ -484,20 +504,15 @@ 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))))
{
// change to load app name

if (isRunProgram)
{
auto newShortcut = std::get<Shortcut>(newKeys);
// not sure why by we need to add the current item in here, so we have it even if does not change.
auto newShortcut = std::get<Shortcut>(newKeys);
shortcutRemapBuffer.push_back(std::make_pair<RemapBufferItem, std::wstring>(RemapBufferItem{ Shortcut(), newShortcut }, std::wstring(targetAppName)));
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ 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 for the type of shortcut, to make it easier to switch on and read
enum class ShortcutType
{
Shortcut,
Expand All @@ -57,9 +58,6 @@ 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 @@ -69,8 +67,10 @@ 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"");

// Function to get the shortcut type
static ShortcutType GetShortcutType(const winrt::Windows::UI::Xaml::Controls::ComboBox& typeCombo);

// Function to remove extra quotes from the start and end of the string (used where we will add them as needed later)
static std::wstring ShortcutControl::RemoveExtraQuotes(const std::wstring& str);

// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,7 @@ namespace KeyboardEventHandlers

if (remapToShortcut)
{
// Get the common keys between the two shortcuts

// Get the common keys between the two shortcuts if this is not a runProgram shortcut
auto shortcut = std::get<Shortcut>(it->second.targetShortcut);
if (shortcut.isRunProgram)
{
Expand Down
7 changes: 1 addition & 6 deletions src/modules/keyboardmanager/common/MappingConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,7 @@ bool MappingConfiguration::LoadShortcutRemaps(const json::JsonObject& jsonData,
auto isRunProgram = it.GetObjectW().GetNamedBoolean(KeyboardManagerConstants::IsRunProgramSettingName, false);

if (isRunProgram)
{
while (!true)
{
// debugger wait
Sleep(1000);
}
{
auto runProgramFilePath = it.GetObjectW().GetNamedString(KeyboardManagerConstants::RunProgramFilePathSettingName, L"");
auto runProgramArgs = it.GetObjectW().GetNamedString(KeyboardManagerConstants::RunProgramArgsSettingName, L"");
auto runProgramStartInDir = it.GetObjectW().GetNamedString(KeyboardManagerConstants::RunProgramStartInDirSettingName, L"");
Expand Down
5 changes: 0 additions & 5 deletions src/modules/keyboardmanager/common/Shortcut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,6 @@ void Shortcut::ResetKey(const DWORD input)
}
}

winrt::hstring Shortcut::ToHstring___() const
{
return runProgramFilePath.c_str();
}

// Function to return the string representation of the shortcut in virtual key codes appended in a string by ";" separator.
winrt::hstring Shortcut::ToHstringVK() const
{
Expand Down
2 changes: 0 additions & 2 deletions src/modules/keyboardmanager/common/Shortcut.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ class Shortcut
// Function to return the string representation of the shortcut in virtual key codes appended in a string by ";" separator.
winrt::hstring ToHstringVK() const;

winrt::hstring ToHstring___() const;

// Function to return a vector of key codes in the display order
std::vector<DWORD> GetKeyCodes();

Expand Down
Loading

1 comment on commit 354a620

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@check-spelling-bot Report

🔴 Please review

See the 📜action log or 📝 job summary for details.

Unrecognized words (18)
ARGSFORPROGRAM
Chrods
cmdow
direcotry
myar
myarg
mycool
pathis
PATHTOPROGRAM
PROCESSENTRY
ritchielawrence
SNAPPROCESS
splitw
STARTINDIRFORPROGRAM
textboxes
tlhelp
Toolhelp
wcsicmp
Previously acknowledged words that are now absent CHT constexpr DEU hashcode HEB JPN nodiscard pcs qps roundf RUS RValue SVE wifi 🫥
To accept these unrecognized words as correct and remove the previously acknowledged and now absent words, you could run the following commands

... in a clone of the [email protected]:jefflord/PowerToys.git repository
on the launch-apps-3350 branch (ℹ️ how do I use this?):

curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/v0.0.22/apply.pl' |
perl - 'https://github.com/jefflord/PowerToys/actions/runs/7077380280/attempts/1'
Available 📚 dictionaries could cover words (expected and unrecognized) not in the 📘 dictionary

This includes both expected items (1848) from .github/actions/spell-check/expect.txt and unrecognized words (18)

Dictionary Entries Covers Uniquely
cspell:r/src/r.txt 543 1 1
cspell:cpp/src/people.txt 23 1
cspell:cpp/src/ecosystem.txt 51 1

Consider adding them (in .github/workflows/spelling2.yml) for uses: check-spelling/[email protected] in its with:

      with:
        extra_dictionaries:
          cspell:r/src/r.txt
          cspell:cpp/src/people.txt
          cspell:cpp/src/ecosystem.txt

To stop checking additional dictionaries, add (in .github/workflows/spelling2.yml) for uses: check-spelling/[email protected] in its with:

check_extra_dictionaries: ''
If the flagged items are 🤯 false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

Please sign in to comment.