// -----------------------------------------------------------------------
//
// Copyright 2015 MIT Licence. See licence.md for more information.
//
// -----------------------------------------------------------------------
namespace Perspex.Controls
{
using System;
using Perspex.Input;
///
/// Defines vertical or horizontal orientation.
///
public enum Orientation
{
///
/// Vertical orientation.
///
Vertical,
///
/// Horizontal orientation.
///
Horizontal,
}
///
/// A panel which lays out its children horizontally or vertically.
///
public class StackPanel : Panel, INavigableContainer
{
///
/// Defines the property.
///
public static readonly PerspexProperty GapProperty =
PerspexProperty.Register(nameof(Gap));
///
/// Defines the property.
///
public static readonly PerspexProperty OrientationProperty =
PerspexProperty.Register(nameof(Orientation));
///
/// Initializes static members of the class.
///
static StackPanel()
{
AffectsMeasure(GapProperty);
AffectsMeasure(OrientationProperty);
}
///
/// Gets or sets the size of the gap to place between child controls.
///
public double Gap
{
get { return this.GetValue(GapProperty); }
set { this.SetValue(GapProperty, value); }
}
///
/// Gets or sets the orientation in which child controls will be layed out.
///
public Orientation Orientation
{
get { return this.GetValue(OrientationProperty); }
set { this.SetValue(OrientationProperty, value); }
}
///
/// Gets the next control in the specified direction.
///
/// The movement direction.
/// The control from which movement begins.
/// The control.
IInputElement INavigableContainer.GetControl(FocusNavigationDirection direction, IInputElement from)
{
var horiz = this.Orientation == Orientation.Horizontal;
int index = this.Children.IndexOf((IControl)from);
switch (direction)
{
case FocusNavigationDirection.First:
index = 0;
break;
case FocusNavigationDirection.Last:
index = this.Children.Count - 1;
break;
case FocusNavigationDirection.Next:
++index;
break;
case FocusNavigationDirection.Previous:
--index;
break;
case FocusNavigationDirection.Left:
index = horiz ? index - 1 : -1;
break;
case FocusNavigationDirection.Right:
index = horiz ? index + 1 : -1;
break;
case FocusNavigationDirection.Up:
index = horiz ? -1 : index - 1;
break;
case FocusNavigationDirection.Down:
index = horiz ? -1 : index + 1;
break;
}
if (index >= 0 && index < this.Children.Count)
{
return this.Children[index];
}
else
{
return null;
}
}
///
/// Measures the control.
///
/// The available size.
/// The desired size of the control.
protected override Size MeasureOverride(Size availableSize)
{
double childAvailableWidth = double.PositiveInfinity;
double childAvailableHeight = double.PositiveInfinity;
if (this.Orientation == Orientation.Vertical)
{
childAvailableWidth = availableSize.Width;
if (!double.IsNaN(this.Width))
{
childAvailableWidth = this.Width;
}
childAvailableWidth = Math.Min(childAvailableWidth, this.MaxWidth);
childAvailableWidth = Math.Max(childAvailableWidth, this.MinWidth);
}
else
{
childAvailableHeight = availableSize.Height;
if (!double.IsNaN(this.Height))
{
childAvailableHeight = this.Height;
}
childAvailableHeight = Math.Min(childAvailableHeight, this.MaxHeight);
childAvailableHeight = Math.Max(childAvailableHeight, this.MinHeight);
}
double measuredWidth = 0;
double measuredHeight = 0;
double gap = this.Gap;
foreach (Control child in this.Children)
{
child.Measure(new Size(childAvailableWidth, childAvailableHeight));
Size size = child.DesiredSize;
if (this.Orientation == Orientation.Vertical)
{
measuredHeight += size.Height + gap;
measuredWidth = Math.Max(measuredWidth, size.Width);
}
else
{
measuredWidth += size.Width + gap;
measuredHeight = Math.Max(measuredHeight, size.Height);
}
}
return new Size(measuredWidth, measuredHeight);
}
///
/// Arranges the control's children.
///
/// The size allocated to the control.
/// The space taken.
protected override Size ArrangeOverride(Size finalSize)
{
double arrangedWidth = finalSize.Width;
double arrangedHeight = finalSize.Height;
double gap = this.Gap;
if (this.Orientation == Orientation.Vertical)
{
arrangedHeight = 0;
}
else
{
arrangedWidth = 0;
}
foreach (Control child in this.Children)
{
double childWidth = child.DesiredSize.Width;
double childHeight = child.DesiredSize.Height;
if (this.Orientation == Orientation.Vertical)
{
double width = Math.Max(childWidth, arrangedWidth);
Rect childFinal = new Rect(0, arrangedHeight, width, childHeight);
child.Arrange(childFinal);
arrangedWidth = Math.Max(arrangedWidth, childWidth);
arrangedHeight += childHeight + gap;
}
else
{
double height = Math.Max(childHeight, arrangedHeight);
Rect childFinal = new Rect(arrangedWidth, 0, childWidth, height);
child.Arrange(childFinal);
arrangedWidth += childWidth + gap;
arrangedHeight = Math.Max(arrangedHeight, childHeight);
}
}
if (this.Orientation == Orientation.Vertical)
{
arrangedHeight = Math.Max(arrangedHeight - gap, finalSize.Height);
}
else
{
arrangedWidth = Math.Max(arrangedWidth - gap, finalSize.Width);
}
return new Size(arrangedWidth, arrangedHeight);
}
}
}