mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-15 19:27:56 +01:00
[AOT] Refactor SettingsLib/SettingsUI for Native AOT compatibility (#42644)
<!-- 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 Key Changes: 1. Settings.UI.Library: - Added SettingsSerializationContext.cs with comprehensive JsonSerializable attributes for all settings types - Updated BasePTModuleSettings.ToJsonString() to use AOT-compatible serialization - Updated SettingsUtils.GetFile<T>() to use AOT-compatible deserialization - Modified all ToString() methods in Properties classes to use SettingsSerializationContext - Converted struct fields to properties in SunTimes and MouseWithoutBordersProperties for serialization compatibility 2. Settings.UI: - Fixed namespace alias in SourceGenerationContextContext.cs to avoid conflicts For any future developers who discover incorrect settings resolution, please follow up my changes to add your setting type into JsonSerilizerContext. <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [ ] Closes: #xxx - [ ] **Communication:** I've discussed this with core contributors already. If the 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 <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed Co-authored-by: Yu Leng <yuleng@microsoft.com>
This commit is contained in:
@@ -10,7 +10,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library;
|
||||
|
||||
public sealed class AdvancedPasteCustomActions
|
||||
{
|
||||
private static readonly JsonSerializerOptions _serializerOptions = new()
|
||||
private static readonly JsonSerializerOptions _serializerOptions = new(SettingsSerializationContext.Default.Options)
|
||||
{
|
||||
WriteIndented = true,
|
||||
};
|
||||
|
||||
@@ -104,6 +104,6 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
public PasteAIConfiguration PasteAIConfiguration { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
=> JsonSerializer.Serialize(this);
|
||||
=> JsonSerializer.Serialize(this, SettingsSerializationContext.Default.AdvancedPasteProperties);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,32 @@
|
||||
// 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;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for all PowerToys module settings.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para><strong>IMPORTANT for Native AOT compatibility:</strong></para>
|
||||
/// <para>When creating a new class that inherits from <see cref="BasePTModuleSettings"/>,
|
||||
/// you MUST register it in <see cref="SettingsSerializationContext"/> by adding a
|
||||
/// <c>[JsonSerializable(typeof(YourNewSettingsClass))]</c> attribute.</para>
|
||||
/// <para>Failure to register the type will cause <see cref="ToJsonString"/> to throw
|
||||
/// <see cref="InvalidOperationException"/> at runtime.</para>
|
||||
/// <para>See <see cref="SettingsSerializationContext"/> for registration instructions.</para>
|
||||
/// </remarks>
|
||||
public abstract class BasePTModuleSettings
|
||||
{
|
||||
// Cached JsonSerializerOptions for Native AOT compatibility
|
||||
private static readonly JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions
|
||||
{
|
||||
TypeInfoResolver = SettingsSerializationContext.Default,
|
||||
};
|
||||
|
||||
// Gets or sets name of the powertoy module.
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; }
|
||||
@@ -17,11 +36,33 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
[JsonPropertyName("version")]
|
||||
public string Version { get; set; }
|
||||
|
||||
// converts the current to a json string.
|
||||
/// <summary>
|
||||
/// Converts the current settings object to a JSON string.
|
||||
/// </summary>
|
||||
/// <returns>JSON string representation of this settings object.</returns>
|
||||
/// <exception cref="InvalidOperationException">
|
||||
/// Thrown when the runtime type is not registered in <see cref="SettingsSerializationContext"/>.
|
||||
/// All derived types must be registered with <c>[JsonSerializable(typeof(YourType))]</c> attribute.
|
||||
/// </exception>
|
||||
/// <remarks>
|
||||
/// This method uses Native AOT-compatible JSON serialization. The runtime type must be
|
||||
/// registered in <see cref="SettingsSerializationContext"/> for serialization to work.
|
||||
/// </remarks>
|
||||
public virtual string ToJsonString()
|
||||
{
|
||||
// By default JsonSerializer will only serialize the properties in the base class. This can be avoided by passing the object type (more details at https://stackoverflow.com/a/62498888)
|
||||
return JsonSerializer.Serialize(this, GetType());
|
||||
var runtimeType = GetType();
|
||||
|
||||
// For Native AOT compatibility, get JsonTypeInfo from the TypeInfoResolver
|
||||
var typeInfo = _jsonSerializerOptions.TypeInfoResolver?.GetTypeInfo(runtimeType, _jsonSerializerOptions);
|
||||
|
||||
if (typeInfo == null)
|
||||
{
|
||||
throw new InvalidOperationException($"Type {runtimeType.FullName} is not registered in SettingsSerializationContext. Please add it to the [JsonSerializable] attributes.");
|
||||
}
|
||||
|
||||
// Use AOT-friendly serialization
|
||||
return JsonSerializer.Serialize(this, typeInfo);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
return JsonSerializer.Serialize(this, SettingsSerializationContext.Default.BoolProperty);
|
||||
}
|
||||
|
||||
public bool TryToCmdRepresentable(out string result)
|
||||
|
||||
@@ -12,14 +12,14 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
{
|
||||
public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var boolProperty = JsonSerializer.Deserialize<BoolProperty>(ref reader, options);
|
||||
var boolProperty = JsonSerializer.Deserialize(ref reader, SettingsSerializationContext.Default.BoolProperty);
|
||||
return boolProperty.Value;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
|
||||
{
|
||||
var boolProperty = new BoolProperty(value);
|
||||
JsonSerializer.Serialize(writer, boolProperty, options);
|
||||
JsonSerializer.Serialize(writer, boolProperty, SettingsSerializationContext.Default.BoolProperty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
|
||||
if (doc.RootElement.TryGetProperty(nameof(Hotkey), out JsonElement hotkeyElement))
|
||||
{
|
||||
Hotkey = JsonSerializer.Deserialize<HotkeySettings>(hotkeyElement.GetRawText());
|
||||
Hotkey = JsonSerializer.Deserialize(hotkeyElement.GetRawText(), SettingsSerializationContext.Default.HotkeySettings);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
|
||||
@@ -87,6 +87,6 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
public bool ShowColorName { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
=> JsonSerializer.Serialize(this);
|
||||
=> JsonSerializer.Serialize(this, SettingsSerializationContext.Default.ColorPickerProperties);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,6 +54,6 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
public bool ShowColorName { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
=> JsonSerializer.Serialize(this);
|
||||
=> JsonSerializer.Serialize(this, SettingsSerializationContext.Default.ColorPickerPropertiesVersion1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
// Returns a JSON version of the class settings configuration class.
|
||||
public override string ToString()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
return JsonSerializer.Serialize(this, SettingsSerializationContext.Default.DoubleProperty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
|
||||
public string ToJsonString()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
return JsonSerializer.Serialize(this, SettingsSerializationContext.Default.FileLocksmithLocalProperties);
|
||||
}
|
||||
|
||||
// This function is required to implement the ISettingsConfig interface and obtain the settings configurations.
|
||||
|
||||
@@ -17,6 +17,6 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
[JsonPropertyName("bool_show_extended_menu")]
|
||||
public BoolProperty ExtendedContextMenuOnly { get; set; }
|
||||
|
||||
public override string ToString() => JsonSerializer.Serialize(this);
|
||||
public override string ToString() => JsonSerializer.Serialize(this, SettingsSerializationContext.Default.FileLocksmithProperties);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +119,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
// converts the current to a json string.
|
||||
public string ToJsonString()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
return JsonSerializer.Serialize(this, SettingsSerializationContext.Default.GeneralSettings);
|
||||
}
|
||||
|
||||
private static string DefaultPowertoysVersion()
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
return JsonSerializer.Serialize(this, SettingsSerializationContext.Default.GeneralSettingsCustomAction);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,13 +12,18 @@ namespace Microsoft.PowerToys.Settings.UI.Library.Helpers
|
||||
{
|
||||
public struct SunTimes
|
||||
{
|
||||
public int SunriseHour;
|
||||
public int SunriseMinute;
|
||||
public int SunsetHour;
|
||||
public int SunsetMinute;
|
||||
public string Text;
|
||||
public int SunriseHour { get; set; }
|
||||
|
||||
public bool HasSunrise;
|
||||
public bool HasSunset;
|
||||
public int SunriseMinute { get; set; }
|
||||
|
||||
public int SunsetHour { get; set; }
|
||||
|
||||
public int SunsetMinute { get; set; }
|
||||
|
||||
public string Text { get; set; }
|
||||
|
||||
public bool HasSunrise { get; set; }
|
||||
|
||||
public bool HasSunset { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
|
||||
public string ToJsonString()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
return JsonSerializer.Serialize(this, SettingsSerializationContext.Default.ImageResizerProperties);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,8 +37,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
|
||||
public override string ToJsonString()
|
||||
{
|
||||
var options = _serializerOptions;
|
||||
return JsonSerializer.Serialize(this, options);
|
||||
return JsonSerializer.Serialize(this, SettingsSerializationContext.Default.ImageResizerSettings);
|
||||
}
|
||||
|
||||
public string GetModuleName()
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
// Returns a JSON version of the class settings configuration class.
|
||||
public override string ToString()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
return JsonSerializer.Serialize(this, SettingsSerializationContext.Default.IntProperty);
|
||||
}
|
||||
|
||||
public static implicit operator IntProperty(int v)
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
|
||||
public string ToJsonString()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
return JsonSerializer.Serialize(this, SettingsSerializationContext.Default.KeyboardManagerProfile);
|
||||
}
|
||||
|
||||
public string GetModuleName()
|
||||
|
||||
@@ -46,6 +46,6 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
|
||||
public IntProperty DefaultMeasureStyle { get; set; }
|
||||
|
||||
public override string ToString() => JsonSerializer.Serialize(this);
|
||||
public override string ToString() => JsonSerializer.Serialize(this, SettingsSerializationContext.Default.MeasureToolProperties);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,8 +15,9 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
public struct ConnectionRequest
|
||||
#pragma warning restore SA1649 // File name should match first type name
|
||||
{
|
||||
public string PCName;
|
||||
public string SecurityKey;
|
||||
public string PCName { get; set; }
|
||||
|
||||
public string SecurityKey { get; set; }
|
||||
}
|
||||
|
||||
public struct NewKeyGenerationRequest
|
||||
|
||||
@@ -33,6 +33,6 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
[JsonPropertyName("ReplaceVariables")]
|
||||
public BoolProperty ReplaceVariables { get; set; }
|
||||
|
||||
public override string ToString() => JsonSerializer.Serialize(this);
|
||||
public override string ToString() => JsonSerializer.Serialize(this, SettingsSerializationContext.Default.NewPlusProperties);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
return JsonSerializer.Serialize(this, SettingsSerializationContext.Default.OutGoingGeneralSettings);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
return JsonSerializer.Serialize(this, SettingsSerializationContext.Default.OutGoingLanguageSettings);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
public AIServiceType ActiveServiceTypeKind => ActiveProvider?.ServiceTypeKind ?? AIServiceType.OpenAI;
|
||||
|
||||
public override string ToString()
|
||||
=> JsonSerializer.Serialize(this);
|
||||
=> JsonSerializer.Serialize(this, SettingsSerializationContext.Default.PasteAIConfiguration);
|
||||
|
||||
protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
|
||||
{
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace Settings.UI.Library
|
||||
|
||||
public string ToJsonString()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
return JsonSerializer.Serialize(this, Microsoft.PowerToys.Settings.UI.Library.SettingsSerializationContext.Default.PeekPreviewSettings);
|
||||
}
|
||||
|
||||
public string GetModuleName()
|
||||
|
||||
@@ -32,6 +32,6 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
|
||||
public BoolProperty EnableSpaceToActivate { get; set; }
|
||||
|
||||
public override string ToString() => JsonSerializer.Serialize(this);
|
||||
public override string ToString() => JsonSerializer.Serialize(this, SettingsSerializationContext.Default.PeekProperties);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,6 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
public string PreferredLanguage { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
=> JsonSerializer.Serialize(this);
|
||||
=> JsonSerializer.Serialize(this, SettingsSerializationContext.Default.PowerOcrProperties);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -340,7 +340,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
return JsonSerializer.Serialize(this, SettingsSerializationContext.Default.PowerPreviewProperties);
|
||||
}
|
||||
|
||||
private static void LogTelemetryEvent(bool value, [CallerMemberName] string propertyName = null)
|
||||
|
||||
@@ -54,7 +54,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
|
||||
public string ToJsonString()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
return JsonSerializer.Serialize(this, SettingsSerializationContext.Default.PowerRenameLocalProperties);
|
||||
}
|
||||
|
||||
// This function is required to implement the ISettingsConfig interface and obtain the settings configurations.
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
// 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 SettingsUILibrary = Settings.UI.Library;
|
||||
using SettingsUILibraryHelpers = Settings.UI.Library.Helpers;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
{
|
||||
/// <summary>
|
||||
/// JSON serialization context for Native AOT compatibility.
|
||||
/// This context provides source-generated serialization for all PowerToys settings types.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para><strong>⚠️ CRITICAL REQUIREMENT FOR ALL NEW SETTINGS CLASSES ⚠️</strong></para>
|
||||
/// <para>
|
||||
/// When adding a new PowerToys module or any class that inherits from <see cref="BasePTModuleSettings"/>,
|
||||
/// you <strong>MUST</strong> add a <c>[JsonSerializable(typeof(YourNewSettingsClass))]</c> attribute
|
||||
/// to this class. This is a MANDATORY step for Native AOT compatibility.
|
||||
/// </para>
|
||||
/// <para><strong>Steps to add a new settings class:</strong></para>
|
||||
/// <list type="number">
|
||||
/// <item><description>Create your new settings class (e.g., <c>MyNewModuleSettings</c>) that inherits from <see cref="BasePTModuleSettings"/></description></item>
|
||||
/// <item><description>Add <c>[JsonSerializable(typeof(MyNewModuleSettings))]</c> attribute to this <see cref="SettingsSerializationContext"/> class</description></item>
|
||||
/// <item><description>If you have a corresponding Properties class, also add <c>[JsonSerializable(typeof(MyNewModuleProperties))]</c></description></item>
|
||||
/// <item><description>Rebuild the project - source generator will create serialization code at compile time</description></item>
|
||||
/// </list>
|
||||
/// <para><strong>⚠️ Failure to register types will cause runtime errors:</strong></para>
|
||||
/// <para>
|
||||
/// If you forget to add the <c>[JsonSerializable]</c> attribute, calling <c>ToJsonString()</c> or
|
||||
/// deserialization methods will throw <see cref="InvalidOperationException"/> at runtime with a clear
|
||||
/// error message indicating which type is missing registration.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
[JsonSourceGenerationOptions(
|
||||
WriteIndented = false,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
|
||||
IncludeFields = true)]
|
||||
|
||||
// Main Settings Classes
|
||||
[JsonSerializable(typeof(GeneralSettings))]
|
||||
[JsonSerializable(typeof(AdvancedPasteSettings))]
|
||||
[JsonSerializable(typeof(AlwaysOnTopSettings))]
|
||||
[JsonSerializable(typeof(AwakeSettings))]
|
||||
[JsonSerializable(typeof(CmdNotFoundSettings))]
|
||||
[JsonSerializable(typeof(ColorPickerSettings))]
|
||||
[JsonSerializable(typeof(ColorPickerSettingsVersion1))]
|
||||
[JsonSerializable(typeof(CropAndLockSettings))]
|
||||
[JsonSerializable(typeof(CursorWrapSettings))]
|
||||
[JsonSerializable(typeof(EnvironmentVariablesSettings))]
|
||||
[JsonSerializable(typeof(FancyZonesSettings))]
|
||||
[JsonSerializable(typeof(FileLocksmithSettings))]
|
||||
[JsonSerializable(typeof(FindMyMouseSettings))]
|
||||
[JsonSerializable(typeof(HostsSettings))]
|
||||
[JsonSerializable(typeof(ImageResizerSettings))]
|
||||
[JsonSerializable(typeof(KeyboardManagerSettings))]
|
||||
[JsonSerializable(typeof(SettingsUILibrary.LightSwitchSettings))]
|
||||
[JsonSerializable(typeof(MeasureToolSettings))]
|
||||
[JsonSerializable(typeof(MouseHighlighterSettings))]
|
||||
[JsonSerializable(typeof(MouseJumpSettings))]
|
||||
[JsonSerializable(typeof(MousePointerCrosshairsSettings))]
|
||||
[JsonSerializable(typeof(MouseWithoutBordersSettings))]
|
||||
[JsonSerializable(typeof(NewPlusSettings))]
|
||||
[JsonSerializable(typeof(PeekSettings))]
|
||||
[JsonSerializable(typeof(PowerAccentSettings))]
|
||||
[JsonSerializable(typeof(PowerLauncherSettings))]
|
||||
[JsonSerializable(typeof(PowerOcrSettings))]
|
||||
[JsonSerializable(typeof(PowerPreviewSettings))]
|
||||
[JsonSerializable(typeof(PowerRenameSettings))]
|
||||
[JsonSerializable(typeof(RegistryPreviewSettings))]
|
||||
[JsonSerializable(typeof(ShortcutGuideSettings))]
|
||||
[JsonSerializable(typeof(WorkspacesSettings))]
|
||||
[JsonSerializable(typeof(ZoomItSettings))]
|
||||
|
||||
// Properties Classes
|
||||
[JsonSerializable(typeof(AdvancedPasteProperties))]
|
||||
[JsonSerializable(typeof(AlwaysOnTopProperties))]
|
||||
[JsonSerializable(typeof(AwakeProperties))]
|
||||
[JsonSerializable(typeof(CmdPalProperties))]
|
||||
[JsonSerializable(typeof(ColorPickerProperties))]
|
||||
[JsonSerializable(typeof(ColorPickerPropertiesVersion1))]
|
||||
[JsonSerializable(typeof(CropAndLockProperties))]
|
||||
[JsonSerializable(typeof(CursorWrapProperties))]
|
||||
[JsonSerializable(typeof(EnvironmentVariablesProperties))]
|
||||
[JsonSerializable(typeof(FileLocksmithProperties))]
|
||||
[JsonSerializable(typeof(FileLocksmithLocalProperties))]
|
||||
[JsonSerializable(typeof(FindMyMouseProperties))]
|
||||
[JsonSerializable(typeof(FZConfigProperties))]
|
||||
[JsonSerializable(typeof(HostsProperties))]
|
||||
[JsonSerializable(typeof(ImageResizerProperties))]
|
||||
[JsonSerializable(typeof(KeyboardManagerProperties))]
|
||||
[JsonSerializable(typeof(KeyboardManagerProfile))]
|
||||
[JsonSerializable(typeof(LightSwitchProperties))]
|
||||
[JsonSerializable(typeof(MeasureToolProperties))]
|
||||
[JsonSerializable(typeof(MouseHighlighterProperties))]
|
||||
[JsonSerializable(typeof(MouseJumpProperties))]
|
||||
[JsonSerializable(typeof(MousePointerCrosshairsProperties))]
|
||||
[JsonSerializable(typeof(MouseWithoutBordersProperties))]
|
||||
[JsonSerializable(typeof(NewPlusProperties))]
|
||||
[JsonSerializable(typeof(PeekProperties))]
|
||||
[JsonSerializable(typeof(SettingsUILibrary.PeekPreviewSettings))]
|
||||
[JsonSerializable(typeof(PowerAccentProperties))]
|
||||
[JsonSerializable(typeof(PowerLauncherProperties))]
|
||||
[JsonSerializable(typeof(PowerOcrProperties))]
|
||||
[JsonSerializable(typeof(PowerPreviewProperties))]
|
||||
[JsonSerializable(typeof(PowerRenameProperties))]
|
||||
[JsonSerializable(typeof(PowerRenameLocalProperties))]
|
||||
[JsonSerializable(typeof(RegistryPreviewProperties))]
|
||||
[JsonSerializable(typeof(ShortcutConflictProperties))]
|
||||
[JsonSerializable(typeof(ShortcutGuideProperties))]
|
||||
[JsonSerializable(typeof(WorkspacesProperties))]
|
||||
[JsonSerializable(typeof(ZoomItProperties))]
|
||||
|
||||
// Base Property Types (used throughout settings)
|
||||
[JsonSerializable(typeof(BoolProperty))]
|
||||
[JsonSerializable(typeof(StringProperty))]
|
||||
[JsonSerializable(typeof(IntProperty))]
|
||||
[JsonSerializable(typeof(DoubleProperty))]
|
||||
|
||||
// Helper and Utility Types
|
||||
[JsonSerializable(typeof(HotkeySettings))]
|
||||
[JsonSerializable(typeof(ColorFormatModel))]
|
||||
[JsonSerializable(typeof(ImageSize))]
|
||||
[JsonSerializable(typeof(KeysDataModel))]
|
||||
[JsonSerializable(typeof(EnabledModules))]
|
||||
[JsonSerializable(typeof(GeneralSettingsCustomAction))]
|
||||
[JsonSerializable(typeof(OutGoingGeneralSettings))]
|
||||
[JsonSerializable(typeof(OutGoingLanguageSettings))]
|
||||
[JsonSerializable(typeof(AdvancedPasteCustomActions))]
|
||||
[JsonSerializable(typeof(AdvancedPasteAdditionalActions))]
|
||||
[JsonSerializable(typeof(AdvancedPasteCustomAction))]
|
||||
[JsonSerializable(typeof(AdvancedPasteAdditionalAction))]
|
||||
[JsonSerializable(typeof(AdvancedPastePasteAsFileAction))]
|
||||
[JsonSerializable(typeof(AdvancedPasteTranscodeAction))]
|
||||
[JsonSerializable(typeof(PasteAIConfiguration))]
|
||||
[JsonSerializable(typeof(PasteAIProviderDefinition))]
|
||||
[JsonSerializable(typeof(ImageResizerSizes))]
|
||||
[JsonSerializable(typeof(ImageResizerCustomSizeProperty))]
|
||||
[JsonSerializable(typeof(KeyboardKeysProperty))]
|
||||
[JsonSerializable(typeof(SettingsUILibraryHelpers.SearchLocation))]
|
||||
|
||||
// IPC Send Message Wrapper Classes (Snd*)
|
||||
[JsonSerializable(typeof(SndAwakeSettings))]
|
||||
[JsonSerializable(typeof(SndCursorWrapSettings))]
|
||||
[JsonSerializable(typeof(SndFindMyMouseSettings))]
|
||||
[JsonSerializable(typeof(SndLightSwitchSettings))]
|
||||
[JsonSerializable(typeof(SndMouseHighlighterSettings))]
|
||||
[JsonSerializable(typeof(SndMouseJumpSettings))]
|
||||
[JsonSerializable(typeof(SndMousePointerCrosshairsSettings))]
|
||||
[JsonSerializable(typeof(SndPowerAccentSettings))]
|
||||
[JsonSerializable(typeof(SndPowerPreviewSettings))]
|
||||
[JsonSerializable(typeof(SndPowerRenameSettings))]
|
||||
[JsonSerializable(typeof(SndShortcutGuideSettings))]
|
||||
|
||||
// IPC Message Generic Wrapper Types (SndModuleSettings<T>)
|
||||
[JsonSerializable(typeof(SndModuleSettings<SndAwakeSettings>))]
|
||||
[JsonSerializable(typeof(SndModuleSettings<SndCursorWrapSettings>))]
|
||||
[JsonSerializable(typeof(SndModuleSettings<SndFindMyMouseSettings>))]
|
||||
[JsonSerializable(typeof(SndModuleSettings<SndLightSwitchSettings>))]
|
||||
[JsonSerializable(typeof(SndModuleSettings<SndMouseHighlighterSettings>))]
|
||||
[JsonSerializable(typeof(SndModuleSettings<SndMouseJumpSettings>))]
|
||||
[JsonSerializable(typeof(SndModuleSettings<SndMousePointerCrosshairsSettings>))]
|
||||
[JsonSerializable(typeof(SndModuleSettings<SndPowerAccentSettings>))]
|
||||
[JsonSerializable(typeof(SndModuleSettings<SndPowerPreviewSettings>))]
|
||||
[JsonSerializable(typeof(SndModuleSettings<SndPowerRenameSettings>))]
|
||||
[JsonSerializable(typeof(SndModuleSettings<SndShortcutGuideSettings>))]
|
||||
|
||||
public partial class SettingsSerializationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.IO.Abstractions;
|
||||
@@ -18,27 +20,28 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
private const string DefaultModuleName = "";
|
||||
private readonly IFile _file;
|
||||
private readonly ISettingsPath _settingsPath;
|
||||
|
||||
private static readonly JsonSerializerOptions _serializerOptions = new JsonSerializerOptions
|
||||
{
|
||||
MaxDepth = 0,
|
||||
IncludeFields = true,
|
||||
};
|
||||
private readonly JsonSerializerOptions _serializerOptions;
|
||||
|
||||
public SettingsUtils()
|
||||
: this(new FileSystem())
|
||||
{
|
||||
}
|
||||
|
||||
public SettingsUtils(IFileSystem fileSystem)
|
||||
: this(fileSystem?.File, new SettingPath(fileSystem?.Directory, fileSystem?.Path))
|
||||
public SettingsUtils(IFileSystem? fileSystem, JsonSerializerOptions? serializerOptions = null)
|
||||
: this(fileSystem?.File!, new SettingPath(fileSystem?.Directory, fileSystem?.Path), serializerOptions)
|
||||
{
|
||||
}
|
||||
|
||||
public SettingsUtils(IFile file, ISettingsPath settingPath)
|
||||
public SettingsUtils(IFile file, ISettingsPath settingPath, JsonSerializerOptions? serializerOptions = null)
|
||||
{
|
||||
_file = file ?? throw new ArgumentNullException(nameof(file));
|
||||
_settingsPath = settingPath;
|
||||
_serializerOptions = serializerOptions ?? new JsonSerializerOptions
|
||||
{
|
||||
MaxDepth = 0,
|
||||
IncludeFields = true,
|
||||
TypeInfoResolver = SettingsSerializationContext.Default,
|
||||
};
|
||||
}
|
||||
|
||||
public bool SettingsExists(string powertoy = DefaultModuleName, string fileName = DefaultFileName)
|
||||
@@ -108,7 +111,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
/// This function creates a file in the powertoy folder if it does not exist and returns an object with default properties.
|
||||
/// </summary>
|
||||
/// <returns>Deserialized json settings object.</returns>
|
||||
public T GetSettingsOrDefault<T, T2>(string powertoy = DefaultModuleName, string fileName = DefaultFileName, Func<object, object> settingsUpgrader = null)
|
||||
public T GetSettingsOrDefault<T, T2>(string powertoy = DefaultModuleName, string fileName = DefaultFileName, Func<object, object>? settingsUpgrader = null)
|
||||
where T : ISettingsConfig, new()
|
||||
where T2 : ISettingsConfig, new()
|
||||
{
|
||||
@@ -128,7 +131,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
try
|
||||
{
|
||||
T2 oldSettings = GetSettings<T2>(powertoy, fileName);
|
||||
T newSettings = (T)settingsUpgrader(oldSettings);
|
||||
T newSettings = (T)settingsUpgrader!(oldSettings);
|
||||
Logger.LogInfo($"Settings file {fileName} for {powertoy} was read successfully in the old format.");
|
||||
|
||||
// If the file needs to be modified, to save the new configurations accordingly.
|
||||
@@ -156,7 +159,22 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
return newSettingsItem;
|
||||
}
|
||||
|
||||
// Given the powerToy folder name and filename to be accessed, this function deserializes and returns the file.
|
||||
/// <summary>
|
||||
/// Deserializes settings from a JSON file.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The settings type to deserialize. Must be registered in <see cref="SettingsSerializationContext"/>.</typeparam>
|
||||
/// <param name="powertoyFolderName">The PowerToy module folder name.</param>
|
||||
/// <param name="fileName">The settings file name.</param>
|
||||
/// <returns>Deserialized settings object of type T.</returns>
|
||||
/// <exception cref="InvalidOperationException">
|
||||
/// Thrown when type T is not registered in <see cref="SettingsSerializationContext"/>.
|
||||
/// All settings types must be registered with <c>[JsonSerializable(typeof(T))]</c> attribute
|
||||
/// for Native AOT compatibility.
|
||||
/// </exception>
|
||||
/// <remarks>
|
||||
/// This method uses Native AOT-compatible JSON deserialization. Type T must be registered
|
||||
/// in <see cref="SettingsSerializationContext"/> before calling this method.
|
||||
/// </remarks>
|
||||
private T GetFile<T>(string powertoyFolderName = DefaultModuleName, string fileName = DefaultFileName)
|
||||
{
|
||||
// Adding Trim('\0') to overcome possible NTFS file corruption.
|
||||
@@ -165,8 +183,16 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
// The file itself did write the content correctly but something is off with the actual end of the file, hence the 0x00 bug
|
||||
var jsonSettingsString = _file.ReadAllText(_settingsPath.GetSettingsPath(powertoyFolderName, fileName)).Trim('\0');
|
||||
|
||||
var options = _serializerOptions;
|
||||
return JsonSerializer.Deserialize<T>(jsonSettingsString, options);
|
||||
// For Native AOT compatibility, get JsonTypeInfo from the TypeInfoResolver
|
||||
var typeInfo = _serializerOptions.TypeInfoResolver?.GetTypeInfo(typeof(T), _serializerOptions);
|
||||
|
||||
if (typeInfo == null)
|
||||
{
|
||||
throw new InvalidOperationException($"Type {typeof(T).FullName} is not registered in SettingsSerializationContext. Please add it to the [JsonSerializable] attributes.");
|
||||
}
|
||||
|
||||
// Use AOT-friendly deserialization
|
||||
return (T)JsonSerializer.Deserialize(jsonSettingsString, typeInfo)!;
|
||||
}
|
||||
|
||||
// Save settings to a json file.
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
// Returns a JSON version of the class settings configuration class.
|
||||
public override string ToString()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
return JsonSerializer.Serialize(this, SettingsSerializationContext.Default.StringProperty);
|
||||
}
|
||||
|
||||
public static StringProperty ToStringProperty(string v)
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
// 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;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace CommonLibTest
|
||||
{
|
||||
[TestClass]
|
||||
public class BasePTModuleSettingsSerializationTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Test to verify that all classes derived from BasePTModuleSettings are registered
|
||||
/// in the SettingsSerializationContext for Native AOT compatibility.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void AllBasePTModuleSettingsClasses_ShouldBeRegisteredInSerializationContext()
|
||||
{
|
||||
// Arrange
|
||||
var assembly = typeof(BasePTModuleSettings).Assembly;
|
||||
var settingsClasses = assembly.GetTypes()
|
||||
.Where(t => typeof(BasePTModuleSettings).IsAssignableFrom(t) && !t.IsAbstract && t != typeof(BasePTModuleSettings))
|
||||
.OrderBy(t => t.Name)
|
||||
.ToList();
|
||||
|
||||
Assert.IsTrue(settingsClasses.Count > 0, "No BasePTModuleSettings derived classes found. This test may be broken.");
|
||||
|
||||
var jsonSerializerOptions = new JsonSerializerOptions
|
||||
{
|
||||
TypeInfoResolver = SettingsSerializationContext.Default,
|
||||
};
|
||||
|
||||
var unregisteredTypes = new System.Collections.Generic.List<string>();
|
||||
|
||||
// Act & Assert
|
||||
foreach (var settingsType in settingsClasses)
|
||||
{
|
||||
var typeInfo = jsonSerializerOptions.TypeInfoResolver?.GetTypeInfo(settingsType, jsonSerializerOptions);
|
||||
|
||||
if (typeInfo == null)
|
||||
{
|
||||
unregisteredTypes.Add(settingsType.FullName ?? settingsType.Name);
|
||||
}
|
||||
}
|
||||
|
||||
// Assert
|
||||
if (unregisteredTypes.Count > 0)
|
||||
{
|
||||
var errorMessage = $"The following {unregisteredTypes.Count} settings class(es) are NOT registered in SettingsSerializationContext:\n" +
|
||||
$"{string.Join("\n", unregisteredTypes.Select(t => $" - {t}"))}\n\n" +
|
||||
$"Please add [JsonSerializable(typeof(ClassName))] attribute to SettingsSerializationContext.cs for each missing type.";
|
||||
Assert.Fail(errorMessage);
|
||||
}
|
||||
|
||||
// Print success message with count
|
||||
Console.WriteLine($"✓ All {settingsClasses.Count} BasePTModuleSettings derived classes are properly registered in SettingsSerializationContext.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test to verify that calling ToJsonString() on an unregistered type throws InvalidOperationException
|
||||
/// with a helpful error message.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(InvalidOperationException))]
|
||||
public void ToJsonString_UnregisteredType_ShouldThrowInvalidOperationException()
|
||||
{
|
||||
// Arrange
|
||||
var unregisteredSettings = new UnregisteredTestSettings
|
||||
{
|
||||
Name = "UnregisteredModule",
|
||||
Version = "1.0.0",
|
||||
};
|
||||
|
||||
// Act - This should throw InvalidOperationException
|
||||
var jsonString = unregisteredSettings.ToJsonString();
|
||||
|
||||
// Assert - Exception should be thrown, so this line should never be reached
|
||||
Assert.Fail("Expected InvalidOperationException was not thrown.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test to verify that the error message for unregistered types is helpful and contains
|
||||
/// necessary information for developers.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void ToJsonString_UnregisteredType_ShouldHaveHelpfulErrorMessage()
|
||||
{
|
||||
// Arrange
|
||||
var unregisteredSettings = new UnregisteredTestSettings
|
||||
{
|
||||
Name = "UnregisteredModule",
|
||||
Version = "1.0.0",
|
||||
};
|
||||
|
||||
// Act & Assert
|
||||
try
|
||||
{
|
||||
var jsonString = unregisteredSettings.ToJsonString();
|
||||
Assert.Fail("Expected InvalidOperationException was not thrown.");
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
// Verify the error message contains helpful information
|
||||
Assert.IsTrue(ex.Message.Contains("UnregisteredTestSettings"), "Error message should contain the type name.");
|
||||
Assert.IsTrue(ex.Message.Contains("SettingsSerializationContext"), "Error message should mention SettingsSerializationContext.");
|
||||
Assert.IsTrue(ex.Message.Contains("JsonSerializable"), "Error message should mention JsonSerializable attribute.");
|
||||
|
||||
Console.WriteLine($"✓ Error message is helpful: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test class that is intentionally NOT registered in SettingsSerializationContext
|
||||
/// to verify error handling for unregistered types.
|
||||
/// </summary>
|
||||
private sealed class UnregisteredTestSettings : BasePTModuleSettings
|
||||
{
|
||||
// Intentionally empty - this class should NOT be registered in SettingsSerializationContext
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,10 @@
|
||||
// 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;
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UnitTest
|
||||
{
|
||||
@@ -24,5 +26,11 @@ namespace Microsoft.PowerToys.Settings.UnitTest
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Override ToJsonString to use test-specific serialization context
|
||||
public override string ToJsonString()
|
||||
{
|
||||
return JsonSerializer.Serialize(this, TestSettingsSerializationContext.Default.BasePTSettingsTest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ using System.Text.Json;
|
||||
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests;
|
||||
using Microsoft.PowerToys.Settings.UnitTest;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
@@ -22,7 +23,13 @@ namespace CommonLibTest
|
||||
{
|
||||
// Arrange
|
||||
var mockFileSystem = new MockFileSystem();
|
||||
var settingsUtils = new SettingsUtils(mockFileSystem);
|
||||
var testSerializerOptions = new JsonSerializerOptions
|
||||
{
|
||||
MaxDepth = 0,
|
||||
IncludeFields = true,
|
||||
TypeInfoResolver = TestSettingsSerializationContext.Default,
|
||||
};
|
||||
var settingsUtils = new SettingsUtils(mockFileSystem, testSerializerOptions);
|
||||
|
||||
string file_name = "\\test";
|
||||
string file_contents_correct_json_content = "{\"name\":\"powertoy module name\",\"version\":\"powertoy version\"}";
|
||||
@@ -42,7 +49,13 @@ namespace CommonLibTest
|
||||
{
|
||||
// Arrange
|
||||
var mockFileSystem = new MockFileSystem();
|
||||
var settingsUtils = new SettingsUtils(mockFileSystem);
|
||||
var testSerializerOptions = new JsonSerializerOptions
|
||||
{
|
||||
MaxDepth = 0,
|
||||
IncludeFields = true,
|
||||
TypeInfoResolver = TestSettingsSerializationContext.Default,
|
||||
};
|
||||
var settingsUtils = new SettingsUtils(mockFileSystem, testSerializerOptions);
|
||||
string file_name = "test\\Test Folder";
|
||||
string file_contents_correct_json_content = "{\"name\":\"powertoy module name\",\"version\":\"powertoy version\"}";
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// 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 Microsoft.PowerToys.Settings.UnitTest;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.UnitTests
|
||||
{
|
||||
/// <summary>
|
||||
/// JSON serialization context for unit tests.
|
||||
/// This context provides source-generated serialization for test-specific types.
|
||||
/// </summary>
|
||||
[JsonSourceGenerationOptions(
|
||||
WriteIndented = false,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
|
||||
IncludeFields = true)]
|
||||
[JsonSerializable(typeof(BasePTSettingsTest))]
|
||||
public partial class TestSettingsSerializationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ using System;
|
||||
using System.Text.Json;
|
||||
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.BackwardsCompatibility;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
||||
using Microsoft.PowerToys.Settings.UI.ViewModels;
|
||||
@@ -100,6 +101,22 @@ namespace ViewModelTests
|
||||
mockFancyZonesSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<FancyZonesSettings>();
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void CleanUp()
|
||||
{
|
||||
// Reset singleton instances to prevent state pollution between tests
|
||||
ResetSettingsRepository<GeneralSettings>();
|
||||
ResetSettingsRepository<FancyZonesSettings>();
|
||||
}
|
||||
|
||||
private void ResetSettingsRepository<T>()
|
||||
where T : class, ISettingsConfig, new()
|
||||
{
|
||||
var repositoryType = typeof(SettingsRepository<T>);
|
||||
var field = repositoryType.GetField("settingsRepository", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
|
||||
field?.SetValue(null, null);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsEnabledShouldDisableModuleWhenSuccessful()
|
||||
{
|
||||
|
||||
@@ -10,7 +10,7 @@ using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.PowerToys.Settings.UI.Helpers;
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Settings.UI.Library;
|
||||
using SettingsUILibrary = Settings.UI.Library;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.SerializationContext;
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace Microsoft.PowerToys.Settings.UI.SerializationContext;
|
||||
[JsonSerializable(typeof(FileLocksmithSettings))]
|
||||
[JsonSerializable(typeof(FindMyMouseSettings))]
|
||||
[JsonSerializable(typeof(IList<PowerToysReleaseInfo>))]
|
||||
[JsonSerializable(typeof(LightSwitchSettings))]
|
||||
[JsonSerializable(typeof(SettingsUILibrary.LightSwitchSettings))]
|
||||
[JsonSerializable(typeof(MeasureToolSettings))]
|
||||
[JsonSerializable(typeof(MouseHighlighterSettings))]
|
||||
[JsonSerializable(typeof(MouseJumpSettings))]
|
||||
|
||||
Reference in New Issue
Block a user