Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PowerAccent with multi-language selection #35539

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 65 additions & 49 deletions src/modules/poweraccent/PowerAccent.Core/Languages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace PowerAccent.Core
{
public enum Language
{
ALL,
SPECIAL,
BG,
CA,
CRH,
Expand Down Expand Up @@ -54,52 +54,68 @@ 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);
}

if (langs.Length == 0)
{
return Array.Empty<string>();
}

var characters = new List<string>();
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.
Expand Down Expand Up @@ -149,7 +165,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;
Expand All @@ -160,7 +176,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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(',', StringSplitOptions.RemoveEmptyEntries)
.Select(lang => Enum.TryParse(lang, out Language selectedLangValue) ? selectedLangValue : Language.SPECIAL)
.ToArray();

switch (settings.Properties.ToolbarPosition.Value)
{
Expand Down Expand Up @@ -187,9 +190,9 @@ public string ExcludedApps
}
}

private Language _selectedLang;
private Language[] _selectedLang;

public Language SelectedLang
public Language[] SelectedLang
{
get
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.Collections.Generic;

namespace Microsoft.PowerToys.Settings.UI.Library
{
public class PowerAccentLanguageGroupModel : List<PowerAccentLanguageModel>
{
public PowerAccentLanguageGroupModel(List<PowerAccentLanguageModel> languages, string group)
: base(languages)
{
this.Group = group;
}

public string Group { get; init; }
}
}
11 changes: 11 additions & 0 deletions src/settings-ui/Settings.UI.Library/PowerAccentLanguageModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// 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.

namespace Microsoft.PowerToys.Settings.UI.Library
{
public record PowerAccentLanguageModel(string LanguageCode, string LanguageResourceID, string GroupResourceID)
{
public string Language { get; set; }
}
}
Loading
Loading