-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Feature: Added option to filter items when typing #17339
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) Files Community | ||
// Licensed under the MIT License. | ||
|
||
namespace Files.App.Data.Enums | ||
{ | ||
public enum KeyboardTypingBehavior | ||
{ | ||
/// <summary> | ||
/// Jump to matching item. | ||
/// </summary> | ||
JumpToFile, | ||
|
||
/// <summary> | ||
/// Filter items. | ||
/// </summary> | ||
FilterItems | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
using System.IO; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices.ComTypes; | ||
using System.Text; | ||
using Vanara.Extensions; | ||
using Vanara.PInvoke; | ||
using Windows.ApplicationModel.DataTransfer; | ||
|
@@ -23,6 +24,7 @@ | |
using Windows.Foundation.Collections; | ||
using Windows.Storage; | ||
using Windows.System; | ||
using Windows.Win32; | ||
using static Files.App.Helpers.PathNormalization; | ||
using DispatcherQueueTimer = Microsoft.UI.Dispatching.DispatcherQueueTimer; | ||
using SortDirection = Files.App.Data.Enums.SortDirection; | ||
|
@@ -40,6 +42,8 @@ public abstract class BaseLayoutPage : Page, IBaseLayoutPage, INotifyPropertyCha | |
protected IFileTagsSettingsService FileTagsSettingsService { get; } = Ioc.Default.GetService<IFileTagsSettingsService>()!; | ||
protected IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetService<IUserSettingsService>()!; | ||
protected ILayoutSettingsService LayoutSettingsService { get; } = Ioc.Default.GetService<ILayoutSettingsService>()!; | ||
protected IGeneralSettingsService GeneralSettingsService { get; } = Ioc.Default.GetService<IGeneralSettingsService>()!; | ||
protected IFoldersSettingsService FoldersSettingsService { get; } = Ioc.Default.GetService<IFoldersSettingsService>()!; | ||
protected ICommandManager Commands { get; } = Ioc.Default.GetRequiredService<ICommandManager>(); | ||
public InfoPaneViewModel InfoPaneViewModel { get; } = Ioc.Default.GetRequiredService<InfoPaneViewModel>(); | ||
protected readonly IWindowContext WindowContext = Ioc.Default.GetRequiredService<IWindowContext>(); | ||
|
@@ -401,7 +405,7 @@ protected override async void OnNavigatedTo(NavigationEventArgs e) | |
base.OnNavigatedTo(e); | ||
|
||
// Add item jumping handler | ||
CharacterReceived += Page_CharacterReceived; | ||
PreviewKeyDown += Page_PreviewKeyDown; | ||
|
||
navigationArguments = (NavigationArguments)e.Parameter; | ||
ParentShellPageInstance = navigationArguments.AssociatedTabInstance; | ||
|
@@ -565,7 +569,7 @@ protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) | |
base.OnNavigatingFrom(e); | ||
|
||
// Remove item jumping handler | ||
CharacterReceived -= Page_CharacterReceived; | ||
PreviewKeyDown -= Page_PreviewKeyDown; | ||
FolderSettings!.LayoutModeChangeRequested -= BaseFolderSettings_LayoutModeChangeRequested; | ||
FolderSettings.GroupOptionPreferenceUpdated -= FolderSettings_GroupOptionPreferenceUpdated; | ||
FolderSettings.GroupDirectionPreferenceUpdated -= FolderSettings_GroupDirectionPreferenceUpdated; | ||
|
@@ -996,12 +1000,82 @@ private void RemoveOverflow(CommandBarFlyout contextMenuFlyout) | |
overflowSeparator.Visibility = Visibility.Collapsed; | ||
} | ||
|
||
protected virtual void Page_CharacterReceived(UIElement sender, CharacterReceivedRoutedEventArgs args) | ||
protected virtual void Page_PreviewKeyDown(object sender, KeyRoutedEventArgs e) | ||
{ | ||
if (ParentShellPageInstance!.IsCurrentInstance) | ||
var shellPage = ParentShellPageInstance; | ||
if (shellPage?.IsCurrentInstance != true) | ||
return; | ||
|
||
var pressedKey = e.Key; | ||
var currentFilter = shellPage.ShellViewModel.FilesAndFoldersFilter ?? string.Empty; | ||
var isFilterModeOn = FoldersSettingsService.KeyboardTypingBehavior == KeyboardTypingBehavior.FilterItems; | ||
var buffer = new StringBuilder(4); | ||
var state = new byte[256]; | ||
char? typedCharacter = null; | ||
|
||
if (PInvoke.GetKeyboardState(state)) | ||
{ | ||
var virtualKey = (uint)pressedKey; | ||
var scanCode = PInvoke.MapVirtualKey(virtualKey, 0); | ||
var keyboardLayout = PInvoke.GetKeyboardLayout(0); | ||
|
||
if (Win32PInvoke.ToUnicodeEx(virtualKey, scanCode, state, buffer, buffer.Capacity, 0, keyboardLayout) > 0) | ||
{ | ||
var character = buffer[^1]; | ||
Comment on lines
+1022
to
+1024
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
yaira2 marked this conversation as resolved.
Show resolved
Hide resolved
yaira2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (character != ' ') | ||
typedCharacter = character; | ||
} | ||
yaira2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if (!typedCharacter.HasValue) | ||
return; | ||
|
||
// Handle valid character input | ||
if (!FilesystemHelpers.ContainsRestrictedCharacters(char.ToString(typedCharacter.Value))) | ||
{ | ||
var lowerCharString = char.ToLowerInvariant(typedCharacter.Value).ToString(); | ||
|
||
if (isFilterModeOn) | ||
{ | ||
if (!GeneralSettingsService.ShowFilterHeader) | ||
GeneralSettingsService.ShowFilterHeader = true; | ||
shellPage.ShellViewModel.FilesAndFoldersFilter += lowerCharString; | ||
} | ||
else | ||
{ | ||
JumpString += lowerCharString; | ||
} | ||
} | ||
// Handle special keys in filter mode | ||
else if (isFilterModeOn && !string.IsNullOrEmpty(currentFilter)) | ||
{ | ||
switch (pressedKey) | ||
{ | ||
case VirtualKey.Back when currentFilter.Length > 1: | ||
shellPage.ShellViewModel.FilesAndFoldersFilter = currentFilter[..^1]; | ||
break; | ||
|
||
case VirtualKey.Back when currentFilter.Length == 1: | ||
shellPage.ShellViewModel.FilesAndFoldersFilter = string.Empty; | ||
GeneralSettingsService.ShowFilterHeader = false; | ||
break; | ||
} | ||
} | ||
|
||
// Update selection in filter mode | ||
if (isFilterModeOn) | ||
{ | ||
char letter = args.Character; | ||
JumpString += letter.ToString().ToLowerInvariant(); | ||
var filterText = shellPage.ShellViewModel.FilesAndFoldersFilter; | ||
var matchedItem = shellPage.ShellViewModel.FilesAndFolders | ||
.FirstOrDefault(item => !string.IsNullOrEmpty(filterText) && | ||
item.Name?.Contains(filterText, StringComparison.OrdinalIgnoreCase) == true); | ||
|
||
if (matchedItem != null) | ||
{ | ||
ItemManipulationModel.SetSelectedItem(matchedItem); | ||
ItemManipulationModel.ScrollIntoView(matchedItem); | ||
ItemManipulationModel.FocusSelectedItems(); | ||
} | ||
yaira2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code uses
Win32PInvoke.ToUnicodeEx
but importsWindows.Win32.PInvoke
. There's an inconsistency in the API usage - it should bePInvoke.ToUnicodeEx
to match the other PInvoke calls in the method.Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fyi @0x5bfa