Skip to content

Commit be90633

Browse files
committed
POC
1 parent 44b4ce5 commit be90633

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

src/Files.App/Views/Layouts/BaseGroupableLayoutPage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected override void UnhookEvents()
9595
ItemManipulationModel.RefreshItemsThumbnailInvoked -= ItemManipulationModel_RefreshItemsThumbnail;
9696
}
9797

98-
protected override void Page_CharacterReceived(UIElement sender, CharacterReceivedRoutedEventArgs args)
98+
protected override void Page_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
9999
{
100100
if (ParentShellPageInstance is null ||
101101
ParentShellPageInstance.CurrentPageType != this.GetType() ||
@@ -112,7 +112,7 @@ focusedElement is PasswordBox ||
112112
DependencyObjectHelpers.FindParent<ContentDialog>(focusedElement) is not null)
113113
return;
114114

115-
base.Page_CharacterReceived(sender, args);
115+
base.Page_PreviewKeyDown(sender, e);
116116
}
117117

118118
// Virtual methods

src/Files.App/Views/Layouts/BaseLayoutPage.cs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Files.App.Helpers.ContextFlyouts;
77
using Files.App.UserControls.Menus;
88
using Files.App.ViewModels.Layouts;
9+
using Microsoft.UI.Input;
910
using Microsoft.UI.Xaml;
1011
using Microsoft.UI.Xaml.Controls;
1112
using Microsoft.UI.Xaml.Controls.Primitives;
@@ -23,6 +24,7 @@
2324
using Windows.Foundation.Collections;
2425
using Windows.Storage;
2526
using Windows.System;
27+
using Windows.UI.Core;
2628
using static Files.App.Helpers.PathNormalization;
2729
using DispatcherQueueTimer = Microsoft.UI.Dispatching.DispatcherQueueTimer;
2830
using SortDirection = Files.App.Data.Enums.SortDirection;
@@ -401,7 +403,7 @@ protected override async void OnNavigatedTo(NavigationEventArgs e)
401403
base.OnNavigatedTo(e);
402404

403405
// Add item jumping handler
404-
CharacterReceived += Page_CharacterReceived;
406+
PreviewKeyDown += Page_PreviewKeyDown; ;
405407

406408
navigationArguments = (NavigationArguments)e.Parameter;
407409
ParentShellPageInstance = navigationArguments.AssociatedTabInstance;
@@ -565,7 +567,7 @@ protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
565567
base.OnNavigatingFrom(e);
566568

567569
// Remove item jumping handler
568-
CharacterReceived -= Page_CharacterReceived;
570+
PreviewKeyDown -= Page_PreviewKeyDown;
569571
FolderSettings!.LayoutModeChangeRequested -= BaseFolderSettings_LayoutModeChangeRequested;
570572
FolderSettings.GroupOptionPreferenceUpdated -= FolderSettings_GroupOptionPreferenceUpdated;
571573
FolderSettings.GroupDirectionPreferenceUpdated -= FolderSettings_GroupDirectionPreferenceUpdated;
@@ -996,12 +998,39 @@ private void RemoveOverflow(CommandBarFlyout contextMenuFlyout)
996998
overflowSeparator.Visibility = Visibility.Collapsed;
997999
}
9981000

999-
protected virtual void Page_CharacterReceived(UIElement sender, CharacterReceivedRoutedEventArgs args)
1001+
protected virtual void Page_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
10001002
{
1001-
if (ParentShellPageInstance!.IsCurrentInstance)
1003+
var key = e.Key;
1004+
var filter = ParentShellPageInstance?.ShellViewModel.FilesAndFoldersFilter;
1005+
1006+
if (ParentShellPageInstance?.IsCurrentInstance != true)
1007+
return;
1008+
1009+
// OemMinus usually maps to virtual key code 189
1010+
bool isOemMinus = (int)key == 189;
1011+
bool isShiftDown = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down);
1012+
1013+
char? keyChar = key switch
1014+
{
1015+
>= VirtualKey.A and <= VirtualKey.Z => (char)('A' + (key - VirtualKey.A)),
1016+
>= VirtualKey.Number0 and <= VirtualKey.Number9 => (char)('0' + (key - VirtualKey.Number0)),
1017+
_ => isOemMinus ? (isShiftDown ? '_' : '-') : null
1018+
};
1019+
1020+
if (keyChar.HasValue &&
1021+
!Path.GetInvalidFileNameChars().Contains(char.ToLowerInvariant(keyChar.Value)))
1022+
{
1023+
string ch = keyChar.Value.ToString().ToLowerInvariant();
1024+
ParentShellPageInstance.ShellViewModel.FilesAndFoldersFilter += ch;
1025+
JumpString += ch;
1026+
}
1027+
else if (key == VirtualKey.Space && !string.IsNullOrEmpty(filter))
1028+
{
1029+
ParentShellPageInstance.ShellViewModel.FilesAndFoldersFilter += " ";
1030+
}
1031+
else if (key == VirtualKey.Back && !string.IsNullOrEmpty(filter))
10021032
{
1003-
char letter = args.Character;
1004-
JumpString += letter.ToString().ToLowerInvariant();
1033+
ParentShellPageInstance.ShellViewModel.FilesAndFoldersFilter = filter[..^1];
10051034
}
10061035
}
10071036

src/Files.App/Views/MainPage.xaml.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ private async Task OnPreviewKeyDownAsync(KeyRoutedEventArgs e)
216216
if (source?.FindAscendantOrSelf<TextBox>() is not null)
217217
break;
218218

219+
// Prevent the back key from executing a command if the filter header is not empty
220+
if (e.Key is VirtualKey.Back && !string.IsNullOrEmpty(SidebarAdaptiveViewModel.PaneHolder?.ActivePaneOrColumn!.ShellViewModel.FilesAndFoldersFilter))
221+
break;
222+
219223
// Execute command for hotkey
220224
var command = Commands[hotKey];
221225

0 commit comments

Comments
 (0)