From aa9f3bb5404f838dcc2b6cdee8756fdb27b510ee Mon Sep 17 00:00:00 2001 From: moooyo <42196638+moooyo@users.noreply.github.com> Date: Mon, 13 Jan 2025 21:01:41 +0800 Subject: [PATCH] [AOT]Clean up some AOT build issues in PowerAccent.Core (#36264) * init * Use AotCompatibility instead * Replace typeof(Lanaguge) with GetValues * Create new folder to place source generation context file. --------- Co-authored-by: Yu Leng (from Dev Box) --- .../poweraccent/PowerAccent.Core/Languages.cs | 2 +- .../PowerAccent.Core/PowerAccent.Core.csproj | 1 + .../poweraccent/PowerAccent.Core/PowerAccent.cs | 2 +- .../SourceGenerationContext.cs | 14 ++++++++++++++ .../PowerAccent.Core/Services/SettingsService.cs | 9 ++------- 5 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 src/modules/poweraccent/PowerAccent.Core/SerializationContext/SourceGenerationContext.cs diff --git a/src/modules/poweraccent/PowerAccent.Core/Languages.cs b/src/modules/poweraccent/PowerAccent.Core/Languages.cs index 881b85e1cf..60b206b516 100644 --- a/src/modules/poweraccent/PowerAccent.Core/Languages.cs +++ b/src/modules/poweraccent/PowerAccent.Core/Languages.cs @@ -57,7 +57,7 @@ namespace PowerAccent.Core { public static string[] GetDefaultLetterKey(LetterKey letter, Language[] langs) { - if (langs.Length == Enum.GetValues(typeof(Language)).Length) + if (langs.Length == Enum.GetValues().Length) { return GetDefaultLetterKeyALL(letter); } diff --git a/src/modules/poweraccent/PowerAccent.Core/PowerAccent.Core.csproj b/src/modules/poweraccent/PowerAccent.Core/PowerAccent.Core.csproj index c98f2c9b30..1f653884cf 100644 --- a/src/modules/poweraccent/PowerAccent.Core/PowerAccent.Core.csproj +++ b/src/modules/poweraccent/PowerAccent.Core/PowerAccent.Core.csproj @@ -2,6 +2,7 @@ + enable diff --git a/src/modules/poweraccent/PowerAccent.Core/PowerAccent.cs b/src/modules/poweraccent/PowerAccent.Core/PowerAccent.cs index a1802ba428..c2f0698f25 100644 --- a/src/modules/poweraccent/PowerAccent.Core/PowerAccent.cs +++ b/src/modules/poweraccent/PowerAccent.Core/PowerAccent.cs @@ -14,7 +14,7 @@ using PowerToys.PowerAccentKeyboardService; namespace PowerAccent.Core; -public class PowerAccent : IDisposable +public partial class PowerAccent : IDisposable { private readonly SettingsService _settingService; diff --git a/src/modules/poweraccent/PowerAccent.Core/SerializationContext/SourceGenerationContext.cs b/src/modules/poweraccent/PowerAccent.Core/SerializationContext/SourceGenerationContext.cs new file mode 100644 index 0000000000..e682aa7b63 --- /dev/null +++ b/src/modules/poweraccent/PowerAccent.Core/SerializationContext/SourceGenerationContext.cs @@ -0,0 +1,14 @@ +// 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.Text.Json.Serialization; +using PowerAccent.Core.Services; + +namespace PowerAccent.Core.SerializationContext; + +[JsonSourceGenerationOptions(WriteIndented = true)] +[JsonSerializable(typeof(SettingsService))] +public partial class SourceGenerationContext : JsonSerializerContext +{ +} diff --git a/src/modules/poweraccent/PowerAccent.Core/Services/SettingsService.cs b/src/modules/poweraccent/PowerAccent.Core/Services/SettingsService.cs index 9647fb13c3..1a1657ecaf 100644 --- a/src/modules/poweraccent/PowerAccent.Core/Services/SettingsService.cs +++ b/src/modules/poweraccent/PowerAccent.Core/Services/SettingsService.cs @@ -9,6 +9,7 @@ using ManagedCommon; using Microsoft.PowerToys.Settings.UI.Library; using Microsoft.PowerToys.Settings.UI.Library.Enumerations; using Microsoft.PowerToys.Settings.UI.Library.Utilities; +using PowerAccent.Core.SerializationContext; using PowerToys.PowerAccentKeyboardService; namespace PowerAccent.Core.Services; @@ -29,11 +30,6 @@ public class SettingsService _watcher = Helper.GetFileWatcher(PowerAccentModuleName, "settings.json", () => { ReadSettings(); }); } - private static readonly JsonSerializerOptions _serializerOptions = new JsonSerializerOptions - { - WriteIndented = true, - }; - private void ReadSettings() { // TODO this IO call should by Async, update GetFileWatcher helper to support async @@ -46,9 +42,8 @@ public class SettingsService { Logger.LogInfo("QuickAccent settings.json was missing, creating a new one"); var defaultSettings = new PowerAccentSettings(); - var options = _serializerOptions; - _settingsUtils.SaveSettings(JsonSerializer.Serialize(this, options), PowerAccentModuleName); + _settingsUtils.SaveSettings(JsonSerializer.Serialize(this, SourceGenerationContext.Default.SettingsService), PowerAccentModuleName); } var settings = _settingsUtils.GetSettingsOrDefault(PowerAccentModuleName);