mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
Refactor profile and color temp application logic
Simplify MainViewModel by removing legacy HardwareId matching and the ApplyColorTemperatureAsync method. Update ApplyProfileAsync to accept only monitor settings and match monitors by InternalName. Improve documentation in ThemeChangedEventArgs and ProfileService for clarity. Streamline method signatures and remove outdated compatibility code.
This commit is contained in:
@@ -43,6 +43,7 @@ namespace PowerDisplay.Common.Services
|
||||
public static IProfileService Instance { get; } = new ProfileService();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ProfileService"/> class.
|
||||
/// Private constructor to enforce singleton pattern for instance-based access.
|
||||
/// Static methods remain available for backward compatibility.
|
||||
/// </summary>
|
||||
|
||||
@@ -12,12 +12,12 @@ namespace PowerDisplay.Common.Services
|
||||
public class ThemeChangedEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Whether the system is currently in light mode
|
||||
/// Gets a value indicating whether whether the system is currently in light mode
|
||||
/// </summary>
|
||||
public bool IsLightMode { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Profile name to apply (null if no profile configured for current theme)
|
||||
/// Gets profile name to apply (null if no profile configured for current theme)
|
||||
/// </summary>
|
||||
public string? ProfileToApply { get; }
|
||||
|
||||
|
||||
@@ -121,7 +121,7 @@ public partial class MainViewModel
|
||||
if (monitorVm != null)
|
||||
{
|
||||
// Apply color temperature directly
|
||||
await ApplyColorTemperatureAsync(monitorVm, pendingOp.ColorTemperatureVcp);
|
||||
await monitorVm.SetColorTemperatureAsync(pendingOp.ColorTemperatureVcp);
|
||||
Logger.LogInfo($"[Settings] Successfully applied color temperature to monitor '{pendingOp.MonitorId}'");
|
||||
|
||||
// Update the monitor's ColorTemperatureVcp in settings to match the applied value
|
||||
@@ -185,7 +185,7 @@ public partial class MainViewModel
|
||||
if (pendingOp.MonitorSettings != null && pendingOp.MonitorSettings.Count > 0)
|
||||
{
|
||||
// Apply profile settings to monitors
|
||||
await ApplyProfileAsync(pendingOp.ProfileName, pendingOp.MonitorSettings);
|
||||
await ApplyProfileAsync(pendingOp.MonitorSettings);
|
||||
|
||||
// Note: We no longer track "current profile" - profiles are just templates
|
||||
Logger.LogInfo($"[Profile] Successfully applied profile '{pendingOp.ProfileName}'");
|
||||
@@ -235,7 +235,7 @@ public partial class MainViewModel
|
||||
}
|
||||
|
||||
// Apply the profile
|
||||
await ApplyProfileAsync(e.ProfileToApply, profile.MonitorSettings);
|
||||
await ApplyProfileAsync(profile.MonitorSettings);
|
||||
Logger.LogInfo($"[LightSwitch Integration] Successfully applied profile '{e.ProfileToApply}'");
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -248,29 +248,18 @@ public partial class MainViewModel
|
||||
/// <summary>
|
||||
/// Apply profile settings to monitors
|
||||
/// </summary>
|
||||
private async Task ApplyProfileAsync(string profileName, List<ProfileMonitorSetting> monitorSettings)
|
||||
private async Task ApplyProfileAsync(List<ProfileMonitorSetting> monitorSettings)
|
||||
{
|
||||
var updateTasks = new List<Task>();
|
||||
|
||||
foreach (var setting in monitorSettings)
|
||||
{
|
||||
// Find monitor by InternalName first (unique identifier), fall back to HardwareId for old profiles
|
||||
MonitorViewModel? monitorVm = null;
|
||||
|
||||
if (!string.IsNullOrEmpty(setting.MonitorInternalName))
|
||||
{
|
||||
monitorVm = Monitors.FirstOrDefault(m => m.InternalName == setting.MonitorInternalName);
|
||||
}
|
||||
|
||||
// Fallback to HardwareId for backward compatibility with old profiles
|
||||
if (monitorVm == null)
|
||||
{
|
||||
monitorVm = Monitors.FirstOrDefault(m => m.HardwareId == setting.HardwareId);
|
||||
}
|
||||
// Find monitor by InternalName (unique identifier)
|
||||
var monitorVm = Monitors.FirstOrDefault(m => m.InternalName == setting.MonitorInternalName);
|
||||
|
||||
if (monitorVm == null)
|
||||
{
|
||||
Logger.LogWarning($"[Profile] Monitor with InternalName '{setting.MonitorInternalName}' or HardwareId '{setting.HardwareId}' not found (disconnected?)");
|
||||
Logger.LogWarning($"[Profile] Monitor with InternalName '{setting.MonitorInternalName}' not found (disconnected?)");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -312,15 +301,6 @@ public partial class MainViewModel
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Apply color temperature to a specific monitor
|
||||
/// </summary>
|
||||
private async Task ApplyColorTemperatureAsync(MonitorViewModel monitorVm, int colorTemperature)
|
||||
{
|
||||
// Use MonitorViewModel's unified method
|
||||
await monitorVm.SetColorTemperatureAsync(colorTemperature);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Restore monitor settings from state file - ONLY called at startup when RestoreSettingsOnStartup is enabled
|
||||
/// </summary>
|
||||
|
||||
@@ -238,7 +238,7 @@ public partial class MainViewModel : INotifyPropertyChanged, IDisposable
|
||||
if (profile != null && profile.IsValid())
|
||||
{
|
||||
Logger.LogInfo($"[Profile] Applying profile '{profile.Name}' from quick apply");
|
||||
await ApplyProfileAsync(profile.Name, profile.MonitorSettings);
|
||||
await ApplyProfileAsync(profile.MonitorSettings);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user