Skip to content

Commit 9d126df

Browse files
committed
Use List replace ObservableCollection to have control toward Capacity
1 parent 1aa119d commit 9d126df

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

Flow.Launcher/ViewModel/ResultsViewModel.cs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -239,49 +239,50 @@ private static void FormattedTextPropertyChanged(DependencyObject d, DependencyP
239239
}
240240
#endregion
241241

242-
public class ResultCollection : ObservableCollection<ResultViewModel>
242+
public class ResultCollection : List<ResultViewModel>, INotifyCollectionChanged
243243
{
244244
private long editTime = 0;
245245

246-
private bool _suppressNotifying = false;
247-
248246
private CancellationToken _token;
249247

250-
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
248+
public event NotifyCollectionChangedEventHandler CollectionChanged;
249+
250+
protected void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
251251
{
252-
if (!_suppressNotifying)
253-
{
254-
base.OnCollectionChanged(e);
255-
}
252+
CollectionChanged(this, e);
256253
}
257254

258-
public void BulkAddRange(IEnumerable<ResultViewModel> resultViews)
255+
public void BulkAddAll(List<ResultViewModel> resultViews)
259256
{
260-
// suppress notifying before adding all element
261-
_suppressNotifying = true;
262-
foreach (var item in resultViews)
263-
{
264-
Add(item);
265-
}
266-
_suppressNotifying = false;
267-
// manually update event
268-
// wpf use directx / double buffered already, so just reset all won't cause ui flickering
257+
AddRange(resultViews);
258+
259+
// can return because the list will be cleared next time updated, which include a reset event
269260
if (_token.IsCancellationRequested)
270261
return;
262+
263+
// manually update event
264+
// wpf use directx / double buffered already, so just reset all won't cause ui flickering
271265
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
272266
}
273-
public void AddRange(IEnumerable<ResultViewModel> Items)
267+
private void AddAll(List<ResultViewModel> Items)
274268
{
275269
foreach (var item in Items)
276270
{
277271
if (_token.IsCancellationRequested)
278272
return;
279273
Add(item);
274+
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
280275
}
281276
}
282-
public void RemoveAll()
277+
public void RemoveAll(int Capacity = 512)
283278
{
284-
ClearItems();
279+
Clear();
280+
if (this.Capacity > 8000)
281+
{
282+
this.Capacity = Capacity;
283+
284+
}
285+
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
285286
}
286287

287288
/// <summary>
@@ -296,15 +297,19 @@ public void Update(List<ResultViewModel> newItems, CancellationToken token = def
296297

297298
if (editTime < 10 || newItems.Count < 30)
298299
{
299-
if (Count != 0) ClearItems();
300-
AddRange(newItems);
300+
if (Count != 0) RemoveAll(newItems.Count);
301+
AddAll(newItems);
301302
editTime++;
302303
return;
303304
}
304305
else
305306
{
306307
Clear();
307-
BulkAddRange(newItems);
308+
BulkAddAll(newItems);
309+
if (Capacity > 8000 && newItems.Count < 3000)
310+
{
311+
Capacity = newItems.Count;
312+
}
308313
editTime++;
309314
}
310315
}

0 commit comments

Comments
 (0)