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:
Yu Leng
2025-12-10 12:42:01 +08:00
parent d48438571e
commit 0965f814ce
4 changed files with 11 additions and 30 deletions

View File

@@ -43,6 +43,7 @@ namespace PowerDisplay.Common.Services
public static IProfileService Instance { get; } = new ProfileService(); public static IProfileService Instance { get; } = new ProfileService();
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ProfileService"/> class.
/// Private constructor to enforce singleton pattern for instance-based access. /// Private constructor to enforce singleton pattern for instance-based access.
/// Static methods remain available for backward compatibility. /// Static methods remain available for backward compatibility.
/// </summary> /// </summary>

View File

@@ -12,12 +12,12 @@ namespace PowerDisplay.Common.Services
public class ThemeChangedEventArgs : EventArgs public class ThemeChangedEventArgs : EventArgs
{ {
/// <summary> /// <summary>
/// Whether the system is currently in light mode /// Gets a value indicating whether whether the system is currently in light mode
/// </summary> /// </summary>
public bool IsLightMode { get; } public bool IsLightMode { get; }
/// <summary> /// <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> /// </summary>
public string? ProfileToApply { get; } public string? ProfileToApply { get; }

View File

@@ -121,7 +121,7 @@ public partial class MainViewModel
if (monitorVm != null) if (monitorVm != null)
{ {
// Apply color temperature directly // 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}'"); Logger.LogInfo($"[Settings] Successfully applied color temperature to monitor '{pendingOp.MonitorId}'");
// Update the monitor's ColorTemperatureVcp in settings to match the applied value // 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) if (pendingOp.MonitorSettings != null && pendingOp.MonitorSettings.Count > 0)
{ {
// Apply profile settings to monitors // 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 // Note: We no longer track "current profile" - profiles are just templates
Logger.LogInfo($"[Profile] Successfully applied profile '{pendingOp.ProfileName}'"); Logger.LogInfo($"[Profile] Successfully applied profile '{pendingOp.ProfileName}'");
@@ -235,7 +235,7 @@ public partial class MainViewModel
} }
// Apply the profile // Apply the profile
await ApplyProfileAsync(e.ProfileToApply, profile.MonitorSettings); await ApplyProfileAsync(profile.MonitorSettings);
Logger.LogInfo($"[LightSwitch Integration] Successfully applied profile '{e.ProfileToApply}'"); Logger.LogInfo($"[LightSwitch Integration] Successfully applied profile '{e.ProfileToApply}'");
} }
catch (Exception ex) catch (Exception ex)
@@ -248,29 +248,18 @@ public partial class MainViewModel
/// <summary> /// <summary>
/// Apply profile settings to monitors /// Apply profile settings to monitors
/// </summary> /// </summary>
private async Task ApplyProfileAsync(string profileName, List<ProfileMonitorSetting> monitorSettings) private async Task ApplyProfileAsync(List<ProfileMonitorSetting> monitorSettings)
{ {
var updateTasks = new List<Task>(); var updateTasks = new List<Task>();
foreach (var setting in monitorSettings) foreach (var setting in monitorSettings)
{ {
// Find monitor by InternalName first (unique identifier), fall back to HardwareId for old profiles // Find monitor by InternalName (unique identifier)
MonitorViewModel? monitorVm = null; var monitorVm = Monitors.FirstOrDefault(m => m.InternalName == setting.MonitorInternalName);
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);
}
if (monitorVm == null) 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; 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> /// <summary>
/// Restore monitor settings from state file - ONLY called at startup when RestoreSettingsOnStartup is enabled /// Restore monitor settings from state file - ONLY called at startup when RestoreSettingsOnStartup is enabled
/// </summary> /// </summary>

View File

@@ -238,7 +238,7 @@ public partial class MainViewModel : INotifyPropertyChanged, IDisposable
if (profile != null && profile.IsValid()) if (profile != null && profile.IsValid())
{ {
Logger.LogInfo($"[Profile] Applying profile '{profile.Name}' from quick apply"); Logger.LogInfo($"[Profile] Applying profile '{profile.Name}' from quick apply");
await ApplyProfileAsync(profile.Name, profile.MonitorSettings); await ApplyProfileAsync(profile.MonitorSettings);
} }
} }