/ src / modules / cmdpal / Microsoft.CmdPal.UI.ViewModels / ProviderSettings.cs
ProviderSettings.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.Text.Json.Serialization;
 6  
 7  namespace Microsoft.CmdPal.UI.ViewModels;
 8  
 9  public class ProviderSettings
10  {
11      // List of built-in fallbacks that should not have global results enabled by default
12      private readonly string[] _excludedBuiltInFallbacks = [
13          "com.microsoft.cmdpal.builtin.indexer.fallback",
14          "com.microsoft.cmdpal.builtin.calculator.fallback",
15          ];
16  
17      public bool IsEnabled { get; set; } = true;
18  
19      public Dictionary<string, FallbackSettings> FallbackCommands { get; set; } = new();
20  
21      [JsonIgnore]
22      public string ProviderDisplayName { get; set; } = string.Empty;
23  
24      [JsonIgnore]
25      public string ProviderId { get; private set; } = string.Empty;
26  
27      [JsonIgnore]
28      public bool IsBuiltin { get; private set; }
29  
30      public ProviderSettings(CommandProviderWrapper wrapper)
31      {
32          Connect(wrapper);
33      }
34  
35      [JsonConstructor]
36      public ProviderSettings(bool isEnabled)
37      {
38          IsEnabled = isEnabled;
39      }
40  
41      public void Connect(CommandProviderWrapper wrapper)
42      {
43          ProviderId = wrapper.ProviderId;
44          IsBuiltin = wrapper.Extension is null;
45  
46          ProviderDisplayName = wrapper.DisplayName;
47  
48          if (wrapper.FallbackItems.Length > 0)
49          {
50              foreach (var fallback in wrapper.FallbackItems)
51              {
52                  if (!FallbackCommands.ContainsKey(fallback.Id))
53                  {
54                      var enableGlobalResults = IsBuiltin && !_excludedBuiltInFallbacks.Contains(fallback.Id);
55                      FallbackCommands[fallback.Id] = new FallbackSettings(enableGlobalResults);
56                  }
57              }
58          }
59  
60          if (string.IsNullOrEmpty(ProviderId))
61          {
62              throw new InvalidDataException("Did you add a built-in command and forget to set the Id? Make sure you do that!");
63          }
64      }
65  }