mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-01-19 15:03:36 +08:00
[PTRun][Calculator]Replace input with result with '=' key (#31391)
* replace input with result * fix and add test * add options and use result property
This commit is contained in:
parent
e573b7a1b1
commit
92c85630a9
@ -4,6 +4,7 @@
|
||||
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using Wox.Plugin;
|
||||
@ -13,6 +14,19 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
|
||||
[TestClass]
|
||||
public class QueryTests
|
||||
{
|
||||
private static Mock<Main> _main;
|
||||
private static Mock<IPublicAPI> _api;
|
||||
|
||||
[TestInitialize]
|
||||
public void Initialize()
|
||||
{
|
||||
_api = new Mock<IPublicAPI>();
|
||||
_api.Setup(api => api.ChangeQuery(It.IsAny<string>(), It.IsAny<bool>())).Verifiable();
|
||||
|
||||
_main = new Mock<Main>();
|
||||
_main.Object.Init(new PluginInitContext() { API = _api.Object });
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow("=pi(9+)", "Expression wrong or incomplete (Did you forget some parentheses?)")]
|
||||
[DataRow("=pi,", "Expression wrong or incomplete (Did you forget some parentheses?)")]
|
||||
@ -28,12 +42,10 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
|
||||
[DataRow("10+(8*9)/0*7", "Expression contains division by zero")]
|
||||
public void ErrorResultOnInvalidKeywordQuery(string typedString, string expectedResult)
|
||||
{
|
||||
// Setup
|
||||
Mock<Main> main = new();
|
||||
Query expectedQuery = new(typedString, "=");
|
||||
|
||||
// Act
|
||||
var result = main.Object.Query(expectedQuery).FirstOrDefault().SubTitle;
|
||||
var result = _main.Object.Query(expectedQuery).FirstOrDefault().SubTitle;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(expectedResult, result);
|
||||
@ -55,11 +67,10 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
|
||||
public void NoResultOnInvalidGlobalQuery(string typedString)
|
||||
{
|
||||
// Setup
|
||||
Mock<Main> main = new();
|
||||
Query expectedQuery = new(typedString);
|
||||
|
||||
// Act
|
||||
var result = main.Object.Query(expectedQuery).Count;
|
||||
var result = _main.Object.Query(expectedQuery).Count;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(result, 0);
|
||||
@ -79,13 +90,12 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
|
||||
public void NoResultIfQueryEndsWithBinaryOperator(string typedString)
|
||||
{
|
||||
// Setup
|
||||
Mock<Main> main = new();
|
||||
Query expectedQuery = new(typedString);
|
||||
Query expectedQueryWithKeyword = new("=" + typedString, "=");
|
||||
|
||||
// Act
|
||||
var result = main.Object.Query(expectedQuery).Count;
|
||||
var resultWithKeyword = main.Object.Query(expectedQueryWithKeyword).Count;
|
||||
var result = _main.Object.Query(expectedQuery).Count;
|
||||
var resultWithKeyword = _main.Object.Query(expectedQueryWithKeyword).Count;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(result, 0);
|
||||
@ -100,13 +110,12 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
|
||||
public void NoErrorForDivisionByNumberWithDecimalDigits(string typedString)
|
||||
{
|
||||
// Setup
|
||||
Mock<Main> main = new();
|
||||
Query expectedQuery = new(typedString);
|
||||
Query expectedQueryWithKeyword = new("=" + typedString, "=");
|
||||
|
||||
// Act
|
||||
var result = main.Object.Query(expectedQuery).FirstOrDefault().SubTitle;
|
||||
var resultWithKeyword = main.Object.Query(expectedQueryWithKeyword).FirstOrDefault().SubTitle;
|
||||
var result = _main.Object.Query(expectedQuery).FirstOrDefault().SubTitle;
|
||||
var resultWithKeyword = _main.Object.Query(expectedQueryWithKeyword).FirstOrDefault().SubTitle;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(result, "Copy this number to the clipboard");
|
||||
@ -190,13 +199,12 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
|
||||
public void NoErrorForHumanMultiplicationExpressions(string typedString)
|
||||
{
|
||||
// Setup
|
||||
Mock<Main> main = new();
|
||||
Query expectedQuery = new(typedString);
|
||||
Query expectedQueryWithKeyword = new("=" + typedString, "=");
|
||||
|
||||
// Act
|
||||
var result = main.Object.Query(expectedQuery).FirstOrDefault()?.SubTitle;
|
||||
var resultWithKeyword = main.Object.Query(expectedQueryWithKeyword).FirstOrDefault()?.SubTitle;
|
||||
var result = _main.Object.Query(expectedQuery).FirstOrDefault()?.SubTitle;
|
||||
var resultWithKeyword = _main.Object.Query(expectedQueryWithKeyword).FirstOrDefault()?.SubTitle;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual("Copy this number to the clipboard", result);
|
||||
@ -221,13 +229,12 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
|
||||
public void RightAnswerForHumanMultiplicationExpressions(string typedString, double answer)
|
||||
{
|
||||
// Setup
|
||||
Mock<Main> main = new();
|
||||
Query expectedQuery = new(typedString);
|
||||
Query expectedQueryWithKeyword = new("=" + typedString, "=");
|
||||
|
||||
// Act
|
||||
var result = main.Object.Query(expectedQuery).FirstOrDefault()?.Title;
|
||||
var resultWithKeyword = main.Object.Query(expectedQueryWithKeyword).FirstOrDefault()?.Title;
|
||||
var result = _main.Object.Query(expectedQuery).FirstOrDefault()?.Title;
|
||||
var resultWithKeyword = _main.Object.Query(expectedQueryWithKeyword).FirstOrDefault()?.Title;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(answer.ToString(CultureInfo.CurrentCulture), result);
|
||||
@ -242,17 +249,34 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
|
||||
public void RightAnswerForLargeHexadecimalNumbers(string typedString, double answer)
|
||||
{
|
||||
// Setup
|
||||
Mock<Main> main = new();
|
||||
Query expectedQuery = new(typedString);
|
||||
Query expectedQueryWithKeyword = new("=" + typedString, "=");
|
||||
|
||||
// Act
|
||||
var result = main.Object.Query(expectedQuery).FirstOrDefault()?.Title;
|
||||
var resultWithKeyword = main.Object.Query(expectedQueryWithKeyword).FirstOrDefault()?.Title;
|
||||
var result = _main.Object.Query(expectedQuery).FirstOrDefault()?.Title;
|
||||
var resultWithKeyword = _main.Object.Query(expectedQueryWithKeyword).FirstOrDefault()?.Title;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(answer.ToString(CultureInfo.CurrentCulture), result);
|
||||
Assert.AreEqual(answer.ToString(CultureInfo.CurrentCulture), resultWithKeyword);
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow("#", "#1+1=", true)]
|
||||
[DataRow("#", "#1+1", false)]
|
||||
[DataRow("#", "#1/0=", false)]
|
||||
[DataRow("", "1+1=", false)]
|
||||
public void HandleInputReplace(string actionKeyword, string query, bool shouldChangeQuery)
|
||||
{
|
||||
// Setup
|
||||
Query expectedQuery = new(query, actionKeyword);
|
||||
_main.Object.UpdateSettings(new PowerLauncherPluginSettings()); // Default settings
|
||||
|
||||
// Act
|
||||
_main.Object.Query(expectedQuery);
|
||||
|
||||
// Assert
|
||||
_api.Verify(api => api.ChangeQuery(It.IsAny<string>(), It.IsAny<bool>()), shouldChangeQuery ? Times.Once : Times.Never);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,10 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
{
|
||||
public class Main : IPlugin, IPluginI18n, IDisposable, ISettingProvider
|
||||
{
|
||||
private const string InputUseEnglishFormat = nameof(InputUseEnglishFormat);
|
||||
private const string OutputUseEnglishFormat = nameof(OutputUseEnglishFormat);
|
||||
private const string ReplaceInput = nameof(ReplaceInput);
|
||||
|
||||
private static readonly CalculateEngine CalculateEngine = new CalculateEngine();
|
||||
|
||||
private PluginInitContext Context { get; set; }
|
||||
@ -25,6 +29,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
|
||||
private bool _inputUseEnglishFormat;
|
||||
private bool _outputUseEnglishFormat;
|
||||
private bool _replaceInput;
|
||||
|
||||
public string Name => Resources.wox_plugin_calculator_plugin_name;
|
||||
|
||||
@ -40,20 +45,27 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>()
|
||||
{
|
||||
// The number examples has to be created at runtime to prevent translation.
|
||||
new PluginAdditionalOption()
|
||||
new PluginAdditionalOption
|
||||
{
|
||||
Key = "InputUseEnglishFormat",
|
||||
Key = InputUseEnglishFormat,
|
||||
DisplayLabel = Resources.wox_plugin_calculator_in_en_format,
|
||||
DisplayDescription = string.Format(CultureInfo.CurrentCulture, WoxPluginCalculatorInEnFormatDescription, 1000.55.ToString("N2", new CultureInfo("en-us"))),
|
||||
Value = false,
|
||||
},
|
||||
new PluginAdditionalOption()
|
||||
new PluginAdditionalOption
|
||||
{
|
||||
Key = "OutputUseEnglishFormat",
|
||||
Key = OutputUseEnglishFormat,
|
||||
DisplayLabel = Resources.wox_plugin_calculator_out_en_format,
|
||||
DisplayDescription = string.Format(CultureInfo.CurrentCulture, WoxPluginCalculatorOutEnFormatDescription, 1000.55.ToString("G", new CultureInfo("en-us"))),
|
||||
Value = false,
|
||||
},
|
||||
new PluginAdditionalOption
|
||||
{
|
||||
Key = ReplaceInput,
|
||||
DisplayLabel = Resources.wox_plugin_calculator_replace_input,
|
||||
DisplayDescription = Resources.wox_plugin_calculator_replace_input_description,
|
||||
Value = true,
|
||||
},
|
||||
};
|
||||
|
||||
public List<Result> Query(Query query)
|
||||
@ -61,6 +73,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
ArgumentNullException.ThrowIfNull(query);
|
||||
|
||||
bool isGlobalQuery = string.IsNullOrEmpty(query.ActionKeyword);
|
||||
bool replaceInput = _replaceInput && !isGlobalQuery && query.Search.EndsWith('=');
|
||||
CultureInfo inputCulture = _inputUseEnglishFormat ? new CultureInfo("en-us") : CultureInfo.CurrentCulture;
|
||||
CultureInfo outputCulture = _outputUseEnglishFormat ? new CultureInfo("en-us") : CultureInfo.CurrentCulture;
|
||||
|
||||
@ -73,6 +86,11 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
NumberTranslator translator = NumberTranslator.Create(inputCulture, new CultureInfo("en-US"));
|
||||
var input = translator.Translate(query.Search.Normalize(NormalizationForm.FormKC));
|
||||
|
||||
if (replaceInput)
|
||||
{
|
||||
input = input[..^1];
|
||||
}
|
||||
|
||||
if (!CalculateHelper.InputValid(input))
|
||||
{
|
||||
return new List<Result>();
|
||||
@ -89,10 +107,16 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
// If errorMessage is not default then do error handling
|
||||
return errorMessage == default ? new List<Result>() : ErrorHandler.OnError(IconPath, isGlobalQuery, query.RawQuery, errorMessage);
|
||||
}
|
||||
else if (replaceInput)
|
||||
{
|
||||
var pluginResult = ResultHelper.CreateResult(result.RoundedResult, IconPath, inputCulture, outputCulture);
|
||||
Context.API.ChangeQuery($"{query.ActionKeyword} {pluginResult.QueryTextDisplay}");
|
||||
return new List<Result>();
|
||||
}
|
||||
|
||||
return new List<Result>
|
||||
{
|
||||
ResultHelper.CreateResult(result.RoundedResult, IconPath, outputCulture),
|
||||
ResultHelper.CreateResult(result.RoundedResult, IconPath, inputCulture, outputCulture),
|
||||
};
|
||||
}
|
||||
catch (Mages.Core.ParseException)
|
||||
@ -157,18 +181,23 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
{
|
||||
var inputUseEnglishFormat = false;
|
||||
var outputUseEnglishFormat = false;
|
||||
var replaceInput = true;
|
||||
|
||||
if (settings != null && settings.AdditionalOptions != null)
|
||||
{
|
||||
var optionInputEn = settings.AdditionalOptions.FirstOrDefault(x => x.Key == "InputUseEnglishFormat");
|
||||
var optionInputEn = settings.AdditionalOptions.FirstOrDefault(x => x.Key == InputUseEnglishFormat);
|
||||
inputUseEnglishFormat = optionInputEn?.Value ?? inputUseEnglishFormat;
|
||||
|
||||
var optionOutputEn = settings.AdditionalOptions.FirstOrDefault(x => x.Key == "OutputUseEnglishFormat");
|
||||
var optionOutputEn = settings.AdditionalOptions.FirstOrDefault(x => x.Key == OutputUseEnglishFormat);
|
||||
outputUseEnglishFormat = optionOutputEn?.Value ?? outputUseEnglishFormat;
|
||||
|
||||
var optionReplaceInput = settings.AdditionalOptions.FirstOrDefault(x => x.Key == ReplaceInput);
|
||||
replaceInput = optionReplaceInput?.Value ?? replaceInput;
|
||||
}
|
||||
|
||||
_inputUseEnglishFormat = inputUseEnglishFormat;
|
||||
_outputUseEnglishFormat = outputUseEnglishFormat;
|
||||
_replaceInput = replaceInput;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
@ -185,5 +185,23 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.Properties {
|
||||
return ResourceManager.GetString("wox_plugin_calculator_plugin_name", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Replace input appending '='.
|
||||
/// </summary>
|
||||
public static string wox_plugin_calculator_replace_input {
|
||||
get {
|
||||
return ResourceManager.GetString("wox_plugin_calculator_replace_input", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to When using direct activation, appending '=' to the expression will replace the input with the calculated result (e.g. =5*3-2=)..
|
||||
/// </summary>
|
||||
public static string wox_plugin_calculator_replace_input_description {
|
||||
get {
|
||||
return ResourceManager.GetString("wox_plugin_calculator_replace_input_description", resourceCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -161,4 +161,10 @@
|
||||
<data name="wox_plugin_calculator_division_by_zero" xml:space="preserve">
|
||||
<value>Expression contains division by zero</value>
|
||||
</data>
|
||||
<data name="wox_plugin_calculator_replace_input" xml:space="preserve">
|
||||
<value>Replace input if query ends with '='</value>
|
||||
</data>
|
||||
<data name="wox_plugin_calculator_replace_input_description" xml:space="preserve">
|
||||
<value>When using direct activation, appending '=' to the expression will replace the input with the calculated result (e.g. '=5*3-2=' will change the query to '=13').</value>
|
||||
</data>
|
||||
</root>
|
@ -12,12 +12,12 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
{
|
||||
public static class ResultHelper
|
||||
{
|
||||
public static Result CreateResult(CalculateResult result, string iconPath, CultureInfo culture)
|
||||
public static Result CreateResult(CalculateResult result, string iconPath, CultureInfo inputCulture, CultureInfo outputCulture)
|
||||
{
|
||||
return CreateResult(result.RoundedResult, iconPath, culture);
|
||||
return CreateResult(result.RoundedResult, iconPath, inputCulture, outputCulture);
|
||||
}
|
||||
|
||||
public static Result CreateResult(decimal? roundedResult, string iconPath, CultureInfo culture)
|
||||
public static Result CreateResult(decimal? roundedResult, string iconPath, CultureInfo inputCulture, CultureInfo outputCulture)
|
||||
{
|
||||
// Return null when the expression is not a valid calculator query.
|
||||
if (roundedResult == null)
|
||||
@ -28,11 +28,12 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
return new Result
|
||||
{
|
||||
// Using CurrentCulture since this is user facing
|
||||
Title = roundedResult?.ToString(culture),
|
||||
Title = roundedResult?.ToString(outputCulture),
|
||||
IcoPath = iconPath,
|
||||
Score = 300,
|
||||
SubTitle = Properties.Resources.wox_plugin_calculator_copy_number_to_clipboard,
|
||||
Action = c => Action(roundedResult, culture),
|
||||
Action = c => Action(roundedResult, outputCulture),
|
||||
QueryTextDisplay = roundedResult?.ToString(inputCulture),
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -842,9 +842,6 @@ namespace PowerLauncher
|
||||
{
|
||||
if (_viewModel.Plugins.Count > 0 && _viewModel.SelectedPlugin != null)
|
||||
{
|
||||
// Needed to update UI in case the user choose the same plugin multiple times
|
||||
_viewModel.ChangeQueryText(string.Empty);
|
||||
|
||||
_viewModel.ChangeQueryText(_viewModel.SelectedPlugin.Metadata.ActionKeyword, true);
|
||||
SearchBox.QueryTextBox.Focus();
|
||||
|
||||
|
@ -49,7 +49,10 @@ namespace Wox
|
||||
|
||||
public void ChangeQuery(string query, bool requery = false)
|
||||
{
|
||||
_mainVM.ChangeQueryText(query, requery);
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
_mainVM.ChangeQueryText(query, requery);
|
||||
});
|
||||
}
|
||||
|
||||
public void CheckForNewUpdate()
|
||||
|
@ -9,7 +9,6 @@ using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
@ -380,8 +379,15 @@ namespace PowerLauncher.ViewModel
|
||||
/// <param name="requery">Optional Parameter that if true, will automatically execute a query against the updated text</param>
|
||||
public void ChangeQueryText(string queryText, bool requery = false)
|
||||
{
|
||||
var sameQueryText = SystemQueryText == queryText;
|
||||
|
||||
SystemQueryText = queryText;
|
||||
|
||||
if (sameQueryText)
|
||||
{
|
||||
OnPropertyChanged(nameof(SystemQueryText));
|
||||
}
|
||||
|
||||
if (requery)
|
||||
{
|
||||
QueryText = queryText;
|
||||
|
Loading…
Reference in New Issue
Block a user