Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Fixed an issue where it didn't work to map certain keys to Actions #15420

Merged
merged 25 commits into from
May 24, 2024

Conversation

XTorLukas
Copy link
Contributor

Resolved / Related Issues

To prevent extra work, all changes to the Files codebase must link to an approved issue marked as Ready to build. Please insert the issue number following the hashtag with the issue number that this Pull Request resolves.

Steps used to test these changes

Stability is a top priority for Files and all changes are required to go through testing before being merged into the repo. Please include a list of steps that you used to test this PR.

  1. Testing the correct mapping of all problem keys

@yaira2 yaira2 changed the title Fix: Problem with mapping certain keys in action section Fix: Fixed an issue where it didn't work to map certain keys to Actions May 16, 2024
@yaira2 yaira2 requested a review from 0x5bfa May 16, 2024 23:05
Copy link
Member

@0x5bfa 0x5bfa left a comment

Choose a reason for hiding this comment

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

You know our codebase well!!!
LGTM. code wise.
As we are not in an area that needs unicode support, could you please send a screenshot or video recording of the validation?

src/Files.App/Data/Commands/HotKey/HotKey.cs Outdated Show resolved Hide resolved
src/Files.App/Data/Commands/HotKey/HotKey.cs Outdated Show resolved Hide resolved
XTorLukas and others added 2 commits May 17, 2024 13:33
Co-authored-by: 0x5bfa <62196528+0x5bfa@users.noreply.github.com>
@XTorLukas
Copy link
Contributor Author

XTorLukas commented May 17, 2024

@yaira2 I found the cause of the NumKeys mapping not working, Not localized Pad0-9
image
image

Now it's worked

image
image

Testing.KeyMap.165756.mp4

@XTorLukas
Copy link
Contributor Author

XTorLukas commented May 17, 2024

Oh, Decimal (NumPad) key not defined too

image

Feature

As I follow the definition of the Numpad keys at PowerToys is not bad as they define instead of "Decimal" --> ". (NumPad)"

Experimental Test

But I thought it would be enough to just localize the name 'NumPad' and the rest is default for all standards, at the same time it would be easier to know that the key is from 'NumPad'
image

Experimental.KeyMap.NumPad.Key.mp4

What do you think about that?

@XTorLukas XTorLukas marked this pull request as draft May 17, 2024 15:57
@XTorLukas
Copy link
Contributor Author

XTorLukas commented May 17, 2024

Test own layout (cs-CZ_QWERTZ)

Qwertz_cz svg (2)

  • All Green lines are correct
  • Orange lines unused
  • Red Box (Caps Lock / Scroll Lock / Num Lock) would be good to disable in the selection, because they are not possible, but they are marked as Empty string and so can be overlooked. After saving the application fails.

@XTorLukas
Copy link
Contributor Author

XTorLukas commented May 17, 2024

Red Box (Caps Lock / Scroll Lock / Num Lock) would be good to disable in the selection, because they are not possible, but they are marked as Empty string and so can be overlooked. After saving the application fails.

Now ignored non-valid keys aka Red Box and more optimalization for speed

  1. I added information about invalid key binding to give the user the option to use a different key binding.
    image

  2. Works also for already defined key bindings
    image

  3. I reworked the logic when the SaveButton and AddButton buttons are active

@0x5bfa 0x5bfa self-requested a review May 18, 2024 22:33
@XTorLukas XTorLukas marked this pull request as ready for review May 18, 2024 23:01
@XTorLukas
Copy link
Contributor Author

XTorLukas commented May 19, 2024

Let's get to testing

  • Testing functionality across different locations
    • cs-CZ
    • sk-SK
    • en-US
    • ru-RU
    • uk-UA
    • ... more
  • Verification and search for possible bugs
  • Finalization

@0x5bfa 0x5bfa self-assigned this May 21, 2024
yaira2
yaira2 previously approved these changes May 21, 2024
@0x5bfa 0x5bfa removed their assignment May 23, 2024
Copy link
Member

@0x5bfa 0x5bfa left a comment

Choose a reason for hiding this comment

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

LGTM other than two things below, code wise.
I can test these on my end this weekend.

Co-authored-by: 0x5BFA <62196528+0x5bfa@users.noreply.github.com>
Copy link
Member

@0x5bfa 0x5bfa left a comment

Choose a reason for hiding this comment

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

Code Quality is over A+.
Looks great to me.

I haven't tested on my end but if someone did I think this can be merged.
If still opened, I'll test this weekend.

@XTorLukas
Copy link
Contributor Author

Code Quality is over A+. Looks great to me.

Thank you, I'm glad I'm not a useless liability here even though I've only been in this community for a short time. I'll definitely keep trying to improve the quality of the code.

@XTorLukas
Copy link
Contributor Author

I just have an idea for the future, it would be good to merge all the work with "Key binding" into one class and split it into functions, at least now it's too much for one method

private void KeyBindingEditorTextBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
// Ensure the sender is a TextBox
if (sender is not TextBox textBox)
return;
// Cast the DataContext of the TextBox to a ModifiableActionItem if possible, or null if the cast fails
var item = textBox.DataContext as ModifiableActionItem;
var pressedKey = e.OriginalKey;
var pressedKeyValue = HotKey.LocalizedKeys.GetValueOrDefault((Keys)pressedKey);
var buffer = new StringBuilder();
// Define invalid keys that shouldn't be processed
var invalidKeys = new HashSet<VirtualKey>
{
VirtualKey.CapitalLock,
VirtualKey.NumberKeyLock,
VirtualKey.Scroll,
};
// Define modifier keys
var modifierKeys = new HashSet<VirtualKey>
{
VirtualKey.Shift,
VirtualKey.Control,
VirtualKey.Menu,
VirtualKey.LeftWindows,
VirtualKey.RightWindows,
VirtualKey.LeftShift,
VirtualKey.LeftControl,
VirtualKey.RightControl,
VirtualKey.LeftMenu,
VirtualKey.RightMenu
};
// Determine if the pressed key is invalid or a modifier
var isInvalidKey = invalidKeys.Contains(pressedKey) || string.IsNullOrEmpty(pressedKeyValue);
var isModifierKey = modifierKeys.Contains(pressedKey);
// Handle invalid keys that are not modifiers
if (isInvalidKey && !isModifierKey)
{
InvalidKeyTeachingTip.Target = textBox;
ViewModel.IsInvalidKeyTeachingTipOpened = true;
}
// Check if the pressed key is invalid, a modifier, or has no value; Don't show it in the TextBox yet
if (isInvalidKey || isModifierKey)
{
// Set the text of the TextBox to the empty buffer
textBox.Text = buffer.ToString();
// Update UI state based on the context item
if (item is null)
ViewModel.EnableAddNewKeyBindingButton = false;
else
item.IsValidKeyBinding = false;
// Prevent key down event in other UIElements from getting invoked
e.Handled = true;
return;
}
// Get the currently pressed modifier keys
var pressedModifiers = HotKeyHelpers.GetCurrentKeyModifiers();
// Append modifier keys to the buffer
if (pressedModifiers.HasFlag(KeyModifiers.Ctrl))
buffer.Append($"{HotKey.LocalizedModifiers.GetValueOrDefault(KeyModifiers.Ctrl)}+");
if (pressedModifiers.HasFlag(KeyModifiers.Alt))
buffer.Append($"{HotKey.LocalizedModifiers.GetValueOrDefault(KeyModifiers.Alt)}+");
if (pressedModifiers.HasFlag(KeyModifiers.Shift))
buffer.Append($"{HotKey.LocalizedModifiers.GetValueOrDefault(KeyModifiers.Shift)}+");
// Append the pressed key to the buffer
buffer.Append(pressedKeyValue);
// Set the text of the TextBox to the constructed key combination
textBox.Text = buffer.ToString();
// Update UI state based on the context item
if (item is null)
ViewModel.EnableAddNewKeyBindingButton = true;
else
item.IsValidKeyBinding = true;
// Prevent key down event in other UIElements from getting invoked
e.Handled = true;
}

@0x5bfa
Copy link
Member

0x5bfa commented May 23, 2024

Definitely.
We might want to use MS.Windows.Interactivity, MS.Window.Interaction.Core and ViewModel.HandleEditorInputCommand or just call ViewModel.HandleEditorInput();

@XTorLukas
Copy link
Contributor Author

Sounds good.

@yaira2
Copy link
Member

yaira2 commented May 24, 2024

Will this have any effect on existing users?

@yaira2 yaira2 added ready to merge Pull requests that are approved and ready to merge and removed needs - code review labels May 24, 2024
@XTorLukas
Copy link
Contributor Author

Will this have any effect on existing users?

It won't be, I just fixed the correct display of mapped keys and added an informative teaching tip about invalid key binding

@yaira2 yaira2 merged commit 8c568f0 into files-community:main May 24, 2024
6 checks passed
@XTorLukas XTorLukas deleted the xtorlukas/Fix-HotKeyCharCorrect branch May 24, 2024 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready to merge Pull requests that are approved and ready to merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants