Remove the setting - always do both checks (#38701)

* Remove the setting - always do both checks

* You're not a word
This commit is contained in:
Mike Griese
2025-04-09 21:33:50 -05:00
committed by GitHub
parent ec4549e876
commit 878f2f3893
6 changed files with 48 additions and 141 deletions

View File

@@ -14,31 +14,26 @@ namespace Microsoft.CmdPal.Ext.Indexer;
internal sealed partial class IndexerPage : DynamicListPage, IDisposable
{
private readonly List<IListItem> _indexerListItems = [];
private readonly SearchEngine _searchEngine;
private uint _queryCookie;
private SettingsManager _settingsManager;
private SearchEngine _searchEngine;
private string initialQuery = string.Empty;
public IndexerPage(SettingsManager settingsManager)
public IndexerPage()
{
Id = "com.microsoft.indexer.fileSearch";
Icon = Icons.FileExplorer;
Name = Resources.Indexer_Title;
PlaceholderText = Resources.Indexer_PlaceholderText;
_settingsManager = settingsManager;
_searchEngine = new();
_queryCookie = 10;
}
public IndexerPage(SettingsManager settings, string query, SearchEngine searchEngine, uint queryCookie, IList<IListItem> firstPageData)
public IndexerPage(string query, SearchEngine searchEngine, uint queryCookie, IList<IListItem> firstPageData)
{
Icon = Icons.FileExplorer;
Name = Resources.Indexer_Title;
_settingsManager = settings;
_searchEngine = searchEngine;
_queryCookie = queryCookie;
_indexerListItems.AddRange(firstPageData);

View File

@@ -1,87 +0,0 @@
// 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.Collections.Generic;
using System.Globalization;
using System.IO;
using Microsoft.CmdPal.Ext.Indexer.Properties;
using Microsoft.CommandPalette.Extensions.Toolkit;
namespace Microsoft.CmdPal.Ext.Indexer;
public class SettingsManager : JsonSettingsManager
{
private static readonly string _namespace = "indexer";
private static string Namespaced(string propertyName) => $"{_namespace}.{propertyName}";
private static readonly List<ChoiceSetSetting.Choice> _fallbackCommandModeChoice =
[
new ChoiceSetSetting.Choice(Resources.Indexer_Settings_FallbackCommand_FilePathExist, ((int)FallbackCommandMode.FilePathExist).ToString(CultureInfo.CurrentCulture)),
new ChoiceSetSetting.Choice(Resources.Indexer_Settings_FallbackCommand_AlwaysOn, ((int)FallbackCommandMode.AlwaysOn).ToString(CultureInfo.CurrentCulture)),
];
public enum FallbackCommandMode
{
FilePathExist = 0,
AlwaysOn = 1,
}
private readonly ChoiceSetSetting _fallbackCommandMode = new(
Namespaced(nameof(FallbackCommandModeSettings)),
Resources.Indexer_Settings_FallbackCommand_Mode,
Resources.Indexer_Settings_FallbackCommand_Mode,
_fallbackCommandModeChoice);
public FallbackCommandMode FallbackCommandModeSettings
{
get
{
if (string.IsNullOrEmpty(_fallbackCommandMode.Value))
{
// default behavior
return FallbackCommandMode.FilePathExist;
}
// convert _fallbackCommandMode.Value from string to FallbackCommandMode
var success = int.TryParse(_fallbackCommandMode.Value, CultureInfo.InvariantCulture, out var parsedValue);
if (!success)
{
return FallbackCommandMode.FilePathExist;
}
switch (parsedValue)
{
case 0:
return FallbackCommandMode.FilePathExist;
case 1:
return FallbackCommandMode.AlwaysOn;
default:
// default behavior
return FallbackCommandMode.FilePathExist;
}
}
}
internal static string SettingsJsonPath()
{
var directory = Utilities.BaseSettingsPath("Microsoft.CmdPal");
Directory.CreateDirectory(directory);
// now, the state is just next to the exe
return Path.Combine(directory, "settings.json");
}
public SettingsManager()
{
FilePath = SettingsJsonPath();
Settings.Add(_fallbackCommandMode);
// Load settings from file upon initialization
LoadSettings();
Settings.SettingsChanged += (s, a) => this.SaveSettings();
}
}

View File

@@ -7,7 +7,6 @@ using System.Globalization;
using System.IO;
using System.Text;
using Microsoft.CmdPal.Ext.Indexer.Data;
using Microsoft.CmdPal.Ext.Indexer.Indexer;
using Microsoft.CmdPal.Ext.Indexer.Properties;
using Microsoft.CommandPalette.Extensions.Toolkit;
using Windows.Storage.Streams;
@@ -16,20 +15,17 @@ namespace Microsoft.CmdPal.Ext.Indexer;
internal sealed partial class FallbackOpenFileItem : FallbackCommandItem, System.IDisposable
{
private readonly SettingsManager _settingsManager;
private readonly CompositeFormat fallbackItemSearchPageTitleCompositeFormat = CompositeFormat.Parse(Resources.Indexer_fallback_searchPage_title);
private SearchEngine _searchEngine = new();
private readonly SearchEngine _searchEngine = new();
private uint _queryCookie = 10;
public FallbackOpenFileItem(SettingsManager settingsManager)
public FallbackOpenFileItem()
: base(new NoOpCommand(), Resources.Indexer_Find_Path_fallback_display_title)
{
Title = string.Empty;
Subtitle = string.Empty;
_settingsManager = settingsManager;
}
public override void UpdateQuery(string query)
@@ -45,42 +41,33 @@ internal sealed partial class FallbackOpenFileItem : FallbackCommandItem, System
return;
}
if (_settingsManager.FallbackCommandModeSettings == SettingsManager.FallbackCommandMode.FilePathExist)
if (Path.Exists(query))
{
if (Path.Exists(query))
{
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;
// 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
{
}
}
else
try
{
Title = string.Empty;
Subtitle = string.Empty;
Icon = null;
Command = new NoOpCommand();
MoreCommands = null;
var stream = ThumbnailHelper.GetThumbnail(item.FullPath).Result;
if (stream != null)
{
var data = new IconData(RandomAccessStreamReference.CreateFromStream(stream));
Icon = new IconInfo(data, data);
}
}
catch
{
}
return;
}
if (_settingsManager.FallbackCommandModeSettings == SettingsManager.FallbackCommandMode.AlwaysOn)
else
{
_queryCookie++;
@@ -88,8 +75,11 @@ internal sealed partial class FallbackOpenFileItem : FallbackCommandItem, System
{
_searchEngine.Query(query, _queryCookie);
var results = _searchEngine.FetchItems(0, 20, _queryCookie, out var _);
if (results.Count == 0 || (results[0] as IndexerListItem == null))
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();
@@ -98,22 +88,34 @@ internal sealed partial class FallbackOpenFileItem : FallbackCommandItem, System
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;
}
var indexerPage = new IndexerPage(_settingsManager, query, _searchEngine, _queryCookie, results);
// 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;
}
}
}

View File

@@ -10,25 +10,22 @@ namespace Microsoft.CmdPal.Ext.Indexer;
public partial class IndexerCommandsProvider : CommandProvider
{
private static readonly SettingsManager _settingsManager = new();
private readonly FallbackOpenFileItem _fallbackFileItem = new(_settingsManager);
private readonly FallbackOpenFileItem _fallbackFileItem = new();
public IndexerCommandsProvider()
{
Id = "Files";
DisplayName = Resources.IndexerCommandsProvider_DisplayName;
Icon = Icons.FileExplorer;
Settings = _settingsManager.Settings;
}
public override ICommandItem[] TopLevelCommands()
{
return [
new CommandItem(new IndexerPage(_settingsManager))
new CommandItem(new IndexerPage())
{
Title = Resources.Indexer_Title,
Subtitle = Resources.Indexer_Subtitle,
MoreCommands = [new CommandContextItem(_settingsManager.Settings.SettingsPage)],
}
];
}

View File

@@ -124,7 +124,7 @@ namespace Microsoft.CmdPal.Ext.Indexer.Properties {
}
/// <summary>
/// Looks up a localized string similar to Search for &quot;{0}&quot; related files.
/// Looks up a localized string similar to Search for &quot;{0}&quot; in files.
/// </summary>
internal static string Indexer_fallback_searchPage_title {
get {

View File

@@ -175,6 +175,6 @@
<value>Search files</value>
</data>
<data name="Indexer_fallback_searchPage_title" xml:space="preserve">
<value>Search for "{0}" related files</value>
<value>Search for "{0}" in files</value>
</data>
</root>