// Copyright (c) The Perspex Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. using Perspex.Media; namespace Perspex.Controls { /// /// 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() { AffectsRender(BackgroundProperty); AffectsRender(BorderBrushProperty); } /// /// Gets or sets a brush with which to paint the background. /// public Brush Background { get { return GetValue(BackgroundProperty); } set { SetValue(BackgroundProperty, value); } } /// /// Gets or sets a brush with which to paint the border. /// public Brush BorderBrush { get { return GetValue(BorderBrushProperty); } set { SetValue(BorderBrushProperty, value); } } /// /// Gets or sets the thickness of the border. /// public double BorderThickness { get { return GetValue(BorderThicknessProperty); } set { SetValue(BorderThicknessProperty, value); } } /// /// Gets or sets the radius of the border rounded corners. /// public float CornerRadius { get { return GetValue(CornerRadiusProperty); } set { SetValue(CornerRadiusProperty, value); } } /// /// Renders the control. /// /// The drawing context. public override void Render(IDrawingContext context) { var background = Background; var borderBrush = BorderBrush; var borderThickness = BorderThickness; var cornerRadius = CornerRadius; var rect = new Rect(Bounds.Size).Deflate(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 = Child; var padding = Padding + new Thickness(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 = Child; if (child != null) { var padding = Padding + new Thickness(BorderThickness); child.Arrange(new Rect(finalSize).Deflate(padding)); } return finalSize; } } }