From 714ce454659de20a6a7c998992268602ce3dae35 Mon Sep 17 00:00:00 2001 From: Sergey Chernyaev Date: Tue, 22 Oct 2024 23:37:19 +0200 Subject: [PATCH 1/2] PowerAccent with multi-language selection - Updated Language enum, method signatures, and settings to support multiple language selections. - Remove ALL language and added special characters language instead. - Modified UI to use ListView with checkboxes for language selection, including a "Select All" option. - Adjusted ViewModel and code-behind to handle multi-selection logic. - Updated Resources.resw and PowerAccentViewModel.cs to reflect these changes. --- .../poweraccent/PowerAccent.Core/Languages.cs | 109 ++++---- .../Services/SettingsService.cs | 9 +- .../SettingsXAML/Views/PowerAccentPage.xaml | 244 ++++++++++++++---- .../Views/PowerAccentPage.xaml.cs | 61 ++++- .../Settings.UI/Strings/en-us/Resources.resw | 83 +++--- .../ViewModels/PowerAccentViewModel.cs | 38 ++- 6 files changed, 391 insertions(+), 153 deletions(-) diff --git a/src/modules/poweraccent/PowerAccent.Core/Languages.cs b/src/modules/poweraccent/PowerAccent.Core/Languages.cs index d02afd2c85c8..0edf936dffb2 100644 --- a/src/modules/poweraccent/PowerAccent.Core/Languages.cs +++ b/src/modules/poweraccent/PowerAccent.Core/Languages.cs @@ -10,7 +10,7 @@ namespace PowerAccent.Core { public enum Language { - ALL, + SPECIAL, BG, CA, CRH, @@ -54,52 +54,63 @@ public enum Language internal sealed class Languages { - public static string[] GetDefaultLetterKey(LetterKey letter, Language lang) - { - return lang switch - { - Language.ALL => GetDefaultLetterKeyALL(letter), // All - Language.BG => GetDefaultLetterKeyBG(letter), // Bulgarian - Language.CA => GetDefaultLetterKeyCA(letter), // Catalan - Language.CRH => GetDefaultLetterKeyCRH(letter), // Crimean Tatar - Language.CUR => GetDefaultLetterKeyCUR(letter), // Currency - Language.CY => GetDefaultLetterKeyCY(letter), // Welsh - Language.CZ => GetDefaultLetterKeyCZ(letter), // Czech - Language.DK => GetDefaultLetterKeyDK(letter), // Danish - Language.GA => GetDefaultLetterKeyGA(letter), // Gaeilge (Irish) - Language.GD => GetDefaultLetterKeyGD(letter), // Gàidhlig (Scottish Gaelic) - Language.DE => GetDefaultLetterKeyDE(letter), // German - Language.EL => GetDefaultLetterKeyEL(letter), // Greek - Language.EST => GetDefaultLetterKeyEST(letter), // Estonian - Language.EPO => GetDefaultLetterKeyEPO(letter), // Esperanto - Language.FI => GetDefaultLetterKeyFI(letter), // Finnish - Language.FR => GetDefaultLetterKeyFR(letter), // French - Language.HR => GetDefaultLetterKeyHR(letter), // Croatian - Language.HE => GetDefaultLetterKeyHE(letter), // Hebrew - Language.HU => GetDefaultLetterKeyHU(letter), // Hungarian - Language.IS => GetDefaultLetterKeyIS(letter), // Iceland - Language.IPA => GetDefaultLetterKeyIPA(letter), // IPA (International phonetic alphabet) - Language.IT => GetDefaultLetterKeyIT(letter), // Italian - Language.KU => GetDefaultLetterKeyKU(letter), // Kurdish - Language.LT => GetDefaultLetterKeyLT(letter), // Lithuanian - Language.MK => GetDefaultLetterKeyMK(letter), // Macedonian - Language.MI => GetDefaultLetterKeyMI(letter), // Maori - Language.NL => GetDefaultLetterKeyNL(letter), // Dutch - Language.NO => GetDefaultLetterKeyNO(letter), // Norwegian - Language.PI => GetDefaultLetterKeyPI(letter), // Pinyin - Language.PL => GetDefaultLetterKeyPL(letter), // Polish - Language.PT => GetDefaultLetterKeyPT(letter), // Portuguese - Language.RO => GetDefaultLetterKeyRO(letter), // Romanian - Language.ROM => GetDefaultLetterKeyROM(letter), // Middle Eastern Romanization - Language.SK => GetDefaultLetterKeySK(letter), // Slovak - Language.SL => GetDefaultLetterKeySL(letter), // Slovenian - Language.SP => GetDefaultLetterKeySP(letter), // Spain - Language.SR => GetDefaultLetterKeySR(letter), // Serbian - Language.SR_CYRL => GetDefaultLetterKeySRCyrillic(letter), // Serbian Cyrillic - Language.SV => GetDefaultLetterKeySV(letter), // Swedish - Language.TK => GetDefaultLetterKeyTK(letter), // Turkish - _ => throw new ArgumentException("The language {0} is not known in this context", lang.ToString()), - }; + public static string[] GetDefaultLetterKey(LetterKey letter, Language[] langs) + { + if (langs.Length == Enum.GetValues(typeof(Language)).Length) + { + return GetDefaultLetterKeyALL(letter); + } + + var characters = new List(); + foreach (var lang in langs) + { + characters.AddRange(lang switch + { + Language.SPECIAL => GetDefaultLetterKeySPECIAL(letter), // Special Characters + Language.BG => GetDefaultLetterKeyBG(letter), // Bulgarian + Language.CA => GetDefaultLetterKeyCA(letter), // Catalan + Language.CRH => GetDefaultLetterKeyCRH(letter), // Crimean Tatar + Language.CUR => GetDefaultLetterKeyCUR(letter), // Currency + Language.CY => GetDefaultLetterKeyCY(letter), // Welsh + Language.CZ => GetDefaultLetterKeyCZ(letter), // Czech + Language.DK => GetDefaultLetterKeyDK(letter), // Danish + Language.GA => GetDefaultLetterKeyGA(letter), // Gaeilge (Irish) + Language.GD => GetDefaultLetterKeyGD(letter), // Gàidhlig (Scottish Gaelic) + Language.DE => GetDefaultLetterKeyDE(letter), // German + Language.EL => GetDefaultLetterKeyEL(letter), // Greek + Language.EST => GetDefaultLetterKeyEST(letter), // Estonian + Language.EPO => GetDefaultLetterKeyEPO(letter), // Esperanto + Language.FI => GetDefaultLetterKeyFI(letter), // Finnish + Language.FR => GetDefaultLetterKeyFR(letter), // French + Language.HR => GetDefaultLetterKeyHR(letter), // Croatian + Language.HE => GetDefaultLetterKeyHE(letter), // Hebrew + Language.HU => GetDefaultLetterKeyHU(letter), // Hungarian + Language.IS => GetDefaultLetterKeyIS(letter), // Iceland + Language.IPA => GetDefaultLetterKeyIPA(letter), // IPA (International phonetic alphabet) + Language.IT => GetDefaultLetterKeyIT(letter), // Italian + Language.KU => GetDefaultLetterKeyKU(letter), // Kurdish + Language.LT => GetDefaultLetterKeyLT(letter), // Lithuanian + Language.MK => GetDefaultLetterKeyMK(letter), // Macedonian + Language.MI => GetDefaultLetterKeyMI(letter), // Maori + Language.NL => GetDefaultLetterKeyNL(letter), // Dutch + Language.NO => GetDefaultLetterKeyNO(letter), // Norwegian + Language.PI => GetDefaultLetterKeyPI(letter), // Pinyin + Language.PL => GetDefaultLetterKeyPL(letter), // Polish + Language.PT => GetDefaultLetterKeyPT(letter), // Portuguese + Language.RO => GetDefaultLetterKeyRO(letter), // Romanian + Language.ROM => GetDefaultLetterKeyROM(letter), // Middle Eastern Romanization + Language.SK => GetDefaultLetterKeySK(letter), // Slovak + Language.SL => GetDefaultLetterKeySL(letter), // Slovenian + Language.SP => GetDefaultLetterKeySP(letter), // Spain + Language.SR => GetDefaultLetterKeySR(letter), // Serbian + Language.SR_CYRL => GetDefaultLetterKeySRCyrillic(letter), // Serbian Cyrillic + Language.SV => GetDefaultLetterKeySV(letter), // Swedish + Language.TK => GetDefaultLetterKeyTK(letter), // Turkish + _ => throw new ArgumentException("The language {0} is not known in this context", lang.ToString()), + }); + } + + return characters.Distinct().ToArray(); } // Store the computed letters for each key, so that subsequent calls don't take as long. @@ -149,7 +160,7 @@ private static string[] GetDefaultLetterKeyALL(LetterKey letter) .Union(GetDefaultLetterKeySRCyrillic(letter)) .Union(GetDefaultLetterKeySV(letter)) .Union(GetDefaultLetterKeyTK(letter)) - .Union(GetDefaultLetterKeyAllLanguagesOnly(letter)) + .Union(GetDefaultLetterKeySPECIAL(letter)) .ToArray(); _allLanguagesCache[letter] = cachedValue; @@ -160,7 +171,7 @@ private static string[] GetDefaultLetterKeyALL(LetterKey letter) // Contains all characters that should be shown in all languages but currently don't belong to any of the single languages available for that letter. // These characters can be removed from this list after they've been added to one of the other languages for that specific letter. - private static string[] GetDefaultLetterKeyAllLanguagesOnly(LetterKey letter) + private static string[] GetDefaultLetterKeySPECIAL(LetterKey letter) { return letter switch { diff --git a/src/modules/poweraccent/PowerAccent.Core/Services/SettingsService.cs b/src/modules/poweraccent/PowerAccent.Core/Services/SettingsService.cs index a745d83bc917..9a35460a43ab 100644 --- a/src/modules/poweraccent/PowerAccent.Core/Services/SettingsService.cs +++ b/src/modules/poweraccent/PowerAccent.Core/Services/SettingsService.cs @@ -66,7 +66,10 @@ private void ReadSettings() ExcludedApps = settings.Properties.ExcludedApps.Value; _keyboardListener.UpdateExcludedApps(ExcludedApps); - SelectedLang = Enum.TryParse(settings.Properties.SelectedLang.Value, out Language selectedLangValue) ? selectedLangValue : Language.ALL; + SelectedLang = settings.Properties.SelectedLang.Value + .Split(',') + .Select(lang => Enum.TryParse(lang, out Language selectedLangValue) ? selectedLangValue : Language.SPECIAL) + .ToArray(); switch (settings.Properties.ToolbarPosition.Value) { @@ -187,9 +190,9 @@ public string ExcludedApps } } - private Language _selectedLang; + private Language[] _selectedLang; - public Language SelectedLang + public Language[] SelectedLang { get { diff --git a/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerAccentPage.xaml b/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerAccentPage.xaml index 77c96b15f690..fbb88b673420 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerAccentPage.xaml +++ b/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerAccentPage.xaml @@ -1,4 +1,4 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerAccentPage.xaml.cs b/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerAccentPage.xaml.cs index 91e57b8105e5..3e1240462008 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerAccentPage.xaml.cs +++ b/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerAccentPage.xaml.cs @@ -1,10 +1,12 @@ // Copyright (c) Microsoft Corporation // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. - +using System.Linq; +using CommunityToolkit.WinUI; using Microsoft.PowerToys.Settings.UI.Helpers; using Microsoft.PowerToys.Settings.UI.Library; using Microsoft.PowerToys.Settings.UI.ViewModels; +using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; namespace Microsoft.PowerToys.Settings.UI.Views @@ -19,11 +21,68 @@ public PowerAccentPage() ViewModel = new PowerAccentViewModel(settingsUtils, SettingsRepository.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage); DataContext = ViewModel; this.InitializeComponent(); + this.InitializeControlsStates(); } public void RefreshEnabledState() { ViewModel.RefreshEnabledState(); } + + private void InitializeControlsStates() + { + SetListViewSelected(); + SetCheckBoxStatus(); + } + + private void SetListViewSelected() + { + foreach (var languageIndex in ViewModel.SelectedLangIndexes) + { + var item = this.QuickAccent_Language_Select.Items.ElementAtOrDefault(languageIndex); + if (item != null) + { + this.QuickAccent_Language_Select.SelectedItems.Add(item); + } + } + } + + private void SetCheckBoxStatus() + { + if (ViewModel.SelectedLangIndexes.Length == 0) + { + this.QuickAccent_SelectedLanguage_All.IsChecked = false; + this.QuickAccent_SelectedLanguage_All.IsThreeState = false; + } + else if (ViewModel.AllSelected) + { + this.QuickAccent_SelectedLanguage_All.IsChecked = true; + this.QuickAccent_SelectedLanguage_All.IsThreeState = false; + } + else + { + this.QuickAccent_SelectedLanguage_All.IsThreeState = true; + this.QuickAccent_SelectedLanguage_All.IsChecked = null; + } + } + + private void QuickAccent_SelectedLanguage_SelectAll(object sender, RoutedEventArgs e) + { + this.QuickAccent_Language_Select.SelectAllSafe(); + } + + private void QuickAccent_SelectedLanguage_UnselectAll(object sender, RoutedEventArgs e) + { + this.QuickAccent_Language_Select.DeselectAll(); + } + + private void QuickAccent_SelectedLanguage_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + var listView = sender as ListView; + var indexes = listView.SelectedItems.Select(listView.Items.IndexOf).ToArray(); + ViewModel.SelectedLangIndexes = indexes; + + SetCheckBoxStatus(); + } } } diff --git a/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw b/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw index 781a89ae8809..01dc78b00cfc 100644 --- a/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw +++ b/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw @@ -1,4 +1,4 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + diff --git a/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerAccentPage.xaml.cs b/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerAccentPage.xaml.cs index 3e1240462008..c0e78d3e87ce 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerAccentPage.xaml.cs +++ b/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerAccentPage.xaml.cs @@ -31,25 +31,12 @@ public void RefreshEnabledState() private void InitializeControlsStates() { - SetListViewSelected(); SetCheckBoxStatus(); } - private void SetListViewSelected() - { - foreach (var languageIndex in ViewModel.SelectedLangIndexes) - { - var item = this.QuickAccent_Language_Select.Items.ElementAtOrDefault(languageIndex); - if (item != null) - { - this.QuickAccent_Language_Select.SelectedItems.Add(item); - } - } - } - private void SetCheckBoxStatus() { - if (ViewModel.SelectedLangIndexes.Length == 0) + if (ViewModel.SelectedLanguageOptions.Length == 0) { this.QuickAccent_SelectedLanguage_All.IsChecked = false; this.QuickAccent_SelectedLanguage_All.IsThreeState = false; @@ -79,10 +66,20 @@ private void QuickAccent_SelectedLanguage_UnselectAll(object sender, RoutedEvent private void QuickAccent_SelectedLanguage_SelectionChanged(object sender, SelectionChangedEventArgs e) { var listView = sender as ListView; - var indexes = listView.SelectedItems.Select(listView.Items.IndexOf).ToArray(); - ViewModel.SelectedLangIndexes = indexes; + + ViewModel.SelectedLanguageOptions = listView.SelectedItems + .Select(item => item as PowerAccentLanguageModel) + .ToArray(); SetCheckBoxStatus(); } + + private void QuickAccent_Language_Select_Loaded(object sender, RoutedEventArgs e) + { + foreach (var languageOption in ViewModel.SelectedLanguageOptions) + { + this.QuickAccent_Language_Select.SelectedItems.Add(languageOption); + } + } } } diff --git a/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw b/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw index 01dc78b00cfc..59626edce64e 100644 --- a/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw +++ b/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw @@ -3540,130 +3540,136 @@ Activate by holding the key for the character you want to add an accent to, then Show only accented characters common to the selected set - + All available - + + Special sets + + + Language sets + + Special Characters - + Bulgarian - + Catalan - + Crimean Tatar - + Currency - + Croatian - + Czech - + Danish - + Gaeilge Gaelic language spoken in Ireland - + Gàidhlig Scottish Gaelic - + German - + Greek - + Hebrew - + French - + Finnish - + Estonian - + Esperanto - + IPA International Phonetic Alphabet - + Lithuanian - + Macedonian - + Maori - + Middle Eastern Romanization - + Dutch - + Norwegian - + Pinyin - + Polish - + Portuguese - + Slovak - + Slovenian - + Spanish - + Swedish - + Turkish - + Icelandic - + Romanian - + Serbian - + Serbian Cyrillic - + Hungarian - + Italian - + Kurdish - + Welsh diff --git a/src/settings-ui/Settings.UI/ViewModels/PowerAccentViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/PowerAccentViewModel.cs index 7947f47a52d1..0c4f9db65092 100644 --- a/src/settings-ui/Settings.UI/ViewModels/PowerAccentViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/PowerAccentViewModel.cs @@ -3,10 +3,11 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; - using global::PowerToys.GPOWrapper; +using Microsoft.PowerToys.Settings.UI.Helpers; using Microsoft.PowerToys.Settings.UI.Library; using Microsoft.PowerToys.Settings.UI.Library.Enumerations; using Microsoft.PowerToys.Settings.UI.Library.Helpers; @@ -22,50 +23,53 @@ public class PowerAccentViewModel : Observable private readonly ISettingsUtils _settingsUtils; - // These should be in the same order as the ComboBoxItems in PowerAccentPage.xaml - private readonly string[] _languageOptions = - { - "SPECIAL", - "BG", - "CA", - "CRH", - "CUR", - "HR", - "CZ", - "DK", - "GA", - "GD", - "NL", - "EL", - "EST", - "EPO", - "FI", - "FR", - "DE", - "HE", - "HU", - "IS", - "IPA", - "IT", - "KU", - "LT", - "MK", - "MI", - "NO", - "PI", - "PL", - "PT", - "RO", - "ROM", - "SK", - "SL", - "SP", - "SR", - "SR_CYRL", - "SV", - "TK", - "CY", - }; + private const string SpecialGroup = "QuickAccent_Group_Special"; + private const string LanguageGroup = "QuickAccent_Group_Language"; + + public List Languages { get; } = [ + new PowerAccentLanguageModel("SPECIAL", "QuickAccent_SelectedLanguage_Special", SpecialGroup), + new PowerAccentLanguageModel("BG", "QuickAccent_SelectedLanguage_Bulgarian", LanguageGroup), + new PowerAccentLanguageModel("CA", "QuickAccent_SelectedLanguage_Catalan", LanguageGroup), + new PowerAccentLanguageModel("CRH", "QuickAccent_SelectedLanguage_Crimean", LanguageGroup), + new PowerAccentLanguageModel("CUR", "QuickAccent_SelectedLanguage_Currency", SpecialGroup), + new PowerAccentLanguageModel("HR", "QuickAccent_SelectedLanguage_Croatian", LanguageGroup), + new PowerAccentLanguageModel("CZ", "QuickAccent_SelectedLanguage_Czech", LanguageGroup), + new PowerAccentLanguageModel("DK", "QuickAccent_SelectedLanguage_Danish", LanguageGroup), + new PowerAccentLanguageModel("GA", "QuickAccent_SelectedLanguage_Gaeilge", LanguageGroup), + new PowerAccentLanguageModel("GD", "QuickAccent_SelectedLanguage_Gaidhlig", LanguageGroup), + new PowerAccentLanguageModel("NL", "QuickAccent_SelectedLanguage_Dutch", LanguageGroup), + new PowerAccentLanguageModel("EL", "QuickAccent_SelectedLanguage_Greek", LanguageGroup), + new PowerAccentLanguageModel("EST", "QuickAccent_SelectedLanguage_Estonian", LanguageGroup), + new PowerAccentLanguageModel("EPO", "QuickAccent_SelectedLanguage_Esperanto", LanguageGroup), + new PowerAccentLanguageModel("FI", "QuickAccent_SelectedLanguage_Finnish", LanguageGroup), + new PowerAccentLanguageModel("FR", "QuickAccent_SelectedLanguage_French", LanguageGroup), + new PowerAccentLanguageModel("DE", "QuickAccent_SelectedLanguage_German", LanguageGroup), + new PowerAccentLanguageModel("HE", "QuickAccent_SelectedLanguage_Hebrew", LanguageGroup), + new PowerAccentLanguageModel("HU", "QuickAccent_SelectedLanguage_Hungarian", LanguageGroup), + new PowerAccentLanguageModel("IS", "QuickAccent_SelectedLanguage_Icelandic", LanguageGroup), + new PowerAccentLanguageModel("IPA", "QuickAccent_SelectedLanguage_IPA", SpecialGroup), + new PowerAccentLanguageModel("IT", "QuickAccent_SelectedLanguage_Italian", LanguageGroup), + new PowerAccentLanguageModel("KU", "QuickAccent_SelectedLanguage_Kurdish", LanguageGroup), + new PowerAccentLanguageModel("LT", "QuickAccent_SelectedLanguage_Lithuanian", LanguageGroup), + new PowerAccentLanguageModel("MK", "QuickAccent_SelectedLanguage_Macedonian", LanguageGroup), + new PowerAccentLanguageModel("MI", "QuickAccent_SelectedLanguage_Maori", LanguageGroup), + new PowerAccentLanguageModel("NO", "QuickAccent_SelectedLanguage_Norwegian", LanguageGroup), + new PowerAccentLanguageModel("PI", "QuickAccent_SelectedLanguage_Pinyin", LanguageGroup), + new PowerAccentLanguageModel("PL", "QuickAccent_SelectedLanguage_Polish", LanguageGroup), + new PowerAccentLanguageModel("PT", "QuickAccent_SelectedLanguage_Portuguese", LanguageGroup), + new PowerAccentLanguageModel("RO", "QuickAccent_SelectedLanguage_Romanian", LanguageGroup), + new PowerAccentLanguageModel("ROM", "QuickAccent_SelectedLanguage_Romanization", SpecialGroup), + new PowerAccentLanguageModel("SK", "QuickAccent_SelectedLanguage_Slovak", LanguageGroup), + new PowerAccentLanguageModel("SL", "QuickAccent_SelectedLanguage_Slovenian", LanguageGroup), + new PowerAccentLanguageModel("SP", "QuickAccent_SelectedLanguage_Spanish", LanguageGroup), + new PowerAccentLanguageModel("SR", "QuickAccent_SelectedLanguage_Serbian", LanguageGroup), + new PowerAccentLanguageModel("SR_CYRL", "QuickAccent_SelectedLanguage_Serbian_Cyrillic", LanguageGroup), + new PowerAccentLanguageModel("SV", "QuickAccent_SelectedLanguage_Swedish", LanguageGroup), + new PowerAccentLanguageModel("TK", "QuickAccent_SelectedLanguage_Turkish", LanguageGroup), + new PowerAccentLanguageModel("CY", "QuickAccent_SelectedLanguage_Welsh", LanguageGroup), + ]; + + public PowerAccentLanguageGroupModel[] LanguageGroups { get; private set; } private readonly string[] _toolbarOptions = { @@ -91,6 +95,7 @@ public PowerAccentViewModel(ISettingsUtils settingsUtils, ISettingsRepository Array.IndexOf(_languageOptions, l)).ToArray(); + SelectedLanguageOptions = _powerAccentSettings.Properties.SelectedLang.Value.Split(',') + .Select(l => Languages.Find(lang => lang.LanguageCode == l)) + .ToArray(); + } + else if (_powerAccentSettings.Properties.SelectedLang.Value.Contains("ALL")) + { + SelectedLanguageOptions = Languages.ToArray(); } else { - SelectedLangIndexes = Enumerable.Range(0, _languageOptions.Length).ToArray(); + SelectedLanguageOptions = Array.Empty(); } _toolbarPositionIndex = Array.IndexOf(_toolbarOptions, _powerAccentSettings.Properties.ToolbarPosition.Value); @@ -135,6 +146,23 @@ private void InitializeEnabledValue() } } + /// + /// Adds Localized Language Name, sorts by it and splits languages into two groups. + /// + private void InitializeLanguages() + { + foreach (var item in Languages) + { + item.Language = ResourceLoaderInstance.ResourceLoader.GetString(item.LanguageResourceID); + } + + Languages.Sort((x, y) => string.Compare(x.Language, y.Language, StringComparison.Ordinal)); + LanguageGroups = Languages + .GroupBy(language => language.GroupResourceID) + .Select(grp => new PowerAccentLanguageGroupModel(grp.ToList(), ResourceLoaderInstance.ResourceLoader.GetString(grp.Key))) + .ToArray(); + } + public bool IsEnabled { get => _isEnabled; @@ -261,27 +289,18 @@ public int ToolbarPositionIndex } } - public bool AllSelected => _selectedLanguages.Length == _languageOptions.Length; + public bool AllSelected => _selectedLanguageOptions.Length == Languages.Count; - private string[] _selectedLanguages; + private PowerAccentLanguageModel[] _selectedLanguageOptions; - private int[] _selectedLangIndexes; - - public int[] SelectedLangIndexes + public PowerAccentLanguageModel[] SelectedLanguageOptions { - get => _selectedLangIndexes; + get => _selectedLanguageOptions; set { - if (_selectedLangIndexes == value) - { - return; - } - - _selectedLangIndexes = value; - _selectedLanguages = _selectedLangIndexes.Select(s => _languageOptions[s]).ToArray(); - - _powerAccentSettings.Properties.SelectedLang.Value = string.Join(',', _selectedLanguages); - RaisePropertyChanged(nameof(SelectedLangIndexes)); + _selectedLanguageOptions = value; + _powerAccentSettings.Properties.SelectedLang.Value = string.Join(',', _selectedLanguageOptions.Select(l => l.LanguageCode)); + RaisePropertyChanged(nameof(SelectedLanguageOptions)); } }