@@ -239,49 +239,50 @@ private static void FormattedTextPropertyChanged(DependencyObject d, DependencyP
239
239
}
240
240
#endregion
241
241
242
- public class ResultCollection : ObservableCollection < ResultViewModel >
242
+ public class ResultCollection : List < ResultViewModel > , INotifyCollectionChanged
243
243
{
244
244
private long editTime = 0 ;
245
245
246
- private bool _suppressNotifying = false ;
247
-
248
246
private CancellationToken _token ;
249
247
250
- protected override void OnCollectionChanged ( NotifyCollectionChangedEventArgs e )
248
+ public event NotifyCollectionChangedEventHandler CollectionChanged ;
249
+
250
+ protected void OnCollectionChanged ( NotifyCollectionChangedEventArgs e )
251
251
{
252
- if ( ! _suppressNotifying )
253
- {
254
- base . OnCollectionChanged ( e ) ;
255
- }
252
+ CollectionChanged ( this , e ) ;
256
253
}
257
254
258
- public void BulkAddRange ( IEnumerable < ResultViewModel > resultViews )
255
+ public void BulkAddAll ( List < ResultViewModel > resultViews )
259
256
{
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
269
260
if ( _token . IsCancellationRequested )
270
261
return ;
262
+
263
+ // manually update event
264
+ // wpf use directx / double buffered already, so just reset all won't cause ui flickering
271
265
OnCollectionChanged ( new NotifyCollectionChangedEventArgs ( NotifyCollectionChangedAction . Reset ) ) ;
272
266
}
273
- public void AddRange ( IEnumerable < ResultViewModel > Items )
267
+ private void AddAll ( List < ResultViewModel > Items )
274
268
{
275
269
foreach ( var item in Items )
276
270
{
277
271
if ( _token . IsCancellationRequested )
278
272
return ;
279
273
Add ( item ) ;
274
+ OnCollectionChanged ( new NotifyCollectionChangedEventArgs ( NotifyCollectionChangedAction . Add , item ) ) ;
280
275
}
281
276
}
282
- public void RemoveAll ( )
277
+ public void RemoveAll ( int Capacity = 512 )
283
278
{
284
- ClearItems ( ) ;
279
+ Clear ( ) ;
280
+ if ( this . Capacity > 8000 )
281
+ {
282
+ this . Capacity = Capacity ;
283
+
284
+ }
285
+ OnCollectionChanged ( new NotifyCollectionChangedEventArgs ( NotifyCollectionChangedAction . Reset ) ) ;
285
286
}
286
287
287
288
/// <summary>
@@ -296,15 +297,19 @@ public void Update(List<ResultViewModel> newItems, CancellationToken token = def
296
297
297
298
if ( editTime < 10 || newItems . Count < 30 )
298
299
{
299
- if ( Count != 0 ) ClearItems ( ) ;
300
- AddRange ( newItems ) ;
300
+ if ( Count != 0 ) RemoveAll ( newItems . Count ) ;
301
+ AddAll ( newItems ) ;
301
302
editTime ++ ;
302
303
return ;
303
304
}
304
305
else
305
306
{
306
307
Clear ( ) ;
307
- BulkAddRange ( newItems ) ;
308
+ BulkAddAll ( newItems ) ;
309
+ if ( Capacity > 8000 && newItems . Count < 3000 )
310
+ {
311
+ Capacity = newItems . Count ;
312
+ }
308
313
editTime ++ ;
309
314
}
310
315
}
0 commit comments