// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. #nullable enable using System; using System.Collections.Generic; namespace osu.Framework.Bindables { /// /// Arguments for a dictionary's CollectionChanged event. /// /// The type of keys in the dictionary. /// The type of values in the dictionary. public class NotifyDictionaryChangedEventArgs : EventArgs where TKey : notnull { /// /// The action that caused the event. /// public readonly NotifyDictionaryChangedAction Action; /// /// All newly-added items. /// public readonly ICollection>? NewItems; /// /// All removed items. /// public readonly ICollection>? OldItems; /// /// Creates a new that describes an add or remove event. /// /// The action. /// The item affected. public NotifyDictionaryChangedEventArgs(NotifyDictionaryChangedAction action, KeyValuePair item) : this(action, new[] { item }) { } /// /// Creates a new that describes an add or remove event. /// /// The action. /// The items affected. public NotifyDictionaryChangedEventArgs(NotifyDictionaryChangedAction action, ICollection> items) { Action = action; switch (action) { case NotifyDictionaryChangedAction.Add: NewItems = items; break; case NotifyDictionaryChangedAction.Remove: OldItems = items; break; default: throw new ArgumentException($"Action must be {nameof(NotifyDictionaryChangedAction.Add)} or {nameof(NotifyDictionaryChangedAction.Remove)}.", nameof(action)); } } /// /// Creates a new that describes a replacement event. /// /// The new (added) item. /// The old (removed) item. public NotifyDictionaryChangedEventArgs(KeyValuePair newItem, KeyValuePair oldItem) { Action = NotifyDictionaryChangedAction.Replace; NewItems = new[] { newItem }; OldItems = new[] { oldItem }; } } /// /// The delegate to use for handlers that receive the CollectionChanged event. /// public delegate void NotifyDictionaryChangedEventHandler(object? sender, NotifyDictionaryChangedEventArgs e) where TKey : notnull; public enum NotifyDictionaryChangedAction { /// /// One or more items were added to the dictionary. /// Add, /// /// One or more items were removed from the dictionary. /// Remove, /// /// One or more items were replaced in the dictionary. /// Replace, } }