diff --git a/osu.Framework/Bindables/BindableList.cs b/osu.Framework/Bindables/BindableList.cs index 2877cf2527..4f770fcef9 100644 --- a/osu.Framework/Bindables/BindableList.cs +++ b/osu.Framework/Bindables/BindableList.cs @@ -76,7 +76,7 @@ private void setIndex(int index, T item, HashSet> appliedInstanc b.setIndex(index, item, appliedInstances); } - notifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, item, lastItem, index)); + CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, item, lastItem, index)); } /// @@ -101,7 +101,7 @@ private void add(T item, HashSet> appliedInstances) b.add(item, appliedInstances); } - notifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, collection.Count - 1)); + CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, collection.Count - 1)); } /// @@ -134,7 +134,7 @@ private void insert(int index, T item, HashSet> appliedInstances b.insert(index, item, appliedInstances); } - notifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index)); + CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index)); } /// @@ -154,7 +154,7 @@ private void clear(HashSet> appliedInstances) return; // Preserve items for subscribers - var clearedItems = collection.ToList(); + List clearedItems = CollectionChanged == null ? null : collection.ToList(); collection.Clear(); @@ -164,7 +164,7 @@ private void clear(HashSet> appliedInstances) b.clear(appliedInstances); } - notifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, clearedItems, 0)); + CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, clearedItems, 0)); } /// @@ -212,7 +212,7 @@ private bool remove(T item, HashSet> appliedInstances) } } - notifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, listItem, index)); + CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, listItem, index)); return true; } @@ -233,12 +233,13 @@ private void removeRange(int index, int count, HashSet> appliedI ensureMutationAllowed(); - var removedItems = collection.GetRange(index, count); + if (count == 0) + return; - collection.RemoveRange(index, count); + // Preserve items for subscribers + List removedItems = CollectionChanged == null ? null : collection.GetRange(index, count); - if (removedItems.Count == 0) - return; + collection.RemoveRange(index, count); if (bindings != null) { @@ -250,7 +251,7 @@ private void removeRange(int index, int count, HashSet> appliedI } } - notifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, removedItems, index)); + CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, removedItems, index)); } /// @@ -277,7 +278,7 @@ private void removeAt(int index, HashSet> appliedInstances) b.removeAt(index, appliedInstances); } - notifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index)); + CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index)); } /// @@ -294,12 +295,13 @@ private int removeAll(Predicate match, HashSet> appliedInstan ensureMutationAllowed(); - var removed = collection.FindAll(match); - - if (removed.Count == 0) return removed.Count; + // Preserve items for subscribers + List removedItems = CollectionChanged == null ? null : collection.FindAll(match); // RemoveAll is internally optimised - collection.RemoveAll(match); + int countRemoved = collection.RemoveAll(match); + if (countRemoved == 0) + return 0; if (bindings != null) { @@ -307,9 +309,8 @@ private int removeAll(Predicate match, HashSet> appliedInstan b.removeAll(match, appliedInstances); } - notifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, removed)); - - return removed.Count; + CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, removedItems)); + return countRemoved; } /// @@ -319,21 +320,22 @@ private int removeAll(Predicate match, HashSet> appliedInstan /// The count of items to be removed. /// The items to replace the removed items with. public void ReplaceRange(int index, int count, IEnumerable newItems) - => replaceRange(index, count, newItems as IList ?? newItems.ToArray(), new HashSet>()); + => replaceRange(index, count, newItems as ICollection ?? newItems.ToArray(), new HashSet>()); - private void replaceRange(int index, int count, IList newItems, HashSet> appliedInstances) + private void replaceRange(int index, int count, ICollection newItems, HashSet> appliedInstances) { if (checkAlreadyApplied(appliedInstances)) return; ensureMutationAllowed(); - var removedItems = collection.GetRange(index, count); + if (count == 0 && newItems.Count == 0) + return; - collection.RemoveRange(index, count); - collection.InsertRange(index, newItems.Cast()); + // Preserve items for subscribers + List removedItems = CollectionChanged == null ? null : collection.GetRange(index, count); - if (removedItems.Count == 0 && newItems.Count == 0) - return; + collection.RemoveRange(index, count); + collection.InsertRange(index, newItems); if (bindings != null) { @@ -345,7 +347,7 @@ private void replaceRange(int index, int count, IList newItems, HashSet @@ -542,15 +544,15 @@ public virtual void UnbindFrom(IUnbindable them) /// The collection whose items should be added to this collection. /// Thrown if this collection is public void AddRange(IEnumerable items) - => addRange(items as IList ?? items.ToArray(), new HashSet>()); + => addRange(items as ICollection ?? items.ToArray(), new HashSet>()); - private void addRange(IList items, HashSet> appliedInstances) + private void addRange(ICollection items, HashSet> appliedInstances) { if (checkAlreadyApplied(appliedInstances)) return; ensureMutationAllowed(); - collection.AddRange(items.Cast()); + collection.AddRange(items); if (bindings != null) { @@ -558,7 +560,7 @@ private void addRange(IList items, HashSet> appliedInstances) b.addRange(items, appliedInstances); } - notifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, items, collection.Count - items.Count)); + CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, (IList)items, collection.Count - items.Count)); } /// @@ -586,7 +588,7 @@ private void move(int oldIndex, int newIndex, HashSet> appliedIn b.move(oldIndex, newIndex, appliedInstances); } - notifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Move, item, newIndex, oldIndex)); + CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Move, item, newIndex, oldIndex)); } void IBindable.BindTo(IBindable them) @@ -680,8 +682,6 @@ private void addWeakReference(WeakReference> weakReference) #endregion IEnumerable - private void notifyCollectionChanged(NotifyCollectionChangedEventArgs args) => CollectionChanged?.Invoke(this, args); - private void ensureMutationAllowed() { if (Disabled)