Files
PowerToys/src/modules/powerdisplay/PowerDisplay/ViewModels/MainViewModel.Monitors.cs
Yu Leng 97560ea6c0 Refactor color temp init to be synchronous in DDC/CI
Simplifies color temperature initialization by moving it from async handling in MainViewModel to synchronous initialization during monitor enumeration in DdcCiController. Removes related async methods and UI update logic, reducing complexity and ensuring color temperature values are available immediately after enumeration.
2025-12-10 11:18:28 +08:00

140 lines
4.2 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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Library;
using PowerDisplay.Common.Models;
using PowerDisplay.Helpers;
using Monitor = PowerDisplay.Common.Models.Monitor;
namespace PowerDisplay.ViewModels;
/// <summary>
/// MainViewModel - Monitor discovery and management methods
/// </summary>
public partial class MainViewModel
{
private async Task InitializeAsync(CancellationToken cancellationToken = default)
{
try
{
IsScanning = true;
// Discover monitors
var monitors = await _monitorManager.DiscoverMonitorsAsync(cancellationToken);
// Update UI on the dispatcher thread
_dispatcherQueue.TryEnqueue(() =>
{
try
{
UpdateMonitorList(monitors);
IsScanning = false;
IsInitialized = true;
// Start watching for display changes after initialization
StartDisplayWatching();
}
catch (Exception lambdaEx)
{
Logger.LogError($"[InitializeAsync] UI update failed: {lambdaEx.Message}");
IsScanning = false;
}
});
}
catch (Exception ex)
{
Logger.LogError($"[InitializeAsync] Monitor discovery failed: {ex.Message}");
_dispatcherQueue.TryEnqueue(() =>
{
IsScanning = false;
});
}
}
public async Task RefreshMonitorsAsync()
{
if (IsScanning)
{
return;
}
try
{
IsScanning = true;
var monitors = await _monitorManager.DiscoverMonitorsAsync(_cancellationTokenSource.Token);
_dispatcherQueue.TryEnqueue(() =>
{
UpdateMonitorList(monitors);
IsScanning = false;
});
}
catch (Exception ex)
{
Logger.LogError($"[RefreshMonitorsAsync] Refresh failed: {ex.Message}");
_dispatcherQueue.TryEnqueue(() =>
{
IsScanning = false;
});
}
}
private void UpdateMonitorList(IReadOnlyList<Monitor> monitors)
{
Monitors.Clear();
// Load settings to check for hidden monitors
var settings = _settingsUtils.GetSettingsOrDefault<PowerDisplaySettings>(PowerDisplaySettings.ModuleName);
var hiddenMonitorIds = GetHiddenMonitorIds(settings);
foreach (var monitor in monitors)
{
// Skip monitors that are marked as hidden in settings
if (hiddenMonitorIds.Contains(monitor.Id))
{
Logger.LogInfo($"[UpdateMonitorList] Skipping hidden monitor: {monitor.Name} ({monitor.Id})");
continue;
}
var vm = new MonitorViewModel(monitor, _monitorManager, this);
Monitors.Add(vm);
}
OnPropertyChanged(nameof(HasMonitors));
OnPropertyChanged(nameof(ShowNoMonitorsMessage));
// Save monitor information to settings and reload
SaveMonitorsToSettings();
_ = ReloadMonitorSettingsAsync(null);
}
public async Task SetAllBrightnessAsync(int brightness)
{
try
{
await _monitorManager.SetAllBrightnessAsync(brightness, _cancellationTokenSource.Token);
}
catch (Exception ex)
{
Logger.LogError($"[SetAllBrightnessAsync] Failed to set brightness: {ex.Message}");
}
}
/// <summary>
/// Get set of hidden monitor IDs from settings
/// </summary>
private HashSet<string> GetHiddenMonitorIds(PowerDisplaySettings settings)
=> new HashSet<string>(
settings.Properties.Monitors
.Where(m => m.IsHidden)
.Select(m => m.InternalName));
}