Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,14 +28,24 @@ 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
Icon = CalculatorIcons.ResultIcon,
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,
],
};
}

Expand All @@ -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<CommandContextItem> 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(),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ListItem> _items = [];
private List<ListItem> history = [];
private readonly SettingsManager _settingsManager;
private readonly List<ListItem> _items = [];
private readonly List<ListItem> 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.
Expand All @@ -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);
}

Expand All @@ -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);
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -34,7 +34,7 @@ public override void UpdateQuery(string query)
_copyCommand.Name = string.Empty;
Title = string.Empty;
Subtitle = string.Empty;

MoreCommands = [];
return;
}

Expand All @@ -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;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,10 @@
<data name="calculator_not_covert_to_decimal" xml:space="preserve">
<value>Result value was either too large or too small for a decimal number</value>
</data>
<data name="calculator_copy_hex" xml:space="preserve">
<value>Copy hexadecimal</value>
</data>
<data name="calculator_copy_binary" xml:space="preserve">
<value>Copy binary</value>
</data>
</root>