From 98e6a1ab8a203201d08de9a8f1bc5273c81a7789 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Wed, 2 Dec 2020 20:57:23 +0800 Subject: [PATCH] Revert using List instead of observablecollection --- Flow.Launcher/ViewModel/ResultsViewModel.cs | 66 ++++++++++----------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/Flow.Launcher/ViewModel/ResultsViewModel.cs b/Flow.Launcher/ViewModel/ResultsViewModel.cs index d373b0fda..5a9043c23 100644 --- a/Flow.Launcher/ViewModel/ResultsViewModel.cs +++ b/Flow.Launcher/ViewModel/ResultsViewModel.cs @@ -12,7 +12,6 @@ using System.Windows.Data; using System.Windows.Documents; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; -using Microsoft.FSharp.Control; namespace Flow.Launcher.ViewModel { @@ -182,8 +181,6 @@ namespace Flow.Launcher.ViewModel // fix selected index flow Results.Update(newResults, token); - if (token.IsCancellationRequested) - return; SelectedItem = newResults[0]; @@ -269,33 +266,35 @@ namespace Flow.Launcher.ViewModel } #endregion - public class ResultCollection : List, INotifyCollectionChanged + public class ResultCollection : ObservableCollection { - - public event NotifyCollectionChangedEventHandler CollectionChanged; + private bool _suppressNotification = false; private long editTime = 0; - // https://peteohanlon.wordpress.com/2008/10/22/bulk-loading-in-observablecollection/ - protected void OnCollectionChanged(NotifyCollectionChangedEventArgs e) + protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { - if (CollectionChanged != null && CollectionChanged.GetInvocationList().Length == 1) - CollectionChanged.Invoke(this, e); + if (!_suppressNotification) + base.OnCollectionChanged(e); } - public void BulkAddRange(IEnumerable resultViews, CancellationToken? token) + public void BulkAddRange(IEnumerable resultViews) { - AddRange(resultViews); + _suppressNotification = true; + foreach (var item in resultViews) + { + Add(item); + } + _suppressNotification = false; OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); + } - public void AddAll(IEnumerable Items, CancellationToken? token) + public void AddRange(IEnumerable Items) { foreach (var item in Items) { - if (token?.IsCancellationRequested ?? false) return; Add(item); - OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item)); } // wpf use directx / double buffered already, so just reset all won't cause ui flickering @@ -304,8 +303,7 @@ namespace Flow.Launcher.ViewModel } public void RemoveAll() { - Clear(); - OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); + ClearItems(); } @@ -314,27 +312,27 @@ namespace Flow.Launcher.ViewModel /// /// Update the results collection with new results, try to keep identical results /// - /// New Items to add into the list view - /// Cancellation Token - public void Update(List newItems, CancellationToken? token = null) + /// + public void Update(List newItems, CancellationToken token) { - if (token?.IsCancellationRequested ?? false) + + if (token.IsCancellationRequested) return; + Update(newItems); + } + public void Update(List newItems) + { + if (editTime == 0) + { + AddRange(newItems); + editTime++; + return; + } + Clear(); - if (editTime < 5 || newItems.Count < 30) - { - if (Count > 0) RemoveAll(); - if (token?.IsCancellationRequested ?? false) return; - AddAll(newItems, token); - editTime++; - } - else - { - Clear(); - BulkAddRange(newItems, token); - editTime++; - } + BulkAddRange(newItems); + editTime++; } }