/ src / modules / cmdpal / ext / Microsoft.CmdPal.Ext.Calc / Pages / FallbackCalculatorItem.cs
FallbackCalculatorItem.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 Microsoft.CmdPal.Ext.Calc.Helper;
 6  using Microsoft.CmdPal.Ext.Calc.Properties;
 7  using Microsoft.CommandPalette.Extensions.Toolkit;
 8  
 9  namespace Microsoft.CmdPal.Ext.Calc.Pages;
10  
11  public sealed partial class FallbackCalculatorItem : FallbackCommandItem
12  {
13      private const string _id = "com.microsoft.cmdpal.builtin.calculator.fallback";
14      private readonly CopyTextCommand _copyCommand = new(string.Empty);
15      private readonly ISettingsInterface _settings;
16  
17      public FallbackCalculatorItem(ISettingsInterface settings)
18          : base(new NoOpCommand(), Resources.calculator_title, _id)
19      {
20          Command = _copyCommand;
21          _copyCommand.Name = string.Empty;
22          Title = string.Empty;
23          Subtitle = Resources.calculator_placeholder_text;
24          Icon = Icons.CalculatorIcon;
25          _settings = settings;
26      }
27  
28      public override void UpdateQuery(string query)
29      {
30          var result = QueryHelper.Query(query, _settings, true, out _);
31  
32          if (result is null)
33          {
34              _copyCommand.Text = string.Empty;
35              _copyCommand.Name = string.Empty;
36              Title = string.Empty;
37              Subtitle = string.Empty;
38              MoreCommands = [];
39              return;
40          }
41  
42          _copyCommand.Text = result.Title;
43          _copyCommand.Name = string.IsNullOrWhiteSpace(query) ? string.Empty : Resources.calculator_copy_command_name;
44          Title = result.Title;
45  
46          // we have to make the subtitle into an equation,
47          // so that we will still string match the original query
48          // Otherwise, something like 1+2 will have a title of "3" and not match
49          Subtitle = query;
50  
51          MoreCommands = result.MoreCommands;
52      }
53  }