From e568d55ef794967a04b6a73ca5abcd08fa7f68f4 Mon Sep 17 00:00:00 2001 From: Nicolas Gnyra Date: Sat, 25 Sep 2021 07:30:46 -0400 Subject: [PATCH] Clean up --- WpfKenBurns/Configuration.cs | 14 ++++---- WpfKenBurns/ConfigurationWindow.xaml.cs | 4 +-- .../Converters/FloatToPercentConverter.cs | 2 +- .../Converters/FloatToSecondsConverter.cs | 2 +- WpfKenBurns/WindowSynchronizer.cs | 35 ++++++++++--------- 5 files changed, 29 insertions(+), 28 deletions(-) diff --git a/WpfKenBurns/Configuration.cs b/WpfKenBurns/Configuration.cs index 558346f..ac4458d 100644 --- a/WpfKenBurns/Configuration.cs +++ b/WpfKenBurns/Configuration.cs @@ -130,21 +130,21 @@ public ObservableCollection ProgramDenylist public event PropertyChangedEventHandler? PropertyChanged; - private ObservableCollection folders = new ObservableCollection(); + private ObservableCollection 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 programDenylist = new ObservableCollection(); + private ObservableCollection 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); @@ -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)) { diff --git a/WpfKenBurns/ConfigurationWindow.xaml.cs b/WpfKenBurns/ConfigurationWindow.xaml.cs index cc9e071..588ca58 100644 --- a/WpfKenBurns/ConfigurationWindow.xaml.cs +++ b/WpfKenBurns/ConfigurationWindow.xaml.cs @@ -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; @@ -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"; diff --git a/WpfKenBurns/Converters/FloatToPercentConverter.cs b/WpfKenBurns/Converters/FloatToPercentConverter.cs index 4ff310b..c59f525 100644 --- a/WpfKenBurns/Converters/FloatToPercentConverter.cs +++ b/WpfKenBurns/Converters/FloatToPercentConverter.cs @@ -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; } diff --git a/WpfKenBurns/Converters/FloatToSecondsConverter.cs b/WpfKenBurns/Converters/FloatToSecondsConverter.cs index f90ad53..f52b5a5 100644 --- a/WpfKenBurns/Converters/FloatToSecondsConverter.cs +++ b/WpfKenBurns/Converters/FloatToSecondsConverter.cs @@ -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; } diff --git a/WpfKenBurns/WindowSynchronizer.cs b/WpfKenBurns/WindowSynchronizer.cs index e7145ab..e6f72d6 100644 --- a/WpfKenBurns/WindowSynchronizer.cs +++ b/WpfKenBurns/WindowSynchronizer.cs @@ -31,14 +31,15 @@ namespace WpfKenBurns { public class WindowSynchronizer { - private readonly List windows = new List(); - private readonly Random random = new Random(); + private readonly List 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? fileEnumerator; @@ -78,7 +79,7 @@ public void Start() } } - List files = new List(); + List files = new(); foreach (ScreensaverImageFolder folder in configuration.Folders) { @@ -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(); @@ -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; @@ -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); } @@ -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; @@ -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); @@ -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;