AllAppsSettings.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.Collections.Generic; 7 using System.IO; 8 using Microsoft.CmdPal.Ext.Apps.Helpers; 9 using Microsoft.CmdPal.Ext.Apps.Programs; 10 using Microsoft.CmdPal.Ext.Apps.Properties; 11 using Microsoft.CommandPalette.Extensions.Toolkit; 12 13 namespace Microsoft.CmdPal.Ext.Apps; 14 15 public class AllAppsSettings : JsonSettingsManager, ISettingsInterface 16 { 17 private static readonly string _namespace = "apps"; 18 19 private static string Namespaced(string propertyName) => $"{_namespace}.{propertyName}"; 20 21 private static readonly List<ChoiceSetSetting.Choice> _searchResultLimitChoices = 22 [ 23 new ChoiceSetSetting.Choice(Resources.limit_0, "0"), 24 new ChoiceSetSetting.Choice(Resources.limit_1, "1"), 25 new ChoiceSetSetting.Choice(Resources.limit_5, "5"), 26 new ChoiceSetSetting.Choice(Resources.limit_10, "10"), 27 ]; 28 29 #pragma warning disable SA1401 // Fields should be private 30 internal static AllAppsSettings Instance = new(); 31 #pragma warning restore SA1401 // Fields should be private 32 33 public DateTime LastIndexTime { get; set; } 34 35 public List<ProgramSource> ProgramSources { get; set; } = []; 36 37 public List<DisabledProgramSource> DisabledProgramSources { get; set; } = []; 38 39 public List<string> ProgramSuffixes { get; set; } = ["bat", "appref-ms", "exe", "lnk", "url"]; 40 41 public List<string> RunCommandSuffixes { get; set; } = ["bat", "appref-ms", "exe", "lnk", "url", "cpl", "msc"]; 42 43 public bool EnableStartMenuSource => _enableStartMenuSource.Value; 44 45 public bool EnableDesktopSource => _enableDesktopSource.Value; 46 47 public bool EnableRegistrySource => _enableRegistrySource.Value; 48 49 public bool EnablePathEnvironmentVariableSource => _enablePathEnvironmentVariableSource.Value; 50 51 private readonly ChoiceSetSetting _searchResultLimitSource = new( 52 Namespaced(nameof(SearchResultLimit)), 53 Resources.limit_fallback_results_source, 54 Resources.limit_fallback_results_source_description, 55 _searchResultLimitChoices); 56 57 public string SearchResultLimit => _searchResultLimitSource.Value ?? string.Empty; 58 59 private readonly ToggleSetting _enableStartMenuSource = new( 60 Namespaced(nameof(EnableStartMenuSource)), 61 Resources.enable_start_menu_source, 62 string.Empty, 63 true); 64 65 private readonly ToggleSetting _enableDesktopSource = new( 66 Namespaced(nameof(EnableDesktopSource)), 67 Resources.enable_desktop_source, 68 string.Empty, 69 true); 70 71 private readonly ToggleSetting _enableRegistrySource = new( 72 Namespaced(nameof(EnableRegistrySource)), 73 Resources.enable_registry_source, 74 string.Empty, 75 false); // This one is very noisy 76 77 private readonly ToggleSetting _enablePathEnvironmentVariableSource = new( 78 Namespaced(nameof(EnablePathEnvironmentVariableSource)), 79 Resources.enable_path_environment_variable_source, 80 string.Empty, 81 false); // this one is very VERY noisy 82 83 public double MinScoreThreshold { get; set; } = 0.75; 84 85 internal const char SuffixSeparator = ';'; 86 87 internal static string SettingsJsonPath() 88 { 89 var directory = Utilities.BaseSettingsPath("Microsoft.CmdPal"); 90 Directory.CreateDirectory(directory); 91 92 // now, the state is just next to the exe 93 return Path.Combine(directory, "settings.json"); 94 } 95 96 public AllAppsSettings() 97 { 98 FilePath = SettingsJsonPath(); 99 100 Settings.Add(_enableStartMenuSource); 101 Settings.Add(_enableDesktopSource); 102 Settings.Add(_enableRegistrySource); 103 Settings.Add(_enablePathEnvironmentVariableSource); 104 Settings.Add(_searchResultLimitSource); 105 106 // Load settings from file upon initialization 107 LoadSettings(); 108 109 Settings.SettingsChanged += (s, a) => this.SaveSettings(); 110 } 111 }