mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 04:07:40 +02:00
File search now has filters (#42141)
Closes #39260 Search for all files & folders, folders only, or files only. Enjoy. https://github.com/user-attachments/assets/43ba93f5-dfc5-4e73-8414-547cf99dcfcf
This commit is contained in:
@@ -7,6 +7,7 @@ using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.CmdPal.Ext.Indexer.Indexer;
|
||||
using Microsoft.CmdPal.Ext.Indexer.Properties;
|
||||
using Microsoft.CommandPalette.Extensions;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
@@ -36,6 +37,11 @@ internal sealed partial class IndexerPage : DynamicListPage, IDisposable
|
||||
PlaceholderText = Resources.Indexer_PlaceholderText;
|
||||
_searchEngine = new();
|
||||
_queryCookie = 10;
|
||||
|
||||
var filters = new SearchFilters();
|
||||
filters.PropChanged += Filters_PropChanged;
|
||||
Filters = filters;
|
||||
|
||||
CreateEmptyContent();
|
||||
}
|
||||
|
||||
@@ -49,6 +55,11 @@ internal sealed partial class IndexerPage : DynamicListPage, IDisposable
|
||||
initialQuery = query;
|
||||
SearchText = query;
|
||||
disposeSearchEngine = false;
|
||||
|
||||
var filters = new SearchFilters();
|
||||
filters.PropChanged += Filters_PropChanged;
|
||||
Filters = filters;
|
||||
|
||||
CreateEmptyContent();
|
||||
}
|
||||
|
||||
@@ -79,30 +90,56 @@ internal sealed partial class IndexerPage : DynamicListPage, IDisposable
|
||||
{
|
||||
// {20D04FE0-3AEA-1069-A2D8-08002B30309D} is CLSID for "This PC"
|
||||
const string template = "search-ms:query={0}&crumb=location:::{{20D04FE0-3AEA-1069-A2D8-08002B30309D}}";
|
||||
var encodedSearchText = UrlEncoder.Default.Encode(SearchText);
|
||||
var fullSearchText = FullSearchString(SearchText);
|
||||
var encodedSearchText = UrlEncoder.Default.Encode(fullSearchText);
|
||||
var command = string.Format(CultureInfo.CurrentCulture, template, encodedSearchText);
|
||||
ShellHelpers.OpenInShell(command);
|
||||
}
|
||||
|
||||
public override ICommandItem EmptyContent => _isEmptyQuery ? _noSearchEmptyContent : _nothingFoundEmptyContent;
|
||||
|
||||
private void Filters_PropChanged(object sender, IPropChangedEventArgs args)
|
||||
{
|
||||
PerformSearch(SearchText);
|
||||
}
|
||||
|
||||
public override void UpdateSearchText(string oldSearch, string newSearch)
|
||||
{
|
||||
if (oldSearch != newSearch && newSearch != initialQuery)
|
||||
{
|
||||
_ = Task.Run(() =>
|
||||
{
|
||||
_isEmptyQuery = string.IsNullOrWhiteSpace(newSearch);
|
||||
Query(newSearch);
|
||||
LoadMore();
|
||||
OnPropertyChanged(nameof(EmptyContent));
|
||||
initialQuery = null;
|
||||
});
|
||||
PerformSearch(newSearch);
|
||||
}
|
||||
}
|
||||
|
||||
private void PerformSearch(string newSearch)
|
||||
{
|
||||
var actualSearch = FullSearchString(newSearch);
|
||||
_ = Task.Run(() =>
|
||||
{
|
||||
_isEmptyQuery = string.IsNullOrWhiteSpace(actualSearch);
|
||||
Query(actualSearch);
|
||||
LoadMore();
|
||||
OnPropertyChanged(nameof(EmptyContent));
|
||||
initialQuery = null;
|
||||
});
|
||||
}
|
||||
|
||||
public override IListItem[] GetItems() => [.. _indexerListItems];
|
||||
|
||||
private string FullSearchString(string query)
|
||||
{
|
||||
switch (Filters.CurrentFilterId)
|
||||
{
|
||||
case "folders":
|
||||
return $"{query} kind:folders";
|
||||
case "files":
|
||||
return $"{query} kind:NOT folders";
|
||||
case "all":
|
||||
default:
|
||||
return query;
|
||||
}
|
||||
}
|
||||
|
||||
public override void LoadMore()
|
||||
{
|
||||
IsLoading = true;
|
||||
|
||||
Reference in New Issue
Block a user