From 2b423d9d80b3c6ced13c78feeec7d867eb3e3b8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Tue, 17 Nov 2020 16:46:36 +0800 Subject: [PATCH] Use single add notify for each element when the new Item is small or the collection hasn't been buffer a lot. This seems solve the startup stunt and potentially make render faster. (Need Testing) --- Flow.Launcher/ViewModel/ResultsViewModel.cs | 47 ++++++++++++++++----- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/Flow.Launcher/ViewModel/ResultsViewModel.cs b/Flow.Launcher/ViewModel/ResultsViewModel.cs index cce532965..3a4b07fd6 100644 --- a/Flow.Launcher/ViewModel/ResultsViewModel.cs +++ b/Flow.Launcher/ViewModel/ResultsViewModel.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; +using System.ComponentModel; +using System.Configuration; using System.Linq; using System.Threading; using System.Windows; @@ -137,6 +139,7 @@ namespace Flow.Launcher.ViewModel public void AddResults(List newRawResults, string resultId) { var newResults = NewResults(newRawResults, resultId); + lock (_collectionLock) { // https://social.msdn.microsoft.com/Forums/vstudio/en-US/5ff71969-f183-4744-909d-50f7cd414954/binding-a-tabcontrols-selectedindex-not-working?forum=wpf @@ -258,30 +261,38 @@ namespace Flow.Launcher.ViewModel } #endregion - public class ResultCollection : ObservableCollection, INotifyCollectionChanged + public class ResultCollection : ObservableCollection { private bool _suppressNotification = false; + + private long editTime = 0; + + // https://peteohanlon.wordpress.com/2008/10/22/bulk-loading-in-observablecollection/ protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { if (!_suppressNotification) base.OnCollectionChanged(e); } - public override event NotifyCollectionChangedEventHandler CollectionChanged; - - // https://peteohanlon.wordpress.com/2008/10/22/bulk-loading-in-observablecollection/ - public void AddRange(IEnumerable Items) + public void BulkAddRange(IEnumerable resultViews) { _suppressNotification = true; + foreach (var item in resultViews) + { + Add(item); + } + _suppressNotification = false; + OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); + } + public void AddRange(IEnumerable Items) + { foreach (var item in Items) { Add(item); } - // wpf use directx / double buffered already, so just reset all won't cause ui flickering - CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); return; } @@ -296,16 +307,32 @@ namespace Flow.Launcher.ViewModel /// public void Update(List newItems, CancellationToken token) { - Clear(); + if (token.IsCancellationRequested) return; - AddRange(newItems); + Update(newItems); } public void Update(List newItems) { + if (editTime == 0) + { + AddRange(newItems); + editTime++; + return; + } + else if (editTime < 15 || newItems.Count < 50) + { + ClearItems(); + AddRange(newItems); + editTime++; + return; + } Clear(); - AddRange(newItems); + + BulkAddRange(newItems); + editTime++; + } } }