Files
PowerToys/src/modules/powerdisplay/PowerDisplay.Lib/Models/PowerDisplayProfiles.cs
Yu Leng 471cad659f 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.
2025-11-24 23:36:25 +08:00

101 lines
2.9 KiB
C#

// 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.
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
namespace PowerDisplay.Common.Models
{
/// <summary>
/// Container for all PowerDisplay profiles
/// </summary>
public class PowerDisplayProfiles
{
// NOTE: Custom profile concept has been removed. Profiles are now templates, not states.
// This constant is kept for backward compatibility (cleaning up legacy Custom profiles).
public const string CustomProfileName = "Custom";
[JsonPropertyName("profiles")]
public List<PowerDisplayProfile> Profiles { get; set; }
[JsonPropertyName("lastUpdated")]
public DateTime LastUpdated { get; set; }
public PowerDisplayProfiles()
{
Profiles = new List<PowerDisplayProfile>();
LastUpdated = DateTime.UtcNow;
}
/// <summary>
/// Gets the profile by name
/// </summary>
public PowerDisplayProfile? GetProfile(string name)
{
return Profiles.FirstOrDefault(p => p.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
}
/// <summary>
/// Adds or updates a profile
/// </summary>
public void SetProfile(PowerDisplayProfile profile)
{
if (profile == null || !profile.IsValid())
{
throw new ArgumentException("Profile is invalid");
}
var existing = GetProfile(profile.Name);
if (existing != null)
{
Profiles.Remove(existing);
}
profile.Touch();
Profiles.Add(profile);
LastUpdated = DateTime.UtcNow;
}
/// <summary>
/// Removes a profile by name
/// </summary>
public bool RemoveProfile(string name)
{
var profile = GetProfile(name);
if (profile != null)
{
Profiles.Remove(profile);
LastUpdated = DateTime.UtcNow;
return true;
}
return false;
}
/// <summary>
/// Checks if a profile name is valid and available
/// </summary>
public bool IsNameAvailable(string name, string? excludeName = null)
{
if (string.IsNullOrWhiteSpace(name))
{
return false;
}
// Check if name is already used (excluding the profile being renamed)
var existing = GetProfile(name);
if (existing != null && (excludeName == null || !existing.Name.Equals(excludeName, StringComparison.OrdinalIgnoreCase)))
{
return false;
}
return true;
}
}
}