Skip to content

Commit c4e6db0

Browse files
committed
Added missing methods in NativeListItem<T> (Fixes #40)
1 parent 87b19ae commit c4e6db0

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

LemonUI/Menus/NativeListItem.cs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,14 @@ private void TriggerEvent(int index, Direction direction)
215215
{
216216
ItemChanged?.Invoke(this, new ItemChangedEventArgs<T>(items[index], index, direction));
217217
}
218+
private void FixIndexIfRequired()
219+
{
220+
if (index >= items.Count)
221+
{
222+
index = items.Count - 1;
223+
UpdateIndex();
224+
}
225+
}
218226
/// <summary>
219227
/// Updates the currently selected item based on the index.
220228
/// </summary>
@@ -230,6 +238,95 @@ private void UpdateIndex()
230238

231239
#region Functions
232240

241+
/// <summary>
242+
/// Adds a <typeparamref name="T" /> into this item.
243+
/// </summary>
244+
/// <param name="item">The <typeparamref name="T" /> to add.</param>
245+
public void Add(T item)
246+
{
247+
if (item == null)
248+
{
249+
throw new ArgumentNullException(nameof(item));
250+
}
251+
252+
if (items.Contains(item))
253+
{
254+
throw new InvalidOperationException("Item is already part of this NativeListItem.");
255+
}
256+
257+
items.Add(item);
258+
259+
if (items.Count == 1)
260+
{
261+
UpdateIndex();
262+
}
263+
}
264+
/// <summary>
265+
/// Adds a <typeparamref name="T" /> in a specific location.
266+
/// </summary>
267+
/// <param name="position">The position where the item should be added.</param>
268+
/// <param name="item">The <typeparamref name="T" /> to add.</param>
269+
public void Add(int position, T item)
270+
{
271+
if (item == null)
272+
{
273+
throw new ArgumentNullException(nameof(item));
274+
}
275+
276+
if (position < 0 || position > Items.Count)
277+
{
278+
throw new ArgumentOutOfRangeException(nameof(position), "The position is out of the range of items.");
279+
}
280+
281+
Items.Insert(position, item);
282+
283+
FixIndexIfRequired();
284+
}
285+
/// <summary>
286+
/// Removes a specific <typeparamref name="T" />.
287+
/// </summary>
288+
/// <param name="item">The <typeparamref name="T" /> to remove.</param>
289+
public void Remove(T item)
290+
{
291+
if (items.Remove(item))
292+
{
293+
FixIndexIfRequired();
294+
}
295+
}
296+
/// <summary>
297+
/// Removes a <typeparamref name="T" /> at a specific location.
298+
/// </summary>
299+
/// <param name="position">The position of the <typeparamref name="T" />.</param>
300+
public void RemoveAt(int position)
301+
{
302+
if (position >= items.Count)
303+
{
304+
return;
305+
}
306+
307+
items.RemoveAt(position);
308+
FixIndexIfRequired();
309+
}
310+
/// <summary>
311+
/// Removes all of the items that match the <paramref name="pred"/>.
312+
/// </summary>
313+
/// <param name="pred">The function to use as a check.</param>
314+
public void Remove(Func<T, bool> pred)
315+
{
316+
if (items.RemoveAll(pred.Invoke) > 0)
317+
{
318+
FixIndexIfRequired();
319+
}
320+
}
321+
/// <summary>
322+
/// Removes all of the <typeparamref name="T" /> from this item.
323+
/// </summary>
324+
public void Clear()
325+
{
326+
items.Clear();
327+
328+
UpdateIndex();
329+
}
233330
/// <summary>
234331
/// Recalculates the item positions and sizes with the specified values.
235332
/// </summary>

0 commit comments

Comments
 (0)