public static void CleanUpMonitors() { var monitors = Task.Run(async() => await DisplayHandler.GetMonitorInfosAsync()).Result; IDesktopWallpaper handler = (IDesktopWallpaper) new DesktopWallpaperClass(); List <MonitorSettings> connectedSettings = new(); foreach (var monitor in monitors) { MonitorSettings settings = builder.Config.WallpaperSwitch.Component.Monitors.Find(m => m.Id == monitor.DeviceId); if (settings != null) { connectedSettings.Add(settings); } } int diff = builder.Config.WallpaperSwitch.Component.Monitors.Count - connectedSettings.Count; if (diff != 0) { Logger.Info($"removing {diff} disconnected monitors"); GlobalState state = GlobalState.Instance(); state.SkipConfigFileReload = true; builder.Config.WallpaperSwitch.Component.Monitors = connectedSettings; builder.Save(); } }
/// <summary> /// Adds missing monitors to the configuration file /// If a monitor configuration is not found, /// it will automatically create a configuration with the respective monitor's current wallpaper /// </summary> public static void DetectMonitors() { var monitors = Task.Run(async() => await DisplayHandler.GetMonitorInfosAsync()).Result; IDesktopWallpaper handler = (IDesktopWallpaper) new DesktopWallpaperClass(); List <string> monitorIds = new(); bool needsUpdate = false; foreach (var monitor in monitors) { MonitorSettings settings = builder.Config.WallpaperSwitch.Component.Monitors.Find(m => m.Id == monitor.DeviceId); if (settings == null) { Logger.Info($"missing monitor found, adding new default config for: {monitor.DeviceId}"); builder.Config.WallpaperSwitch.Component.Monitors.Add(new MonitorSettings() { DarkThemeWallpaper = handler.GetWallpaper(monitor.DeviceId), LightThemeWallpaper = handler.GetWallpaper(monitor.DeviceId), Id = monitor.DeviceId }); needsUpdate = true; } } if (needsUpdate) { GlobalState state = GlobalState.Instance(); state.SkipConfigFileReload = true; builder.Save(); } }