Skip to content

Commit 9d2ffd3

Browse files
committed
POC
1 parent 44b4ce5 commit 9d2ffd3

File tree

12 files changed

+183
-11
lines changed

12 files changed

+183
-11
lines changed

src/Files.App/Actions/Show/ToggleFilterHeaderAction.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public Task ExecuteAsync(object? parameter = null)
3131

3232
if (IsOn)
3333
ContentPageContext.ShellPage!.ShellViewModel.InvokeFocusFilterHeader();
34+
else
35+
{
36+
// Clear the filter query when the header is hidden
37+
ContentPageContext.ShellPage!.ShellViewModel.FilesAndFoldersFilter = string.Empty;
38+
}
3439

3540
return Task.CompletedTask;
3641
}

src/Files.App/Data/Contracts/IFoldersSettingsService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,10 @@ public interface IFoldersSettingsService : IBaseSettingsService, INotifyProperty
8989
/// Gets or sets a value indicating which format to use when displaying item sizes.
9090
/// </summary>
9191
SizeUnitTypes SizeUnitFormat { get; set; }
92+
93+
/// <summary>
94+
/// Gets or sets a value indicating the keyboard typing behavior.
95+
/// </summary>
96+
KeyboardTypingBehavior KeyboardTypingBehavior { get; set; }
9297
}
9398
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Files Community
2+
// Licensed under the MIT License.
3+
4+
namespace Files.App.Data.Enums
5+
{
6+
public enum KeyboardTypingBehavior
7+
{
8+
/// <summary>
9+
/// Jump to matching item.
10+
/// </summary>
11+
JumpToFile,
12+
13+
/// <summary>
14+
/// Filter items.
15+
/// </summary>
16+
FilterItems
17+
}
18+
}

src/Files.App/Services/Settings/FoldersSettingsService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ public SizeUnitTypes SizeUnitFormat
114114
set => Set(value);
115115
}
116116

117+
/// <inheritdoc/>
118+
public KeyboardTypingBehavior KeyboardTypingBehavior
119+
{
120+
get => (KeyboardTypingBehavior)Get((long)KeyboardTypingBehavior.JumpToFile);
121+
set => Set((long)value);
122+
}
123+
117124
protected override void RaiseOnSettingChangedEvent(object sender, SettingChangedEventArgs e)
118125
{
119126
base.RaiseOnSettingChangedEvent(sender, e);

src/Files.App/Strings/en-US/Resources.resw

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4272,5 +4272,14 @@
42724272
</data>
42734273
<data name="Filename" xml:space="preserve">
42744274
<value>Filename</value>
4275+
</data>
4276+
<data name="KeyboardTypingBehavior" xml:space="preserve">
4277+
<value>Behavior when typing in the file area</value>
4278+
</data>
4279+
<data name="JumpToFile" xml:space="preserve">
4280+
<value>Jump to file</value>
4281+
</data>
4282+
<data name="FilterItems" xml:space="preserve">
4283+
<value>Filter items</value>
42754284
</data>
42764285
</root>

src/Files.App/ViewModels/Settings/FoldersViewModel.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public sealed partial class FoldersViewModel : ObservableObject
99

1010

1111
public Dictionary<SizeUnitTypes, string> SizeUnitsOptions { get; private set; } = [];
12+
public Dictionary<KeyboardTypingBehavior, string> KeyboardTypingBehaviors { get; private set; } = [];
1213

1314
public FoldersViewModel()
1415
{
@@ -18,6 +19,11 @@ public FoldersViewModel()
1819
SizeUnitsOptions.Add(SizeUnitTypes.BinaryUnits, Strings.Binary.GetLocalizedResource());
1920
SizeUnitsOptions.Add(SizeUnitTypes.DecimalUnits, Strings.Decimal.GetLocalizedResource());
2021
SizeUnitFormat = SizeUnitsOptions[UserSettingsService.FoldersSettingsService.SizeUnitFormat];
22+
23+
// Keyboard typing behavior
24+
KeyboardTypingBehaviors.Add(Data.Enums.KeyboardTypingBehavior.JumpToFile, Strings.JumpToFile.GetLocalizedResource());
25+
KeyboardTypingBehaviors.Add(Data.Enums.KeyboardTypingBehavior.FilterItems, Strings.FilterItems.GetLocalizedResource());
26+
KeyboardTypingBehavior = KeyboardTypingBehaviors[UserSettingsService.FoldersSettingsService.KeyboardTypingBehavior];
2127
}
2228

2329
// Properties
@@ -274,5 +280,18 @@ public string SizeUnitFormat
274280
}
275281
}
276282
}
283+
284+
private string keyboardTypingBehavior;
285+
public string KeyboardTypingBehavior
286+
{
287+
get => keyboardTypingBehavior;
288+
set
289+
{
290+
if (SetProperty(ref keyboardTypingBehavior, value))
291+
{
292+
UserSettingsService.FoldersSettingsService.KeyboardTypingBehavior = KeyboardTypingBehaviors.First(e => e.Value == value).Key;
293+
}
294+
}
295+
}
277296
}
278297
}

src/Files.App/ViewModels/ShellViewModel.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ public string? FilesAndFoldersFilter
754754
{
755755
if (SetProperty(ref _filesAndFoldersFilter, value))
756756
{
757+
// Apply the updated filter to the files and folders list
757758
FilesAndFolderFilterUpdated();
758759
}
759760
}
@@ -764,6 +765,27 @@ private void FilesAndFolderFilterUpdated()
764765
_ = ApplyFilesAndFoldersChangesAsync();
765766
}
766767

768+
/// <summary>
769+
/// Clears the files and folder filter.
770+
/// This is used when the directory is changed or refreshed.
771+
/// </summary>
772+
private void ClearFilesAndFolderFilter()
773+
{
774+
// Hide the filter header if:
775+
// - Keyboard behavior is set to filter items
776+
// - A filter is currently applied
777+
//
778+
// Keep the header visible if:
779+
// - The filter is already empty (e.g. opened manually)
780+
if (UserSettingsService.FoldersSettingsService.KeyboardTypingBehavior == KeyboardTypingBehavior.FilterItems &&
781+
!string.IsNullOrEmpty(FilesAndFoldersFilter))
782+
{
783+
UserSettingsService.GeneralSettingsService.ShowFilterHeader = false;
784+
}
785+
786+
// Clear the filter
787+
FilesAndFoldersFilter = string.Empty;
788+
}
767789

768790
// Apply changes immediately after manipulating on filesAndFolders completed
769791
public async Task ApplyFilesAndFoldersChangesAsync()
@@ -1886,7 +1908,7 @@ await dispatcherQueue.EnqueueOrInvokeAsync(() =>
18861908
{
18871909
GetDesktopIniFileData();
18881910
CheckForBackgroundImage();
1889-
FilesAndFoldersFilter = null;
1911+
ClearFilesAndFolderFilter();
18901912
},
18911913
Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
18921914
});

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: 76 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;
@@ -40,6 +42,8 @@ public abstract class BaseLayoutPage : Page, IBaseLayoutPage, INotifyPropertyCha
4042
protected IFileTagsSettingsService FileTagsSettingsService { get; } = Ioc.Default.GetService<IFileTagsSettingsService>()!;
4143
protected IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetService<IUserSettingsService>()!;
4244
protected ILayoutSettingsService LayoutSettingsService { get; } = Ioc.Default.GetService<ILayoutSettingsService>()!;
45+
protected IGeneralSettingsService GeneralSettingsService { get; } = Ioc.Default.GetService<IGeneralSettingsService>()!;
46+
protected IFoldersSettingsService FoldersSettingsService { get; } = Ioc.Default.GetService<IFoldersSettingsService>()!;
4347
protected ICommandManager Commands { get; } = Ioc.Default.GetRequiredService<ICommandManager>();
4448
public InfoPaneViewModel InfoPaneViewModel { get; } = Ioc.Default.GetRequiredService<InfoPaneViewModel>();
4549
protected readonly IWindowContext WindowContext = Ioc.Default.GetRequiredService<IWindowContext>();
@@ -401,7 +405,7 @@ protected override async void OnNavigatedTo(NavigationEventArgs e)
401405
base.OnNavigatedTo(e);
402406

403407
// Add item jumping handler
404-
CharacterReceived += Page_CharacterReceived;
408+
PreviewKeyDown += Page_PreviewKeyDown; ;
405409

406410
navigationArguments = (NavigationArguments)e.Parameter;
407411
ParentShellPageInstance = navigationArguments.AssociatedTabInstance;
@@ -565,7 +569,7 @@ protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
565569
base.OnNavigatingFrom(e);
566570

567571
// Remove item jumping handler
568-
CharacterReceived -= Page_CharacterReceived;
572+
PreviewKeyDown -= Page_PreviewKeyDown;
569573
FolderSettings!.LayoutModeChangeRequested -= BaseFolderSettings_LayoutModeChangeRequested;
570574
FolderSettings.GroupOptionPreferenceUpdated -= FolderSettings_GroupOptionPreferenceUpdated;
571575
FolderSettings.GroupDirectionPreferenceUpdated -= FolderSettings_GroupDirectionPreferenceUpdated;
@@ -996,12 +1000,78 @@ private void RemoveOverflow(CommandBarFlyout contextMenuFlyout)
9961000
overflowSeparator.Visibility = Visibility.Collapsed;
9971001
}
9981002

999-
protected virtual void Page_CharacterReceived(UIElement sender, CharacterReceivedRoutedEventArgs args)
1003+
protected virtual void Page_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
10001004
{
1001-
if (ParentShellPageInstance!.IsCurrentInstance)
1005+
var shellPage = ParentShellPageInstance;
1006+
if (shellPage?.IsCurrentInstance != true)
1007+
return;
1008+
1009+
var pressedKey = e.Key;
1010+
var currentFilter = shellPage.ShellViewModel.FilesAndFoldersFilter ?? string.Empty;
1011+
var isFilterModeOn = FoldersSettingsService.KeyboardTypingBehavior == KeyboardTypingBehavior.FilterItems;
1012+
var isShiftPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Shift)
1013+
.HasFlag(CoreVirtualKeyStates.Down);
1014+
1015+
// Get typed character
1016+
var typedCharacter = pressedKey switch
10021017
{
1003-
char letter = args.Character;
1004-
JumpString += letter.ToString().ToLowerInvariant();
1018+
>= VirtualKey.A and <= VirtualKey.Z => (char)('A' + (pressedKey - VirtualKey.A)),
1019+
>= VirtualKey.Number0 and <= VirtualKey.Number9 => (char)('0' + (pressedKey - VirtualKey.Number0)),
1020+
_ when (int)pressedKey == (int)Keys.OemMinus => isShiftPressed ? '_' : '-',
1021+
_ when (int)pressedKey == (int)Keys.OemPeriod => '.',
1022+
_ => (char?)null
1023+
};
1024+
1025+
// Handle valid character input
1026+
if (typedCharacter.HasValue && !Path.GetInvalidFileNameChars().Contains(char.ToLowerInvariant(typedCharacter.Value)))
1027+
{
1028+
var lowerCharString = char.ToLowerInvariant(typedCharacter.Value).ToString();
1029+
1030+
if (isFilterModeOn)
1031+
{
1032+
if (!GeneralSettingsService.ShowFilterHeader)
1033+
GeneralSettingsService.ShowFilterHeader = true;
1034+
shellPage.ShellViewModel.FilesAndFoldersFilter += lowerCharString;
1035+
}
1036+
else
1037+
{
1038+
JumpString += lowerCharString;
1039+
}
1040+
}
1041+
// Handle special keys in filter mode
1042+
else if (isFilterModeOn && !string.IsNullOrEmpty(currentFilter))
1043+
{
1044+
switch (pressedKey)
1045+
{
1046+
case VirtualKey.Space:
1047+
shellPage.ShellViewModel.FilesAndFoldersFilter += " ";
1048+
break;
1049+
1050+
case VirtualKey.Back when currentFilter.Length > 1:
1051+
shellPage.ShellViewModel.FilesAndFoldersFilter = currentFilter[..^1];
1052+
break;
1053+
1054+
case VirtualKey.Back when currentFilter.Length == 1:
1055+
shellPage.ShellViewModel.FilesAndFoldersFilter = string.Empty;
1056+
GeneralSettingsService.ShowFilterHeader = false;
1057+
break;
1058+
}
1059+
}
1060+
1061+
// Update selection in filter mode
1062+
if (isFilterModeOn)
1063+
{
1064+
var filterText = shellPage.ShellViewModel.FilesAndFoldersFilter;
1065+
var matchedItem = shellPage.ShellViewModel.FilesAndFolders
1066+
.FirstOrDefault(item => !string.IsNullOrEmpty(filterText) &&
1067+
item.Name?.Contains(filterText, StringComparison.OrdinalIgnoreCase) == true);
1068+
1069+
if (matchedItem != null)
1070+
{
1071+
ItemManipulationModel.SetSelectedItem(matchedItem);
1072+
ItemManipulationModel.ScrollIntoView(matchedItem);
1073+
ItemManipulationModel.FocusSelectedItems();
1074+
}
10051075
}
10061076
}
10071077

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

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

219+
// Prevent the Back and Space keys from executing a command if the keyboard
220+
// typing behavior is set to filter items and a filter is currently applied.
221+
if ((e.Key is VirtualKey.Back or VirtualKey.Space) &&
222+
UserSettingsService.FoldersSettingsService.KeyboardTypingBehavior == KeyboardTypingBehavior.FilterItems &&
223+
!string.IsNullOrEmpty(SidebarAdaptiveViewModel.PaneHolder?.ActivePaneOrColumn!.ShellViewModel.FilesAndFoldersFilter))
224+
break;
225+
219226
// Execute command for hotkey
220227
var command = Commands[hotKey];
221228

0 commit comments

Comments
 (0)