// -----------------------------------------------------------------------
//
// Copyright 2015 MIT Licence. See licence.md for more information.
//
// -----------------------------------------------------------------------
namespace Perspex.Controls
{
using Perspex.Media;
///
/// A control which decorates a child with a border and background.
///
public class Border : Decorator
{
///
/// Defines the property.
///
public static readonly PerspexProperty BackgroundProperty =
PerspexProperty.Register(nameof(Background));
///
/// Defines the property.
///
public static readonly PerspexProperty BorderBrushProperty =
PerspexProperty.Register(nameof(BorderBrush));
///
/// Defines the property.
///
public static readonly PerspexProperty BorderThicknessProperty =
PerspexProperty.Register(nameof(BorderThickness));
///
/// Defines the property.
///
public static readonly PerspexProperty CornerRadiusProperty =
PerspexProperty.Register(nameof(CornerRadius));
///
/// Initializes static members of the class.
///
static Border()
{
Control.AffectsRender(Border.BackgroundProperty);
Control.AffectsRender(Border.BorderBrushProperty);
}
///
/// Gets or sets a brush with which to paint the background.
///
public Brush Background
{
get { return this.GetValue(BackgroundProperty); }
set { this.SetValue(BackgroundProperty, value); }
}
///
/// Gets or sets a brush with which to paint the border.
///
public Brush BorderBrush
{
get { return this.GetValue(BorderBrushProperty); }
set { this.SetValue(BorderBrushProperty, value); }
}
///
/// Gets or sets the thickness of the border.
///
public double BorderThickness
{
get { return this.GetValue(BorderThicknessProperty); }
set { this.SetValue(BorderThicknessProperty, value); }
}
///
/// Gets or sets the radius of the border rounded corners.
///
public float CornerRadius
{
get { return this.GetValue(CornerRadiusProperty); }
set { this.SetValue(CornerRadiusProperty, value); }
}
///
/// Renders the control.
///
/// The drawing context.
public override void Render(IDrawingContext context)
{
var background = this.Background;
var borderBrush = this.BorderBrush;
var borderThickness = this.BorderThickness;
var cornerRadius = this.CornerRadius;
var rect = new Rect(this.Bounds.Size).Deflate(this.BorderThickness);
if (background != null)
{
context.FillRectange(background, rect, cornerRadius);
}
if (borderBrush != null && borderThickness > 0)
{
context.DrawRectange(new Pen(borderBrush, borderThickness), rect, cornerRadius);
}
}
///
/// Measures the control.
///
/// The available size.
/// The desired size of the control.
protected override Size MeasureOverride(Size availableSize)
{
var child = this.Child;
var padding = this.Padding + new Thickness(this.BorderThickness);
if (child != null)
{
child.Measure(availableSize.Deflate(padding));
return child.DesiredSize.Inflate(padding);
}
else
{
return new Size(padding.Left + padding.Right, padding.Bottom + padding.Top);
}
}
///
/// Arranges the control's child.
///
/// The size allocated to the control.
/// The space taken.
protected override Size ArrangeOverride(Size finalSize)
{
var child = this.Child;
if (child != null)
{
var padding = this.Padding + new Thickness(this.BorderThickness);
child.Arrange(new Rect(finalSize).Deflate(padding));
}
return finalSize;
}
}
}