Skip to content
This repository was archived by the owner on Feb 12, 2021. It is now read-only.

Commit 98e89d6

Browse files
committed
Merge branch 'dev' into release_branch
2 parents 3d2fa83 + 1932515 commit 98e89d6

14 files changed

+411
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Globalization;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using Wox.Core;
9+
10+
namespace Wox.Plugin.Caculator
11+
{
12+
[TypeConverter(typeof(LocalizationConverter))]
13+
public enum DecimalSeparator
14+
{
15+
[LocalizedDescription("wox_plugin_calculator_decimal_seperator_use_system_locale")]
16+
UseSystemLocale,
17+
18+
[LocalizedDescription("wox_plugin_calculator_decimal_seperator_dot")]
19+
Dot,
20+
21+
[LocalizedDescription("wox_plugin_calculator_decimal_seperator_comma")]
22+
Comma
23+
}
24+
}

Plugins/Wox.Plugin.Calculator/Languages/de.xaml

+6
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@
77
<system:String x:Key="wox_plugin_calculator_not_a_number">Keine Zahl (NaN)</system:String>
88
<system:String x:Key="wox_plugin_calculator_expression_not_complete">Ausdruck falsch oder nicht vollständig (Klammern vergessen?)</system:String>
99
<system:String x:Key="wox_plugin_calculator_copy_number_to_clipboard">Diese Zahl in die Zwischenablage kopieren</system:String>
10+
<system:String x:Key="wox_plugin_calculator_output_decimal_seperator">Dezimaltrennzeichen</system:String>
11+
<system:String x:Key="wox_plugin_calculator_output_decimal_seperator_help">Das Dezimaltrennzeichen, welches bei der Ausgabe verwendet werden soll.</system:String>
12+
<system:String x:Key="wox_plugin_calculator_decimal_seperator_use_system_locale">Systemeinstellung nutzen</system:String>
13+
<system:String x:Key="wox_plugin_calculator_decimal_seperator_comma">Komma (,)</system:String>
14+
<system:String x:Key="wox_plugin_calculator_decimal_seperator_dot">Punkt (.)</system:String>
15+
<system:String x:Key="wox_plugin_calculator_max_decimal_places">Max. Nachkommastellen</system:String>
1016
</ResourceDictionary>

Plugins/Wox.Plugin.Calculator/Languages/en.xaml

+6
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@
77
<system:String x:Key="wox_plugin_calculator_not_a_number">Not a number (NaN)</system:String>
88
<system:String x:Key="wox_plugin_calculator_expression_not_complete">Expression wrong or incomplete (Did you forget some parentheses?)</system:String>
99
<system:String x:Key="wox_plugin_calculator_copy_number_to_clipboard">Copy this number to the clipboard</system:String>
10+
<system:String x:Key="wox_plugin_calculator_output_decimal_seperator">Decimal separator</system:String>
11+
<system:String x:Key="wox_plugin_calculator_output_decimal_seperator_help">The decimal separator to be used in the output.</system:String>
12+
<system:String x:Key="wox_plugin_calculator_decimal_seperator_use_system_locale">Use system locale</system:String>
13+
<system:String x:Key="wox_plugin_calculator_decimal_seperator_comma">Comma (,)</system:String>
14+
<system:String x:Key="wox_plugin_calculator_decimal_seperator_dot">Dot (.)</system:String>
15+
<system:String x:Key="wox_plugin_calculator_max_decimal_places">Max. decimal places</system:String>
1016
</ResourceDictionary>

Plugins/Wox.Plugin.Calculator/Main.cs

+89-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
24
using System.Runtime.InteropServices;
35
using System.Text.RegularExpressions;
46
using System.Windows;
7+
using System.Windows.Controls;
58
using Mages.Core;
9+
using Wox.Infrastructure.Storage;
10+
using Wox.Plugin.Caculator.ViewModels;
11+
using Wox.Plugin.Caculator.Views;
612

713
namespace 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
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Wox.Plugin.Caculator
8+
{
9+
public class Settings
10+
{
11+
public DecimalSeparator DecimalSeparator { get; set; } = DecimalSeparator.UseSystemLocale;
12+
public int MaxDecimalPlaces { get; set; } = 10;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Wox.Infrastructure.Storage;
7+
using Wox.Infrastructure.UserSettings;
8+
9+
namespace Wox.Plugin.Caculator.ViewModels
10+
{
11+
public class SettingsViewModel : BaseModel, ISavable
12+
{
13+
private readonly PluginJsonStorage<Settings> _storage;
14+
15+
public SettingsViewModel()
16+
{
17+
_storage = new PluginJsonStorage<Settings>();
18+
Settings = _storage.Load();
19+
}
20+
21+
public Settings Settings { get; set; }
22+
23+
public IEnumerable<int> MaxDecimalPlacesRange => Enumerable.Range(1, 20);
24+
25+
public void Save()
26+
{
27+
_storage.Save();
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<UserControl x:Class="Wox.Plugin.Caculator.Views.CalculatorSettings"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:ui="clr-namespace:Wox.Infrastructure.UI;assembly=Wox.Infrastructure"
7+
xmlns:calculator="clr-namespace:Wox.Plugin.Caculator"
8+
xmlns:core="clr-namespace:Wox.Core;assembly=Wox.Core"
9+
xmlns:viewModels="clr-namespace:Wox.Plugin.Caculator.ViewModels"
10+
mc:Ignorable="d"
11+
Loaded="CalculatorSettings_Loaded"
12+
d:DesignHeight="450" d:DesignWidth="800">
13+
14+
<UserControl.Resources>
15+
<core:LocalizationConverter x:Key="LocalizationConverter"/>
16+
</UserControl.Resources>
17+
18+
<Border BorderBrush="Gray" Margin="10" BorderThickness="1">
19+
<Grid Margin="10">
20+
<Grid.RowDefinitions>
21+
<RowDefinition Height="auto"/>
22+
<RowDefinition Height="auto"/>
23+
</Grid.RowDefinitions>
24+
<Grid.ColumnDefinitions>
25+
<ColumnDefinition Width="*"/>
26+
<ColumnDefinition Width="3*"/>
27+
</Grid.ColumnDefinitions>
28+
29+
<TextBlock Grid.Column="0" Grid.Row="0" Text="{DynamicResource wox_plugin_calculator_output_decimal_seperator}" VerticalAlignment="Center" />
30+
<ComboBox x:Name="DecimalSeparatorComboBox"
31+
Grid.Column="1"
32+
Grid.Row="0"
33+
Margin="0 5 0 5"
34+
HorizontalAlignment="Left"
35+
SelectedItem="{Binding Settings.DecimalSeparator}"
36+
ItemsSource="{Binding Source={ui:EnumBindingSource {x:Type calculator:DecimalSeparator}}}">
37+
<ComboBox.ItemTemplate>
38+
<DataTemplate>
39+
<TextBlock Text="{Binding Converter={StaticResource LocalizationConverter}}" />
40+
</DataTemplate>
41+
</ComboBox.ItemTemplate>
42+
</ComboBox>
43+
44+
<TextBlock Grid.Column="0" Grid.Row="1" Text="{DynamicResource wox_plugin_calculator_max_decimal_places}" VerticalAlignment="Center" />
45+
<ComboBox x:Name="MaxDecimalPlaces"
46+
Grid.Column="1"
47+
Grid.Row="1"
48+
Margin="0 5 0 5"
49+
HorizontalAlignment="Left"
50+
SelectedItem="{Binding Settings.MaxDecimalPlaces}"
51+
ItemsSource="{Binding MaxDecimalPlacesRange}">
52+
</ComboBox>
53+
54+
</Grid>
55+
</Border>
56+
</UserControl>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows;
7+
using System.Windows.Controls;
8+
using System.Windows.Data;
9+
using System.Windows.Documents;
10+
using System.Windows.Input;
11+
using System.Windows.Media;
12+
using System.Windows.Media.Imaging;
13+
using System.Windows.Navigation;
14+
using System.Windows.Shapes;
15+
using Wox.Plugin.Caculator.ViewModels;
16+
17+
namespace Wox.Plugin.Caculator.Views
18+
{
19+
/// <summary>
20+
/// Interaction logic for CalculatorSettings.xaml
21+
/// </summary>
22+
public partial class CalculatorSettings : UserControl
23+
{
24+
private readonly SettingsViewModel _viewModel;
25+
private readonly Settings _settings;
26+
27+
public CalculatorSettings(SettingsViewModel viewModel)
28+
{
29+
_viewModel = viewModel;
30+
_settings = viewModel.Settings;
31+
DataContext = viewModel;
32+
InitializeComponent();
33+
}
34+
35+
private void CalculatorSettings_Loaded(object sender, RoutedEventArgs e)
36+
{
37+
DecimalSeparatorComboBox.SelectedItem = _settings.DecimalSeparator;
38+
MaxDecimalPlaces.SelectedItem = _settings.MaxDecimalPlaces;
39+
}
40+
}
41+
42+
43+
}

0 commit comments

Comments
 (0)