mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 10:46:33 +02:00
[QuickAccent] Persist characters usage between runs (#37577)
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist Persist characters usage between PowerToys/QuickAccent runs. - [x] **Closes:** #26034 - [ ] **Communication:** I've discussed this with core contributors already. If work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end user facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments - Persist the dictionaries used to determine the characters usage in a JSON file `%LOCALAPPDATA%\Microsoft\PowerToys\QuickAccent\UsageInfo.json` <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed Manually tested: - JSON is saved when PowerToys is closed and the **Sort characters by usage frequency** is on - JSON is deleted when QuickAccent is called and **Sort characters by usage frequency** is off - JSON is read when QuickAccent is started and characters order is applied from the previous run --------- Co-authored-by: Gleb Khmyznikov <gleb.khmyznikov@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
8c4a3a6944
commit
a50d548a07
@@ -0,0 +1,13 @@
|
|||||||
|
// 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 PowerAccent.Core.Models
|
||||||
|
{
|
||||||
|
public class UsageInfoData
|
||||||
|
{
|
||||||
|
public Dictionary<string, uint> CharacterUsageCounters { get; set; } = [];
|
||||||
|
|
||||||
|
public Dictionary<string, long> CharacterUsageTimestamp { get; set; } = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -117,8 +117,8 @@ public partial class PowerAccent : IDisposable
|
|||||||
if (_settingService.SortByUsageFrequency)
|
if (_settingService.SortByUsageFrequency)
|
||||||
{
|
{
|
||||||
characters = characters.OrderByDescending(character => _usageInfo.GetUsageFrequency(character))
|
characters = characters.OrderByDescending(character => _usageInfo.GetUsageFrequency(character))
|
||||||
.ThenByDescending(character => _usageInfo.GetLastUsageTimestamp(character)).
|
.ThenByDescending(character => _usageInfo.GetLastUsageTimestamp(character))
|
||||||
ToArray<string>();
|
.ToArray<string>();
|
||||||
}
|
}
|
||||||
else if (!_usageInfo.Empty())
|
else if (!_usageInfo.Empty())
|
||||||
{
|
{
|
||||||
@@ -337,6 +337,14 @@ public partial class PowerAccent : IDisposable
|
|||||||
return _settingService.Position;
|
return _settingService.Position;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SaveUsageInfo()
|
||||||
|
{
|
||||||
|
if (_settingService.SortByUsageFrequency)
|
||||||
|
{
|
||||||
|
_usageInfo.Save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_keyboardListener.UnInitHook();
|
_keyboardListener.UnInitHook();
|
||||||
|
|||||||
@@ -3,12 +3,14 @@
|
|||||||
// See the LICENSE file in the project root for more information.
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
using PowerAccent.Core.Models;
|
||||||
using PowerAccent.Core.Services;
|
using PowerAccent.Core.Services;
|
||||||
|
|
||||||
namespace PowerAccent.Core.SerializationContext;
|
namespace PowerAccent.Core.SerializationContext;
|
||||||
|
|
||||||
[JsonSourceGenerationOptions(WriteIndented = true)]
|
[JsonSourceGenerationOptions(WriteIndented = true)]
|
||||||
[JsonSerializable(typeof(SettingsService))]
|
[JsonSerializable(typeof(SettingsService))]
|
||||||
|
[JsonSerializable(typeof(UsageInfoData))]
|
||||||
public partial class SourceGenerationContext : JsonSerializerContext
|
public partial class SourceGenerationContext : JsonSerializerContext
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,28 @@
|
|||||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||||
// See the LICENSE file in the project root for more information.
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
|
using System.IO;
|
||||||
|
using System.Text.Json;
|
||||||
|
using ManagedCommon;
|
||||||
|
using Microsoft.PowerToys.Settings.UI.Library;
|
||||||
|
using PowerAccent.Core.Models;
|
||||||
|
using PowerAccent.Core.SerializationContext;
|
||||||
|
|
||||||
namespace PowerAccent.Core.Tools
|
namespace PowerAccent.Core.Tools
|
||||||
{
|
{
|
||||||
public class CharactersUsageInfo
|
public class CharactersUsageInfo
|
||||||
{
|
{
|
||||||
private Dictionary<string, uint> _characterUsageCounters = new Dictionary<string, uint>();
|
private readonly string _filePath;
|
||||||
private Dictionary<string, long> _characterUsageTimestamp = new Dictionary<string, long>();
|
private readonly Dictionary<string, uint> _characterUsageCounters;
|
||||||
|
private readonly Dictionary<string, long> _characterUsageTimestamp;
|
||||||
|
|
||||||
|
public CharactersUsageInfo()
|
||||||
|
{
|
||||||
|
_filePath = new SettingsUtils().GetSettingsFilePath(PowerAccentSettings.ModuleName, "UsageInfo.json");
|
||||||
|
var data = GetUsageInfoData();
|
||||||
|
_characterUsageCounters = data.CharacterUsageCounters;
|
||||||
|
_characterUsageTimestamp = data.CharacterUsageTimestamp;
|
||||||
|
}
|
||||||
|
|
||||||
public bool Empty()
|
public bool Empty()
|
||||||
{
|
{
|
||||||
@@ -18,19 +34,18 @@ namespace PowerAccent.Core.Tools
|
|||||||
{
|
{
|
||||||
_characterUsageCounters.Clear();
|
_characterUsageCounters.Clear();
|
||||||
_characterUsageTimestamp.Clear();
|
_characterUsageTimestamp.Clear();
|
||||||
|
Delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
public uint GetUsageFrequency(string character)
|
public uint GetUsageFrequency(string character)
|
||||||
{
|
{
|
||||||
_characterUsageCounters.TryGetValue(character, out uint frequency);
|
_characterUsageCounters.TryGetValue(character, out uint frequency);
|
||||||
|
|
||||||
return frequency;
|
return frequency;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long GetLastUsageTimestamp(string character)
|
public long GetLastUsageTimestamp(string character)
|
||||||
{
|
{
|
||||||
_characterUsageTimestamp.TryGetValue(character, out long timestamp);
|
_characterUsageTimestamp.TryGetValue(character, out long timestamp);
|
||||||
|
|
||||||
return timestamp;
|
return timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,5 +62,58 @@ namespace PowerAccent.Core.Tools
|
|||||||
|
|
||||||
_characterUsageTimestamp[character] = DateTimeOffset.Now.ToUnixTimeSeconds();
|
_characterUsageTimestamp[character] = DateTimeOffset.Now.ToUnixTimeSeconds();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Save()
|
||||||
|
{
|
||||||
|
var data = new UsageInfoData
|
||||||
|
{
|
||||||
|
CharacterUsageCounters = _characterUsageCounters,
|
||||||
|
CharacterUsageTimestamp = _characterUsageTimestamp,
|
||||||
|
};
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var json = JsonSerializer.Serialize(data, SourceGenerationContext.Default.UsageInfoData);
|
||||||
|
File.WriteAllText(_filePath, json);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logger.LogError("Failed to save usage file", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Delete()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (File.Exists(_filePath))
|
||||||
|
{
|
||||||
|
File.Delete(_filePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logger.LogError("Failed to delete usage file", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private UsageInfoData GetUsageInfoData()
|
||||||
|
{
|
||||||
|
if (!File.Exists(_filePath))
|
||||||
|
{
|
||||||
|
return new UsageInfoData();
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var json = File.ReadAllText(_filePath);
|
||||||
|
return JsonSerializer.Deserialize(json, SourceGenerationContext.Default.UsageInfoData) ?? new UsageInfoData();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logger.LogError("Failed to read usage file", ex);
|
||||||
|
return new UsageInfoData();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,6 +98,7 @@ public partial class Selector : FluentWindow, IDisposable, INotifyPropertyChange
|
|||||||
|
|
||||||
protected override void OnClosed(EventArgs e)
|
protected override void OnClosed(EventArgs e)
|
||||||
{
|
{
|
||||||
|
_powerAccent.SaveUsageInfo();
|
||||||
_powerAccent.Dispose();
|
_powerAccent.Dispose();
|
||||||
base.OnClosed(e);
|
base.OnClosed(e);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user