1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Globalization ;
24using System . Runtime . InteropServices ;
35using System . Text . RegularExpressions ;
46using System . Windows ;
7+ using System . Windows . Controls ;
58using Mages . Core ;
9+ using Wox . Infrastructure . Storage ;
10+ using Wox . Plugin . Caculator . ViewModels ;
11+ using Wox . Plugin . Caculator . Views ;
612
713namespace Wox . Plugin . Caculator
814{
9- public class Main : IPlugin , IPluginI18n
15+ public class Main : IPlugin , IPluginI18n , ISavable , ISettingProvider
1016 {
1117 private static readonly Regex RegValidExpressChar = new Regex (
1218 @"^(" +
@@ -21,43 +27,58 @@ public class Main : IPlugin, IPluginI18n
2127 private static readonly Engine MagesEngine ;
2228 private PluginInitContext Context { get ; set ; }
2329
30+ private static Settings _settings ;
31+ private static SettingsViewModel _viewModel ;
32+
2433 static Main ( )
2534 {
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 ;
2744 }
2845
2946 public List < Result > Query ( Query query )
3047 {
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+ }
3452
3553 try
3654 {
37- var result = MagesEngine . Interpret ( query . Search ) ;
55+ var expression = query . Search . Replace ( "," , "." ) ;
56+ var result = MagesEngine . Interpret ( expression ) ;
3857
3958 if ( result . ToString ( ) == "NaN" )
4059 result = Context . API . GetTranslation ( "wox_plugin_calculator_not_a_number" ) ;
4160
4261 if ( result is Function )
4362 result = Context . API . GetTranslation ( "wox_plugin_calculator_expression_not_complete" ) ;
4463
45-
4664 if ( ! string . IsNullOrEmpty ( result ? . ToString ( ) ) )
4765 {
66+ decimal roundedResult = Math . Round ( Convert . ToDecimal ( result ) , _settings . MaxDecimalPlaces , MidpointRounding . AwayFromZero ) ;
67+ string newResult = ChangeDecimalSeparator ( roundedResult , GetDecimalSeparator ( ) ) ;
68+
4869 return new List < Result >
4970 {
5071 new Result
5172 {
52- Title = result . ToString ( ) ,
73+ Title = newResult ,
5374 IcoPath = "Images/calculator.png" ,
5475 Score = 300 ,
5576 SubTitle = Context . API . GetTranslation ( "wox_plugin_calculator_copy_number_to_clipboard" ) ,
5677 Action = c =>
5778 {
5879 try
5980 {
60- Clipboard . SetText ( result . ToString ( ) ) ;
81+ Clipboard . SetText ( newResult ) ;
6182 return true ;
6283 }
6384 catch ( ExternalException )
@@ -78,6 +99,53 @@ public List<Result> Query(Query query)
7899 return new List < Result > ( ) ;
79100 }
80101
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+
81149 private bool IsBracketComplete ( string query )
82150 {
83151 var matchs = RegBrackets . Matches ( query ) ;
@@ -96,12 +164,7 @@ private bool IsBracketComplete(string query)
96164
97165 return leftBracketCount == 0 ;
98166 }
99-
100- public void Init ( PluginInitContext context )
101- {
102- Context = context ;
103- }
104-
167+
105168 public string GetTranslatedPluginTitle ( )
106169 {
107170 return Context . API . GetTranslation ( "wox_plugin_caculator_plugin_name" ) ;
@@ -111,5 +174,15 @@ public string GetTranslatedPluginDescription()
111174 {
112175 return Context . API . GetTranslation ( "wox_plugin_caculator_plugin_description" ) ;
113176 }
177+
178+ public Control CreateSettingPanel ( )
179+ {
180+ return new CalculatorSettings ( _viewModel ) ;
181+ }
182+
183+ public void Save ( )
184+ {
185+ _viewModel . Save ( ) ;
186+ }
114187 }
115188}
0 commit comments