diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/CalculatorIcons.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/CalculatorIcons.cs index eab745bf4a62..e3be5f2149c0 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/CalculatorIcons.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/CalculatorIcons.cs @@ -8,9 +8,11 @@ namespace Microsoft.CmdPal.Ext.Calc.Helper; public static class CalculatorIcons { - public static IconInfo ResultIcon => new IconInfo("\uE94E"); + public static IconInfo ResultIcon => new("\uE94E"); - public static IconInfo ErrorIcon => new IconInfo("\uE783"); + public static IconInfo SaveIcon => new("\uE74E"); + + public static IconInfo ErrorIcon => new("\uE783"); public static IconInfo ProviderIcon => IconHelpers.FromRelativePath("Assets\\Calculator.svg"); } diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/ResultHelper.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/ResultHelper.cs index fb804755cc8f..0fab0a82455b 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/ResultHelper.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/ResultHelper.cs @@ -2,7 +2,10 @@ // 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; +using System.Collections.Generic; using System.Globalization; +using ManagedCommon; using Microsoft.CommandPalette.Extensions.Toolkit; using Windows.Foundation; @@ -25,6 +28,8 @@ public static ListItem CreateResult(decimal? roundedResult, CultureInfo inputCul var saveCommand = new SaveCommand(result); saveCommand.SaveRequested += handleSave; + var copyCommandItem = CreateResult(roundedResult, inputCulture, outputCulture, query); + return new ListItem(saveCommand) { // Using CurrentCulture since this is user facing @@ -32,7 +37,15 @@ public static ListItem CreateResult(decimal? roundedResult, CultureInfo inputCul Title = result, Subtitle = query, TextToSuggest = result, - MoreCommands = [new CommandContextItem(new CopyTextCommand(result))], + MoreCommands = [ + new CommandContextItem(copyCommandItem.Command) + { + Icon = copyCommandItem.Icon, + Title = copyCommandItem.Title, + Subtitle = copyCommandItem.Subtitle, + }, + ..copyCommandItem.MoreCommands, + ], }; } @@ -44,15 +57,47 @@ public static ListItem CreateResult(decimal? roundedResult, CultureInfo inputCul return null; } - var result = roundedResult?.ToString(outputCulture); + var decimalResult = roundedResult?.ToString(outputCulture); + + List context = []; + + if (decimal.IsInteger((decimal)roundedResult)) + { + var i = decimal.ToInt64((decimal)roundedResult); + try + { + var hexResult = "0x" + i.ToString("X", outputCulture); + context.Add(new CommandContextItem(new CopyTextCommand(hexResult) { Name = Properties.Resources.calculator_copy_hex }) + { + Title = hexResult, + }); + } + catch (Exception ex) + { + Logger.LogError("Error parsing hex format", ex); + } - return new ListItem(new CopyTextCommand(result)) + try + { + var binaryResult = "0b" + i.ToString("B", outputCulture); + context.Add(new CommandContextItem(new CopyTextCommand(binaryResult) { Name = Properties.Resources.calculator_copy_binary }) + { + Title = binaryResult, + }); + } + catch (Exception ex) + { + Logger.LogError("Error parsing binary format", ex); + } + } + + return new ListItem(new CopyTextCommand(decimalResult)) { // Using CurrentCulture since this is user facing - Icon = CalculatorIcons.ResultIcon, - Title = result, + Title = decimalResult, Subtitle = query, - TextToSuggest = result, + TextToSuggest = decimalResult, + MoreCommands = context.ToArray(), }; } } diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/SaveCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/SaveCommand.cs index c1372d0458de..850f8511e364 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/SaveCommand.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/SaveCommand.cs @@ -2,7 +2,6 @@ // 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 Microsoft.CmdPal.Ext.Calc.Properties; using Microsoft.CommandPalette.Extensions; using Microsoft.CommandPalette.Extensions.Toolkit; @@ -19,6 +18,7 @@ public sealed partial class SaveCommand : InvokableCommand public SaveCommand(string result) { Name = Resources.calculator_save_command_name; + Icon = CalculatorIcons.SaveIcon; _result = result; } diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/CalculatorListPage.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/CalculatorListPage.cs index 6974570865d9..24f26646c501 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/CalculatorListPage.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/CalculatorListPage.cs @@ -23,9 +23,10 @@ namespace Microsoft.CmdPal.Ext.Calc.Pages; public sealed partial class CalculatorListPage : DynamicListPage { private readonly Lock _resultsLock = new(); - private SettingsManager _settingsManager; - private List _items = []; - private List history = []; + private readonly SettingsManager _settingsManager; + private readonly List _items = []; + private readonly List history = []; + private readonly ListItem _emptyItem; // This is the text that saved when the user click the result. // We need to avoid the double calculation. This may cause some wierd behaviors. @@ -39,6 +40,17 @@ public CalculatorListPage(SettingsManager settings) PlaceholderText = Resources.calculator_placeholder_text; Id = "com.microsoft.cmdpal.calculator"; + _emptyItem = new ListItem(new NoOpCommand()) + { + Title = Resources.calculator_placeholder_text, + Icon = CalculatorIcons.ResultIcon, + }; + EmptyContent = new CommandItem(new NoOpCommand()) + { + Icon = CalculatorIcons.ProviderIcon, + Title = Resources.calculator_placeholder_text, + }; + UpdateSearchText(string.Empty, string.Empty); } @@ -57,6 +69,9 @@ public override void UpdateSearchText(string oldSearch, string newSearch) } skipQuerySearchText = string.Empty; + + _emptyItem.Subtitle = newSearch; + var result = QueryHelper.Query(newSearch, _settingsManager, false, HandleSave); UpdateResult(result); } @@ -70,8 +85,13 @@ private void UpdateResult(ListItem result) if (result != null) { this._items.Add(result); - this._items.AddRange(history); } + else + { + _items.Add(_emptyItem); + } + + this._items.AddRange(history); } RaiseItemsChanged(this._items.Count); diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/FallbackCalculatorItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/FallbackCalculatorItem.cs index 854cd4929585..b309f4ed3a7b 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/FallbackCalculatorItem.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/FallbackCalculatorItem.cs @@ -11,7 +11,7 @@ namespace Microsoft.CmdPal.Ext.Calc.Pages; public sealed partial class FallbackCalculatorItem : FallbackCommandItem { private readonly CopyTextCommand _copyCommand = new(string.Empty); - private SettingsManager _settings; + private readonly SettingsManager _settings; public FallbackCalculatorItem(SettingsManager settings) : base(new NoOpCommand(), Resources.calculator_title) @@ -34,7 +34,7 @@ public override void UpdateQuery(string query) _copyCommand.Name = string.Empty; Title = string.Empty; Subtitle = string.Empty; - + MoreCommands = []; return; } @@ -46,5 +46,7 @@ public override void UpdateQuery(string query) // so that we will still string match the original query // Otherwise, something like 1+2 will have a title of "3" and not match Subtitle = query; + + MoreCommands = result.MoreCommands; } } diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.Designer.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.Designer.cs index ca393f64de20..8cd385be32d7 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.Designer.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.Designer.cs @@ -69,6 +69,15 @@ public static string calculator_calculation_failed_title { } } + /// + /// Looks up a localized string similar to Copy binary. + /// + public static string calculator_copy_binary { + get { + return ResourceManager.GetString("calculator_copy_binary", resourceCulture); + } + } + /// /// Looks up a localized string similar to Copy. /// @@ -78,6 +87,15 @@ public static string calculator_copy_command_name { } } + /// + /// Looks up a localized string similar to Copy hexadecimal. + /// + public static string calculator_copy_hex { + get { + return ResourceManager.GetString("calculator_copy_hex", resourceCulture); + } + } + /// /// Looks up a localized string similar to Calculator. /// diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.resx b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.resx index f78a96593e0d..3c50d3a1c57f 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.resx +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.resx @@ -193,4 +193,10 @@ Result value was either too large or too small for a decimal number + + Copy hexadecimal + + + Copy binary + \ No newline at end of file