Skip to content

Commit

Permalink
[AOT]Clean up some AOT build issues in PowerAccent.Core (#36264)
Browse files Browse the repository at this point in the history
* init

* Use AotCompatibility instead

* Replace typeof(Lanaguge) with GetValues<Language>

* Create new folder to place source generation context file.

---------

Co-authored-by: Yu Leng (from Dev Box) <[email protected]>
  • Loading branch information
moooyo and Yu Leng (from Dev Box) authored Jan 13, 2025
1 parent a665975 commit aa9f3bb
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/modules/poweraccent/PowerAccent.Core/Languages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal sealed class Languages
{
public static string[] GetDefaultLetterKey(LetterKey letter, Language[] langs)
{
if (langs.Length == Enum.GetValues(typeof(Language)).Length)
if (langs.Length == Enum.GetValues<Language>().Length)
{
return GetDefaultLetterKeyALL(letter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<!-- Look at Directory.Build.props in root for common stuff as well -->
<Import Project="..\..\..\Common.Dotnet.CsWinRT.props" />
<Import Project="..\..\..\Common.SelfContained.props" />
<Import Project="..\..\..\Common.Dotnet.AotCompatibility.props" />

<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
2 changes: 1 addition & 1 deletion src/modules/poweraccent/PowerAccent.Core/PowerAccent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace PowerAccent.Core;

public class PowerAccent : IDisposable
public partial class PowerAccent : IDisposable
{
private readonly SettingsService _settingService;

Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;
Expand All @@ -29,11 +30,6 @@ public SettingsService(KeyboardListener keyboardListener)
_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
Expand All @@ -46,9 +42,8 @@ private void ReadSettings()
{
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<PowerAccentSettings>(PowerAccentModuleName);
Expand Down

0 comments on commit aa9f3bb

Please sign in to comment.