Merge pull request #1108 from Flow-Launcher/CalculatorDecimalSeparator

Respect Decimal Separator for query not just result
This commit is contained in:
Jeremy Wu 2022-04-24 19:42:30 +10:00 committed by GitHub
commit dffbd35dbd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 12 deletions

View file

@ -6,7 +6,6 @@ using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using Mages.Core;
using Flow.Launcher.Infrastructure.Storage;
using Flow.Launcher.Plugin.Caculator.ViewModels;
using Flow.Launcher.Plugin.Caculator.Views;
@ -25,6 +24,9 @@ namespace Flow.Launcher.Plugin.Caculator
@")+$", RegexOptions.Compiled);
private static readonly Regex RegBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled);
private static Engine MagesEngine;
private const string comma = ",";
private const string dot = ".";
private PluginInitContext Context { get; set; }
private static Settings _settings;
@ -35,7 +37,7 @@ namespace Flow.Launcher.Plugin.Caculator
Context = context;
_settings = context.API.LoadSettingJsonStorage<Settings>();
_viewModel = new SettingsViewModel(_settings);
MagesEngine = new Engine(new Configuration
{
Scope = new Dictionary<string, object>
@ -54,7 +56,19 @@ namespace Flow.Launcher.Plugin.Caculator
try
{
var expression = query.Search.Replace(",", ".");
string expression;
switch (_settings.DecimalSeparator)
{
case DecimalSeparator.Comma:
case DecimalSeparator.UseSystemLocale when CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator == ",":
expression = query.Search.Replace(",", ".");
break;
default:
expression = query.Search;
break;
}
var result = MagesEngine.Interpret(expression);
if (result?.ToString() == "NaN")
@ -76,6 +90,7 @@ namespace Flow.Launcher.Plugin.Caculator
IcoPath = "Images/calculator.png",
Score = 300,
SubTitle = Context.API.GetTranslation("flowlauncher_plugin_calculator_copy_number_to_clipboard"),
CopyText = newResult,
Action = c =>
{
try
@ -119,6 +134,10 @@ namespace Flow.Launcher.Plugin.Caculator
return false;
}
if ((query.Search.Contains(dot) && GetDecimalSeparator() != dot) ||
(query.Search.Contains(comma) && GetDecimalSeparator() != comma))
return false;
return true;
}
@ -142,8 +161,8 @@ namespace Flow.Launcher.Plugin.Caculator
switch (_settings.DecimalSeparator)
{
case DecimalSeparator.UseSystemLocale: return systemDecimalSeperator;
case DecimalSeparator.Dot: return ".";
case DecimalSeparator.Comma: return ",";
case DecimalSeparator.Dot: return dot;
case DecimalSeparator.Comma: return comma;
default: return systemDecimalSeperator;
}
}

View file

@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Flow.Launcher.Plugin.Caculator
{
public class Settings

View file

@ -4,7 +4,7 @@
"Name": "Calculator",
"Description": "Provide mathematical calculations.(Try 5*3-2 in Flow Launcher)",
"Author": "cxfksword",
"Version": "1.1.9",
"Version": "1.1.10",
"Language": "csharp",
"Website": "https://github.com/Flow-Launcher/Flow.Launcher",
"ExecuteFileName": "Flow.Launcher.Plugin.Caculator.dll",