Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions LemonUI/Menus/ItemModifiedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace LemonUI.Menus
{
/// <summary>
/// Represents the addition or removal of an item in a list.
/// </summary>
/// <typeparam name="T">The type of object that was modified.</typeparam>
public class ItemModifiedEventArgs<T>
{
#region Properties

/// <summary>
/// The item that was added or removed.
/// </summary>
public T Item { get; }

/// <summary>
/// The position where the item was added or removed.
/// </summary>
public int Position { get; }

#endregion

#region Constructors

/// <summary>
/// Creates a new instance of <see cref="ItemModifiedEventArgs{T}"/>.
/// </summary>
/// <param name="item">The item that was added or removed.</param>
/// <param name="position">The position where the item was added or removed.</param>
public ItemModifiedEventArgs(T item, int position)
{
Item = item;
Position = position;
}

#endregion
}
}
10 changes: 10 additions & 0 deletions LemonUI/Menus/ItemModifiedEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace LemonUI.Menus
{
/// <summary>
/// Represents the method that is called when an item is added to or removed from a List Item.
/// </summary>
/// <typeparam name="T">The type of item that was modified.</typeparam>
/// <param name="sender">The source of the event.</param>
/// <param name="e">A <see cref="ItemModifiedEventArgs{T}"/> with the information of the modified item.</param>
public delegate void ItemModifiedEventHandler<T>(object sender, ItemModifiedEventArgs<T> e);
}
33 changes: 30 additions & 3 deletions LemonUI/Menus/NativeListItem{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ public List<T> Items
/// Event triggered when the selected item is changed.
/// </summary>
public event ItemChangedEventHandler<T> ItemChanged;

/// <summary>
/// Event triggered when an item is added to the list.
/// </summary>
public event ItemModifiedEventHandler<T> ItemAdded;

/// <summary>
/// Event triggered when an item is removed from the list.
/// </summary>
public event ItemModifiedEventHandler<T> ItemRemoved;

#endregion

Expand Down Expand Up @@ -202,18 +212,30 @@ public void Add(int position, T item)
{
UpdateIndex();
}

// Trigger the ItemAdded event
ItemAdded?.Invoke(this, new ItemModifiedEventArgs<T>(item, position));
}

/// <summary>
/// Removes a specific <typeparamref name="T" />.
/// </summary>
/// <param name="item">The <typeparamref name="T" /> to remove.</param>
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<T>(removedItem, position));
}
}
}

/// <summary>
/// Removes a <typeparamref name="T" /> at a specific location.
/// </summary>
Expand All @@ -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<T>(removedItem, position));
}

/// <summary>
/// Removes all of the items that match the <paramref name="pred"/>.
/// </summary>
Expand Down