A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4#nullable enable
5
6using System;
7using System.Collections.Generic;
8
9namespace osu.Framework.Bindables
10{
11 /// <summary>
12 /// Arguments for a dictionary's CollectionChanged event.
13 /// </summary>
14 /// <typeparam name="TKey">The type of keys in the dictionary.</typeparam>
15 /// <typeparam name="TValue">The type of values in the dictionary.</typeparam>
16 public class NotifyDictionaryChangedEventArgs<TKey, TValue> : EventArgs
17 where TKey : notnull
18 {
19 /// <summary>
20 /// The action that caused the event.
21 /// </summary>
22 public readonly NotifyDictionaryChangedAction Action;
23
24 /// <summary>
25 /// All newly-added items.
26 /// </summary>
27 public readonly ICollection<KeyValuePair<TKey, TValue>>? NewItems;
28
29 /// <summary>
30 /// All removed items.
31 /// </summary>
32 public readonly ICollection<KeyValuePair<TKey, TValue>>? OldItems;
33
34 /// <summary>
35 /// Creates a new <see cref="NotifyDictionaryChangedEventArgs{TKey,TValue}"/> that describes an add or remove event.
36 /// </summary>
37 /// <param name="action">The action.</param>
38 /// <param name="item">The item affected.</param>
39 public NotifyDictionaryChangedEventArgs(NotifyDictionaryChangedAction action, KeyValuePair<TKey, TValue> item)
40 : this(action, new[] { item })
41 {
42 }
43
44 /// <summary>
45 /// Creates a new <see cref="NotifyDictionaryChangedEventArgs{TKey,TValue}"/> that describes an add or remove event.
46 /// </summary>
47 /// <param name="action">The action.</param>
48 /// <param name="items">The items affected.</param>
49 public NotifyDictionaryChangedEventArgs(NotifyDictionaryChangedAction action, ICollection<KeyValuePair<TKey, TValue>> items)
50 {
51 Action = action;
52
53 switch (action)
54 {
55 case NotifyDictionaryChangedAction.Add:
56 NewItems = items;
57 break;
58
59 case NotifyDictionaryChangedAction.Remove:
60 OldItems = items;
61 break;
62
63 default:
64 throw new ArgumentException($"Action must be {nameof(NotifyDictionaryChangedAction.Add)} or {nameof(NotifyDictionaryChangedAction.Remove)}.", nameof(action));
65 }
66 }
67
68 /// <summary>
69 /// Creates a new <see cref="NotifyDictionaryChangedEventArgs{TKey,TValue}"/> that describes a replacement event.
70 /// </summary>
71 /// <param name="newItem">The new (added) item.</param>
72 /// <param name="oldItem">The old (removed) item.</param>
73 public NotifyDictionaryChangedEventArgs(KeyValuePair<TKey, TValue> newItem, KeyValuePair<TKey, TValue> oldItem)
74 {
75 Action = NotifyDictionaryChangedAction.Replace;
76 NewItems = new[] { newItem };
77 OldItems = new[] { oldItem };
78 }
79 }
80
81 /// <summary>
82 /// The delegate to use for handlers that receive the CollectionChanged event.
83 /// </summary>
84 public delegate void NotifyDictionaryChangedEventHandler<TKey, TValue>(object? sender, NotifyDictionaryChangedEventArgs<TKey, TValue> e)
85 where TKey : notnull;
86
87 public enum NotifyDictionaryChangedAction
88 {
89 /// <summary>
90 /// One or more items were added to the dictionary.
91 /// </summary>
92 Add,
93
94 /// <summary>
95 /// One or more items were removed from the dictionary.
96 /// </summary>
97 Remove,
98
99 /// <summary>
100 /// One or more items were replaced in the dictionary.
101 /// </summary>
102 Replace,
103 }
104}