1
- using System . Collections . Generic ;
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Globalization ;
2
4
using System . Runtime . InteropServices ;
3
5
using System . Text . RegularExpressions ;
4
6
using System . Windows ;
7
+ using System . Windows . Controls ;
5
8
using Mages . Core ;
9
+ using Wox . Infrastructure . Storage ;
10
+ using Wox . Plugin . Caculator . ViewModels ;
11
+ using Wox . Plugin . Caculator . Views ;
6
12
7
13
namespace Wox . Plugin . Caculator
8
14
{
9
- public class Main : IPlugin , IPluginI18n
15
+ public class Main : IPlugin , IPluginI18n , ISavable , ISettingProvider
10
16
{
11
17
private static readonly Regex RegValidExpressChar = new Regex (
12
18
@"^(" +
@@ -21,43 +27,58 @@ public class Main : IPlugin, IPluginI18n
21
27
private static readonly Engine MagesEngine ;
22
28
private PluginInitContext Context { get ; set ; }
23
29
30
+ private static Settings _settings ;
31
+ private static SettingsViewModel _viewModel ;
32
+
24
33
static Main ( )
25
34
{
26
- MagesEngine = new Engine ( ) ;
35
+ MagesEngine = new Engine ( ) ;
36
+ }
37
+
38
+ public void Init ( PluginInitContext context )
39
+ {
40
+ Context = context ;
41
+
42
+ _viewModel = new SettingsViewModel ( ) ;
43
+ _settings = _viewModel . Settings ;
27
44
}
28
45
29
46
public List < Result > Query ( Query query )
30
47
{
31
- if ( query . Search . Length <= 2 // don't affect when user only input "e" or "i" keyword
32
- || ! RegValidExpressChar . IsMatch ( query . Search )
33
- || ! IsBracketComplete ( query . Search ) ) return new List < Result > ( ) ;
48
+ if ( ! CanCalculate ( query ) )
49
+ {
50
+ return new List < Result > ( ) ;
51
+ }
34
52
35
53
try
36
54
{
37
- var result = MagesEngine . Interpret ( query . Search ) ;
55
+ var expression = query . Search . Replace ( "," , "." ) ;
56
+ var result = MagesEngine . Interpret ( expression ) ;
38
57
39
58
if ( result . ToString ( ) == "NaN" )
40
59
result = Context . API . GetTranslation ( "wox_plugin_calculator_not_a_number" ) ;
41
60
42
61
if ( result is Function )
43
62
result = Context . API . GetTranslation ( "wox_plugin_calculator_expression_not_complete" ) ;
44
63
45
-
46
64
if ( ! string . IsNullOrEmpty ( result ? . ToString ( ) ) )
47
65
{
66
+ decimal roundedResult = Math . Round ( Convert . ToDecimal ( result ) , _settings . MaxDecimalPlaces , MidpointRounding . AwayFromZero ) ;
67
+ string newResult = ChangeDecimalSeparator ( roundedResult , GetDecimalSeparator ( ) ) ;
68
+
48
69
return new List < Result >
49
70
{
50
71
new Result
51
72
{
52
- Title = result . ToString ( ) ,
73
+ Title = newResult ,
53
74
IcoPath = "Images/calculator.png" ,
54
75
Score = 300 ,
55
76
SubTitle = Context . API . GetTranslation ( "wox_plugin_calculator_copy_number_to_clipboard" ) ,
56
77
Action = c =>
57
78
{
58
79
try
59
80
{
60
- Clipboard . SetText ( result . ToString ( ) ) ;
81
+ Clipboard . SetText ( newResult ) ;
61
82
return true ;
62
83
}
63
84
catch ( ExternalException )
@@ -78,6 +99,53 @@ public List<Result> Query(Query query)
78
99
return new List < Result > ( ) ;
79
100
}
80
101
102
+ private bool CanCalculate ( Query query )
103
+ {
104
+ // Don't execute when user only input "e" or "i" keyword
105
+ if ( query . Search . Length < 2 )
106
+ {
107
+ return false ;
108
+ }
109
+
110
+ if ( ! RegValidExpressChar . IsMatch ( query . Search ) )
111
+ {
112
+ return false ;
113
+ }
114
+
115
+ if ( ! IsBracketComplete ( query . Search ) )
116
+ {
117
+ return false ;
118
+ }
119
+
120
+ return true ;
121
+ }
122
+
123
+ private string ChangeDecimalSeparator ( decimal value , string newDecimalSeparator )
124
+ {
125
+ if ( String . IsNullOrEmpty ( newDecimalSeparator ) )
126
+ {
127
+ return value . ToString ( ) ;
128
+ }
129
+
130
+ var numberFormatInfo = new NumberFormatInfo
131
+ {
132
+ NumberDecimalSeparator = newDecimalSeparator
133
+ } ;
134
+ return value . ToString ( numberFormatInfo ) ;
135
+ }
136
+
137
+ private string GetDecimalSeparator ( )
138
+ {
139
+ string systemDecimalSeperator = CultureInfo . CurrentCulture . NumberFormat . NumberDecimalSeparator ;
140
+ switch ( _settings . DecimalSeparator )
141
+ {
142
+ case DecimalSeparator . UseSystemLocale : return systemDecimalSeperator ;
143
+ case DecimalSeparator . Dot : return "." ;
144
+ case DecimalSeparator . Comma : return "," ;
145
+ default : return systemDecimalSeperator ;
146
+ }
147
+ }
148
+
81
149
private bool IsBracketComplete ( string query )
82
150
{
83
151
var matchs = RegBrackets . Matches ( query ) ;
@@ -96,12 +164,7 @@ private bool IsBracketComplete(string query)
96
164
97
165
return leftBracketCount == 0 ;
98
166
}
99
-
100
- public void Init ( PluginInitContext context )
101
- {
102
- Context = context ;
103
- }
104
-
167
+
105
168
public string GetTranslatedPluginTitle ( )
106
169
{
107
170
return Context . API . GetTranslation ( "wox_plugin_caculator_plugin_name" ) ;
@@ -111,5 +174,15 @@ public string GetTranslatedPluginDescription()
111
174
{
112
175
return Context . API . GetTranslation ( "wox_plugin_caculator_plugin_description" ) ;
113
176
}
177
+
178
+ public Control CreateSettingPanel ( )
179
+ {
180
+ return new CalculatorSettings ( _viewModel ) ;
181
+ }
182
+
183
+ public void Save ( )
184
+ {
185
+ _viewModel . Save ( ) ;
186
+ }
114
187
}
115
188
}
0 commit comments