Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoco007 committed Sep 25, 2021
1 parent 75cdd8e commit e568d55
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
14 changes: 7 additions & 7 deletions WpfKenBurns/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,21 @@ public ObservableCollection<string> ProgramDenylist

public event PropertyChangedEventHandler? PropertyChanged;

private ObservableCollection<ScreensaverImageFolder> folders = new ObservableCollection<ScreensaverImageFolder>();
private ObservableCollection<ScreensaverImageFolder> folders = new();
private float duration = 7;
private float fadeDuration = 1.5f;
private float movementFactor = 0.05f;
private float scaleFactor = 0.05f;
private byte mouseSensitivity = 8;
private BitmapScalingMode quality = BitmapScalingMode.HighQuality;
private ObservableCollection<string> programDenylist = new ObservableCollection<string>();
private ObservableCollection<string> programDenylist = new();

public static void Save(Configuration configuration)
{
if (!Directory.Exists(ConfigurationFolder)) Directory.CreateDirectory(ConfigurationFolder);

using FileStream fileStream = new FileStream(ConfigurationFile, FileMode.Create, FileAccess.Write);
using BinaryWriter writer = new BinaryWriter(fileStream);
using FileStream fileStream = new(ConfigurationFile, FileMode.Create, FileAccess.Write);
using BinaryWriter writer = new(fileStream);

writer.Write(Magic);
writer.Write(Revision);
Expand Down Expand Up @@ -174,15 +174,15 @@ public static void Save(Configuration configuration)

public static Configuration Load()
{
Configuration configuration = new Configuration();
Configuration configuration = new();

if (!Directory.Exists(ConfigurationFolder)) Directory.CreateDirectory(ConfigurationFolder);

using FileStream fileStream = new FileStream(ConfigurationFile, FileMode.OpenOrCreate, FileAccess.Read);
using FileStream fileStream = new(ConfigurationFile, FileMode.OpenOrCreate, FileAccess.Read);

if (fileStream.Length == 0) return configuration;

using BinaryReader reader = new BinaryReader(fileStream);
using BinaryReader reader = new(fileStream);

if (!reader.ReadBytes(Magic.Length).SequenceEqual(Magic))
{
Expand Down
4 changes: 2 additions & 2 deletions WpfKenBurns/ConfigurationWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void AddFolderButton_Click(object sender, RoutedEventArgs e)

try
{
VistaFolderBrowserDialog dialog = new VistaFolderBrowserDialog();
VistaFolderBrowserDialog dialog = new();

if (dialog.ShowDialog() != true || string.IsNullOrWhiteSpace(dialog.SelectedPath)) return;

Expand Down Expand Up @@ -144,7 +144,7 @@ private void AddFileButton_Click(object sender, RoutedEventArgs e)

try
{
VistaOpenFileDialog dialog = new VistaOpenFileDialog();
VistaOpenFileDialog dialog = new();

dialog.Filter = "Executable Files (*.exe)|*.exe";

Expand Down
2 changes: 1 addition & 1 deletion WpfKenBurns/Converters/FloatToPercentConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public object Convert(object value, Type targetType, object parameter, CultureIn

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (float.TryParse(value as string, out var result))
if (float.TryParse(value as string, out float result))
{
return result / 100;
}
Expand Down
2 changes: 1 addition & 1 deletion WpfKenBurns/Converters/FloatToSecondsConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public object Convert(object value, Type targetType, object parameter, CultureIn

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (float.TryParse(value as string, out var result))
if (float.TryParse(value as string, out float result))
{
return result;
}
Expand Down
35 changes: 18 additions & 17 deletions WpfKenBurns/WindowSynchronizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ namespace WpfKenBurns
{
public class WindowSynchronizer
{
private readonly List<ScreensaverWindow> windows = new List<ScreensaverWindow>();
private readonly Random random = new Random();
private readonly List<ScreensaverWindow> windows = new();
private readonly Random random = new();

private readonly IntPtr handle;

private Configuration? configuration;
private bool running = false;
private CancellationTokenSource? cancellationTokenSource;
private Task? task;
private IntPtr handle;
private bool resetting = false;

private RandomizedEnumerator<string>? fileEnumerator;
Expand Down Expand Up @@ -78,7 +79,7 @@ public void Start()
}
}

List<string> files = new List<string>();
List<string> files = new();

foreach (ScreensaverImageFolder folder in configuration.Folders)
{
Expand All @@ -90,7 +91,7 @@ public void Start()

if (handle != IntPtr.Zero)
{
ScreensaverWindow window = new ScreensaverWindow(configuration, handle);
ScreensaverWindow window = new(configuration, handle);
window.Show();
windows.Add(window);
RestartTask();
Expand Down Expand Up @@ -124,7 +125,7 @@ private void EnumerateMonitors()

NativeMethods.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, (IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData) =>
{
ScreensaverWindow window = new ScreensaverWindow(configuration, lprcMonitor);
ScreensaverWindow window = new(configuration, lprcMonitor);
window.Show();
windows.Add(window);
window.DisplayChanged += OnDisplayChanged;
Expand Down Expand Up @@ -176,18 +177,18 @@ private void Worker()
BitmapImage source = GetImage();

double scale = Math.Max(window.ActualWidth / source.PixelWidth, window.ActualHeight / source.PixelHeight);
Size size = new Size(source.PixelWidth * scale, source.PixelHeight * scale);
Size size = new(source.PixelWidth * scale, source.PixelHeight * scale);

Image image = uiDispatcher.Invoke(() => window.CreateImage(source, size));

Panel container = (Panel) image.Parent;

var resetEvent = new ManualResetEventSlim(false);
ManualResetEventSlim resetEvent = new(false);
storyboards[i] = SetupAnimation(container, image, size, resetEvent);
resetEvents[i] = resetEvent;
}

foreach (var previousResetEvent in previousResetEvents)
foreach (ManualResetEventSlim previousResetEvent in previousResetEvents)
{
previousResetEvent?.Wait(cancellationTokenSource.Token);
}
Expand Down Expand Up @@ -223,9 +224,9 @@ private BitmapImage GetImage()

if (fileName == default) return new BitmapImage();

FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
FileStream fileStream = new(fileName, FileMode.Open, FileAccess.Read);

BitmapImage image = new BitmapImage();
BitmapImage image = new();

image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
Expand Down Expand Up @@ -253,10 +254,10 @@ private Storyboard SetupAnimation(Panel container, Image image, Size imageSize,
double width = container.ActualWidth;
double height = container.ActualHeight;

ThicknessAnimation marginAnimation = new ThicknessAnimation();
DoubleAnimation widthAnimation = new DoubleAnimation();
DoubleAnimation heightAnimation = new DoubleAnimation();
DoubleAnimation opacityAnimation = new DoubleAnimation();
ThicknessAnimation marginAnimation = new();
DoubleAnimation widthAnimation = new();
DoubleAnimation heightAnimation = new();
DoubleAnimation opacityAnimation = new();

bool zoomDirection = random.Next(2) == 1;
double fromScale = (zoomDirection ? scaleFactor : 1);
Expand Down Expand Up @@ -300,14 +301,14 @@ private Storyboard SetupAnimation(Panel container, Image image, Size imageSize,
Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(Image.HeightProperty));
Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath(Image.OpacityProperty));

Storyboard storyboard = new Storyboard();
Storyboard storyboard = new();

storyboard.Children.Add(marginAnimation);
storyboard.Children.Add(widthAnimation);
storyboard.Children.Add(heightAnimation);
storyboard.Children.Add(opacityAnimation);

storyboard.Duration = new Duration(TimeSpan.FromSeconds(totalDuration));
storyboard.Duration = new(TimeSpan.FromSeconds(totalDuration));

bool nextStarted = false;

Expand Down

0 comments on commit e568d55

Please sign in to comment.