Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Auto Dark Mode #502

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ConfigMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/JsonConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
9 changes: 9 additions & 0 deletions src/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class MainMenu
private static readonly Func<string, string> _ = 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;
Expand Down Expand Up @@ -51,6 +52,8 @@ private static List<ToolStripItem> 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"));
Expand All @@ -59,6 +62,7 @@ private static List<ToolStripItem> GetMenuItems()
items.AddRange(new List<ToolStripItem>()
{
darkModeItem,
autoDarkMode,
startOnBootItem,
optionsItem,
new ToolStripSeparator(),
Expand Down Expand Up @@ -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();
Expand Down
30 changes: 29 additions & 1 deletion src/WallpaperEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down