mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-18 20:49:46 +01:00
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:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user