Skip to content

Commit

Permalink
More stuff working, but setting is messed up again.
Browse files Browse the repository at this point in the history
  • Loading branch information
jefflord committed Oct 20, 2023
1 parent 607c6f0 commit f57c8c2
Show file tree
Hide file tree
Showing 16 changed files with 398 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ bool KeyboardManagerEditor::StartLowLevelKeyboardHook()

void KeyboardManagerEditor::OpenEditorWindow(KeyboardManagerEditorType type)
{
/*bool wait = true;
while (wait)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}*/

switch (type)
{
case KeyboardManagerEditorType::KeyEditor:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ inline void CreateEditRunProgramsWindowImpl(HINSTANCE hInst, KBMEditor::Keyboard
originalRunProgramHeader.FontWeight(Text::FontWeights::Bold());

// Second header textblock in the header row of the runProgram table
TextBlock newRunProgramHeader;
/*TextBlock newRunProgramHeader;
newRunProgramHeader.Text(GET_RESOURCE_STRING(IDS_EDITSHORTCUTS_TARGETHEADER));
newRunProgramHeader.FontWeight(Text::FontWeights::Bold());
newRunProgramHeader.FontWeight(Text::FontWeights::Bold());*/

// Third header textblock in the header row of the runProgram table
TextBlock targetAppHeader;
Expand All @@ -201,8 +201,10 @@ inline void CreateEditRunProgramsWindowImpl(HINSTANCE hInst, KBMEditor::Keyboard
tableHeader.Margin({ 10, 0, 0, 10 });
auto originalRunProgramContainer = UIHelpers::GetWrapped(originalRunProgramHeader, EditorConstants::RunProgramOriginColumnWidth + static_cast<double>(EditorConstants::RunProgramArrowColumnWidth));
tableHeader.Children().Append(originalRunProgramContainer.as<FrameworkElement>());
auto newRunProgramHeaderContainer = UIHelpers::GetWrapped(newRunProgramHeader, EditorConstants::RunProgramTargetColumnWidth);
tableHeader.Children().Append(newRunProgramHeaderContainer.as<FrameworkElement>());

//auto newRunProgramHeaderContainer = UIHelpers::GetWrapped(newRunProgramHeader, EditorConstants::RunProgramTargetColumnWidth);
//tableHeader.Children().Append(newRunProgramHeaderContainer.as<FrameworkElement>());

tableHeader.Children().Append(targetAppHeader);

// Store handle of edit runPrograms window
Expand Down Expand Up @@ -244,7 +246,7 @@ inline void CreateEditRunProgramsWindowImpl(HINSTANCE hInst, KBMEditor::Keyboard
// Iterate through runPrograms for each app
for (const auto& itRunProgram : itApp.second)
{
RunProgramControl::AddNewRunProgramControlRow(runProgramTable, keyboardRemapControlObjects, itRunProgram.first, itRunProgram.second.targetShortcut, itApp.first);
RunProgramControl::AddNewRunProgramControlRow(runProgramTable, keyboardRemapControlObjects, itRunProgram.first, itApp.first);
}
}

Expand All @@ -258,8 +260,8 @@ inline void CreateEditRunProgramsWindowImpl(HINSTANCE hInst, KBMEditor::Keyboard
header.SetLeftOf(applyButton, cancelButton);

auto ApplyRemappings = [&mappingConfiguration, _hWndEditRunProgramsWindow]() {
//LoadingAndSavingRemappingHelper::ApplyRunProgramRemappings(mappingConfiguration, RunProgramControl::runProgramRemapBuffer, true);
LoadingAndSavingRemappingHelper::ApplyShortcutRemappings(mappingConfiguration, RunProgramControl::runProgramRemapBuffer, true);
LoadingAndSavingRemappingHelper::ApplyRunProgramRemappings(mappingConfiguration, RunProgramControl::runProgramRemapBuffer, true);
//LoadingAndSavingRemappingHelper::ApplyShortcutRemappings(mappingConfiguration, RunProgramControl::runProgramRemapBuffer, true);

bool saveResult = mappingConfiguration.SaveSettingsToFile();
PostMessage(_hWndEditRunProgramsWindow, WM_CLOSE, 0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ namespace EditorConstants
inline const int MinimumEditShortcutsWindowHeight = 500;
inline const int EditShortcutsTableMinWidth = 1000;

inline const int DefaultEditRunProgramsWindowWidth = 1050;
inline const int DefaultEditRunProgramsWindowWidth = 750;
inline const int DefaultEditRunProgramsWindowHeight = 600;
inline const int MinimumEditRunProgramsWindowWidth = 500;
inline const int MinimumEditRunProgramsWindowHeight = 500;
inline const int EditRunProgramsTableMinWidth = 1000;
inline const int EditRunProgramsTableMinWidth = 700;


// Key Remap table constants
inline const long RemapTableColCount = 4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ void KeyboardManagerState::ConfigureDetectRunProgramUI(const StackPanel& textBlo
currentShortcutUI2 = textBlock2.as<winrt::Windows::Foundation::IInspectable>();
}


// Function to set the textblock of the detect remap key UI so that it can be accessed by the hook
void KeyboardManagerState::ConfigureDetectSingleKeyRemapUI(const StackPanel& textBlock)
{
Expand Down Expand Up @@ -299,7 +298,14 @@ Helpers::KeyboardHookDecision KeyboardManagerState::DetectSingleRemapKeyUIBacken
Helpers::KeyboardHookDecision KeyboardManagerState::DetectShortcutUIBackend(LowlevelKeyboardEvent* data, bool isRemapKey)
{
// Check if the detect shortcut UI window has been activated
if ((!isRemapKey && CheckUIState(KeyboardManagerUIState::DetectShortcutWindowActivated)) || (isRemapKey && CheckUIState(KeyboardManagerUIState::DetectShortcutWindowInEditKeyboardWindowActivated)))
if (

(!isRemapKey && (CheckUIState(KeyboardManagerUIState::DetectShortcutWindowActivated) || CheckUIState(KeyboardManagerUIState::DetectRunProgramWindowActivated))) ||


(isRemapKey && (CheckUIState(KeyboardManagerUIState::DetectShortcutWindowInEditKeyboardWindowActivated) || CheckUIState(KeyboardManagerUIState::DetectRunProgramWindowInEditKeyboardWindowActivated)))

)
{
if (HandleKeyDelayEvent(data))
{
Expand All @@ -322,7 +328,11 @@ Helpers::KeyboardHookDecision KeyboardManagerState::DetectShortcutUIBackend(Lowl
}

// If the detect shortcut UI window is not activated, then clear the shortcut buffer if it isn't empty
else if (!CheckUIState(KeyboardManagerUIState::DetectShortcutWindowActivated) && !CheckUIState(KeyboardManagerUIState::DetectShortcutWindowInEditKeyboardWindowActivated))
else if (
!CheckUIState(KeyboardManagerUIState::DetectShortcutWindowActivated)
&& !CheckUIState(KeyboardManagerUIState::DetectShortcutWindowInEditKeyboardWindowActivated)
&& !CheckUIState(KeyboardManagerUIState::DetectRunProgramWindowActivated)
&& !CheckUIState(KeyboardManagerUIState::DetectRunProgramWindowInEditKeyboardWindowActivated))
{
std::lock_guard<std::mutex> lock(detectedShortcut_mutex);
if (!detectedShortcut.IsEmpty())
Expand All @@ -332,7 +342,13 @@ Helpers::KeyboardHookDecision KeyboardManagerState::DetectShortcutUIBackend(Lowl
}

// If the settings window is up, shortcut remappings should not be applied, but we should not suppress events in the hook
if (!isRemapKey && (CheckUIState(KeyboardManagerUIState::EditShortcutsWindowActivated)) || (isRemapKey && uiState == KeyboardManagerUIState::DetectShortcutWindowInEditKeyboardWindowActivated))
if (
(!isRemapKey
&& ((CheckUIState(KeyboardManagerUIState::EditShortcutsWindowActivated) || CheckUIState(KeyboardManagerUIState::EditRunProgramsWindowActivated))))
||
(isRemapKey
&& (uiState == KeyboardManagerUIState::DetectShortcutWindowInEditKeyboardWindowActivated || uiState == KeyboardManagerUIState::DetectRunProgramWindowInEditKeyboardWindowActivated))
)
{
return Helpers::KeyboardHookDecision::SkipHook;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ namespace LoadingAndSavingRemappingHelper
std::wstring appName = remappings[i].second;

bool ogKeyValidity = (ogKey.index() == 0 && std::get<DWORD>(ogKey) != NULL) || (ogKey.index() == 1 && EditorHelpers::IsValidShortcut(std::get<Shortcut>(ogKey)));
bool newKeyValidity = (newKey.index() == 0 && std::get<DWORD>(newKey) != NULL) || (newKey.index() == 1 && EditorHelpers::IsValidShortcut(std::get<Shortcut>(newKey)));
//bool newKeyValidity = (newKey.index() == 0 && std::get<DWORD>(newKey) != NULL) || (newKey.index() == 1 && EditorHelpers::IsValidShortcut(std::get<Shortcut>(newKey)));

// Add new set for a new target app name
if (ogKeys.find(appName) == ogKeys.end())
{
ogKeys[appName] = std::set<KeyShortcutUnion>();
}

if (ogKeyValidity && newKeyValidity && ogKeys[appName].find(ogKey) == ogKeys[appName].end())
if (ogKeyValidity && ogKeys[appName].find(ogKey) == ogKeys[appName].end())
{
ogKeys[appName].insert(ogKey);
}
else if (ogKeyValidity && newKeyValidity && ogKeys[appName].find(ogKey) != ogKeys[appName].end())
else if (ogKeyValidity && ogKeys[appName].find(ogKey) != ogKeys[appName].end())
{
isSuccess = RunProgramErrorType::RemapUnsuccessful;
}
Expand Down Expand Up @@ -217,9 +217,7 @@ namespace LoadingAndSavingRemappingHelper
void ApplyRunProgramRemappings(MappingConfiguration& mappingConfiguration, const RemapBuffer& remappings, bool isTelemetryRequired)
{
// Clear existing RunPrograms
mappingConfiguration.ClearOSLevelShortcuts();
DWORD successfulOSLevelRunProgramToRunProgramRemapCount = 0;
DWORD successfulOSLevelRunProgramToKeyRemapCount = 0;
mappingConfiguration.ClearAppSpecificRunProgram();
DWORD successfulAppSpecificRunProgramToRunProgramRemapCount = 0;
DWORD successfulAppSpecificRunProgramToKeyRemapCount = 0;

Expand All @@ -229,45 +227,20 @@ namespace LoadingAndSavingRemappingHelper
Shortcut originalRunProgram = std::get<Shortcut>(remappings[i].first[0]);
KeyShortcutUnion newRunProgram = remappings[i].first[1];

if (EditorHelpers::IsValidShortcut(originalRunProgram) && ((newRunProgram.index() == 0 && std::get<DWORD>(newRunProgram) != NULL) || (newRunProgram.index() == 1 && EditorHelpers::IsValidShortcut(std::get<Shortcut>(newRunProgram)))))
if (EditorHelpers::IsValidShortcut(originalRunProgram) && ((newRunProgram.index() == 0 && std::get<DWORD>(newRunProgram) != NULL)))
{
if (remappings[i].second == L"")
{
bool result = mappingConfiguration.AddOSLevelShortcut(originalRunProgram, newRunProgram);
if (result)
{
if (newRunProgram.index() == 0)
{
successfulOSLevelRunProgramToKeyRemapCount += 1;
}
else
{
successfulOSLevelRunProgramToRunProgramRemapCount += 1;
}
}
}
else
bool result = mappingConfiguration.AddAppSpecificRunProgram(remappings[i].second, originalRunProgram, newRunProgram);
if (result)
{
bool result = mappingConfiguration.AddAppSpecificShortcut(remappings[i].second, originalRunProgram, newRunProgram);
if (result)
{
if (newRunProgram.index() == 0)
{
successfulAppSpecificRunProgramToKeyRemapCount += 1;
}
else
{
successfulAppSpecificRunProgramToRunProgramRemapCount += 1;
}
}
successfulAppSpecificRunProgramToRunProgramRemapCount += 1;
}
}
}

// If telemetry is to be logged, log the RunProgram remap counts
if (isTelemetryRequired)
{
Trace::OSLevelShortcutRemapCount(successfulOSLevelRunProgramToRunProgramRemapCount, successfulOSLevelRunProgramToKeyRemapCount);
// If telemetry is to be logged, log the RunProgram remap counts
if (isTelemetryRequired)
{
Trace::RunProgramRemapCount(successfulAppSpecificRunProgramToRunProgramRemapCount);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ namespace LoadingAndSavingRemappingHelper

// Function to apply the shortcut remappings from the buffer to the KeyboardManagerState variable
void ApplyShortcutRemappings(MappingConfiguration& mappingConfiguration, const RemapBuffer& remappings, bool isTelemetryRequired);

void ApplyRunProgramRemappings(MappingConfiguration& mappingConfiguration, const RemapBuffer& remappings, bool isTelemetryRequired);


}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ RunProgramControl::RunProgramControl(StackPanel table, StackPanel row, const int
typeRunProgram.as<Button>().Content(winrt::box_value(GET_RESOURCE_STRING(IDS_TYPE_BUTTON)));
typeRunProgram.as<Button>().Width(EditorConstants::RunProgramTableDropDownWidth);
typeRunProgram.as<Button>().Click([&, table, row, colIndex, isHybridControl, targetApp](winrt::Windows::Foundation::IInspectable const& sender, RoutedEventArgs const&) {
keyboardManagerState->SetUIState(KBMEditor::KeyboardManagerUIState::DetectRunProgramWindowActivated, editRunProgramsWindowHandle);
//keyboardManagerState->SetUIState(KBMEditor::KeyboardManagerUIState::DetectRunProgramWindowActivated, editRunProgramsWindowHandle);
keyboardManagerState->SetUIState(KBMEditor::KeyboardManagerUIState::DetectShortcutWindowActivated, editRunProgramsWindowHandle);
//
// Using the XamlRoot of the typeRunProgram to get the root of the XAML host
CreateDetectRunProgramWindow(sender, sender.as<Button>().XamlRoot(), *keyboardManagerState, colIndex, table, keyDropDownControlObjects, row, targetApp, isHybridControl, false, editRunProgramsWindowHandle, runProgramRemapBuffer);
});
Expand Down Expand Up @@ -77,7 +79,7 @@ void RunProgramControl::UpdateAccessibleNames(StackPanel sourceColumn, StackPane
}

// Function to add a new row to the runProgram table. If the originalKeys and newKeys args are provided, then the displayed runPrograms are set to those values.
void RunProgramControl::AddNewRunProgramControlRow(StackPanel& parent, std::vector<std::vector<std::unique_ptr<RunProgramControl>>>& keyboardRemapControlObjects, const Shortcut& originalKeys, const KeyShortcutUnion& newKeys, const std::wstring& targetAppName)
void RunProgramControl::AddNewRunProgramControlRow(StackPanel& parent, std::vector<std::vector<std::unique_ptr<RunProgramControl>>>& keyboardRemapControlObjects, const Shortcut& originalKeys, const std::wstring& targetAppName)
{
// Textbox for target application
TextBox targetAppTextBox;
Expand Down Expand Up @@ -115,9 +117,9 @@ void RunProgramControl::AddNewRunProgramControlRow(StackPanel& parent, std::vect
row.Children().Append(arrowIconContainer);

// RunProgramControl for the new runProgram
auto target = keyboardRemapControlObjects.back()[1]->GetRunProgramControl();
target.Width(EditorConstants::RunProgramTargetColumnWidth);
row.Children().Append(target);
//auto target = keyboardRemapControlObjects.back()[1]->GetRunProgramControl();
//target.Width(EditorConstants::RunProgramTargetColumnWidth);
//row.Children().Append(target);

targetAppTextBox.Width(EditorConstants::RunProgramTableDropDownWidth);
targetAppTextBox.PlaceholderText(KeyboardManagerEditorStrings::DefaultAppName());
Expand Down Expand Up @@ -257,20 +259,24 @@ void RunProgramControl::AddNewRunProgramControlRow(StackPanel& parent, std::vect
UpdateAccessibleNames(keyboardRemapControlObjects[keyboardRemapControlObjects.size() - 1][0]->GetRunProgramControl(), keyboardRemapControlObjects[keyboardRemapControlObjects.size() - 1][1]->GetRunProgramControl(), targetAppTextBox, deleteRunProgram, static_cast<int>(keyboardRemapControlObjects.size()));

// Set the runProgram 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))))
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
runProgramRemapBuffer.push_back(std::make_pair<RemapBufferItem, std::wstring>(RemapBufferItem{ Shortcut(), Shortcut() }, std::wstring(targetAppName)));
KeyDropDownControl::AddShortcutToControl(originalKeys, parent, keyboardRemapControlObjects[keyboardRemapControlObjects.size() - 1][0]->runProgramDropDownVariableSizedWrapGrid.as<VariableSizedWrapGrid>(), *keyboardManagerState, 0, keyboardRemapControlObjects[keyboardRemapControlObjects.size() - 1][0]->keyDropDownControlObjects, runProgramRemapBuffer, row, targetAppTextBox, false, false);

if (newKeys.index() == 0)
/*if (newKeys.index() == 0)
{
keyboardRemapControlObjects[keyboardRemapControlObjects.size() - 1][1]->keyDropDownControlObjects[0]->SetSelectedValue(std::to_wstring(std::get<DWORD>(newKeys)));
}
else
{
KeyDropDownControl::AddShortcutToControl(std::get<Shortcut>(newKeys), parent, keyboardRemapControlObjects.back()[1]->runProgramDropDownVariableSizedWrapGrid.as<VariableSizedWrapGrid>(), *keyboardManagerState, 1, keyboardRemapControlObjects[keyboardRemapControlObjects.size() - 1][1]->keyDropDownControlObjects, runProgramRemapBuffer, row, targetAppTextBox, true, false);
}
}*/
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class RunProgramControl
RunProgramControl(StackPanel table, StackPanel row, const int colIndex, TextBox targetApp);

// Function to add a new row to the runProgram table. If the originalKeys and newKeys args are provided, then the displayed runPrograms are set to those values.
static void AddNewRunProgramControlRow(StackPanel& parent, std::vector<std::vector<std::unique_ptr<RunProgramControl>>>& keyboardRemapControlObjects, const Shortcut& originalKeys = Shortcut(), const KeyShortcutUnion& newKeys = Shortcut(), const std::wstring& targetAppName = L"");
static void AddNewRunProgramControlRow(StackPanel& parent, std::vector<std::vector<std::unique_ptr<RunProgramControl>>>& keyboardRemapControlObjects, const Shortcut& originalKeys = Shortcut(), const std::wstring& targetAppName = L"");

// Function to return the stack panel element of the RunProgramControl. This is the externally visible UI element which can be used to add it to other layouts
StackPanel GetRunProgramControl();
Expand Down
13 changes: 13 additions & 0 deletions src/modules/keyboardmanager/KeyboardManagerEditorLibrary/trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ void Trace::KeyRemapCount(const DWORD keyToKeyCount, const DWORD keyToShortcutCo
TraceLoggingValue(keyToShortcutCount, "KeyToShortcutRemapCount"));
}

// Log number of os level shortcut remaps when the user uses Edit RunPrograms and saves settings
void Trace::RunProgramRemapCount(const DWORD runProgramToRunProgramCount) noexcept
{
//TraceLoggingWrite(
// g_hProvider,
// "KeyboardManager_OSLevelRunProgramRemapCount",
// ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance),
// TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE),
// TraceLoggingValue(runProgramToRunProgramCount + runProgramToKeyCount, "OSLevelRunProgramRemapCount"),
// TraceLoggingValue(runProgramToRunProgramCount, "OSLevelRunProgramToRunProgramRemapCount"),
// TraceLoggingValue(runProgramToKeyCount, "OSLevelRunProgramToKeyRemapCount"));
}

// Log number of os level shortcut remaps when the user uses Edit Shortcuts and saves settings
void Trace::OSLevelShortcutRemapCount(const DWORD shortcutToShortcutCount, const DWORD shortcutToKeyCount) noexcept
{
Expand Down
Loading

1 comment on commit f57c8c2

@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 for details.

Unrecognized words (3)

EDITRUNPROGRAMS
JLO
Programtool

Previously acknowledged words that are now absent administra RSAT systemroot sysvol :arrow_right:
To accept ✔️ these unrecognized words as correct and remove the previously acknowledged and now absent words, run the following commands

... in a clone of the git@github.com:jefflord/PowerToys.git repository
on the run-program-shortcuts branch (ℹ️ how do I use this?):

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

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

Dictionary Entries Covers
cspell:win32/src/win32.txt 53509 139
cspell:cpp/src/cpp.txt 30216 132
cspell:python/src/python/python-lib.txt 3873 29
cspell:php/php.txt 2597 20
cspell:node/node.txt 1768 15
cspell:typescript/typescript.txt 1211 13
cspell:java/java.txt 7642 12
cspell:python/src/python/python.txt 453 10
cspell:aws/aws.txt 218 9
cspell:python/src/common/extra.txt 741 8

Consider adding them using (in .github/workflows/spelling2.yml):

      with:
        extra_dictionaries:
          cspell:win32/src/win32.txt
          cspell:cpp/src/cpp.txt
          cspell:python/src/python/python-lib.txt
          cspell:php/php.txt
          cspell:node/node.txt
          cspell:typescript/typescript.txt
          cspell:java/java.txt
          cspell:python/src/python/python.txt
          cspell:aws/aws.txt
          cspell:python/src/common/extra.txt

To stop checking additional dictionaries, add:

      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.