Skip to content

Commit

Permalink
Fix bug where wallpaper didn't update by changing Timer class
Browse files Browse the repository at this point in the history
  • Loading branch information
t1m0thyj committed Jun 23, 2018
1 parent 706d44a commit 7004c01
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion installer.iss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "WinDynamicDesktop"
#define MyAppVersion "1.2.1"
#define MyAppVersion "1.2.2"
#define MyAppPublisher "Timothy Johnson"
#define MyAppURL "https://github.com/t1m0thyj/WinDynamicDesktop"
#define MyAppExeName "WinDynamicDesktop.exe"
Expand Down
26 changes: 13 additions & 13 deletions src/FormWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ class FormWrapper : ApplicationContext
private InputDialog locationDialog;
private NotifyIcon notifyIcon;

public StartupManager startupManager;
public WallpaperChangeScheduler wcsService;
public StartupManager _startupManager;
public WallpaperChangeScheduler _wcsService;

public FormWrapper()
{
Application.ApplicationExit += new EventHandler(OnApplicationExit);
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(OnPowerModeChanged);

JsonConfig.LoadConfig();
wcsService = new WallpaperChangeScheduler();
_wcsService = new WallpaperChangeScheduler();

InitializeComponent();
startupManager = new StartupManager(notifyIcon.ContextMenu.MenuItems[5]);
_startupManager = new StartupManager(notifyIcon.ContextMenu.MenuItems[5]);

if (!Directory.Exists("images"))
{
Expand All @@ -40,7 +40,7 @@ public FormWrapper()
}
else
{
wcsService.StartScheduler();
_wcsService.StartScheduler();
}
}

Expand Down Expand Up @@ -75,12 +75,12 @@ private void OnLocationItemClick(object sender, EventArgs e)

private void OnRefreshItemClick(object sender, EventArgs e)
{
wcsService.StartScheduler(true);
_wcsService.StartScheduler(true);
}

private void OnStartupItemClick(object sender, EventArgs e)
{
startupManager.ToggleStartOnBoot();
_startupManager.ToggleStartOnBoot();
}

private void OnExitItemClick(object sender, EventArgs e)
Expand Down Expand Up @@ -146,7 +146,7 @@ public void UpdateLocation()
{
if (locationDialog == null)
{
locationDialog = new InputDialog { wcsService = wcsService };
locationDialog = new InputDialog { _wcsService = _wcsService };
locationDialog.FormClosed += OnLocationDialogClosed;
locationDialog.Show();
}
Expand Down Expand Up @@ -174,21 +174,21 @@ private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
if (e.Mode == PowerModes.Suspend)
{
if (wcsService.wallpaperTimer != null)
if (_wcsService.wallpaperTimer != null)
{
wcsService.wallpaperTimer.Stop();
_wcsService.wallpaperTimer.Stop();
}

wcsService.enableTransitions = false;
_wcsService.enableTransitions = false;
}
else if (e.Mode == PowerModes.Resume)
{
if (JsonConfig.settings.location != null)
{
wcsService.StartScheduler();
_wcsService.StartScheduler();
}

wcsService.enableTransitions = true;
_wcsService.enableTransitions = true;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/InputDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace WinDynamicDesktop
{
public partial class InputDialog : Form
{
internal WallpaperChangeScheduler wcsService;
internal WallpaperChangeScheduler _wcsService;

public InputDialog()
{
Expand Down Expand Up @@ -48,7 +48,7 @@ private void okButton_Click(object sender, EventArgs e)
JsonConfig.settings.longitude = data.lon;
JsonConfig.SaveConfig();

wcsService.StartScheduler(true);
_wcsService.StartScheduler(true);

MessageBox.Show("Location set successfully to: " + data.display_name +
Environment.NewLine + "Latitude = " + data.lat + ", Longitude = " + data.lon,
Expand Down
2 changes: 1 addition & 1 deletion src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.1")]
[assembly: AssemblyVersion("1.2.2")]
//[assembly: AssemblyFileVersion("1.0.0.0")]
26 changes: 14 additions & 12 deletions src/WallpaperChangeScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Threading.Tasks;
using System.Globalization;
using System.IO;
using System.Timers;
using System.Windows.Forms;

namespace WinDynamicDesktop
{
Expand Down Expand Up @@ -82,7 +82,7 @@ private void SetWallpaper(int imageId)
}

WallpaperChanger.SetWallpaper(imagePath);

lastImageId = imageId;
}

Expand Down Expand Up @@ -122,6 +122,8 @@ public void StartScheduler(bool forceRefresh = false)
isSunUp = true;
}

lastImageId = -1;

if (isSunUp)
{
StartDaySchedule();
Expand All @@ -130,8 +132,6 @@ public void StartScheduler(bool forceRefresh = false)
{
StartNightSchedule();
}

lastImageId = 0;
}

private void StartDaySchedule()
Expand All @@ -143,8 +143,9 @@ private void StartDaySchedule()
TimeSpan interval = new TimeSpan(todaysData.SunriseTime.Ticks + timerLength.Ticks *
(imageNumber + 1) - DateTime.Now.Ticks);

wallpaperTimer = new Timer(interval.TotalMilliseconds);
wallpaperTimer.Elapsed += new ElapsedEventHandler(wallpaperTimer_Elapsed);
wallpaperTimer = new Timer();
wallpaperTimer.Interval = (int)interval.TotalMilliseconds;
wallpaperTimer.Tick += new EventHandler(OnWallpaperTimerTick);
wallpaperTimer.Start();

if (dayImages[imageNumber] != lastImageId)
Expand All @@ -165,7 +166,7 @@ private void NextDayImage()
TimeSpan dayTime = todaysData.SunsetTime - todaysData.SunriseTime;
TimeSpan timerLength = new TimeSpan(dayTime.Ticks / dayImages.Length);

wallpaperTimer.Interval = timerLength.TotalMilliseconds;
wallpaperTimer.Interval = (int)timerLength.TotalMilliseconds;
wallpaperTimer.Start();

lastImageNumber++;
Expand All @@ -184,16 +185,17 @@ private void StartNightSchedule()
{
WeatherData day1Data = (yesterdaysData == null) ? todaysData : yesterdaysData;
WeatherData day2Data = (yesterdaysData == null) ? tomorrowsData : todaysData;

TimeSpan nightTime = day2Data.SunriseTime - day1Data.SunsetTime;
TimeSpan timerLength = new TimeSpan(nightTime.Ticks / nightImages.Length);

int imageNumber = GetImageNumber(day1Data.SunsetTime, timerLength, nightImages.Length);
TimeSpan interval = new TimeSpan(day1Data.SunsetTime.Ticks + timerLength.Ticks *
(imageNumber + 1) - DateTime.Now.Ticks);

wallpaperTimer = new Timer(interval.TotalMilliseconds);
wallpaperTimer.Elapsed += new ElapsedEventHandler(wallpaperTimer_Elapsed);
wallpaperTimer = new Timer();
wallpaperTimer.Interval = (int)interval.TotalMilliseconds;
wallpaperTimer.Tick += new EventHandler(OnWallpaperTimerTick);
wallpaperTimer.Start();

if (nightImages[imageNumber] != lastImageId)
Expand All @@ -217,7 +219,7 @@ private void NextNightImage()
TimeSpan nightTime = day2Data.SunriseTime - day1Data.SunsetTime;
TimeSpan timerLength = new TimeSpan(nightTime.Ticks / nightImages.Length);

wallpaperTimer.Interval = timerLength.TotalMilliseconds;
wallpaperTimer.Interval = (int)timerLength.TotalMilliseconds;
wallpaperTimer.Start();

lastImageNumber++;
Expand All @@ -240,7 +242,7 @@ private void SwitchToDay()
StartDaySchedule();
}

private void wallpaperTimer_Elapsed(object sender, EventArgs e)
private void OnWallpaperTimerTick(object sender, EventArgs e)
{
if (isSunUp)
{
Expand Down

0 comments on commit 7004c01

Please sign in to comment.