Skip to content

Commit

Permalink
Switch to C# 9
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoco007 committed Jan 9, 2021
1 parent 91195a8 commit eb5929d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 28 deletions.
8 changes: 2 additions & 6 deletions WpfKenBurns/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public ObservableCollection<string> ProgramDenylist
}
}

public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangedEventHandler? PropertyChanged;

private ObservableCollection<ScreensaverImageFolder> folders = new ObservableCollection<ScreensaverImageFolder>();
private float duration = 7;
Expand Down Expand Up @@ -210,11 +210,7 @@ public static Configuration Load()

for (int i = 0; i < count; i++)
{
configuration.Folders.Add(new ScreensaverImageFolder
{
Path = reader.ReadString(),
Recursive = reader.ReadBoolean()
});
configuration.Folders.Add(new ScreensaverImageFolder(reader.ReadString(), reader.ReadBoolean()));
}

count = reader.ReadInt32();
Expand Down
22 changes: 14 additions & 8 deletions WpfKenBurns/ConfigurationWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace WpfKenBurns
/// </summary>
public partial class ConfigurationWindow : Window
{
private Configuration configuration;
private Configuration? configuration;
private bool changed;

public ConfigurationWindow()
Expand Down Expand Up @@ -57,17 +57,15 @@ private void Window_Loaded(object sender, RoutedEventArgs e)

private void AddFolderButton_Click(object sender, RoutedEventArgs e)
{
if (configuration == null) return;

try
{
VistaFolderBrowserDialog dialog = new VistaFolderBrowserDialog();

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

configuration.Folders.Add(new ScreensaverImageFolder
{
Path = dialog.SelectedPath,
Recursive = false
});
configuration.Folders.Add(new ScreensaverImageFolder(dialog.SelectedPath, false));
}
catch (Exception ex)
{
Expand All @@ -78,6 +76,8 @@ private void AddFolderButton_Click(object sender, RoutedEventArgs e)

private void SaveButton_Click(object sender, RoutedEventArgs e)
{
if (configuration == null) return;

try
{
Configuration.Save(configuration);
Expand Down Expand Up @@ -115,6 +115,8 @@ private void CloseButton_Click(object sender, RoutedEventArgs e)

private void RemoveFolderButton_Click(object sender, RoutedEventArgs e)
{
if (configuration == null) return;

if (imageSourcesListView.SelectedIndex >= 0)
{
configuration.Folders.RemoveAt(imageSourcesListView.SelectedIndex);
Expand All @@ -128,13 +130,15 @@ private void FoldersListView_SelectionChanged(object sender, System.Windows.Cont

private void AddFileButton_Click(object sender, RoutedEventArgs e)
{
if (configuration == null) return;

try
{
VistaOpenFileDialog dialog = new VistaOpenFileDialog();

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

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

configuration.ProgramDenylist.Add(dialog.FileName);
}
Expand All @@ -147,6 +151,8 @@ private void AddFileButton_Click(object sender, RoutedEventArgs e)

private void RemoveFileButton_Click(object sender, RoutedEventArgs e)
{
if (configuration == null) return;

if (programDenylistListView.SelectedIndex >= 0)
{
configuration.ProgramDenylist.RemoveAt(programDenylistListView.SelectedIndex);
Expand Down
4 changes: 2 additions & 2 deletions WpfKenBurns/RandomizedEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace WpfKenBurns
{
class RandomizedEnumerator<T> : IEnumerator<T>
class RandomizedEnumerator<T> : IEnumerator<T> where T : notnull
{
private T[] items;
private int currentIndex = -1;
Expand All @@ -32,7 +32,7 @@ public RandomizedEnumerator(IEnumerable<T> items)
Shuffle();
}

public T Current => currentIndex >= 0 && currentIndex < items.Length ? items[currentIndex] : default;
public T Current => currentIndex >= 0 && currentIndex < items.Length ? items[currentIndex] : throw new InvalidOperationException();

object IEnumerator.Current => Current;

Expand Down
12 changes: 9 additions & 3 deletions WpfKenBurns/ScreensaverImageFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@

namespace WpfKenBurns
{
public class ScreensaverImageFolder
public readonly struct ScreensaverImageFolder
{
public string Path { get; set; }
public bool Recursive { get; set; }
public readonly string Path;
public readonly bool Recursive;

public ScreensaverImageFolder(string path, bool recursive)
{
Path = path;
Recursive = recursive;
}
}
}
2 changes: 1 addition & 1 deletion WpfKenBurns/ScreensaverWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace WpfKenBurns
{
public partial class ScreensaverWindow : Window
{
public event Action DisplayChanged;
public event Action? DisplayChanged;

private IntPtr windowHandle;
private Configuration configuration;
Expand Down
25 changes: 17 additions & 8 deletions WpfKenBurns/WindowSynchronizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ namespace WpfKenBurns
{
public class WindowSynchronizer
{
private Configuration configuration;
private List<ScreensaverWindow> windows = new List<ScreensaverWindow>();
private Random random = new Random();
private readonly List<ScreensaverWindow> windows = new List<ScreensaverWindow>();
private readonly Random random = new Random();

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

private RandomizedEnumerator<string> fileEnumerator;
private RandomizedEnumerator<string>? fileEnumerator;

public WindowSynchronizer() { }

Expand All @@ -62,6 +63,7 @@ public void Start()
Debug.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
MessageBox.Show("Failed to load configuration: " + ex.Message);
Application.Current.Shutdown();
return;
}

foreach (string filePath in configuration.ProgramDenylist)
Expand Down Expand Up @@ -109,6 +111,7 @@ public void Start()
private void EnumerateMonitors()
{
if (resetting) return;
if (configuration == null) return;

resetting = true;

Expand Down Expand Up @@ -141,8 +144,8 @@ private void OnDisplayChanged()
private void RestartTask()
{
cancellationTokenSource?.Cancel();
cancellationTokenSource = null;
task?.Wait();
cancellationTokenSource = new CancellationTokenSource();
task = Task.Run(Worker);
running = true;
}
Expand All @@ -160,6 +163,8 @@ private void Worker()

try
{
if (cancellationTokenSource == null) cancellationTokenSource = new CancellationTokenSource();

while (!cancellationTokenSource.IsCancellationRequested)
{
var storyboards = new Storyboard[windows.Count];
Expand Down Expand Up @@ -206,6 +211,8 @@ private void Worker()

private BitmapImage GetImage()
{
if (fileEnumerator == null) return new BitmapImage();

if (!fileEnumerator.MoveNext())
{
fileEnumerator.Reset();
Expand All @@ -214,7 +221,7 @@ private BitmapImage GetImage()

string fileName = fileEnumerator.Current;

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

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

Expand All @@ -235,6 +242,8 @@ private BitmapImage GetImage()

private Storyboard SetupAnimation(Panel container, Image image, Size imageSize, ManualResetEventSlim resetEvent)
{
if (configuration == null) return new Storyboard();

double duration = configuration.Duration;
double movementFactor = configuration.MovementFactor + 1;
double scaleFactor = configuration.ScaleFactor + 1;
Expand Down
3 changes: 3 additions & 0 deletions WpfKenBurns/WpfKenBurns.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<TargetFramework>net472</TargetFramework>
<UseWpf>true</UseWpf>
<OutputType>WinExe</OutputType>
<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
<LangVersion>9</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Fody" Version="6.3.0">
Expand Down

0 comments on commit eb5929d

Please sign in to comment.