From a4770a84cff11f0b378243944aee6c5a12d4736e Mon Sep 17 00:00:00 2001 From: Yu Leng Date: Thu, 11 Dec 2025 10:27:17 +0800 Subject: [PATCH] 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. --- .../Services/LightSwitchService.cs | 112 ------------------ .../Services/LightSwitchService.cs | 77 ++++++++++++ .../ViewModels/MainViewModel.Settings.cs | 1 + 3 files changed, 78 insertions(+), 112 deletions(-) delete mode 100644 src/modules/powerdisplay/PowerDisplay.Lib/Services/LightSwitchService.cs create mode 100644 src/modules/powerdisplay/PowerDisplay/Services/LightSwitchService.cs diff --git a/src/modules/powerdisplay/PowerDisplay.Lib/Services/LightSwitchService.cs b/src/modules/powerdisplay/PowerDisplay.Lib/Services/LightSwitchService.cs deleted file mode 100644 index 92f81be6e8..0000000000 --- a/src/modules/powerdisplay/PowerDisplay.Lib/Services/LightSwitchService.cs +++ /dev/null @@ -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 -{ - /// - /// Service for handling LightSwitch theme change events. - /// Provides methods to process theme changes and read LightSwitch settings. - /// Event listening is handled externally via NativeEventWaiter. - /// - public static class LightSwitchService - { - private const string LogPrefix = "[LightSwitch]"; - - /// - /// Process a theme change event and return the profile name to apply. - /// - /// Whether the theme changed to light mode. - /// The profile name to apply, or null if no profile is configured. - 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; - } - } - - /// - /// Reads LightSwitch settings and returns the profile name to apply for the given theme. - /// - /// Whether the theme is light mode. - /// The profile name to apply, or null if not configured. - 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; - } - } -} diff --git a/src/modules/powerdisplay/PowerDisplay/Services/LightSwitchService.cs b/src/modules/powerdisplay/PowerDisplay/Services/LightSwitchService.cs new file mode 100644 index 0000000000..7182fd32ed --- /dev/null +++ b/src/modules/powerdisplay/PowerDisplay/Services/LightSwitchService.cs @@ -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 +{ + /// + /// Service for handling LightSwitch theme change events. + /// Reads LightSwitch settings using the standard PowerToys settings pattern. + /// + public static class LightSwitchService + { + private const string LogPrefix = "[LightSwitch]"; + + /// + /// Get the profile name to apply for the given theme. + /// + /// Whether the theme changed to light mode. + /// The profile name to apply, or null if no profile is configured. + public static string? GetProfileForTheme(bool isLightMode) + { + try + { + Logger.LogInfo($"{LogPrefix} Processing theme change to {(isLightMode ? "light" : "dark")} mode"); + + var settings = SettingsUtils.Default.GetSettingsOrDefault(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; + } + } + } +} diff --git a/src/modules/powerdisplay/PowerDisplay/ViewModels/MainViewModel.Settings.cs b/src/modules/powerdisplay/PowerDisplay/ViewModels/MainViewModel.Settings.cs index 4dc94a38c3..31f1906c9a 100644 --- a/src/modules/powerdisplay/PowerDisplay/ViewModels/MainViewModel.Settings.cs +++ b/src/modules/powerdisplay/PowerDisplay/ViewModels/MainViewModel.Settings.cs @@ -12,6 +12,7 @@ using PowerDisplay.Common.Models; using PowerDisplay.Common.Services; using PowerDisplay.Common.Utils; using PowerDisplay.Serialization; +using PowerDisplay.Services; using PowerToys.Interop; namespace PowerDisplay.ViewModels;