diff --git a/DesktopClock.Tests/PixelShifterTests.cs b/DesktopClock.Tests/PixelShifterTests.cs new file mode 100644 index 0000000..fde752a --- /dev/null +++ b/DesktopClock.Tests/PixelShifterTests.cs @@ -0,0 +1,55 @@ +using System; +using DesktopClock.Utilities; + +namespace DesktopClock.Tests; + +public class PixelShifterTests +{ + [Theory] + [InlineData(5, 10)] // Evenly divisible. + [InlineData(3, 10)] // Not evenly divisible. + [InlineData(10, 5)] // Amount is larger than total. + public void ShiftX_ShouldNotExceedMaxTotalShift(int shiftAmount, int maxTotalShift) + { + var shifter = new PixelShifter + { + ShiftAmount = shiftAmount, + MaxTotalShift = maxTotalShift, + }; + + double totalShiftX = 0; + + // Test 100 times because it's random. + for (var i = 0; i < 100; i++) + { + var shift = shifter.ShiftX(); + totalShiftX += shift; + + Assert.InRange(Math.Abs(totalShiftX), 0, maxTotalShift); + } + } + + [Theory] + [InlineData(5, 10)] // Evenly divisible. + [InlineData(3, 10)] // Not evenly divisible. + [InlineData(10, 5)] // Amount is larger than total. + public void ShiftY_ShouldNotExceedMaxTotalShift(int shiftAmount, int maxTotalShift) + { + var shifter = new PixelShifter + { + ShiftAmount = shiftAmount, + MaxTotalShift = maxTotalShift, + }; + + double totalShiftY = 0; + + // Test 100 times because it's random. + for (var i = 0; i < 100; i++) + { + var shift = shifter.ShiftY(); + totalShiftY += shift; + + Assert.InRange(Math.Abs(totalShiftY), 0, maxTotalShift); + } + } +} diff --git a/DesktopClock/MainWindow.xaml.cs b/DesktopClock/MainWindow.xaml.cs index e386390..7b4d675 100644 --- a/DesktopClock/MainWindow.xaml.cs +++ b/DesktopClock/MainWindow.xaml.cs @@ -214,8 +214,12 @@ private void SystemClockTimer_SecondChanged(object sender, EventArgs e) if (Settings.Default.BurnInMitigation) { - _pixelShifter ??= new(this); - Dispatcher.Invoke(_pixelShifter.ShiftWindow); + _pixelShifter ??= new(); + Dispatcher.Invoke(() => + { + Left += _pixelShifter.ShiftX(); + Top += _pixelShifter.ShiftY(); + }); } } diff --git a/DesktopClock/Utilities/PixelShifter.cs b/DesktopClock/Utilities/PixelShifter.cs index fb675c5..a57100a 100644 --- a/DesktopClock/Utilities/PixelShifter.cs +++ b/DesktopClock/Utilities/PixelShifter.cs @@ -1,12 +1,10 @@ using System; -using System.Windows; namespace DesktopClock.Utilities; public class PixelShifter { private readonly Random _random = new(); - private readonly Window _window; private double _totalShiftX; private double _totalShiftY; @@ -20,34 +18,31 @@ public class PixelShifter /// public int MaxTotalShift { get; set; } = 4; - public PixelShifter(Window window) + public double ShiftX() { - _window = window; - } + var shift = _random.Next(-ShiftAmount, ShiftAmount + 1); + var newTotalShiftX = _totalShiftX + shift; - /// - /// Shifts the location of the window randomly to help prevent screen burn-in. - /// - public void ShiftWindow() - { - double CalculateShift(ref double totalShift) + if (Math.Abs(newTotalShiftX) <= MaxTotalShift) { - var shift = _random.Next(-ShiftAmount, ShiftAmount + 1); - var newTotalShift = totalShift + shift; + _totalShiftX = newTotalShiftX; + return shift; + } - if (Math.Abs(newTotalShift) <= MaxTotalShift) - { - totalShift = newTotalShift; - return shift; - } + return 0; + } - return 0; - } + public double ShiftY() + { + var shift = _random.Next(-ShiftAmount, ShiftAmount + 1); + var newTotalShiftY = _totalShiftY + shift; - var shiftX = CalculateShift(ref _totalShiftX); - var shiftY = CalculateShift(ref _totalShiftY); + if (Math.Abs(newTotalShiftY) <= MaxTotalShift) + { + _totalShiftY = newTotalShiftY; + return shift; + } - _window.Left += shiftX; - _window.Top += shiftY; + return 0; } }