From b7f6158300b1d7f9ac0a659bcb0c1b9a38503228 Mon Sep 17 00:00:00 2001 From: adroslice Date: Sat, 23 Nov 2019 18:40:55 +0100 Subject: [PATCH] Improved Performance - Removed redundant calls by disabling event handling when an operation is created or moved, as well as before the end of construction - Also removed unused and ordered namespaces --- BFR/MainWindow.xaml.cs | 10 ++++++++-- BFR/Operations/Add.cs | 3 +-- BFR/Operations/Number.cs | 4 ++-- BFR/Operations/Operation.cs | 2 +- BFR/Operations/Sort.cs | 1 - 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/BFR/MainWindow.xaml.cs b/BFR/MainWindow.xaml.cs index 196e6d8..05f5ee3 100644 --- a/BFR/MainWindow.xaml.cs +++ b/BFR/MainWindow.xaml.cs @@ -17,6 +17,7 @@ namespace BFR public partial class MainWindow : Window { public readonly AvaloniaProperty[] HandledProperties = new AvaloniaProperty[] { TextBox.TextProperty, ComboBox.SelectedItemProperty, NumericUpDown.ValueProperty, CheckBox.IsCheckedProperty }; + private bool HandleEvents = false; public async Task OpenDirectoryButtonClick() => OpenDirectory(await new OpenFolderDialog() { Directory = WorkingDirectory }.ShowAsync(this)); public void OpenDirectory(string directory) @@ -28,7 +29,7 @@ namespace BFR public void FilterChanged(object sender, AvaloniaPropertyChangedEventArgs e) { - if(HandledProperties.Contains(e.Property)) + if(HandledProperties.Contains(e.Property) && HandleEvents) Filter(); // Bind to PropertyChanged } public void Filter() @@ -43,7 +44,7 @@ namespace BFR public void PreviewChanged(object sender, AvaloniaPropertyChangedEventArgs e) { - if (HandledProperties.Contains(e.Property)) + if (HandledProperties.Contains(e.Property) && HandleEvents) Preview(); // Bind to PropertyChanged } public void Preview() @@ -69,11 +70,13 @@ namespace BFR public void AddOperation() { + HandleEvents = false; if (Operations.Count >= 1) Operations.Insert( SelectedOperation >= 0 ? SelectedOperation + 1 : Operations.Count, OperationTypes[SelectedOperationType].Create()); else Operations.Add(OperationTypes[SelectedOperationType].Create()); + HandleEvents = true; Preview(); } @@ -87,11 +90,13 @@ namespace BFR public void MoveOperation(object sender, SpinEventArgs e) { + HandleEvents = false; if (Operations.Count > 1) if (e.Direction == SpinDirection.Increase && SelectedOperation > 0) Operations.Move(SelectedOperation, SelectedOperation - 1); else if (e.Direction == SpinDirection.Decrease && SelectedOperation < Operations.Count - 1) Operations.Move(SelectedOperation, SelectedOperation + 1); + HandleEvents = true; Preview(); } @@ -122,6 +127,7 @@ namespace BFR AvaloniaXamlLoader.Load(this); DataContext = this; OpenDirectory(Environment.OSVersion.Platform == PlatformID.Win32NT ? @"C:\Users" : @"\home"); + HandleEvents = true; } } } \ No newline at end of file diff --git a/BFR/Operations/Add.cs b/BFR/Operations/Add.cs index ca1b4b6..6c59105 100644 --- a/BFR/Operations/Add.cs +++ b/BFR/Operations/Add.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Xml.Serialization; using System.Linq; diff --git a/BFR/Operations/Number.cs b/BFR/Operations/Number.cs index e24b98b..1098133 100644 --- a/BFR/Operations/Number.cs +++ b/BFR/Operations/Number.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using System.Xml.Serialization; using System.Collections.Generic; @@ -6,7 +7,6 @@ using Avalonia; using BFR.Helpers; using BFR.DataModels; -using System; namespace BFR.Operations { diff --git a/BFR/Operations/Operation.cs b/BFR/Operations/Operation.cs index 7487fcb..6e820a1 100644 --- a/BFR/Operations/Operation.cs +++ b/BFR/Operations/Operation.cs @@ -1,7 +1,7 @@ using System; using System.Linq; -using System.Collections.Generic; using System.Xml.Serialization; +using System.Collections.Generic; using Avalonia; diff --git a/BFR/Operations/Sort.cs b/BFR/Operations/Sort.cs index e30b7c5..4278fa7 100644 --- a/BFR/Operations/Sort.cs +++ b/BFR/Operations/Sort.cs @@ -1,6 +1,5 @@ using System; using System.Linq; -using System.Text; using System.Globalization; using System.Collections.Generic;