Skip to content

Commit dffbd35

Browse files
authored
Merge pull request #1108 from Flow-Launcher/CalculatorDecimalSeparator
Respect Decimal Separator for query not just result
2 parents 15fd62a + 66f835f commit dffbd35

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

Plugins/Flow.Launcher.Plugin.Calculator/Main.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Windows;
77
using System.Windows.Controls;
88
using Mages.Core;
9-
using Flow.Launcher.Infrastructure.Storage;
109
using Flow.Launcher.Plugin.Caculator.ViewModels;
1110
using Flow.Launcher.Plugin.Caculator.Views;
1211

@@ -25,6 +24,9 @@ public class Main : IPlugin, IPluginI18n, ISettingProvider
2524
@")+$", RegexOptions.Compiled);
2625
private static readonly Regex RegBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled);
2726
private static Engine MagesEngine;
27+
private const string comma = ",";
28+
private const string dot = ".";
29+
2830
private PluginInitContext Context { get; set; }
2931

3032
private static Settings _settings;
@@ -35,7 +37,7 @@ public void Init(PluginInitContext context)
3537
Context = context;
3638
_settings = context.API.LoadSettingJsonStorage<Settings>();
3739
_viewModel = new SettingsViewModel(_settings);
38-
40+
3941
MagesEngine = new Engine(new Configuration
4042
{
4143
Scope = new Dictionary<string, object>
@@ -54,7 +56,19 @@ public List<Result> Query(Query query)
5456

5557
try
5658
{
57-
var expression = query.Search.Replace(",", ".");
59+
string expression;
60+
61+
switch (_settings.DecimalSeparator)
62+
{
63+
case DecimalSeparator.Comma:
64+
case DecimalSeparator.UseSystemLocale when CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator == ",":
65+
expression = query.Search.Replace(",", ".");
66+
break;
67+
default:
68+
expression = query.Search;
69+
break;
70+
}
71+
5872
var result = MagesEngine.Interpret(expression);
5973

6074
if (result?.ToString() == "NaN")
@@ -76,6 +90,7 @@ public List<Result> Query(Query query)
7690
IcoPath = "Images/calculator.png",
7791
Score = 300,
7892
SubTitle = Context.API.GetTranslation("flowlauncher_plugin_calculator_copy_number_to_clipboard"),
93+
CopyText = newResult,
7994
Action = c =>
8095
{
8196
try
@@ -119,6 +134,10 @@ private bool CanCalculate(Query query)
119134
return false;
120135
}
121136

137+
if ((query.Search.Contains(dot) && GetDecimalSeparator() != dot) ||
138+
(query.Search.Contains(comma) && GetDecimalSeparator() != comma))
139+
return false;
140+
122141
return true;
123142
}
124143

@@ -142,8 +161,8 @@ private string GetDecimalSeparator()
142161
switch (_settings.DecimalSeparator)
143162
{
144163
case DecimalSeparator.UseSystemLocale: return systemDecimalSeperator;
145-
case DecimalSeparator.Dot: return ".";
146-
case DecimalSeparator.Comma: return ",";
164+
case DecimalSeparator.Dot: return dot;
165+
case DecimalSeparator.Comma: return comma;
147166
default: return systemDecimalSeperator;
148167
}
149168
}

Plugins/Flow.Launcher.Plugin.Calculator/Settings.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
1+

72
namespace Flow.Launcher.Plugin.Caculator
83
{
94
public class Settings

Plugins/Flow.Launcher.Plugin.Calculator/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Name": "Calculator",
55
"Description": "Provide mathematical calculations.(Try 5*3-2 in Flow Launcher)",
66
"Author": "cxfksword",
7-
"Version": "1.1.9",
7+
"Version": "1.1.10",
88
"Language": "csharp",
99
"Website": "https://github.com/Flow-Launcher/Flow.Launcher",
1010
"ExecuteFileName": "Flow.Launcher.Plugin.Caculator.dll",

0 commit comments

Comments
 (0)