// -----------------------------------------------------------------------
//
// 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.Layout;
public class Decorator : Control, IVisual
{
public static readonly PerspexProperty ContentProperty =
PerspexProperty.Register("Content");
public static readonly PerspexProperty PaddingProperty =
PerspexProperty.Register("Padding");
public Decorator()
{
this.GetObservable(ContentProperty).Subscribe(x =>
{
this.ClearVisualChildren();
if (x != null)
{
this.AddVisualChild(x);
}
});
}
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); }
}
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.Value.Inflate(padding);
}
return new Size();
}
}
}