From 0e6fbf236ac480df749430bfb5a81f27519ffb92 Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Thu, 24 Nov 2022 15:29:05 +0800 Subject: [PATCH 1/5] Avoid new CultureInfo every second --- Flow.Launcher/ViewModel/MainViewModel.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 29aef88fd6b..109cfc81e65 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -67,7 +67,14 @@ public MainViewModel(Settings settings) { if (args.PropertyName == nameof(Settings.WindowSize)) { - OnPropertyChanged(nameof(MainWindowWidth)); + } + switch (args.PropertyName) { + case nameof(Settings.WindowSize): + OnPropertyChanged(nameof(MainWindowWidth)); + break; + case nameof(Settings.Language): + Culture = new CultureInfo(Settings.Language); + break; } }; @@ -99,6 +106,8 @@ public MainViewModel(Settings settings) RegisterClockAndDateUpdateAsync(); SetOpenResultModifiers(); + + Culture = new CultureInfo(Settings.Language); } private void RegisterViewUpdate() @@ -334,8 +343,8 @@ private void Esc() public Settings Settings { get; } public string ClockText { get; private set; } public string DateText { get; private set; } + public CultureInfo Culture { get; set; } - public CultureInfo cultureInfo => new CultureInfo(Settings.Language); private async Task RegisterClockAndDateUpdateAsync() { var timer = new PeriodicTimer(TimeSpan.FromSeconds(1)); @@ -343,11 +352,12 @@ private async Task RegisterClockAndDateUpdateAsync() while (await timer.WaitForNextTickAsync().ConfigureAwait(false)) { if (Settings.UseClock) - ClockText = DateTime.Now.ToString(Settings.TimeFormat, cultureInfo); + ClockText = DateTime.Now.ToString(Settings.TimeFormat, Culture); if (Settings.UseDate) - DateText = DateTime.Now.ToString(Settings.DateFormat, cultureInfo); + DateText = DateTime.Now.ToString(Settings.DateFormat, Culture); } } + public ResultsViewModel Results { get; private set; } public ResultsViewModel ContextMenu { get; private set; } From 4aaa268be79f55e1cf0fa1e1e00fceaeb007e648 Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Thu, 24 Nov 2022 15:30:57 +0800 Subject: [PATCH 2/5] cache culture info --- Flow.Launcher/ViewModel/SettingWindowViewModel.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs index 60dc95e2ef9..c00fed851ba 100644 --- a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs +++ b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs @@ -56,11 +56,14 @@ public SettingWindowViewModel(Updater updater, IPortable portable) OnPropertyChanged(nameof(ClockText)); break; case nameof(Settings.Language): + Culture = new CultureInfo(Settings.Language); OnPropertyChanged(nameof(ClockText)); OnPropertyChanged(nameof(DateText)); break; } }; + + Culture = new CultureInfo(Settings.Language); } public Settings Settings { get; set; } @@ -84,7 +87,7 @@ public bool AutoUpdates } } - public CultureInfo cultureInfo => new CultureInfo(Settings.Language); + public CultureInfo Culture { get; private set; } public bool StartFlowLauncherOnSystemStartup { @@ -506,10 +509,9 @@ public string DateFormat set { Settings.DateFormat = value; } } - public string ClockText => DateTime.Now.ToString(TimeFormat, cultureInfo); - - public string DateText => DateTime.Now.ToString(DateFormat, cultureInfo); + public string ClockText => DateTime.Now.ToString(TimeFormat, Culture); + public string DateText => DateTime.Now.ToString(DateFormat, Culture); public double WindowWidthSize { From 082c1c9eb61f232c0442fe22a785c66e67412d52 Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Thu, 24 Nov 2022 15:31:05 +0800 Subject: [PATCH 3/5] formatting --- Flow.Launcher/ViewModel/SettingWindowViewModel.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs index c00fed851ba..c51e3e421a3 100644 --- a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs +++ b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs @@ -499,14 +499,14 @@ public List SearchWindowPositions public string TimeFormat { - get { return Settings.TimeFormat; } - set { Settings.TimeFormat = value; } + get => Settings.TimeFormat; + set => Settings.TimeFormat = value; } public string DateFormat { - get { return Settings.DateFormat; } - set { Settings.DateFormat = value; } + get => Settings.DateFormat; + set => Settings.DateFormat = value; } public string ClockText => DateTime.Now.ToString(TimeFormat, Culture); From a15a40807794d48ba601d7710eae86f9b3193335 Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Thu, 24 Nov 2022 20:05:07 +0800 Subject: [PATCH 4/5] Store CultureInfo in i18n manager --- Flow.Launcher.Core/Resource/Internationalization.cs | 7 ++++++- Flow.Launcher/ViewModel/MainViewModel.cs | 10 +--------- Flow.Launcher/ViewModel/SettingWindowViewModel.cs | 4 +--- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/Flow.Launcher.Core/Resource/Internationalization.cs b/Flow.Launcher.Core/Resource/Internationalization.cs index 4568e92f36b..91bcfdf5a65 100644 --- a/Flow.Launcher.Core/Resource/Internationalization.cs +++ b/Flow.Launcher.Core/Resource/Internationalization.cs @@ -17,6 +17,7 @@ namespace Flow.Launcher.Core.Resource public class Internationalization { public Settings Settings { get; set; } + public CultureInfo CurrentCulture { get; private set; } private const string Folder = "Languages"; private const string DefaultFile = "en.xaml"; private const string Extension = ".xaml"; @@ -62,6 +63,7 @@ private void LoadDefaultLanguage() { LoadLanguage(AvailableLanguages.English); _oldResources.Clear(); + CurrentCulture = new CultureInfo("en"); } public void ChangeLanguage(string languageCode) @@ -96,9 +98,12 @@ public void ChangeLanguage(Language language) { LoadLanguage(language); } - Settings.Language = language.LanguageCode; CultureInfo.CurrentCulture = new CultureInfo(language.LanguageCode); CultureInfo.CurrentUICulture = CultureInfo.CurrentCulture; + CurrentCulture = CultureInfo.CurrentCulture; + + // Raise event after this.CurrentCulture is set + Settings.Language = language.LanguageCode; _ = Task.Run(() => { UpdatePluginMetadataTranslations(); diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 109cfc81e65..5be065429b1 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -65,16 +65,10 @@ public MainViewModel(Settings settings) Settings = settings; Settings.PropertyChanged += (_, args) => { - if (args.PropertyName == nameof(Settings.WindowSize)) - { - } switch (args.PropertyName) { case nameof(Settings.WindowSize): OnPropertyChanged(nameof(MainWindowWidth)); break; - case nameof(Settings.Language): - Culture = new CultureInfo(Settings.Language); - break; } }; @@ -106,8 +100,6 @@ public MainViewModel(Settings settings) RegisterClockAndDateUpdateAsync(); SetOpenResultModifiers(); - - Culture = new CultureInfo(Settings.Language); } private void RegisterViewUpdate() @@ -343,7 +335,7 @@ private void Esc() public Settings Settings { get; } public string ClockText { get; private set; } public string DateText { get; private set; } - public CultureInfo Culture { get; set; } + public CultureInfo Culture => InternationalizationManager.Instance.CurrentCulture; private async Task RegisterClockAndDateUpdateAsync() { diff --git a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs index c51e3e421a3..f31e838fa2a 100644 --- a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs +++ b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs @@ -56,14 +56,12 @@ public SettingWindowViewModel(Updater updater, IPortable portable) OnPropertyChanged(nameof(ClockText)); break; case nameof(Settings.Language): - Culture = new CultureInfo(Settings.Language); OnPropertyChanged(nameof(ClockText)); OnPropertyChanged(nameof(DateText)); break; } }; - Culture = new CultureInfo(Settings.Language); } public Settings Settings { get; set; } @@ -87,7 +85,7 @@ public bool AutoUpdates } } - public CultureInfo Culture { get; private set; } + public CultureInfo Culture => InternationalizationManager.Instance.CurrentCulture; public bool StartFlowLauncherOnSystemStartup { From 30987f9c95e22bee43f16ccb8b7cb3ce74cbcf38 Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Fri, 25 Nov 2022 12:00:37 +0800 Subject: [PATCH 5/5] Use DefaultThreadCurrentCulture for culture in app domain --- .../Resource/Internationalization.cs | 14 ++++++++------ .../UserSettings/Settings.cs | 1 - .../Converters/QuerySuggestionBoxConverter.cs | 5 +++-- Flow.Launcher/ViewModel/MainViewModel.cs | 2 +- Flow.Launcher/ViewModel/SettingWindowViewModel.cs | 2 +- Plugins/Flow.Launcher.Plugin.Calculator/Main.cs | 6 +++--- 6 files changed, 16 insertions(+), 14 deletions(-) diff --git a/Flow.Launcher.Core/Resource/Internationalization.cs b/Flow.Launcher.Core/Resource/Internationalization.cs index 91bcfdf5a65..acc693ed527 100644 --- a/Flow.Launcher.Core/Resource/Internationalization.cs +++ b/Flow.Launcher.Core/Resource/Internationalization.cs @@ -17,7 +17,6 @@ namespace Flow.Launcher.Core.Resource public class Internationalization { public Settings Settings { get; set; } - public CultureInfo CurrentCulture { get; private set; } private const string Folder = "Languages"; private const string DefaultFile = "en.xaml"; private const string Extension = ".xaml"; @@ -63,7 +62,6 @@ private void LoadDefaultLanguage() { LoadLanguage(AvailableLanguages.English); _oldResources.Clear(); - CurrentCulture = new CultureInfo("en"); } public void ChangeLanguage(string languageCode) @@ -98,11 +96,15 @@ public void ChangeLanguage(Language language) { LoadLanguage(language); } - CultureInfo.CurrentCulture = new CultureInfo(language.LanguageCode); + // Culture of this thread + // Use CreateSpecificCulture to preserve possible user-override settings in Windows + CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture(language.LanguageCode); CultureInfo.CurrentUICulture = CultureInfo.CurrentCulture; - CurrentCulture = CultureInfo.CurrentCulture; + // App domain + CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture(language.LanguageCode); + CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.DefaultThreadCurrentCulture; - // Raise event after this.CurrentCulture is set + // Raise event after culture is set Settings.Language = language.LanguageCode; _ = Task.Run(() => { @@ -191,7 +193,7 @@ private void UpdatePluginMetadataTranslations() { p.Metadata.Name = pluginI18N.GetTranslatedPluginTitle(); p.Metadata.Description = pluginI18N.GetTranslatedPluginDescription(); - pluginI18N.OnCultureInfoChanged(CultureInfo.CurrentCulture); + pluginI18N.OnCultureInfoChanged(CultureInfo.DefaultThreadCurrentCulture); } catch (Exception e) { diff --git a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs index 33072b53d22..3561c6ffe55 100644 --- a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs +++ b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs @@ -6,7 +6,6 @@ using System.Windows; using Flow.Launcher.Plugin; using Flow.Launcher.Plugin.SharedModels; -using Flow.Launcher; using Flow.Launcher.ViewModel; namespace Flow.Launcher.Infrastructure.UserSettings diff --git a/Flow.Launcher/Converters/QuerySuggestionBoxConverter.cs b/Flow.Launcher/Converters/QuerySuggestionBoxConverter.cs index ecdfc5851a0..1e39473e055 100644 --- a/Flow.Launcher/Converters/QuerySuggestionBoxConverter.cs +++ b/Flow.Launcher/Converters/QuerySuggestionBoxConverter.cs @@ -52,7 +52,8 @@ public object Convert(object[] values, Type targetType, object parameter, Cultur // Check if Text will be larger then our QueryTextBox System.Windows.Media.Typeface typeface = new Typeface(QueryTextBox.FontFamily, QueryTextBox.FontStyle, QueryTextBox.FontWeight, QueryTextBox.FontStretch); - System.Windows.Media.FormattedText ft = new FormattedText(QueryTextBox.Text, System.Globalization.CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight, typeface, QueryTextBox.FontSize, Brushes.Black); + // TODO: Obsolete warning? + System.Windows.Media.FormattedText ft = new FormattedText(QueryTextBox.Text, System.Globalization.CultureInfo.DefaultThreadCurrentCulture, System.Windows.FlowDirection.LeftToRight, typeface, QueryTextBox.FontSize, Brushes.Black); var offset = QueryTextBox.Padding.Right; @@ -75,4 +76,4 @@ public object[] ConvertBack(object value, Type[] targetTypes, object parameter, throw new NotImplementedException(); } } -} \ No newline at end of file +} diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 5be065429b1..4db8e3df3b7 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -335,7 +335,7 @@ private void Esc() public Settings Settings { get; } public string ClockText { get; private set; } public string DateText { get; private set; } - public CultureInfo Culture => InternationalizationManager.Instance.CurrentCulture; + public CultureInfo Culture => CultureInfo.DefaultThreadCurrentCulture; private async Task RegisterClockAndDateUpdateAsync() { diff --git a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs index f31e838fa2a..de4544233e5 100644 --- a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs +++ b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs @@ -85,7 +85,7 @@ public bool AutoUpdates } } - public CultureInfo Culture => InternationalizationManager.Instance.CurrentCulture; + public CultureInfo Culture => CultureInfo.DefaultThreadCurrentCulture; public bool StartFlowLauncherOnSystemStartup { diff --git a/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs b/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs index bc79219b6d7..d5dcdacea5c 100644 --- a/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Globalization; using System.Runtime.InteropServices; @@ -61,7 +61,7 @@ public List Query(Query query) switch (_settings.DecimalSeparator) { case DecimalSeparator.Comma: - case DecimalSeparator.UseSystemLocale when CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator == ",": + case DecimalSeparator.UseSystemLocale when CultureInfo.DefaultThreadCurrentCulture.NumberFormat.NumberDecimalSeparator == ",": expression = query.Search.Replace(",", "."); break; default: @@ -157,7 +157,7 @@ private string ChangeDecimalSeparator(decimal value, string newDecimalSeparator) private string GetDecimalSeparator() { - string systemDecimalSeperator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator; + string systemDecimalSeperator = CultureInfo.DefaultThreadCurrentCulture.NumberFormat.NumberDecimalSeparator; switch (_settings.DecimalSeparator) { case DecimalSeparator.UseSystemLocale: return systemDecimalSeperator;