Refactor and optimize profile and monitor handling

Refactored profile name generation by centralizing logic in `ProfileHelper` with overloads for flexibility. Simplified folder creation logic in `PathConstants` using a reusable helper method.

Improved profile loading and saving in `ProfileService` with internal helper methods for better error handling and reduced duplication. Optimized monitor key generation and lookup with concise expressions and dictionary-based retrieval for O(1) performance.

Introduced caching for color presets in `MonitorInfo` to avoid redundant computations and added a helper for range validation in `MainViewModel.Settings`. Centralized percentage formatting and property change handling to reduce duplication.

Removed redundant methods in `PowerDisplayViewModel` and streamlined event unsubscription in `MainWindow`. Enhanced logging, readability, and maintainability across the codebase.
This commit is contained in:
Yu Leng
2025-11-24 23:36:25 +08:00
parent 15746e8f45
commit 471cad659f
12 changed files with 225 additions and 288 deletions

View File

@@ -506,22 +506,6 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
/// <summary>
/// Load profiles data from disk
/// </summary>
private PowerDisplayProfiles LoadProfilesFromDisk()
{
return ProfileService.LoadProfiles();
}
/// <summary>
/// Save profiles data to disk
/// </summary>
private void SaveProfilesToDisk(PowerDisplayProfiles profiles)
{
ProfileService.SaveProfiles(profiles);
}
/// <summary>
/// Apply a profile to monitors
/// </summary>
@@ -583,9 +567,9 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
Logger.LogInfo($"Creating profile: {profile.Name}");
var profilesData = LoadProfilesFromDisk();
var profilesData = ProfileService.LoadProfiles();
profilesData.SetProfile(profile);
SaveProfilesToDisk(profilesData);
ProfileService.SaveProfiles(profilesData);
// Reload profile list
LoadProfiles();
@@ -613,12 +597,12 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
Logger.LogInfo($"Updating profile: {oldName} -> {newProfile.Name}");
var profilesData = LoadProfilesFromDisk();
var profilesData = ProfileService.LoadProfiles();
// Remove old profile and add updated one
profilesData.RemoveProfile(oldName);
profilesData.SetProfile(newProfile);
SaveProfilesToDisk(profilesData);
ProfileService.SaveProfiles(profilesData);
// Reload profile list
LoadProfiles();
@@ -645,9 +629,9 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
Logger.LogInfo($"Deleting profile: {profileName}");
var profilesData = LoadProfilesFromDisk();
var profilesData = ProfileService.LoadProfiles();
profilesData.RemoveProfile(profileName);
SaveProfilesToDisk(profilesData);
ProfileService.SaveProfiles(profilesData);
// Reload profile list
LoadProfiles();