Skip to content

Commit

Permalink
simplify class and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchalmers committed Jul 17, 2024
1 parent ca41991 commit b7bc628
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 26 deletions.
55 changes: 55 additions & 0 deletions DesktopClock.Tests/PixelShifterTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
8 changes: 6 additions & 2 deletions DesktopClock/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
}

Expand Down
43 changes: 19 additions & 24 deletions DesktopClock/Utilities/PixelShifter.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -20,34 +18,31 @@ public class PixelShifter
/// </summary>
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;

/// <summary>
/// Shifts the location of the window randomly to help prevent screen burn-in.
/// </summary>
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;
}
}

0 comments on commit b7bc628

Please sign in to comment.