From 878f2f3893e69a3ccac062aee379838427264bbd Mon Sep 17 00:00:00 2001 From: Mike Griese Date: Wed, 9 Apr 2025 21:33:50 -0500 Subject: [PATCH] Remove the setting - always do both checks (#38701) * Remove the setting - always do both checks * You're not a word --- .../Pages/IndexerPage.cs | 11 +-- .../SettingsManager.cs | 87 ------------------- .../FallbackOpenFileItem.cs | 80 ++++++++--------- .../IndexerCommandsProvider.cs | 7 +- .../Properties/Resources.Designer.cs | 2 +- .../Properties/Resources.resx | 2 +- 6 files changed, 48 insertions(+), 141 deletions(-) delete mode 100644 src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.Indexer/SettingsManager.cs diff --git a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.Indexer/Pages/IndexerPage.cs b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.Indexer/Pages/IndexerPage.cs index ac831490e4..3b3209fb6e 100644 --- a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.Indexer/Pages/IndexerPage.cs +++ b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.Indexer/Pages/IndexerPage.cs @@ -14,31 +14,26 @@ namespace Microsoft.CmdPal.Ext.Indexer; internal sealed partial class IndexerPage : DynamicListPage, IDisposable { private readonly List _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 firstPageData) + public IndexerPage(string query, SearchEngine searchEngine, uint queryCookie, IList firstPageData) { Icon = Icons.FileExplorer; Name = Resources.Indexer_Title; - _settingsManager = settings; _searchEngine = searchEngine; _queryCookie = queryCookie; _indexerListItems.AddRange(firstPageData); diff --git a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.Indexer/SettingsManager.cs b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.Indexer/SettingsManager.cs deleted file mode 100644 index 4be92b2c8b..0000000000 --- a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.Indexer/SettingsManager.cs +++ /dev/null @@ -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 _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(); - } -} diff --git a/src/modules/cmdpal/exts/Microsoft.CmdPal.Ext.Indexer/FallbackOpenFileItem.cs b/src/modules/cmdpal/exts/Microsoft.CmdPal.Ext.Indexer/FallbackOpenFileItem.cs index ac8db97e32..5005a5595f 100644 --- a/src/modules/cmdpal/exts/Microsoft.CmdPal.Ext.Indexer/FallbackOpenFileItem.cs +++ b/src/modules/cmdpal/exts/Microsoft.CmdPal.Ext.Indexer/FallbackOpenFileItem.cs @@ -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; } } } diff --git a/src/modules/cmdpal/exts/Microsoft.CmdPal.Ext.Indexer/IndexerCommandsProvider.cs b/src/modules/cmdpal/exts/Microsoft.CmdPal.Ext.Indexer/IndexerCommandsProvider.cs index ce92569021..c31d7c5176 100644 --- a/src/modules/cmdpal/exts/Microsoft.CmdPal.Ext.Indexer/IndexerCommandsProvider.cs +++ b/src/modules/cmdpal/exts/Microsoft.CmdPal.Ext.Indexer/IndexerCommandsProvider.cs @@ -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)], } ]; } diff --git a/src/modules/cmdpal/exts/Microsoft.CmdPal.Ext.Indexer/Properties/Resources.Designer.cs b/src/modules/cmdpal/exts/Microsoft.CmdPal.Ext.Indexer/Properties/Resources.Designer.cs index 0f27c20688..37a53c1cef 100644 --- a/src/modules/cmdpal/exts/Microsoft.CmdPal.Ext.Indexer/Properties/Resources.Designer.cs +++ b/src/modules/cmdpal/exts/Microsoft.CmdPal.Ext.Indexer/Properties/Resources.Designer.cs @@ -124,7 +124,7 @@ namespace Microsoft.CmdPal.Ext.Indexer.Properties { } /// - /// Looks up a localized string similar to Search for "{0}" related files. + /// Looks up a localized string similar to Search for "{0}" in files. /// internal static string Indexer_fallback_searchPage_title { get { diff --git a/src/modules/cmdpal/exts/Microsoft.CmdPal.Ext.Indexer/Properties/Resources.resx b/src/modules/cmdpal/exts/Microsoft.CmdPal.Ext.Indexer/Properties/Resources.resx index 79682098c7..8e4f70bc1a 100644 --- a/src/modules/cmdpal/exts/Microsoft.CmdPal.Ext.Indexer/Properties/Resources.resx +++ b/src/modules/cmdpal/exts/Microsoft.CmdPal.Ext.Indexer/Properties/Resources.resx @@ -175,6 +175,6 @@ Search files - Search for "{0}" related files + Search for "{0}" in files \ No newline at end of file