2026-01-04 00:25:01 -05:00
|
|
|
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,
|
2026-01-10 02:03:11 -05:00
|
|
|
List<InputSourceOption> options,
|
|
|
|
|
int? currentInput = null)
|
2026-01-04 00:25:01 -05:00
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
2026-01-10 02:03:11 -05:00
|
|
|
// 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)
|
2026-01-04 00:25:01 -05:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|