[QuickAccent]Add setting to sort characters by use frequency (#22847)

* [Quick Accent] add toggle switch to sort characters by usage frequency

* [Quick Accent] if enabled, then sort by frequency for all toolbar triggers

* [Quick Accent] add "start selection from the left" toggle switch

* [Quick Accent] fix error SA1000: The keyword 'new' should not be followed by a space

* Fix C# analyzer build error
This commit is contained in:
Taras
2023-01-03 14:44:57 +02:00
committed by GitHub
parent b1c535a2ce
commit 14ad19e1e3
7 changed files with 178 additions and 3 deletions

View File

@@ -37,6 +37,8 @@ public class PowerAccent : IDisposable
private readonly KeyboardListener _keyboardListener;
private readonly CharactersUsageInfo _usageInfo;
public PowerAccent()
{
LoadUnicodeInfoCache();
@@ -44,6 +46,7 @@ public class PowerAccent : IDisposable
_keyboardListener = new KeyboardListener();
_keyboardListener.InitHook();
_settingService = new SettingsService(_keyboardListener);
_usageInfo = new CharactersUsageInfo();
SetEvents();
}
@@ -88,9 +91,9 @@ public class PowerAccent : IDisposable
private void ShowToolbar(LetterKey letterKey)
{
_visible = true;
_characters = (WindowsFunctions.IsCapsLockState() || WindowsFunctions.IsShiftState()) ? ToUpper(Languages.GetDefaultLetterKey(letterKey, _settingService.SelectedLang)) : Languages.GetDefaultLetterKey(letterKey, _settingService.SelectedLang);
_characterDescriptions = GetCharacterDescriptions(_characters);
_characters = GetCharacters(letterKey);
_characterDescriptions = GetCharacterDescriptions(_characters);
_showUnicodeDescription = _settingService.ShowUnicodeDescription;
Task.Delay(_settingService.InputTime).ContinueWith(
@@ -104,6 +107,28 @@ public class PowerAccent : IDisposable
TaskScheduler.FromCurrentSynchronizationContext());
}
private string[] GetCharacters(LetterKey letterKey)
{
var characters = Languages.GetDefaultLetterKey(letterKey, _settingService.SelectedLang);
if (_settingService.SortByUsageFrequency)
{
characters = characters.OrderByDescending(character => _usageInfo.GetUsageFrequency(character))
.ThenByDescending(character => _usageInfo.GetLastUsageTimestamp(character)).
ToArray<string>();
}
else if (!_usageInfo.Empty())
{
_usageInfo.Clear();
}
if (WindowsFunctions.IsCapsLockState() || WindowsFunctions.IsShiftState())
{
return ToUpper(characters);
}
return characters;
}
private string GetCharacterDescription(string character)
{
List<UnicodeCharInfo> unicodeList = new List<UnicodeCharInfo>();
@@ -180,6 +205,11 @@ public class PowerAccent : IDisposable
if (_selectedIndex != -1)
{
WindowsFunctions.Insert(_characters[_selectedIndex], true);
if (_settingService.SortByUsageFrequency)
{
_usageInfo.IncrementUsageFrequency(_characters[_selectedIndex]);
}
}
break;
@@ -205,7 +235,7 @@ public class PowerAccent : IDisposable
_selectedIndex = _characters.Length / 2;
}
if (triggerKey == TriggerKey.Space)
if (triggerKey == TriggerKey.Space || _settingService.StartSelectionFromTheLeft)
{
_selectedIndex = 0;
}