Browse Source

Documented/updated Decorator.

pull/63/head
Steven Kirk 11 years ago
parent
commit
d064508f25
  1. 93
      Perspex.Controls/Decorator.cs

93
Perspex.Controls/Decorator.cs

@ -1,65 +1,84 @@
// -----------------------------------------------------------------------
// <copyright file="Decorator.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// Copyright 2015 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Controls
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using Perspex.Collections;
using Perspex.Layout;
/// <summary>
/// Base class for controls which decorate a single child control.
/// </summary>
public class Decorator : Control, IVisual, ILogical
{
/// <summary>
/// Defines the <see cref="Child"/> property.
/// </summary>
public static readonly PerspexProperty<Control> ChildProperty =
PerspexProperty.Register<Decorator, Control>("Child");
PerspexProperty.Register<Decorator, Control>(nameof(Child));
/// <summary>
/// Defines the <see cref="Padding"/> property.
/// </summary>
public static readonly PerspexProperty<Thickness> PaddingProperty =
PerspexProperty.Register<Decorator, Thickness>("Padding");
PerspexProperty.Register<Decorator, Thickness>(nameof(Padding));
private PerspexSingleItemList<ILogical> logicalChild = new PerspexSingleItemList<ILogical>();
public Decorator()
/// <summary>
/// Initializes static members of the <see cref="Decorator"/> class.
/// </summary>
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<Decorator>(x => x.ChildChanged);
}
/// <summary>
/// Gets or sets the decorated control.
/// </summary>
public Control Child
{
get { return this.GetValue(ChildProperty); }
set { this.SetValue(ChildProperty, value); }
}
/// <summary>
/// Gets or sets the padding to place around the <see cref="Child"/> control.
/// </summary>
public Thickness Padding
{
get { return this.GetValue(PaddingProperty); }
set { this.SetValue(PaddingProperty, value); }
}
/// <summary>
/// Gets the logical children of the control.
/// </summary>
IPerspexReadOnlyList<ILogical> ILogical.LogicalChildren
{
get { return this.logicalChild; }
}
/// <inheritdoc/>
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);
}
}
/// <inheritdoc/>
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)
/// <summary>
/// Called when the <see cref="Child"/> property changes.
/// </summary>
/// <param name="e">The event args.</param>
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;
}
}
}

Loading…
Cancel
Save