diff --git a/src/ConfigMigrator.cs b/src/ConfigMigrator.cs index 34908b6..e11cc08 100644 --- a/src/ConfigMigrator.cs +++ b/src/ConfigMigrator.cs @@ -24,6 +24,7 @@ public class OldAppConfigV4 public string latitude { get; set; } public string longitude { get; set; } public bool darkMode { get; set; } + public bool autoDarkMode { get; set; } public bool hideTrayIcon { get; set; } public bool disableAutoUpdate { get; set; } public string lastUpdateCheck { get; set; } @@ -146,6 +147,7 @@ private static void UpdateToVersion5(string jsonText) // Added 2021-11-30 sunriseSunsetDuration = oldSettings.sunriseSunsetDuration, activeThemes = new string[] { oldSettings.themeName }, darkMode = oldSettings.darkMode, + autoDarkMode = oldSettings.autoDarkMode, enableShuffle = oldSettings.enableShuffle, lastShuffleDate = oldSettings.lastShuffleDate != null ? SafeParse( oldSettings.lastShuffleDate).ToString(CultureInfo.InvariantCulture) : null, diff --git a/src/JsonConfig.cs b/src/JsonConfig.cs index f34aa6d..c9a1140 100644 --- a/src/JsonConfig.cs +++ b/src/JsonConfig.cs @@ -31,6 +31,7 @@ public class AppConfig : INotifyPropertyChanged // Theme settings public string[] activeThemes { get; set; } public bool darkMode { get; set; } + public bool autoDarkMode { get; set; } public bool changeLockScreen { get; set; } public bool enableShuffle { get; set; } public string lastShuffleDate { get; set; } diff --git a/src/MainMenu.cs b/src/MainMenu.cs index 0821d80..76fcc46 100644 --- a/src/MainMenu.cs +++ b/src/MainMenu.cs @@ -14,6 +14,7 @@ class MainMenu private static readonly Func _ = Localization.GetTranslation; public static ToolStripMenuItem themeItem; public static ToolStripMenuItem darkModeItem; + public static ToolStripMenuItem autoDarkMode; public static ToolStripMenuItem startOnBootItem; public static ToolStripMenuItem enableScriptsItem; public static ToolStripMenuItem shuffleItem; @@ -51,6 +52,8 @@ private static List GetMenuItems() items.AddRange(LockScreenChanger.GetMenuItems()); darkModeItem = new ToolStripMenuItem(_("Enable &Night Mode"), null, OnDarkModeClick); darkModeItem.Checked = JsonConfig.settings.darkMode; + autoDarkMode = new ToolStripMenuItem(_("Enable Auto Dark Mode"), null, OnAutoDarkModeClick); + autoDarkMode.Checked = JsonConfig.settings.autoDarkMode; startOnBootItem = new ToolStripMenuItem(_("Start on &Boot"), null, OnStartOnBootClick); ToolStripMenuItem optionsItem = new ToolStripMenuItem(_("More &Options")); @@ -59,6 +62,7 @@ private static List GetMenuItems() items.AddRange(new List() { darkModeItem, + autoDarkMode, startOnBootItem, optionsItem, new ToolStripSeparator(), @@ -121,6 +125,11 @@ private static void OnDarkModeClick(object sender, EventArgs e) AppContext.wpEngine.ToggleDarkMode(); } + private static void OnAutoDarkModeClick(object sender, EventArgs e) + { + AppContext.wpEngine.ToggleAutoDarkMode(); + } + private static void OnStartOnBootClick(object sender, EventArgs e) { UwpDesktop.GetHelper().ToggleStartOnBoot(); diff --git a/src/WallpaperEngine.cs b/src/WallpaperEngine.cs index 6c41c37..6801167 100644 --- a/src/WallpaperEngine.cs +++ b/src/WallpaperEngine.cs @@ -32,7 +32,7 @@ class WallpaperEngine private System.Timers.Timer backgroundTimer = new System.Timers.Timer(); private System.Timers.Timer schedulerTimer = new System.Timers.Timer(); private const long timerError = (long)(TimeSpan.TicksPerMillisecond * 15.6); - + public WallpaperEngine() { fullScreenChecker = new FullScreenApi(this); @@ -91,6 +91,11 @@ public void RunScheduler(bool forceImageUpdate = false) if (displayEvents[i].currentTheme != null) { SetWallpaper(displayEvents[i]); + + if (JsonConfig.settings.autoDarkMode) + { + SwitchTheme(data); + } if (displayEvents[i].nextUpdateTime.Ticks < nextDisplayUpdateTicks) { @@ -141,6 +146,13 @@ public void ToggleDarkMode() RunScheduler(); } + public void ToggleAutoDarkMode() + { + bool isEnabled = JsonConfig.settings.autoDarkMode ^ true; + JsonConfig.settings.autoDarkMode = isEnabled; + MainMenu.autoDarkMode.Checked = isEnabled; + } + private bool UpdateDisplayList() { if (JsonConfig.settings.activeThemes == null || JsonConfig.settings.activeThemes[0] != null) @@ -185,6 +197,22 @@ private void SetWallpaper(DisplayEvent e) e.lastImagePath = imagePath; } + public void SwitchTheme(SolarData data) + { + RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", true); + + if (DateTime.Now.TimeOfDay >= data.sunsetTime.TimeOfDay) + { + key.SetValue("AppsUseLightTheme", 0, RegistryValueKind.DWord); + key.SetValue("SystemUsesLightTheme", 0, RegistryValueKind.DWord); + } + else if (DateTime.Now.TimeOfDay >= data.sunriseTime.TimeOfDay) + { + key.SetValue("AppsUseLightTheme", 1, RegistryValueKind.DWord); + key.SetValue("SystemUsesLightTheme", 1, RegistryValueKind.DWord); + } + } + private void StartTimer(DateTime futureTime) { long intervalTicks = futureTime.Ticks - DateTime.Now.Ticks;