From 18a976dc5521901289902a4f9c09615ff620a27e Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sat, 2 May 2015 19:11:04 +0200 Subject: [PATCH] Added IEnumerable extension methods. As we're working on a non-generic IEnumerable in ItemsControl related controls. --- Perspex.Controls/Perspex.Controls.csproj | 1 + .../Primitives/SelectingItemsControl.cs | 65 ++-------------- Perspex.Controls/Utils/IEnumerableUtils.cs | 76 +++++++++++++++++++ 3 files changed, 83 insertions(+), 59 deletions(-) create mode 100644 Perspex.Controls/Utils/IEnumerableUtils.cs diff --git a/Perspex.Controls/Perspex.Controls.csproj b/Perspex.Controls/Perspex.Controls.csproj index 3cdd69bbbb..360d044f75 100644 --- a/Perspex.Controls/Perspex.Controls.csproj +++ b/Perspex.Controls/Perspex.Controls.csproj @@ -92,6 +92,7 @@ + diff --git a/Perspex.Controls/Primitives/SelectingItemsControl.cs b/Perspex.Controls/Primitives/SelectingItemsControl.cs index 70421ca0e8..b73af7712c 100644 --- a/Perspex.Controls/Primitives/SelectingItemsControl.cs +++ b/Perspex.Controls/Primitives/SelectingItemsControl.cs @@ -6,12 +6,11 @@ namespace Perspex.Controls.Primitives { - using System; - using System.Collections; - using System.Linq; + using Perspex.Controls.Utils; using Perspex.Input; using Perspex.VisualTree; - using System.Collections.Generic; + using System; + using System.Linq; public abstract class SelectingItemsControl : ItemsControl { @@ -31,7 +30,7 @@ namespace Perspex.Controls.Primitives if (control != null) { - control.SelectedItem = control.GetItemAt((int)x.NewValue); + control.SelectedItem = control.Items.ElementAt((int)x.NewValue); } }); @@ -58,58 +57,6 @@ namespace Perspex.Controls.Primitives set { this.SetValue(SelectedItemProperty, value); } } - protected static int GetIndexOfItem(IEnumerable items, object item) - { - var list = items as IList; - - if (list != null) - { - return list.IndexOf(item); - } - else if (items != null) - { - int index = 0; - - foreach (var i in items) - { - if (object.ReferenceEquals(i, item)) - { - return index; - } - - ++index; - } - } - - return -1; - } - - protected static object GetItemAt(IEnumerable items, int index) - { - var list = items as IList; - - if (list != null) - { - return list[index]; - } - else if (items != null) - { - return items.Cast().ElementAt(index); - } - - return -1; - } - - protected int GetIndexOfItem(object item) - { - return GetIndexOfItem(this.Items, item); - } - - protected object GetItemAt(int index) - { - return GetItemAt(this.Items, index); - } - protected virtual void MoveSelection(FocusNavigationDirection direction) { var panel = this.Presenter?.Panel as INavigablePanel; @@ -209,7 +156,7 @@ namespace Perspex.Controls.Primitives } else if (value > -1) { - var count = control.Items.Cast().Count(); + var count = control.Items.Count(); return Math.Min(value, count - 1); } } @@ -242,7 +189,7 @@ namespace Perspex.Controls.Primitives } else { - this.SelectedIndex = GetIndexOfItem(selected); + this.SelectedIndex = this.Items.IndexOf(selected); } } } diff --git a/Perspex.Controls/Utils/IEnumerableUtils.cs b/Perspex.Controls/Utils/IEnumerableUtils.cs new file mode 100644 index 0000000000..247dcf34d7 --- /dev/null +++ b/Perspex.Controls/Utils/IEnumerableUtils.cs @@ -0,0 +1,76 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2014 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Controls.Utils +{ + using System; + using System.Collections; + using System.Globalization; + using System.Linq; + + internal static class IEnumerableUtils + { + public static int Count(this IEnumerable items) + { + Contract.Requires(items != null); + + var collection = items as ICollection; + + if (collection != null) + { + return collection.Count; + } + else + { + return items.Cast().Count(); + } + } + + public static int IndexOf(this IEnumerable items, object item) + { + Contract.Requires(items != null); + + var list = items as IList; + + if (list != null) + { + return list.IndexOf(item); + } + else + { + int index = 0; + + foreach (var i in items) + { + if (object.ReferenceEquals(i, item)) + { + return index; + } + + ++index; + } + + return -1; + } + } + + public static object ElementAt(this IEnumerable items, int index) + { + Contract.Requires(items != null); + + var list = items as IList; + + if (list != null) + { + return list[index]; + } + else + { + return items.Cast().ElementAt(index); + } + } + } +}