// ----------------------------------------------------------------------- // // Copyright 2014 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; public class Decorator : Control, IVisual, ILogical { public static readonly PerspexProperty ContentProperty = PerspexProperty.Register("Content"); public static readonly PerspexProperty PaddingProperty = PerspexProperty.Register("Padding"); private PerspexSingleItemList logicalChild = new PerspexSingleItemList(); public Decorator() { this.GetObservableWithHistory(ContentProperty).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; }); } public Control Content { get { return this.GetValue(ContentProperty); } set { this.SetValue(ContentProperty, value); } } public Thickness Padding { get { return this.GetValue(PaddingProperty); } set { this.SetValue(PaddingProperty, value); } } IPerspexReadOnlyList ILogical.LogicalChildren { get { return this.logicalChild; } } protected override Size ArrangeOverride(Size finalSize) { Control content = this.Content; if (content != null) { content.Arrange(new Rect(finalSize).Deflate(this.Padding)); } return finalSize; } protected override Size MeasureOverride(Size availableSize) { var content = this.Content; 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); } } } }