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

Commit

Permalink
Merge branch 'dev' into release_branch
Browse files Browse the repository at this point in the history
  • Loading branch information
jjw24 committed Mar 1, 2020
2 parents 3d2fa83 + 1932515 commit 98e89d6
Show file tree
Hide file tree
Showing 14 changed files with 411 additions and 17 deletions.
24 changes: 24 additions & 0 deletions Plugins/Wox.Plugin.Calculator/DecimalSeparator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wox.Core;

namespace Wox.Plugin.Caculator
{
[TypeConverter(typeof(LocalizationConverter))]
public enum DecimalSeparator
{
[LocalizedDescription("wox_plugin_calculator_decimal_seperator_use_system_locale")]
UseSystemLocale,

[LocalizedDescription("wox_plugin_calculator_decimal_seperator_dot")]
Dot,

[LocalizedDescription("wox_plugin_calculator_decimal_seperator_comma")]
Comma
}
}
6 changes: 6 additions & 0 deletions Plugins/Wox.Plugin.Calculator/Languages/de.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@
<system:String x:Key="wox_plugin_calculator_not_a_number">Keine Zahl (NaN)</system:String>
<system:String x:Key="wox_plugin_calculator_expression_not_complete">Ausdruck falsch oder nicht vollständig (Klammern vergessen?)</system:String>
<system:String x:Key="wox_plugin_calculator_copy_number_to_clipboard">Diese Zahl in die Zwischenablage kopieren</system:String>
<system:String x:Key="wox_plugin_calculator_output_decimal_seperator">Dezimaltrennzeichen</system:String>
<system:String x:Key="wox_plugin_calculator_output_decimal_seperator_help">Das Dezimaltrennzeichen, welches bei der Ausgabe verwendet werden soll.</system:String>
<system:String x:Key="wox_plugin_calculator_decimal_seperator_use_system_locale">Systemeinstellung nutzen</system:String>
<system:String x:Key="wox_plugin_calculator_decimal_seperator_comma">Komma (,)</system:String>
<system:String x:Key="wox_plugin_calculator_decimal_seperator_dot">Punkt (.)</system:String>
<system:String x:Key="wox_plugin_calculator_max_decimal_places">Max. Nachkommastellen</system:String>
</ResourceDictionary>
6 changes: 6 additions & 0 deletions Plugins/Wox.Plugin.Calculator/Languages/en.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@
<system:String x:Key="wox_plugin_calculator_not_a_number">Not a number (NaN)</system:String>
<system:String x:Key="wox_plugin_calculator_expression_not_complete">Expression wrong or incomplete (Did you forget some parentheses?)</system:String>
<system:String x:Key="wox_plugin_calculator_copy_number_to_clipboard">Copy this number to the clipboard</system:String>
<system:String x:Key="wox_plugin_calculator_output_decimal_seperator">Decimal separator</system:String>
<system:String x:Key="wox_plugin_calculator_output_decimal_seperator_help">The decimal separator to be used in the output.</system:String>
<system:String x:Key="wox_plugin_calculator_decimal_seperator_use_system_locale">Use system locale</system:String>
<system:String x:Key="wox_plugin_calculator_decimal_seperator_comma">Comma (,)</system:String>
<system:String x:Key="wox_plugin_calculator_decimal_seperator_dot">Dot (.)</system:String>
<system:String x:Key="wox_plugin_calculator_max_decimal_places">Max. decimal places</system:String>
</ResourceDictionary>
105 changes: 89 additions & 16 deletions Plugins/Wox.Plugin.Calculator/Main.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using Mages.Core;
using Wox.Infrastructure.Storage;
using Wox.Plugin.Caculator.ViewModels;
using Wox.Plugin.Caculator.Views;

namespace Wox.Plugin.Caculator
{
public class Main : IPlugin, IPluginI18n
public class Main : IPlugin, IPluginI18n, ISavable, ISettingProvider
{
private static readonly Regex RegValidExpressChar = new Regex(
@"^(" +
Expand All @@ -21,43 +27,58 @@ public class Main : IPlugin, IPluginI18n
private static readonly Engine MagesEngine;
private PluginInitContext Context { get; set; }

private static Settings _settings;
private static SettingsViewModel _viewModel;

static Main()
{
MagesEngine = new Engine();
MagesEngine = new Engine();
}

public void Init(PluginInitContext context)
{
Context = context;

_viewModel = new SettingsViewModel();
_settings = _viewModel.Settings;
}

public List<Result> Query(Query query)
{
if (query.Search.Length <= 2 // don't affect when user only input "e" or "i" keyword
|| !RegValidExpressChar.IsMatch(query.Search)
|| !IsBracketComplete(query.Search)) return new List<Result>();
if (!CanCalculate(query))
{
return new List<Result>();
}

try
{
var result = MagesEngine.Interpret(query.Search);
var expression = query.Search.Replace(",", ".");
var result = MagesEngine.Interpret(expression);

if (result.ToString() == "NaN")
result = Context.API.GetTranslation("wox_plugin_calculator_not_a_number");

if (result is Function)
result = Context.API.GetTranslation("wox_plugin_calculator_expression_not_complete");


if (!string.IsNullOrEmpty(result?.ToString()))
{
decimal roundedResult = Math.Round(Convert.ToDecimal(result), _settings.MaxDecimalPlaces, MidpointRounding.AwayFromZero);
string newResult = ChangeDecimalSeparator(roundedResult, GetDecimalSeparator());

return new List<Result>
{
new Result
{
Title = result.ToString(),
Title = newResult,
IcoPath = "Images/calculator.png",
Score = 300,
SubTitle = Context.API.GetTranslation("wox_plugin_calculator_copy_number_to_clipboard"),
Action = c =>
{
try
{
Clipboard.SetText(result.ToString());
Clipboard.SetText(newResult);
return true;
}
catch (ExternalException)
Expand All @@ -78,6 +99,53 @@ public List<Result> Query(Query query)
return new List<Result>();
}

private bool CanCalculate(Query query)
{
// Don't execute when user only input "e" or "i" keyword
if (query.Search.Length < 2)
{
return false;
}

if (!RegValidExpressChar.IsMatch(query.Search))
{
return false;
}

if (!IsBracketComplete(query.Search))
{
return false;
}

return true;
}

private string ChangeDecimalSeparator(decimal value, string newDecimalSeparator)
{
if (String.IsNullOrEmpty(newDecimalSeparator))
{
return value.ToString();
}

var numberFormatInfo = new NumberFormatInfo
{
NumberDecimalSeparator = newDecimalSeparator
};
return value.ToString(numberFormatInfo);
}

private string GetDecimalSeparator()
{
string systemDecimalSeperator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
switch (_settings.DecimalSeparator)
{
case DecimalSeparator.UseSystemLocale: return systemDecimalSeperator;
case DecimalSeparator.Dot: return ".";
case DecimalSeparator.Comma: return ",";
default: return systemDecimalSeperator;
}
}

private bool IsBracketComplete(string query)
{
var matchs = RegBrackets.Matches(query);
Expand All @@ -96,12 +164,7 @@ private bool IsBracketComplete(string query)

return leftBracketCount == 0;
}

public void Init(PluginInitContext context)
{
Context = context;
}


public string GetTranslatedPluginTitle()
{
return Context.API.GetTranslation("wox_plugin_caculator_plugin_name");
Expand All @@ -111,5 +174,15 @@ public string GetTranslatedPluginDescription()
{
return Context.API.GetTranslation("wox_plugin_caculator_plugin_description");
}

public Control CreateSettingPanel()
{
return new CalculatorSettings(_viewModel);
}

public void Save()
{
_viewModel.Save();
}
}
}
14 changes: 14 additions & 0 deletions Plugins/Wox.Plugin.Calculator/Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wox.Plugin.Caculator
{
public class Settings
{
public DecimalSeparator DecimalSeparator { get; set; } = DecimalSeparator.UseSystemLocale;
public int MaxDecimalPlaces { get; set; } = 10;
}
}
30 changes: 30 additions & 0 deletions Plugins/Wox.Plugin.Calculator/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wox.Infrastructure.Storage;
using Wox.Infrastructure.UserSettings;

namespace Wox.Plugin.Caculator.ViewModels
{
public class SettingsViewModel : BaseModel, ISavable
{
private readonly PluginJsonStorage<Settings> _storage;

public SettingsViewModel()
{
_storage = new PluginJsonStorage<Settings>();
Settings = _storage.Load();
}

public Settings Settings { get; set; }

public IEnumerable<int> MaxDecimalPlacesRange => Enumerable.Range(1, 20);

public void Save()
{
_storage.Save();
}
}
}
56 changes: 56 additions & 0 deletions Plugins/Wox.Plugin.Calculator/Views/CalculatorSettings.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<UserControl x:Class="Wox.Plugin.Caculator.Views.CalculatorSettings"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ui="clr-namespace:Wox.Infrastructure.UI;assembly=Wox.Infrastructure"
xmlns:calculator="clr-namespace:Wox.Plugin.Caculator"
xmlns:core="clr-namespace:Wox.Core;assembly=Wox.Core"
xmlns:viewModels="clr-namespace:Wox.Plugin.Caculator.ViewModels"
mc:Ignorable="d"
Loaded="CalculatorSettings_Loaded"
d:DesignHeight="450" d:DesignWidth="800">

<UserControl.Resources>
<core:LocalizationConverter x:Key="LocalizationConverter"/>
</UserControl.Resources>

<Border BorderBrush="Gray" Margin="10" BorderThickness="1">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>

<TextBlock Grid.Column="0" Grid.Row="0" Text="{DynamicResource wox_plugin_calculator_output_decimal_seperator}" VerticalAlignment="Center" />
<ComboBox x:Name="DecimalSeparatorComboBox"
Grid.Column="1"
Grid.Row="0"
Margin="0 5 0 5"
HorizontalAlignment="Left"
SelectedItem="{Binding Settings.DecimalSeparator}"
ItemsSource="{Binding Source={ui:EnumBindingSource {x:Type calculator:DecimalSeparator}}}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource LocalizationConverter}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

<TextBlock Grid.Column="0" Grid.Row="1" Text="{DynamicResource wox_plugin_calculator_max_decimal_places}" VerticalAlignment="Center" />
<ComboBox x:Name="MaxDecimalPlaces"
Grid.Column="1"
Grid.Row="1"
Margin="0 5 0 5"
HorizontalAlignment="Left"
SelectedItem="{Binding Settings.MaxDecimalPlaces}"
ItemsSource="{Binding MaxDecimalPlacesRange}">
</ComboBox>

</Grid>
</Border>
</UserControl>
43 changes: 43 additions & 0 deletions Plugins/Wox.Plugin.Calculator/Views/CalculatorSettings.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Wox.Plugin.Caculator.ViewModels;

namespace Wox.Plugin.Caculator.Views
{
/// <summary>
/// Interaction logic for CalculatorSettings.xaml
/// </summary>
public partial class CalculatorSettings : UserControl
{
private readonly SettingsViewModel _viewModel;
private readonly Settings _settings;

public CalculatorSettings(SettingsViewModel viewModel)
{
_viewModel = viewModel;
_settings = viewModel.Settings;
DataContext = viewModel;
InitializeComponent();
}

private void CalculatorSettings_Loaded(object sender, RoutedEventArgs e)
{
DecimalSeparatorComboBox.SelectedItem = _settings.DecimalSeparator;
MaxDecimalPlaces.SelectedItem = _settings.MaxDecimalPlaces;
}
}


}
Loading

0 comments on commit 98e89d6

Please sign in to comment.