Refactor LightSwitchService to use PowerToys settings API

Replaced manual JSON parsing with strongly-typed settings access via SettingsUtils and LightSwitchSettings. Updated logic to use EnableLightModeProfile and EnableDarkModeProfile flags. Changed namespace to PowerDisplay.Services and updated using directives accordingly. Removed obsolete helper methods and improved code clarity.
This commit is contained in:
Yu Leng
2025-12-11 10:27:17 +08:00
parent 87eb7cc07f
commit a4770a84cf
3 changed files with 78 additions and 112 deletions

View File

@@ -1,112 +0,0 @@
// 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.IO;
using System.Text.Json;
using ManagedCommon;
namespace PowerDisplay.Common.Services
{
/// <summary>
/// Service for handling LightSwitch theme change events.
/// Provides methods to process theme changes and read LightSwitch settings.
/// Event listening is handled externally via NativeEventWaiter.
/// </summary>
public static class LightSwitchService
{
private const string LogPrefix = "[LightSwitch]";
/// <summary>
/// Process a theme change event and return the profile name to apply.
/// </summary>
/// <param name="isLightMode">Whether the theme changed to light mode.</param>
/// <returns>The profile name to apply, or null if no profile is configured.</returns>
public static string? GetProfileForTheme(bool isLightMode)
{
try
{
Logger.LogInfo($"{LogPrefix} Processing theme change to {(isLightMode ? "light" : "dark")} mode");
var profileToApply = ReadProfileFromLightSwitchSettings(isLightMode);
if (string.IsNullOrEmpty(profileToApply) || profileToApply == "(None)")
{
Logger.LogInfo($"{LogPrefix} No profile configured for {(isLightMode ? "light" : "dark")} mode");
return null;
}
Logger.LogInfo($"{LogPrefix} Profile to apply: {profileToApply}");
return profileToApply;
}
catch (Exception ex)
{
Logger.LogError($"{LogPrefix} Failed to process theme change: {ex.Message}");
return null;
}
}
/// <summary>
/// Reads LightSwitch settings and returns the profile name to apply for the given theme.
/// </summary>
/// <param name="isLightMode">Whether the theme is light mode.</param>
/// <returns>The profile name to apply, or null if not configured.</returns>
private static string? ReadProfileFromLightSwitchSettings(bool isLightMode)
{
var settingsPath = PathConstants.LightSwitchSettingsFilePath;
if (!File.Exists(settingsPath))
{
Logger.LogWarning($"{LogPrefix} LightSwitch settings file not found");
return null;
}
var json = File.ReadAllText(settingsPath);
var settings = JsonDocument.Parse(json);
var root = settings.RootElement;
if (!root.TryGetProperty("properties", out var properties))
{
Logger.LogWarning($"{LogPrefix} LightSwitch settings has no properties");
return null;
}
// Check if monitor settings integration is enabled
if (!properties.TryGetProperty("apply_monitor_settings", out var applyMonitorSettingsElement) ||
!applyMonitorSettingsElement.TryGetProperty("value", out var applyValue) ||
!applyValue.GetBoolean())
{
Logger.LogInfo($"{LogPrefix} Monitor settings integration is disabled");
return null;
}
// Get the appropriate profile name based on the theme
if (isLightMode)
{
return GetProfileFromSettings(properties, "enable_light_mode_profile", "light_mode_profile");
}
else
{
return GetProfileFromSettings(properties, "enable_dark_mode_profile", "dark_mode_profile");
}
}
private static string? GetProfileFromSettings(
JsonElement properties,
string enableKey,
string profileKey)
{
if (properties.TryGetProperty(enableKey, out var enableElement) &&
enableElement.TryGetProperty("value", out var enableValue) &&
enableValue.GetBoolean() &&
properties.TryGetProperty(profileKey, out var profileElement) &&
profileElement.TryGetProperty("value", out var profileValue))
{
return profileValue.GetString();
}
return null;
}
}
}

View File

@@ -0,0 +1,77 @@
// 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 ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Library;
using Settings.UI.Library;
namespace PowerDisplay.Services
{
/// <summary>
/// Service for handling LightSwitch theme change events.
/// Reads LightSwitch settings using the standard PowerToys settings pattern.
/// </summary>
public static class LightSwitchService
{
private const string LogPrefix = "[LightSwitch]";
/// <summary>
/// Get the profile name to apply for the given theme.
/// </summary>
/// <param name="isLightMode">Whether the theme changed to light mode.</param>
/// <returns>The profile name to apply, or null if no profile is configured.</returns>
public static string? GetProfileForTheme(bool isLightMode)
{
try
{
Logger.LogInfo($"{LogPrefix} Processing theme change to {(isLightMode ? "light" : "dark")} mode");
var settings = SettingsUtils.Default.GetSettingsOrDefault<LightSwitchSettings>(LightSwitchSettings.ModuleName);
if (settings?.Properties == null)
{
Logger.LogWarning($"{LogPrefix} LightSwitch settings not found");
return null;
}
string? profileName;
if (isLightMode)
{
if (!settings.Properties.EnableLightModeProfile.Value)
{
Logger.LogInfo($"{LogPrefix} Light mode profile is disabled");
return null;
}
profileName = settings.Properties.LightModeProfile.Value;
}
else
{
if (!settings.Properties.EnableDarkModeProfile.Value)
{
Logger.LogInfo($"{LogPrefix} Dark mode profile is disabled");
return null;
}
profileName = settings.Properties.DarkModeProfile.Value;
}
if (string.IsNullOrEmpty(profileName) || profileName == "(None)")
{
Logger.LogInfo($"{LogPrefix} No profile configured for {(isLightMode ? "light" : "dark")} mode");
return null;
}
Logger.LogInfo($"{LogPrefix} Profile to apply: {profileName}");
return profileName;
}
catch (Exception ex)
{
Logger.LogError($"{LogPrefix} Failed to get profile for theme: {ex.Message}");
return null;
}
}
}
}

View File

@@ -12,6 +12,7 @@ using PowerDisplay.Common.Models;
using PowerDisplay.Common.Services; using PowerDisplay.Common.Services;
using PowerDisplay.Common.Utils; using PowerDisplay.Common.Utils;
using PowerDisplay.Serialization; using PowerDisplay.Serialization;
using PowerDisplay.Services;
using PowerToys.Interop; using PowerToys.Interop;
namespace PowerDisplay.ViewModels; namespace PowerDisplay.ViewModels;