Refactor and enhance monitor matching logic

- Renamed project entries in `PowerToys.sln` for consistency.
- Added new projects: "runner," "NewShellExtensionContextMenu," and "BgcodePreviewHandlerCpp."
- Introduced `MonitorMatchingHelper` to centralize monitor identification logic.
- Added unit tests for `MonitorMatchingHelper` to validate parsing and matching.
- Updated `MonitorInfo` with `MonitorNumber`, `TotalMonitorCount`, and `DisplayName` for dynamic formatting.
- Enhanced WMI monitor matching with pre-fetched display devices.
- Updated UI components to use dynamic `DisplayName` for monitors.
- Added `PowerDisplay.Lib.UnitTests` project for testing.
- Improved serialization, logging, and null handling in `PowerDisplayViewModel`.
- Removed redundant parsing logic from `MonitorDiscoveryHelper`.
This commit is contained in:
Yu Leng
2025-11-26 19:08:01 +08:00
parent 65af62e77a
commit 3598c2c126
11 changed files with 2821 additions and 64 deletions

View File

@@ -21,7 +21,6 @@ using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
using Microsoft.PowerToys.Settings.UI.Library.ViewModels.Commands;
using Microsoft.PowerToys.Settings.UI.SerializationContext;
using Microsoft.PowerToys.Settings.UI.Services;
using PowerDisplay.Common.Models;
using PowerDisplay.Common.Services;
@@ -154,6 +153,9 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
OnPropertyChanged(nameof(Monitors));
HasMonitors = _monitors?.Count > 0;
// Update TotalMonitorCount for dynamic DisplayName
UpdateTotalMonitorCount();
}
}
@@ -178,6 +180,27 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
HasMonitors = _monitors.Count > 0;
_settings.Properties.Monitors = _monitors.ToList();
NotifySettingsChanged();
// Update TotalMonitorCount for dynamic DisplayName
UpdateTotalMonitorCount();
}
/// <summary>
/// Update TotalMonitorCount on all monitors for dynamic DisplayName formatting.
/// When multiple monitors exist, DisplayName shows "Name N" format.
/// </summary>
private void UpdateTotalMonitorCount()
{
if (_monitors == null)
{
return;
}
var count = _monitors.Count;
foreach (var monitor in _monitors)
{
monitor.TotalMonitorCount = count;
}
}
public void UpdateMonitors(MonitorInfo[] monitors)
@@ -326,7 +349,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
},
};
SendConfigMSG(JsonSerializer.Serialize(actionMessage));
SendConfigMSG(JsonSerializer.Serialize(actionMessage, SettingsSerializationContext.Default.PowerDisplayActionMessage));
}
/// <summary>
@@ -362,7 +385,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
},
};
SendConfigMSG(JsonSerializer.Serialize(actionMessage));
SendConfigMSG(JsonSerializer.Serialize(actionMessage, SettingsSerializationContext.Default.PowerDisplayActionMessage));
}
/// <summary>
@@ -672,6 +695,12 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
private void NotifySettingsChanged()
{
// Skip during initialization when SendConfigMSG is not yet set
if (SendConfigMSG == null)
{
return;
}
// Persist locally first so settings survive even if the module DLL isn't loaded yet.
SettingsUtils.SaveSettings(_settings.ToJsonString(), PowerDisplaySettings.ModuleName);
@@ -683,7 +712,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
CultureInfo.InvariantCulture,
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
PowerDisplaySettings.ModuleName,
JsonSerializer.Serialize(_settings, SourceGenerationContextContext.Default.PowerDisplaySettings)));
_settings.ToJsonString()));
}
}
}