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() ?? 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 GetQuickSwitchItems() { var config = Load(); var items = new List(); 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 ApplyConfigToOptions( string serialNumber, List options, int? currentInput = null) { var monitorConfig = GetMonitorConfig(serialNumber); if (monitorConfig.Ports.Count == 0) return options; var result = new List(); 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; } /// /// 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. /// 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); } /// /// Get any discovered ports from config that the monitor didn't report. /// public static List GetDiscoveredPorts(string serialNumber, List reportedOptions) { var discovered = new List(); 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; } }