mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 17:56:44 +02:00
## Summary of the Pull Request 1. Add new setting to control this behaviour 2. Support always on and only when file exist in the fallback command 3. Change the condition of updateFallbackCommand in toplevelvm demo: https://github.com/user-attachments/assets/19e4ced3-30ad-44f4-8f3a-93620f46bb3d ## PR Checklist - [x] **Closes:** #38370 --------- Co-authored-by: Yu Leng (from Dev Box) <yuleng@microsoft.com> Co-authored-by: Mike Griese <migrie@microsoft.com>
129 lines
4.2 KiB
C#
129 lines
4.2 KiB
C#
// Copyright (c) Microsoft Corporation
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Text;
|
|
using Microsoft.CmdPal.Ext.Indexer.Data;
|
|
using Microsoft.CmdPal.Ext.Indexer.Properties;
|
|
using Microsoft.CommandPalette.Extensions.Toolkit;
|
|
using Windows.Storage.Streams;
|
|
|
|
namespace Microsoft.CmdPal.Ext.Indexer;
|
|
|
|
internal sealed partial class FallbackOpenFileItem : FallbackCommandItem, System.IDisposable
|
|
{
|
|
private readonly CompositeFormat fallbackItemSearchPageTitleCompositeFormat = CompositeFormat.Parse(Resources.Indexer_fallback_searchPage_title);
|
|
|
|
private readonly SearchEngine _searchEngine = new();
|
|
|
|
private uint _queryCookie = 10;
|
|
|
|
public FallbackOpenFileItem()
|
|
: base(new NoOpCommand(), Resources.Indexer_Find_Path_fallback_display_title)
|
|
{
|
|
Title = string.Empty;
|
|
Subtitle = string.Empty;
|
|
}
|
|
|
|
public override void UpdateQuery(string query)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(query))
|
|
{
|
|
Command = new NoOpCommand();
|
|
Title = string.Empty;
|
|
Subtitle = string.Empty;
|
|
Icon = null;
|
|
MoreCommands = null;
|
|
|
|
return;
|
|
}
|
|
|
|
if (Path.Exists(query))
|
|
{
|
|
// Exit 1: The query is a direct path to a file. Great! Return it.
|
|
var item = new IndexerItem() { FullPath = query, FileName = Path.GetFileName(query) };
|
|
var listItemForUs = new IndexerListItem(item, IncludeBrowseCommand.AsDefault);
|
|
Command = listItemForUs.Command;
|
|
MoreCommands = listItemForUs.MoreCommands;
|
|
Subtitle = item.FileName;
|
|
Title = item.FullPath;
|
|
Icon = listItemForUs.Icon;
|
|
|
|
try
|
|
{
|
|
var stream = ThumbnailHelper.GetThumbnail(item.FullPath).Result;
|
|
if (stream != null)
|
|
{
|
|
var data = new IconData(RandomAccessStreamReference.CreateFromStream(stream));
|
|
Icon = new IconInfo(data, data);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
_queryCookie++;
|
|
|
|
try
|
|
{
|
|
_searchEngine.Query(query, _queryCookie);
|
|
var results = _searchEngine.FetchItems(0, 20, _queryCookie, out var _);
|
|
|
|
if (results.Count == 0 || ((results[0] as IndexerListItem) == null))
|
|
{
|
|
// Exit 2: We searched for the file, and found nothing. Oh well.
|
|
// Hide ourselves.
|
|
Title = string.Empty;
|
|
Subtitle = string.Empty;
|
|
Command = new NoOpCommand();
|
|
return;
|
|
}
|
|
|
|
if (results.Count == 1)
|
|
{
|
|
// Exit 3: We searched for the file, and found exactly one thing. Awesome!
|
|
// Return it.
|
|
Title = results[0].Title;
|
|
Subtitle = results[0].Subtitle;
|
|
Icon = results[0].Icon;
|
|
Command = results[0].Command;
|
|
MoreCommands = results[0].MoreCommands;
|
|
|
|
return;
|
|
}
|
|
|
|
// Exit 4: We found more than one result. Make our command take
|
|
// us to the file search page, prepopulated with this search.
|
|
var indexerPage = new IndexerPage(query, _searchEngine, _queryCookie, results);
|
|
Title = string.Format(CultureInfo.CurrentCulture, fallbackItemSearchPageTitleCompositeFormat, query);
|
|
Icon = Icons.FileExplorer;
|
|
Subtitle = Resources.Indexer_Subtitle;
|
|
Command = indexerPage;
|
|
|
|
return;
|
|
}
|
|
catch
|
|
{
|
|
Title = string.Empty;
|
|
Subtitle = string.Empty;
|
|
Icon = null;
|
|
Command = new NoOpCommand();
|
|
MoreCommands = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_searchEngine.Dispose();
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
}
|