Files
logikonline fc3ebe14be
All checks were successful
Build / build (push) Successful in 9h0m7s
Build and Release / build (push) Successful in 9h0m12s
Add Reset and Detect buttons to Config dialog
- Reset button: clears custom labels, unhides all ports, removes discovered ports
- Detect button: tries common VCP 60 values to discover available input ports
- Version bumped to 1.1.2

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 02:41:42 -05:00

183 lines
5.5 KiB
C#

using CMM.Library.Helpers;
using CMM.Library.ViewModel;
using System.IO;
namespace CMM.Library.Config;
public static class MonitorConfigManager
{
private static readonly string ConfigFolder = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"MonitorControl");
private static readonly string ConfigFile = Path.Combine(ConfigFolder, "config.json");
private static AppConfig? _cachedConfig;
public static AppConfig Load()
{
if (_cachedConfig != null)
return _cachedConfig;
if (!File.Exists(ConfigFile))
{
_cachedConfig = new AppConfig();
return _cachedConfig;
}
try
{
_cachedConfig = ConfigFile.JsonFormFile<AppConfig>() ?? new AppConfig();
}
catch
{
_cachedConfig = new AppConfig();
}
return _cachedConfig;
}
public static void Save(AppConfig config)
{
_cachedConfig = config;
config.FileToJson(ConfigFile);
}
public static MonitorConfig GetMonitorConfig(string serialNumber)
{
var config = Load();
return config.Monitors.FirstOrDefault(m => m.SerialNumber == serialNumber)
?? new MonitorConfig { SerialNumber = serialNumber };
}
public static void SaveMonitorConfig(MonitorConfig monitorConfig)
{
var config = Load();
var existing = config.Monitors.FirstOrDefault(m => m.SerialNumber == monitorConfig.SerialNumber);
if (existing != null)
config.Monitors.Remove(existing);
config.Monitors.Add(monitorConfig);
Save(config);
}
public static List<QuickSwitchItem> GetQuickSwitchItems()
{
var config = Load();
var items = new List<QuickSwitchItem>();
foreach (var monitor in config.Monitors)
{
foreach (var port in monitor.Ports.Where(p => p.ShowInQuickSwitch && !p.IsHidden))
{
items.Add(new QuickSwitchItem
{
MonitorSerialNumber = monitor.SerialNumber,
MonitorName = monitor.MonitorName,
PortVcpValue = port.VcpValue,
PortLabel = port.DisplayName
});
}
}
return items;
}
public static List<InputSourceOption> ApplyConfigToOptions(
string serialNumber,
List<InputSourceOption> options,
int? currentInput = null)
{
var monitorConfig = GetMonitorConfig(serialNumber);
if (monitorConfig.Ports.Count == 0)
return options;
var result = new List<InputSourceOption>();
foreach (var option in options)
{
var portConfig = monitorConfig.Ports.FirstOrDefault(p => p.VcpValue == option.Value);
if (portConfig != null)
{
// Never hide the currently active input - user needs to see what's selected
bool isCurrentInput = currentInput.HasValue && option.Value == currentInput.Value;
// Respect hidden setting - don't show hidden ports (unless it's the current input)
if (portConfig.IsHidden && !isCurrentInput)
continue;
if (!string.IsNullOrWhiteSpace(portConfig.CustomLabel))
{
result.Add(new InputSourceOption(option.Value, portConfig.CustomLabel));
continue;
}
}
result.Add(option);
}
return result;
}
public static void ClearCache()
{
_cachedConfig = null;
}
/// <summary>
/// Save a discovered port that the monitor didn't report in its possible values.
/// This ensures ports like DisplayPort-1 are remembered even if the monitor firmware doesn't list them.
/// </summary>
public static void AddDiscoveredPort(string serialNumber, string monitorName, int vcpValue, string defaultName)
{
var config = Load();
var monitorConfig = config.Monitors.FirstOrDefault(m => m.SerialNumber == serialNumber);
if (monitorConfig == null)
{
monitorConfig = new MonitorConfig { SerialNumber = serialNumber, MonitorName = monitorName };
config.Monitors.Add(monitorConfig);
}
// Check if port already exists
if (monitorConfig.Ports.Any(p => p.VcpValue == vcpValue))
return;
// Add the discovered port
monitorConfig.Ports.Add(new PortConfig
{
VcpValue = vcpValue,
DefaultName = defaultName,
CustomLabel = "",
IsHidden = false,
ShowInQuickSwitch = false
});
Save(config);
}
/// <summary>
/// Get any discovered ports from config that the monitor didn't report.
/// </summary>
public static List<InputSourceOption> GetDiscoveredPorts(string serialNumber, List<InputSourceOption> reportedOptions)
{
var discovered = new List<InputSourceOption>();
var monitorConfig = GetMonitorConfig(serialNumber);
foreach (var port in monitorConfig.Ports)
{
// If this port isn't in the reported options, it's a discovered port
if (!reportedOptions.Any(o => o.Value == port.VcpValue))
{
var name = !string.IsNullOrWhiteSpace(port.CustomLabel) ? port.CustomLabel : port.DefaultName;
discovered.Add(new InputSourceOption(port.VcpValue, name));
}
}
return discovered;
}
}