mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
Hopefully fix the settings set
This commit is contained in:
@@ -9,6 +9,8 @@ using System.Diagnostics.CodeAnalysis;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using Microsoft.PowerToys.Settings.UI.Library;
|
||||||
|
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
|
||||||
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
|
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
|
||||||
|
|
||||||
namespace Microsoft.PowerToys.UITest
|
namespace Microsoft.PowerToys.UITest
|
||||||
@@ -34,53 +36,60 @@ namespace Microsoft.PowerToys.UITest
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string localAppData = Helper.LocalApplicationDataFolder();
|
var settingsUtils = new SettingsUtils();
|
||||||
string globalSettingsPath = Path.Combine(localAppData, "Microsoft", "PowerToys", "settings.json");
|
|
||||||
|
|
||||||
if (!File.Exists(globalSettingsPath))
|
// Get settings or create default if they don't exist
|
||||||
|
GeneralSettings settings;
|
||||||
|
try
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException($"Global settings file not found at {globalSettingsPath}");
|
settings = settingsUtils.GetSettingsOrDefault<GeneralSettings>();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Debug.WriteLine($"Failed to load settings, creating defaults: {ex.Message}");
|
||||||
|
settings = new GeneralSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
string globalSettingsJson = File.ReadAllText(globalSettingsPath);
|
// Convert settings to JSON string
|
||||||
using var doc = JsonDocument.Parse(globalSettingsJson);
|
string settingsJson = settings.ToJsonString();
|
||||||
var root = doc.RootElement;
|
|
||||||
|
|
||||||
// Create a dictionary to hold the modified settings
|
// Deserialize to JsonDocument to manipulate the Enabled modules
|
||||||
var modifiedSettings = new Dictionary<string, object>();
|
using (JsonDocument doc = JsonDocument.Parse(settingsJson))
|
||||||
|
{
|
||||||
|
var options = new JsonSerializerOptions { WriteIndented = true };
|
||||||
|
var root = doc.RootElement.Clone();
|
||||||
|
|
||||||
// Copy all existing properties
|
// Get the enabled modules object
|
||||||
foreach (var property in root.EnumerateObject())
|
if (root.TryGetProperty("enabled", out var enabledElement))
|
||||||
{
|
{
|
||||||
if (property.Name == "enabled")
|
// Create a dictionary of module properties with their enable states
|
||||||
{
|
|
||||||
// Modify the enabled property to enable only specified modules
|
|
||||||
var enabledModules = new Dictionary<string, bool>();
|
var enabledModules = new Dictionary<string, bool>();
|
||||||
foreach (var module in property.Value.EnumerateObject())
|
|
||||||
|
// Iterate through all properties in the enabled object
|
||||||
|
foreach (var property in enabledElement.EnumerateObject())
|
||||||
{
|
{
|
||||||
// Set module to true if in modulesToEnable array, otherwise false
|
string moduleName = property.Name;
|
||||||
enabledModules[module.Name] = modulesToEnable.Contains(module.Name, StringComparer.OrdinalIgnoreCase);
|
|
||||||
|
// Check if this module should be enabled
|
||||||
|
bool shouldEnable = Array.Exists(modulesToEnable, m => string.Equals(m, moduleName, StringComparison.Ordinal));
|
||||||
|
enabledModules[moduleName] = shouldEnable;
|
||||||
}
|
}
|
||||||
|
|
||||||
modifiedSettings[property.Name] = enabledModules;
|
// Rebuild the settings with updated enabled modules
|
||||||
}
|
var settingsDict = JsonSerializer.Deserialize<Dictionary<string, object>>(settingsJson);
|
||||||
else
|
if (settingsDict != null)
|
||||||
{
|
{
|
||||||
// Copy other properties as-is
|
settingsDict["enabled"] = enabledModules;
|
||||||
object? deserializedValue = JsonSerializer.Deserialize<object>(property.Value.GetRawText());
|
settingsJson = JsonSerializer.Serialize(settingsDict, IndentedJsonOptions);
|
||||||
if (deserializedValue != null)
|
|
||||||
{
|
|
||||||
modifiedSettings[property.Name] = deserializedValue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serialize and save the modified settings
|
// Save the modified settings
|
||||||
string modifiedJson = JsonSerializer.Serialize(modifiedSettings, IndentedJsonOptions);
|
settingsUtils.SaveSettings(settingsJson);
|
||||||
File.WriteAllText(globalSettingsPath, modifiedJson);
|
|
||||||
|
|
||||||
string enabledList = modulesToEnable.Length > 0 ? string.Join(", ", modulesToEnable) : "none";
|
string enabledList = modulesToEnable.Length > 0 ? string.Join(", ", modulesToEnable) : "none";
|
||||||
Debug.WriteLine($"Successfully updated global settings at {globalSettingsPath}");
|
Debug.WriteLine($"Successfully updated global settings");
|
||||||
Debug.WriteLine($"Enabled modules: {enabledList}");
|
Debug.WriteLine($"Enabled modules: {enabledList}");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
Reference in New Issue
Block a user