Skip to content

Commit

Permalink
Added missing methods in NativeListItem<T> (Fixes #40)
Browse files Browse the repository at this point in the history
  • Loading branch information
justalemon committed Sep 5, 2021
1 parent 87b19ae commit c4e6db0
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions LemonUI/Menus/NativeListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ private void TriggerEvent(int index, Direction direction)
{
ItemChanged?.Invoke(this, new ItemChangedEventArgs<T>(items[index], index, direction));
}
private void FixIndexIfRequired()
{
if (index >= items.Count)
{
index = items.Count - 1;
UpdateIndex();
}
}
/// <summary>
/// Updates the currently selected item based on the index.
/// </summary>
Expand All @@ -230,6 +238,95 @@ private void UpdateIndex()

#region Functions

/// <summary>
/// Adds a <typeparamref name="T" /> into this item.
/// </summary>
/// <param name="item">The <typeparamref name="T" /> to add.</param>
public void Add(T item)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item));
}

if (items.Contains(item))
{
throw new InvalidOperationException("Item is already part of this NativeListItem.");
}

items.Add(item);

if (items.Count == 1)
{
UpdateIndex();
}
}
/// <summary>
/// Adds a <typeparamref name="T" /> in a specific location.
/// </summary>
/// <param name="position">The position where the item should be added.</param>
/// <param name="item">The <typeparamref name="T" /> to add.</param>
public void Add(int position, T item)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item));
}

if (position < 0 || position > Items.Count)
{
throw new ArgumentOutOfRangeException(nameof(position), "The position is out of the range of items.");
}

Items.Insert(position, item);

FixIndexIfRequired();
}
/// <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))
{
FixIndexIfRequired();
}
}
/// <summary>
/// Removes a <typeparamref name="T" /> at a specific location.
/// </summary>
/// <param name="position">The position of the <typeparamref name="T" />.</param>
public void RemoveAt(int position)
{
if (position >= items.Count)
{
return;
}

items.RemoveAt(position);
FixIndexIfRequired();
}
/// <summary>
/// Removes all of the items that match the <paramref name="pred"/>.
/// </summary>
/// <param name="pred">The function to use as a check.</param>
public void Remove(Func<T, bool> pred)
{
if (items.RemoveAll(pred.Invoke) > 0)
{
FixIndexIfRequired();
}
}
/// <summary>
/// Removes all of the <typeparamref name="T" /> from this item.
/// </summary>
public void Clear()
{
items.Clear();

UpdateIndex();
}
/// <summary>
/// Recalculates the item positions and sizes with the specified values.
/// </summary>
Expand Down

0 comments on commit c4e6db0

Please sign in to comment.