From cf8921a940bd1e9d587af3b65731dfedfed613df Mon Sep 17 00:00:00 2001 From: Harv <86970698+Hxrv3y@users.noreply.github.com> Date: Wed, 28 May 2025 21:35:51 +0100 Subject: [PATCH] Add NativeListItem.ItemAdded --- LemonUI/Menus/ItemModifiedEventArgs.cs | 38 +++++++++++++++++++++++ LemonUI/Menus/ItemModifiedEventHandler.cs | 10 ++++++ LemonUI/Menus/NativeListItem{T}.cs | 33 ++++++++++++++++++-- 3 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 LemonUI/Menus/ItemModifiedEventArgs.cs create mode 100644 LemonUI/Menus/ItemModifiedEventHandler.cs diff --git a/LemonUI/Menus/ItemModifiedEventArgs.cs b/LemonUI/Menus/ItemModifiedEventArgs.cs new file mode 100644 index 0000000..9cbcaea --- /dev/null +++ b/LemonUI/Menus/ItemModifiedEventArgs.cs @@ -0,0 +1,38 @@ +namespace LemonUI.Menus +{ + /// + /// Represents the addition or removal of an item in a list. + /// + /// The type of object that was modified. + public class ItemModifiedEventArgs + { + #region Properties + + /// + /// The item that was added or removed. + /// + public T Item { get; } + + /// + /// The position where the item was added or removed. + /// + public int Position { get; } + + #endregion + + #region Constructors + + /// + /// Creates a new instance of . + /// + /// The item that was added or removed. + /// The position where the item was added or removed. + public ItemModifiedEventArgs(T item, int position) + { + Item = item; + Position = position; + } + + #endregion + } +} \ No newline at end of file diff --git a/LemonUI/Menus/ItemModifiedEventHandler.cs b/LemonUI/Menus/ItemModifiedEventHandler.cs new file mode 100644 index 0000000..78d4a39 --- /dev/null +++ b/LemonUI/Menus/ItemModifiedEventHandler.cs @@ -0,0 +1,10 @@ +namespace LemonUI.Menus +{ + /// + /// Represents the method that is called when an item is added to or removed from a List Item. + /// + /// The type of item that was modified. + /// The source of the event. + /// A with the information of the modified item. + public delegate void ItemModifiedEventHandler(object sender, ItemModifiedEventArgs e); +} \ No newline at end of file diff --git a/LemonUI/Menus/NativeListItem{T}.cs b/LemonUI/Menus/NativeListItem{T}.cs index 00853ea..11daa70 100644 --- a/LemonUI/Menus/NativeListItem{T}.cs +++ b/LemonUI/Menus/NativeListItem{T}.cs @@ -103,6 +103,16 @@ public List Items /// Event triggered when the selected item is changed. /// public event ItemChangedEventHandler ItemChanged; + + /// + /// Event triggered when an item is added to the list. + /// + public event ItemModifiedEventHandler ItemAdded; + + /// + /// Event triggered when an item is removed from the list. + /// + public event ItemModifiedEventHandler ItemRemoved; #endregion @@ -202,18 +212,30 @@ public void Add(int position, T item) { UpdateIndex(); } + + // Trigger the ItemAdded event + ItemAdded?.Invoke(this, new ItemModifiedEventArgs(item, position)); } + /// /// Removes a specific . /// /// The to remove. public void Remove(T item) { - if (items.Remove(item)) + int position = items.IndexOf(item); + if (position >= 0) { - FixIndexIfRequired(); + T removedItem = items[position]; + if (items.Remove(item)) + { + FixIndexIfRequired(); + // Trigger the ItemRemoved event + ItemRemoved?.Invoke(this, new ItemModifiedEventArgs(removedItem, position)); + } } } + /// /// Removes a at a specific location. /// @@ -224,11 +246,16 @@ public void RemoveAt(int position) { return; } - + + T removedItem = items[position]; items.RemoveAt(position); FixIndexIfRequired(); UpdateIndex(); + + // Trigger the ItemRemoved event + ItemRemoved?.Invoke(this, new ItemModifiedEventArgs(removedItem, position)); } + /// /// Removes all of the items that match the . ///