From 14139affc52179d2a1cc76242455e1bb668c3a93 Mon Sep 17 00:00:00 2001 From: Davide Giacometti <25966642+davidegiacometti@users.noreply.github.com> Date: Fri, 18 Oct 2024 15:09:29 +0200 Subject: [PATCH] [Settings][QuickAccent]Sort Languages ComboBox (#35462) sort languages --- .../ViewModels/GeneralViewModel.cs | 40 +++++++++++++++---- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/settings-ui/Settings.UI/ViewModels/GeneralViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/GeneralViewModel.cs index 25f8b6ef3f..adbc6cd0d3 100644 --- a/src/settings-ui/Settings.UI/ViewModels/GeneralViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/GeneralViewModel.cs @@ -9,7 +9,6 @@ using System.Diagnostics; using System.Globalization; using System.IO; using System.IO.Abstractions; -using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Text.Json; @@ -1083,25 +1082,50 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private void InitializeLanguages() { var lang = LanguageModel.LoadSetting(); - int i = 0; + var selectedLanguageIndex = 0; foreach (var item in langTagsAndIds) { - Languages.Add(new LanguageModel { Tag = item.Key, ResourceID = item.Value, Language = GetResourceString(item.Value) }); + var language = new LanguageModel { Tag = item.Key, ResourceID = item.Value, Language = GetResourceString(item.Value) }; + var index = GetLanguageIndex(language.Language, item.Key == string.Empty); + Languages.Insert(index, language); if (item.Key.Equals(lang, StringComparison.Ordinal)) { - _initLanguagesIndex = i; - LanguagesIndex = i; + selectedLanguageIndex = index; + } + else if (index <= selectedLanguageIndex) + { + selectedLanguageIndex++; } - - i++; } + + _initLanguagesIndex = selectedLanguageIndex; + LanguagesIndex = selectedLanguageIndex; + } + + [System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1309:Use ordinal string comparison", Justification = "Building a user facing list")] + private int GetLanguageIndex(string language, bool isDefault) + { + if (Languages.Count == 0 || isDefault) + { + return 0; + } + + for (var i = 1; i < Languages.Count; i++) + { + if (string.Compare(Languages[i].Language, language, StringComparison.CurrentCultureIgnoreCase) > 0) + { + return i; + } + } + + return Languages.Count; } private void NotifyLanguageChanged() { - OutGoingLanguageSettings outsettings = new OutGoingLanguageSettings(langTagsAndIds.ElementAt(LanguagesIndex).Key); + OutGoingLanguageSettings outsettings = new OutGoingLanguageSettings(Languages[_languagesIndex].Tag); SendConfigMSG(outsettings.ToString()); }