Update labels on language change and other refactor

- Bring back viewmodel construction on init
- Remove redundant init code
This commit is contained in:
VictoriousRaptor 2025-07-22 23:48:39 +08:00
parent 08f17c4f05
commit 22ffc80aeb
4 changed files with 11 additions and 11 deletions

View file

@ -6,6 +6,7 @@ using System.Text.RegularExpressions;
using System.Windows.Controls;
using Mages.Core;
using Flow.Launcher.Plugin.Calculator.Views;
using Flow.Launcher.Plugin.Calculator.ViewModels;
namespace Flow.Launcher.Plugin.Calculator
{
@ -28,12 +29,14 @@ namespace Flow.Launcher.Plugin.Calculator
internal static PluginInitContext Context { get; set; } = null!;
private static Settings _settings;
private Settings _settings;
private SettingsViewModel _viewModel;
public void Init(PluginInitContext context)
{
Context = context;
_settings = context.API.LoadSettingJsonStorage<Settings>();
_viewModel = new SettingsViewModel(_settings);
MagesEngine = new Engine(new Configuration
{
@ -152,7 +155,7 @@ namespace Flow.Launcher.Plugin.Calculator
return value.ToString(numberFormatInfo);
}
private static string GetDecimalSeparator()
private string GetDecimalSeparator()
{
string systemDecimalSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
return _settings.DecimalSeparator switch
@ -197,5 +200,10 @@ namespace Flow.Launcher.Plugin.Calculator
{
return new CalculatorSettings(_settings);
}
public void OnCultureInfoChanged(CultureInfo newCulture)
{
DecimalSeparatorLocalized.UpdateLabels(_viewModel.AllDecimalSeparator);
}
}
}

View file

@ -8,12 +8,11 @@ namespace Flow.Launcher.Plugin.Calculator.ViewModels
public SettingsViewModel(Settings settings)
{
Settings = settings;
DecimalSeparatorLocalized.UpdateLabels(AllDecimalSeparator);
}
public Settings Settings { get; init; }
public IEnumerable<int> MaxDecimalPlacesRange => Enumerable.Range(1, 20);
public static IEnumerable<int> MaxDecimalPlacesRange => Enumerable.Range(1, 20);
public List<DecimalSeparatorLocalized> AllDecimalSeparator { get; } = DecimalSeparatorLocalized.GetValues();

View file

@ -9,7 +9,6 @@
d:DataContext="{d:DesignInstance Type=viewModels:SettingsViewModel}"
d:DesignHeight="450"
d:DesignWidth="800"
Loaded="CalculatorSettings_Loaded"
mc:Ignorable="d">
<Grid Margin="{StaticResource SettingPanelMargin}">

View file

@ -19,11 +19,5 @@ namespace Flow.Launcher.Plugin.Calculator.Views
DataContext = _viewModel;
InitializeComponent();
}
private void CalculatorSettings_Loaded(object sender, RoutedEventArgs e)
{
DecimalSeparatorComboBox.SelectedItem = _settings.DecimalSeparator;
MaxDecimalPlaces.SelectedItem = _settings.MaxDecimalPlaces;
}
}
}