Skip to content

Commit

Permalink
better algo
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchalmers committed Jul 17, 2024
1 parent cd152b2 commit ca41991
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 22 deletions.
28 changes: 7 additions & 21 deletions DesktopClock/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DesktopClock.Properties;
using DesktopClock.Utilities;
using H.NotifyIcon;
using H.NotifyIcon.EfficiencyMode;
using Humanizer;
Expand All @@ -23,13 +24,11 @@ namespace DesktopClock;
[ObservableObject]
public partial class MainWindow : Window
{
private readonly Random _random = new();
private readonly SystemClockTimer _systemClockTimer;
private TaskbarIcon _trayIcon;
private TimeZoneInfo _timeZone;
private SoundPlayer _soundPlayer;
private double totalShiftX;
private double totalShiftY;
private PixelShifter _pixelShifter;

/// <summary>
/// The date and time to countdown to, or <c>null</c> if regular clock is desired.
Expand Down Expand Up @@ -204,23 +203,6 @@ private void Settings_PropertyChanged(object sender, PropertyChangedEventArgs e)
}
}

private void ShiftWindow()
{
const int MaxTotalShift = 10;
const int MaxShiftPerTick = 5;

double ApplyShift(ref double totalShift)
{
var shift = _random.Next(-MaxShiftPerTick, MaxShiftPerTick + 1);
totalShift += shift;

return Math.Min(MaxTotalShift, totalShift);
}

Left += ApplyShift(ref totalShiftX);
Top += ApplyShift(ref totalShiftY);
}

/// <summary>
/// Handles the event when the system clock timer signals a second change.
/// </summary>
Expand All @@ -230,7 +212,11 @@ private void SystemClockTimer_SecondChanged(object sender, EventArgs e)

TryPlaySound();

Dispatcher.Invoke(ShiftWindow);
if (Settings.Default.BurnInMitigation)
{
_pixelShifter ??= new(this);
Dispatcher.Invoke(_pixelShifter.ShiftWindow);
}
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion DesktopClock/Properties/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private Settings()
public bool RightAligned { get; set; } = false;

/// <summary>
/// Experimental: Shifts the clock's pixels and makes the background more transparent in order to prevent burn-in.
/// Experimental: Shifts the clock around in order to prevent burn-in.
/// </summary>
public bool BurnInMitigation { get; set; } = false;

Expand Down
53 changes: 53 additions & 0 deletions DesktopClock/Utilities/PixelShifter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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;

/// <summary>
/// The number of pixels that will be shifted each time.
/// </summary>
public int ShiftAmount { get; set; } = 2;

/// <summary>
/// The maximum amount of drift that can occur in each direction.
/// </summary>
public int MaxTotalShift { get; set; } = 4;

public PixelShifter(Window window)
{
_window = window;
}

/// <summary>
/// Shifts the location of the window randomly to help prevent screen burn-in.
/// </summary>
public void ShiftWindow()
{
double CalculateShift(ref double totalShift)
{
var shift = _random.Next(-ShiftAmount, ShiftAmount + 1);
var newTotalShift = totalShift + shift;

if (Math.Abs(newTotalShift) <= MaxTotalShift)
{
totalShift = newTotalShift;
return shift;
}

return 0;
}

var shiftX = CalculateShift(ref _totalShiftX);
var shiftY = CalculateShift(ref _totalShiftY);

_window.Left += shiftX;
_window.Top += shiftY;
}
}

0 comments on commit ca41991

Please sign in to comment.