Skip to content

Commit fe7985d

Browse files
committed
Use Flow.Launcher.Localization to improve code quality
1 parent 40e5c04 commit fe7985d

File tree

6 files changed

+48
-47
lines changed

6 files changed

+48
-47
lines changed
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
using System.ComponentModel;
2-
using Flow.Launcher.Core.Resource;
1+
using Flow.Launcher.Localization.Attributes;
32

43
namespace Flow.Launcher.Plugin.Calculator
54
{
6-
[TypeConverter(typeof(LocalizationConverter))]
5+
[EnumLocalize]
76
public enum DecimalSeparator
87
{
9-
[LocalizedDescription("flowlauncher_plugin_calculator_decimal_seperator_use_system_locale")]
8+
[EnumLocalizeKey(nameof(Localize.flowlauncher_plugin_calculator_decimal_seperator_use_system_locale))]
109
UseSystemLocale,
1110

12-
[LocalizedDescription("flowlauncher_plugin_calculator_decimal_seperator_dot")]
11+
[EnumLocalizeKey(nameof(Localize.flowlauncher_plugin_calculator_decimal_seperator_dot))]
1312
Dot,
1413

15-
[LocalizedDescription("flowlauncher_plugin_calculator_decimal_seperator_comma")]
14+
[EnumLocalizeKey(nameof(Localize.flowlauncher_plugin_calculator_decimal_seperator_comma))]
1615
Comma
1716
}
1817
}

Plugins/Flow.Launcher.Plugin.Calculator/Flow.Launcher.Plugin.Calculator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
</ItemGroup>
4343

4444
<ItemGroup>
45-
<ProjectReference Include="..\..\Flow.Launcher.Core\Flow.Launcher.Core.csproj" />
4645
<ProjectReference Include="..\..\Flow.Launcher.Plugin\Flow.Launcher.Plugin.csproj" />
4746
</ItemGroup>
4847

@@ -63,6 +62,7 @@
6362
</ItemGroup>
6463

6564
<ItemGroup>
65+
<PackageReference Include="Flow.Launcher.Localization" Version="0.0.3"/>
6666
<PackageReference Include="Mages" Version="3.0.0" />
6767
</ItemGroup>
6868

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

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Text.RegularExpressions;
66
using System.Windows.Controls;
77
using Mages.Core;
8-
using Flow.Launcher.Plugin.Calculator.ViewModels;
98
using Flow.Launcher.Plugin.Calculator.Views;
109

1110
namespace Flow.Launcher.Plugin.Calculator
@@ -24,19 +23,17 @@ public class Main : IPlugin, IPluginI18n, ISettingProvider
2423
@")+$", RegexOptions.Compiled);
2524
private static readonly Regex RegBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled);
2625
private static Engine MagesEngine;
27-
private const string comma = ",";
28-
private const string dot = ".";
26+
private const string Comma = ",";
27+
private const string Dot = ".";
2928

30-
private PluginInitContext Context { get; set; }
29+
internal static PluginInitContext Context { get; set; } = null!;
3130

3231
private static Settings _settings;
33-
private static SettingsViewModel _viewModel;
3432

3533
public void Init(PluginInitContext context)
3634
{
3735
Context = context;
3836
_settings = context.API.LoadSettingJsonStorage<Settings>();
39-
_viewModel = new SettingsViewModel(_settings);
4037

4138
MagesEngine = new Engine(new Configuration
4239
{
@@ -72,10 +69,10 @@ public List<Result> Query(Query query)
7269
var result = MagesEngine.Interpret(expression);
7370

7471
if (result?.ToString() == "NaN")
75-
result = Context.API.GetTranslation("flowlauncher_plugin_calculator_not_a_number");
72+
result = Localize.flowlauncher_plugin_calculator_not_a_number();
7673

7774
if (result is Function)
78-
result = Context.API.GetTranslation("flowlauncher_plugin_calculator_expression_not_complete");
75+
result = Localize.flowlauncher_plugin_calculator_expression_not_complete();
7976

8077
if (!string.IsNullOrEmpty(result?.ToString()))
8178
{
@@ -89,7 +86,7 @@ public List<Result> Query(Query query)
8986
Title = newResult,
9087
IcoPath = "Images/calculator.png",
9188
Score = 300,
92-
SubTitle = Context.API.GetTranslation("flowlauncher_plugin_calculator_copy_number_to_clipboard"),
89+
SubTitle = Localize.flowlauncher_plugin_calculator_copy_number_to_clipboard(),
9390
CopyText = newResult,
9491
Action = c =>
9592
{
@@ -134,16 +131,16 @@ private bool CanCalculate(Query query)
134131
return false;
135132
}
136133

137-
if ((query.Search.Contains(dot) && GetDecimalSeparator() != dot) ||
138-
(query.Search.Contains(comma) && GetDecimalSeparator() != comma))
134+
if ((query.Search.Contains(Dot) && GetDecimalSeparator() != Dot) ||
135+
(query.Search.Contains(Comma) && GetDecimalSeparator() != Comma))
139136
return false;
140137

141138
return true;
142139
}
143140

144-
private string ChangeDecimalSeparator(decimal value, string newDecimalSeparator)
141+
private static string ChangeDecimalSeparator(decimal value, string newDecimalSeparator)
145142
{
146-
if (String.IsNullOrEmpty(newDecimalSeparator))
143+
if (string.IsNullOrEmpty(newDecimalSeparator))
147144
{
148145
return value.ToString();
149146
}
@@ -161,13 +158,13 @@ private static string GetDecimalSeparator()
161158
return _settings.DecimalSeparator switch
162159
{
163160
DecimalSeparator.UseSystemLocale => systemDecimalSeparator,
164-
DecimalSeparator.Dot => dot,
165-
DecimalSeparator.Comma => comma,
161+
DecimalSeparator.Dot => Dot,
162+
DecimalSeparator.Comma => Comma,
166163
_ => systemDecimalSeparator,
167164
};
168165
}
169166

170-
private bool IsBracketComplete(string query)
167+
private static bool IsBracketComplete(string query)
171168
{
172169
var matchs = RegBrackets.Matches(query);
173170
var leftBracketCount = 0;
@@ -188,17 +185,17 @@ private bool IsBracketComplete(string query)
188185

189186
public string GetTranslatedPluginTitle()
190187
{
191-
return Context.API.GetTranslation("flowlauncher_plugin_caculator_plugin_name");
188+
return Localize.flowlauncher_plugin_caculator_plugin_name();
192189
}
193190

194191
public string GetTranslatedPluginDescription()
195192
{
196-
return Context.API.GetTranslation("flowlauncher_plugin_caculator_plugin_description");
193+
return Localize.flowlauncher_plugin_caculator_plugin_description();
197194
}
198195

199196
public Control CreateSettingPanel()
200197
{
201-
return new CalculatorSettings(_viewModel);
198+
return new CalculatorSettings(_settings);
202199
}
203200
}
204201
}

Plugins/Flow.Launcher.Plugin.Calculator/ViewModels/SettingsViewModel.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,26 @@ public class SettingsViewModel : BaseModel
88
public SettingsViewModel(Settings settings)
99
{
1010
Settings = settings;
11+
DecimalSeparatorLocalized.UpdateLabels(AllDecimalSeparator);
1112
}
1213

1314
public Settings Settings { get; init; }
1415

1516
public IEnumerable<int> MaxDecimalPlacesRange => Enumerable.Range(1, 20);
17+
18+
public List<DecimalSeparatorLocalized> AllDecimalSeparator { get; } = DecimalSeparatorLocalized.GetValues();
19+
20+
public DecimalSeparator SelectedDecimalSeparator
21+
{
22+
get => Settings.DecimalSeparator;
23+
set
24+
{
25+
if (Settings.DecimalSeparator != value)
26+
{
27+
Settings.DecimalSeparator = value;
28+
OnPropertyChanged();
29+
}
30+
}
31+
}
1632
}
1733
}

Plugins/Flow.Launcher.Plugin.Calculator/Views/CalculatorSettings.xaml

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,15 @@
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
55
xmlns:calculator="clr-namespace:Flow.Launcher.Plugin.Calculator"
6-
xmlns:core="clr-namespace:Flow.Launcher.Core.Resource;assembly=Flow.Launcher.Core"
76
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
87
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9-
xmlns:ui="clr-namespace:Flow.Launcher.Infrastructure.UI;assembly=Flow.Launcher.Infrastructure"
108
xmlns:viewModels="clr-namespace:Flow.Launcher.Plugin.Calculator.ViewModels"
9+
d:DataContext="{d:DesignInstance Type=viewModels:SettingsViewModel}"
1110
d:DesignHeight="450"
1211
d:DesignWidth="800"
1312
Loaded="CalculatorSettings_Loaded"
1413
mc:Ignorable="d">
1514

16-
<UserControl.Resources>
17-
<core:LocalizationConverter x:Key="LocalizationConverter" />
18-
</UserControl.Resources>
19-
2015
<Grid Margin="{StaticResource SettingPanelMargin}">
2116
<Grid.RowDefinitions>
2217
<RowDefinition Height="auto" />
@@ -42,14 +37,10 @@
4237
Margin="{StaticResource SettingPanelItemRightTopBottomMargin}"
4338
HorizontalAlignment="Left"
4439
VerticalAlignment="Center"
45-
ItemsSource="{Binding Source={ui:EnumBindingSource {x:Type calculator:DecimalSeparator}}}"
46-
SelectedItem="{Binding Settings.DecimalSeparator}">
47-
<ComboBox.ItemTemplate>
48-
<DataTemplate>
49-
<TextBlock FontSize="14" Text="{Binding Converter={StaticResource LocalizationConverter}}" />
50-
</DataTemplate>
51-
</ComboBox.ItemTemplate>
52-
</ComboBox>
40+
DisplayMemberPath="Display"
41+
ItemsSource="{Binding AllDecimalSeparator}"
42+
SelectedValue="{Binding SelectedDecimalSeparator, Mode=TwoWay}"
43+
SelectedValuePath="Value" />
5344

5445
<TextBlock
5546
Grid.Row="1"

Plugins/Flow.Launcher.Plugin.Calculator/Views/CalculatorSettings.xaml.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Windows;
1+
using System.Windows;
22
using System.Windows.Controls;
33
using Flow.Launcher.Plugin.Calculator.ViewModels;
44

@@ -12,11 +12,11 @@ public partial class CalculatorSettings : UserControl
1212
private readonly SettingsViewModel _viewModel;
1313
private readonly Settings _settings;
1414

15-
public CalculatorSettings(SettingsViewModel viewModel)
15+
public CalculatorSettings(Settings settings)
1616
{
17-
_viewModel = viewModel;
18-
_settings = viewModel.Settings;
19-
DataContext = viewModel;
17+
_viewModel = new SettingsViewModel(settings);
18+
_settings = _viewModel.Settings;
19+
DataContext = _viewModel;
2020
InitializeComponent();
2121
}
2222

@@ -26,6 +26,4 @@ private void CalculatorSettings_Loaded(object sender, RoutedEventArgs e)
2626
MaxDecimalPlaces.SelectedItem = _settings.MaxDecimalPlaces;
2727
}
2828
}
29-
30-
3129
}

0 commit comments

Comments
 (0)