From fe7985d5644a2c3b86ff369736fb461e7fc0fba4 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Mon, 23 Jun 2025 12:25:16 +0800 Subject: [PATCH] Use Flow.Launcher.Localization to improve code quality --- .../DecimalSeparator.cs | 11 +++--- .../Flow.Launcher.Plugin.Calculator.csproj | 2 +- .../Flow.Launcher.Plugin.Calculator/Main.cs | 35 +++++++++---------- .../ViewModels/SettingsViewModel.cs | 16 +++++++++ .../Views/CalculatorSettings.xaml | 19 +++------- .../Views/CalculatorSettings.xaml.cs | 12 +++---- 6 files changed, 48 insertions(+), 47 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Calculator/DecimalSeparator.cs b/Plugins/Flow.Launcher.Plugin.Calculator/DecimalSeparator.cs index 81a68739b..0ece36d54 100644 --- a/Plugins/Flow.Launcher.Plugin.Calculator/DecimalSeparator.cs +++ b/Plugins/Flow.Launcher.Plugin.Calculator/DecimalSeparator.cs @@ -1,18 +1,17 @@ -using System.ComponentModel; -using Flow.Launcher.Core.Resource; +using Flow.Launcher.Localization.Attributes; namespace Flow.Launcher.Plugin.Calculator { - [TypeConverter(typeof(LocalizationConverter))] + [EnumLocalize] public enum DecimalSeparator { - [LocalizedDescription("flowlauncher_plugin_calculator_decimal_seperator_use_system_locale")] + [EnumLocalizeKey(nameof(Localize.flowlauncher_plugin_calculator_decimal_seperator_use_system_locale))] UseSystemLocale, - [LocalizedDescription("flowlauncher_plugin_calculator_decimal_seperator_dot")] + [EnumLocalizeKey(nameof(Localize.flowlauncher_plugin_calculator_decimal_seperator_dot))] Dot, - [LocalizedDescription("flowlauncher_plugin_calculator_decimal_seperator_comma")] + [EnumLocalizeKey(nameof(Localize.flowlauncher_plugin_calculator_decimal_seperator_comma))] Comma } } diff --git a/Plugins/Flow.Launcher.Plugin.Calculator/Flow.Launcher.Plugin.Calculator.csproj b/Plugins/Flow.Launcher.Plugin.Calculator/Flow.Launcher.Plugin.Calculator.csproj index 9cdef365d..73dacf3d1 100644 --- a/Plugins/Flow.Launcher.Plugin.Calculator/Flow.Launcher.Plugin.Calculator.csproj +++ b/Plugins/Flow.Launcher.Plugin.Calculator/Flow.Launcher.Plugin.Calculator.csproj @@ -42,7 +42,6 @@ - @@ -63,6 +62,7 @@ + diff --git a/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs b/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs index b1e4cd606..f35e64237 100644 --- a/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs @@ -5,7 +5,6 @@ using System.Runtime.InteropServices; using System.Text.RegularExpressions; using System.Windows.Controls; using Mages.Core; -using Flow.Launcher.Plugin.Calculator.ViewModels; using Flow.Launcher.Plugin.Calculator.Views; namespace Flow.Launcher.Plugin.Calculator @@ -24,19 +23,17 @@ namespace Flow.Launcher.Plugin.Calculator @")+$", RegexOptions.Compiled); private static readonly Regex RegBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled); private static Engine MagesEngine; - private const string comma = ","; - private const string dot = "."; + private const string Comma = ","; + private const string Dot = "."; - private PluginInitContext Context { get; set; } + internal static PluginInitContext Context { get; set; } = null!; private static Settings _settings; - private static SettingsViewModel _viewModel; public void Init(PluginInitContext context) { Context = context; _settings = context.API.LoadSettingJsonStorage(); - _viewModel = new SettingsViewModel(_settings); MagesEngine = new Engine(new Configuration { @@ -72,10 +69,10 @@ namespace Flow.Launcher.Plugin.Calculator var result = MagesEngine.Interpret(expression); if (result?.ToString() == "NaN") - result = Context.API.GetTranslation("flowlauncher_plugin_calculator_not_a_number"); + result = Localize.flowlauncher_plugin_calculator_not_a_number(); if (result is Function) - result = Context.API.GetTranslation("flowlauncher_plugin_calculator_expression_not_complete"); + result = Localize.flowlauncher_plugin_calculator_expression_not_complete(); if (!string.IsNullOrEmpty(result?.ToString())) { @@ -89,7 +86,7 @@ namespace Flow.Launcher.Plugin.Calculator Title = newResult, IcoPath = "Images/calculator.png", Score = 300, - SubTitle = Context.API.GetTranslation("flowlauncher_plugin_calculator_copy_number_to_clipboard"), + SubTitle = Localize.flowlauncher_plugin_calculator_copy_number_to_clipboard(), CopyText = newResult, Action = c => { @@ -134,16 +131,16 @@ namespace Flow.Launcher.Plugin.Calculator return false; } - if ((query.Search.Contains(dot) && GetDecimalSeparator() != dot) || - (query.Search.Contains(comma) && GetDecimalSeparator() != comma)) + if ((query.Search.Contains(Dot) && GetDecimalSeparator() != Dot) || + (query.Search.Contains(Comma) && GetDecimalSeparator() != Comma)) return false; return true; } - private string ChangeDecimalSeparator(decimal value, string newDecimalSeparator) + private static string ChangeDecimalSeparator(decimal value, string newDecimalSeparator) { - if (String.IsNullOrEmpty(newDecimalSeparator)) + if (string.IsNullOrEmpty(newDecimalSeparator)) { return value.ToString(); } @@ -161,13 +158,13 @@ namespace Flow.Launcher.Plugin.Calculator return _settings.DecimalSeparator switch { DecimalSeparator.UseSystemLocale => systemDecimalSeparator, - DecimalSeparator.Dot => dot, - DecimalSeparator.Comma => comma, + DecimalSeparator.Dot => Dot, + DecimalSeparator.Comma => Comma, _ => systemDecimalSeparator, }; } - private bool IsBracketComplete(string query) + private static bool IsBracketComplete(string query) { var matchs = RegBrackets.Matches(query); var leftBracketCount = 0; @@ -188,17 +185,17 @@ namespace Flow.Launcher.Plugin.Calculator public string GetTranslatedPluginTitle() { - return Context.API.GetTranslation("flowlauncher_plugin_caculator_plugin_name"); + return Localize.flowlauncher_plugin_caculator_plugin_name(); } public string GetTranslatedPluginDescription() { - return Context.API.GetTranslation("flowlauncher_plugin_caculator_plugin_description"); + return Localize.flowlauncher_plugin_caculator_plugin_description(); } public Control CreateSettingPanel() { - return new CalculatorSettings(_viewModel); + return new CalculatorSettings(_settings); } } } diff --git a/Plugins/Flow.Launcher.Plugin.Calculator/ViewModels/SettingsViewModel.cs b/Plugins/Flow.Launcher.Plugin.Calculator/ViewModels/SettingsViewModel.cs index 09f745669..a1f07bd17 100644 --- a/Plugins/Flow.Launcher.Plugin.Calculator/ViewModels/SettingsViewModel.cs +++ b/Plugins/Flow.Launcher.Plugin.Calculator/ViewModels/SettingsViewModel.cs @@ -8,10 +8,26 @@ namespace Flow.Launcher.Plugin.Calculator.ViewModels public SettingsViewModel(Settings settings) { Settings = settings; + DecimalSeparatorLocalized.UpdateLabels(AllDecimalSeparator); } public Settings Settings { get; init; } public IEnumerable MaxDecimalPlacesRange => Enumerable.Range(1, 20); + + public List AllDecimalSeparator { get; } = DecimalSeparatorLocalized.GetValues(); + + public DecimalSeparator SelectedDecimalSeparator + { + get => Settings.DecimalSeparator; + set + { + if (Settings.DecimalSeparator != value) + { + Settings.DecimalSeparator = value; + OnPropertyChanged(); + } + } + } } } diff --git a/Plugins/Flow.Launcher.Plugin.Calculator/Views/CalculatorSettings.xaml b/Plugins/Flow.Launcher.Plugin.Calculator/Views/CalculatorSettings.xaml index ceee3897c..589f3ddcd 100644 --- a/Plugins/Flow.Launcher.Plugin.Calculator/Views/CalculatorSettings.xaml +++ b/Plugins/Flow.Launcher.Plugin.Calculator/Views/CalculatorSettings.xaml @@ -3,20 +3,15 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:calculator="clr-namespace:Flow.Launcher.Plugin.Calculator" - xmlns:core="clr-namespace:Flow.Launcher.Core.Resource;assembly=Flow.Launcher.Core" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - xmlns:ui="clr-namespace:Flow.Launcher.Infrastructure.UI;assembly=Flow.Launcher.Infrastructure" xmlns:viewModels="clr-namespace:Flow.Launcher.Plugin.Calculator.ViewModels" + d:DataContext="{d:DesignInstance Type=viewModels:SettingsViewModel}" d:DesignHeight="450" d:DesignWidth="800" Loaded="CalculatorSettings_Loaded" mc:Ignorable="d"> - - - - @@ -42,14 +37,10 @@ Margin="{StaticResource SettingPanelItemRightTopBottomMargin}" HorizontalAlignment="Left" VerticalAlignment="Center" - ItemsSource="{Binding Source={ui:EnumBindingSource {x:Type calculator:DecimalSeparator}}}" - SelectedItem="{Binding Settings.DecimalSeparator}"> - - - - - - + DisplayMemberPath="Display" + ItemsSource="{Binding AllDecimalSeparator}" + SelectedValue="{Binding SelectedDecimalSeparator, Mode=TwoWay}" + SelectedValuePath="Value" />