Improve code quality

This commit is contained in:
Jack251970 2025-09-15 13:02:42 +08:00
parent 1906d68541
commit f9facda521
4 changed files with 41 additions and 54 deletions

View file

@ -80,14 +80,14 @@ namespace Flow.Launcher.Plugin.Calculator
if (result == null || string.IsNullOrEmpty(result.ToString()))
{
if (!_settings.ShowErrorMessage) return EmptyResults;
return new List<Result>
{
return
[
new Result
{
Title = Localize.flowlauncher_plugin_calculator_expression_not_complete(),
IcoPath = IcoPath
}
};
];
}
if (result.ToString() == "NaN")
@ -105,8 +105,8 @@ namespace Flow.Launcher.Plugin.Calculator
decimal roundedResult = Math.Round(Convert.ToDecimal(result), _settings.MaxDecimalPlaces, MidpointRounding.AwayFromZero);
string newResult = FormatResult(roundedResult);
return new List<Result>
{
return
[
new Result
{
Title = newResult,
@ -128,21 +128,21 @@ namespace Flow.Launcher.Plugin.Calculator
}
}
}
};
];
}
}
catch (Exception)
{
// Mages engine can throw various exceptions, for simplicity we catch them all and show a generic message.
if (!_settings.ShowErrorMessage) return EmptyResults;
return new List<Result>
{
return
[
new Result
{
Title = Localize.flowlauncher_plugin_calculator_expression_not_complete(),
IcoPath = IcoPath
}
};
];
}
return EmptyResults;
@ -153,7 +153,7 @@ namespace Flow.Launcher.Plugin.Calculator
// m.Groups[1].Value will be `(...)` with parens
var contentWithParen = m.Groups[1].Value;
// remove outer parens. `(min(2,3), 4)` becomes `min(2,3), 4`
var argsContent = contentWithParen.Substring(1, contentWithParen.Length - 2);
var argsContent = contentWithParen[1..^1];
var bracketCount = 0;
var splitIndex = -1;

View file

@ -1,12 +1,11 @@

namespace Flow.Launcher.Plugin.Calculator
namespace Flow.Launcher.Plugin.Calculator;
public class Settings
{
public class Settings
{
public DecimalSeparator DecimalSeparator { get; set; } = DecimalSeparator.UseSystemLocale;
public DecimalSeparator DecimalSeparator { get; set; } = DecimalSeparator.UseSystemLocale;
public int MaxDecimalPlaces { get; set; } = 10;
public int MaxDecimalPlaces { get; set; } = 10;
public bool ShowErrorMessage { get; set; } = false;
}
public bool ShowErrorMessage { get; set; } = false;
}

View file

@ -1,31 +1,25 @@
using System.Collections.Generic;
using System.Linq;
namespace Flow.Launcher.Plugin.Calculator.ViewModels
namespace Flow.Launcher.Plugin.Calculator.ViewModels;
public class SettingsViewModel(Settings settings) : BaseModel
{
public class SettingsViewModel : BaseModel
public Settings Settings { get; init; } = settings;
public static IEnumerable<int> MaxDecimalPlacesRange => Enumerable.Range(1, 20);
public List<DecimalSeparatorLocalized> AllDecimalSeparator { get; } = DecimalSeparatorLocalized.GetValues();
public DecimalSeparator SelectedDecimalSeparator
{
public SettingsViewModel(Settings settings)
get => Settings.DecimalSeparator;
set
{
Settings = settings;
}
public Settings Settings { get; init; }
public static IEnumerable<int> MaxDecimalPlacesRange => Enumerable.Range(1, 20);
public List<DecimalSeparatorLocalized> AllDecimalSeparator { get; } = DecimalSeparatorLocalized.GetValues();
public DecimalSeparator SelectedDecimalSeparator
{
get => Settings.DecimalSeparator;
set
if (Settings.DecimalSeparator != value)
{
if (Settings.DecimalSeparator != value)
{
Settings.DecimalSeparator = value;
OnPropertyChanged();
}
Settings.DecimalSeparator = value;
OnPropertyChanged();
}
}
}

View file

@ -1,22 +1,16 @@
using System.Windows.Controls;
using Flow.Launcher.Plugin.Calculator.ViewModels;
namespace Flow.Launcher.Plugin.Calculator.Views
{
/// <summary>
/// Interaction logic for CalculatorSettings.xaml
/// </summary>
public partial class CalculatorSettings : UserControl
{
private readonly SettingsViewModel _viewModel;
private readonly Settings _settings;
namespace Flow.Launcher.Plugin.Calculator.Views;
public CalculatorSettings(Settings settings)
{
_viewModel = new SettingsViewModel(settings);
_settings = _viewModel.Settings;
DataContext = _viewModel;
InitializeComponent();
}
public partial class CalculatorSettings : UserControl
{
private readonly SettingsViewModel _viewModel;
public CalculatorSettings(Settings settings)
{
_viewModel = new SettingsViewModel(settings);
DataContext = _viewModel;
InitializeComponent();
}
}