diff --git a/Perspex.Controls/Decorator.cs b/Perspex.Controls/Decorator.cs index c83ffbf091..4ca080be55 100644 --- a/Perspex.Controls/Decorator.cs +++ b/Perspex.Controls/Decorator.cs @@ -1,65 +1,84 @@ // ----------------------------------------------------------------------- // -// Copyright 2014 MIT Licence. See licence.md for more information. +// Copyright 2015 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Controls { - using System; - using System.Collections.Generic; - using System.Linq; - using System.Reactive.Linq; using Perspex.Collections; - using Perspex.Layout; + /// + /// Base class for controls which decorate a single child control. + /// public class Decorator : Control, IVisual, ILogical { + /// + /// Defines the property. + /// public static readonly PerspexProperty ChildProperty = - PerspexProperty.Register("Child"); + PerspexProperty.Register(nameof(Child)); + /// + /// Defines the property. + /// public static readonly PerspexProperty PaddingProperty = - PerspexProperty.Register("Padding"); + PerspexProperty.Register(nameof(Padding)); private PerspexSingleItemList logicalChild = new PerspexSingleItemList(); - public Decorator() + /// + /// Initializes static members of the class. + /// + static Decorator() { - this.GetObservableWithHistory(ChildProperty).Subscribe(x => - { - if (x.Item1 != null) - { - this.RemoveVisualChild(x.Item1); - x.Item1.Parent = null; - } - - if (x.Item2 != null) - { - this.AddVisualChild(x.Item2); - x.Item2.Parent = this; - } - - this.logicalChild.SingleItem = x.Item2; - }); + ChildProperty.Changed.AddClassHandler(x => x.ChildChanged); } + /// + /// Gets or sets the decorated control. + /// public Control Child { get { return this.GetValue(ChildProperty); } set { this.SetValue(ChildProperty, value); } } + /// + /// Gets or sets the padding to place around the control. + /// public Thickness Padding { get { return this.GetValue(PaddingProperty); } set { this.SetValue(PaddingProperty, value); } } + /// + /// Gets the logical children of the control. + /// IPerspexReadOnlyList ILogical.LogicalChildren { get { return this.logicalChild; } } + /// + protected override Size MeasureOverride(Size availableSize) + { + var content = this.Child; + var padding = this.Padding; + + if (content != null) + { + content.Measure(availableSize.Deflate(padding)); + return content.DesiredSize.Inflate(padding); + } + else + { + return new Size(padding.Left + padding.Right, padding.Bottom + padding.Top); + } + } + + /// protected override Size ArrangeOverride(Size finalSize) { Control content = this.Child; @@ -72,20 +91,28 @@ namespace Perspex.Controls return finalSize; } - protected override Size MeasureOverride(Size availableSize) + /// + /// Called when the property changes. + /// + /// The event args. + private void ChildChanged(PerspexPropertyChangedEventArgs e) { - var content = this.Child; - var padding = this.Padding; + var oldChild = (Control)e.OldValue; + var newChild = (Control)e.NewValue; - if (content != null) + if (oldChild != null) { - content.Measure(availableSize.Deflate(padding)); - return content.DesiredSize.Inflate(padding); + oldChild.Parent = null; + this.RemoveVisualChild(oldChild); } - else + + if (newChild != null) { - return new Size(padding.Left + padding.Right, padding.Bottom + padding.Top); + this.AddVisualChild(newChild); + newChild.Parent = this; } + + this.logicalChild.SingleItem = newChild; } } }