From 3aa621cd963543689cf41b681be12c7c16d47563 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 8 May 2015 00:31:58 +0200 Subject: [PATCH] Revert "Removed PerspexReadOnlyListView" This reverts commit 0359f8c9a371c1377c8f9628fe139840bce9b93f. --- .../Collections/PerspexReadOnlyListView.cs | 128 ++++++++++++++++++ Perspex.Controls/ContentControl.cs | 8 +- Perspex.Controls/DropDown.cs | 4 +- Perspex.Controls/TabControl.cs | 5 +- 4 files changed, 139 insertions(+), 6 deletions(-) diff --git a/Perspex.Base/Collections/PerspexReadOnlyListView.cs b/Perspex.Base/Collections/PerspexReadOnlyListView.cs index 918fd6e885..b12e52efbb 100644 --- a/Perspex.Base/Collections/PerspexReadOnlyListView.cs +++ b/Perspex.Base/Collections/PerspexReadOnlyListView.cs @@ -13,6 +13,134 @@ namespace Perspex.Collections using System.ComponentModel; using System.Linq; + public class PerspexReadOnlyListView : IPerspexReadOnlyList, IDisposable + { + private IPerspexReadOnlyList source; + + public PerspexReadOnlyListView() + : this(null) + { + } + + public PerspexReadOnlyListView(IPerspexReadOnlyList source) + { + this.source = source; + + if (source != null) + { + this.source.CollectionChanged += this.SourceCollectionChanged; + } + } + + public event NotifyCollectionChangedEventHandler CollectionChanged; + + public event PropertyChangedEventHandler PropertyChanged; + + public int Count + { + get { return this.source?.Count ?? 0; } + } + + public IPerspexReadOnlyList Source + { + get + { + return this.source; + } + + set + { + if (this.source != null) + { + this.source.CollectionChanged -= this.SourceCollectionChanged; + + if (this.CollectionChanged != null) + { + var ev = new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Remove, + this.source, + 0); + this.CollectionChanged(this, ev); + } + } + + this.source = value; + + if (this.source != null) + { + this.source.CollectionChanged += this.SourceCollectionChanged; + + if (this.CollectionChanged != null) + { + var ev = new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Add, + this.source.ToList(), + 0); + this.CollectionChanged(this, ev); + } + } + } + } + + public T this[int index] + { + get { return this.source[index]; } + } + + public void Dispose() + { + this.source.CollectionChanged -= this.SourceCollectionChanged; + } + + public IEnumerator GetEnumerator() + { + return (this.source != null) ? + this.source.GetEnumerator() : + Enumerable.Empty().GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return this.GetEnumerator(); + } + + private void SourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) + { + if (this.CollectionChanged != null) + { + NotifyCollectionChangedEventArgs ev; + + switch (e.Action) + { + case NotifyCollectionChangedAction.Add: + ev = new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Add, + e.NewItems, + e.NewStartingIndex); + break; + case NotifyCollectionChangedAction.Remove: + ev = new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Remove, + e.OldItems, + e.OldStartingIndex); + break; + case NotifyCollectionChangedAction.Replace: + ev = new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Replace, + e.NewItems, + e.OldItems, + e.OldStartingIndex); + break; + + default: + throw new NotSupportedException("Action not yet implemented."); + } + + this.CollectionChanged(this, ev); + } + } + } + public class PerspexReadOnlyListView : IPerspexReadOnlyList, IDisposable { private IPerspexReadOnlyList source; diff --git a/Perspex.Controls/ContentControl.cs b/Perspex.Controls/ContentControl.cs index 84c327a36b..aa0bfd803f 100644 --- a/Perspex.Controls/ContentControl.cs +++ b/Perspex.Controls/ContentControl.cs @@ -24,7 +24,7 @@ namespace Perspex.Controls public static readonly PerspexProperty VerticalContentAlignmentProperty = PerspexProperty.Register("VerticalContentAlignment"); - private IPerspexReadOnlyList logicalChildren = new PerspexList(); + private PerspexReadOnlyListView logicalChildren = new PerspexReadOnlyListView(); public ContentControl() { @@ -63,7 +63,11 @@ namespace Perspex.Controls if (presenter != null) { - this.logicalChildren = ((ILogical)presenter)?.LogicalChildren; + this.logicalChildren.Source = ((ILogical)presenter).LogicalChildren; + } + else + { + this.logicalChildren.Source = null; } } diff --git a/Perspex.Controls/DropDown.cs b/Perspex.Controls/DropDown.cs index f20409ac8a..ee1a03f1a3 100644 --- a/Perspex.Controls/DropDown.cs +++ b/Perspex.Controls/DropDown.cs @@ -28,7 +28,7 @@ namespace Perspex.Controls public static readonly PerspexProperty IsDropDownOpenProperty = PerspexProperty.Register("IsDropDownOpen"); - private IPerspexReadOnlyList logicalChildren = new PerspexSingleItemList(); + private PerspexReadOnlyListView logicalChildren = new PerspexReadOnlyListView(); public DropDown() { @@ -77,7 +77,7 @@ namespace Perspex.Controls { var container = this.GetTemplateChild("container"); ((IItemsPanel)container).ChildLogicalParent = this; - this.logicalChildren = ((ILogical)container).LogicalChildren; + this.logicalChildren.Source = ((ILogical)container).LogicalChildren; } private void SetContentParent(Tuple change) diff --git a/Perspex.Controls/TabControl.cs b/Perspex.Controls/TabControl.cs index 4f6654f966..e0e7b1bd4d 100644 --- a/Perspex.Controls/TabControl.cs +++ b/Perspex.Controls/TabControl.cs @@ -25,7 +25,8 @@ namespace Perspex.Controls public static readonly PerspexProperty TransitionProperty = Deck.TransitionProperty.AddOwner(); - private IPerspexReadOnlyList logicalChildren = new PerspexSingleItemList(); + private PerspexReadOnlyListView logicalChildren = + new PerspexReadOnlyListView(); static TabControl() { @@ -80,7 +81,7 @@ namespace Perspex.Controls base.OnTemplateApplied(); this.deck = this.GetTemplateChild("deck"); - this.logicalChildren = ((ILogical)deck).LogicalChildren; + this.logicalChildren.Source = ((ILogical)deck).LogicalChildren; } } }