csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
282 lines
11 KiB
282 lines
11 KiB
// Copyright (c) The Avalonia Project. All rights reserved.
|
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|
|
|
using System;
|
|
using Avalonia.Input
|
|
|
|
namespace Avalonia.Controls
|
|
{
|
|
/// <summary>
|
|
/// A panel which lays out its children horizontally or vertically.
|
|
/// </summary>
|
|
public class StackPanel : Panel, INavigableContainer
|
|
{
|
|
/// <summary>
|
|
/// Defines the <see cref="Spacing"/> property.
|
|
/// </summary>
|
|
public static readonly StyledProperty<double> SpacingProperty =
|
|
AvaloniaProperty.Register<StackPanel, double>(nameof(Spacing));
|
|
|
|
/// <summary>
|
|
/// Defines the <see cref="Orientation"/> property.
|
|
/// </summary>
|
|
public static readonly StyledProperty<Orientation> OrientationProperty =
|
|
AvaloniaProperty.Register<StackPanel, Orientation>(nameof(Orientation), Orientation.Vertical);
|
|
|
|
/// <summary>
|
|
/// Initializes static members of the <see cref="StackPanel"/> class.
|
|
/// </summary>
|
|
static StackPanel()
|
|
{
|
|
AffectsMeasure<StackPanel>(SpacingProperty);
|
|
AffectsMeasure<StackPanel>(OrientationProperty);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the size of the spacing to place between child controls.
|
|
/// </summary>
|
|
public double Spacing
|
|
{
|
|
get { return GetValue(SpacingProperty); }
|
|
set { SetValue(SpacingProperty, value); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the orientation in which child controls will be layed out.
|
|
/// </summary>
|
|
public Orientation Orientation
|
|
{
|
|
get { return GetValue(OrientationProperty); }
|
|
set { SetValue(OrientationProperty, value); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the next control in the specified direction.
|
|
/// </summary>
|
|
/// <param name="direction">The movement direction.</param>
|
|
/// <param name="from">The control from which movement begins.</param>
|
|
/// <param name="wrap">Whether to wrap around when the first or last item is reached.</param>
|
|
/// <returns>The control.</returns>
|
|
IInputElement INavigableContainer.GetControl(NavigationDirection direction, IInputElement from, bool wrap)
|
|
{
|
|
var result = GetControlInDirection(direction, from as IControl);
|
|
|
|
if (result == null && wrap)
|
|
{
|
|
if (Orientation == Orientation.Vertical)
|
|
{
|
|
switch (direction)
|
|
{
|
|
case NavigationDirection.Up:
|
|
case NavigationDirection.Previous:
|
|
case NavigationDirection.PageUp:
|
|
result = GetControlInDirection(NavigationDirection.Last, null);
|
|
break;
|
|
case NavigationDirection.Down:
|
|
case NavigationDirection.Next:
|
|
case NavigationDirection.PageDown:
|
|
result = GetControlInDirection(NavigationDirection.First, null);
|
|
break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
switch (direction)
|
|
{
|
|
case NavigationDirection.Left:
|
|
case NavigationDirection.Previous:
|
|
case NavigationDirection.PageUp:
|
|
result = GetControlInDirection(NavigationDirection.Last, null);
|
|
break;
|
|
case NavigationDirection.Right:
|
|
case NavigationDirection.Next:
|
|
case NavigationDirection.PageDown:
|
|
result = GetControlInDirection(NavigationDirection.First, null);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the next control in the specified direction.
|
|
/// </summary>
|
|
/// <param name="direction">The movement direction.</param>
|
|
/// <param name="from">The control from which movement begins.</param>
|
|
/// <returns>The control.</returns>
|
|
protected virtual IInputElement GetControlInDirection(NavigationDirection direction, IControl from)
|
|
{
|
|
var horiz = Orientation == Orientation.Horizontal;
|
|
int index = from != null ? Children.IndexOf(from) : -1;
|
|
|
|
switch (direction)
|
|
{
|
|
case NavigationDirection.First:
|
|
index = 0;
|
|
break;
|
|
case NavigationDirection.Last:
|
|
index = Children.Count - 1;
|
|
break;
|
|
case NavigationDirection.Next:
|
|
if (index != -1) ++index;
|
|
break;
|
|
case NavigationDirection.Previous:
|
|
if (index != -1) --index;
|
|
break;
|
|
case NavigationDirection.Left:
|
|
if (index != -1) index = horiz ? index - 1 : -1;
|
|
break;
|
|
case NavigationDirection.Right:
|
|
if (index != -1) index = horiz ? index + 1 : -1;
|
|
break;
|
|
case NavigationDirection.Up:
|
|
if (index != -1) index = horiz ? -1 : index - 1;
|
|
break;
|
|
case NavigationDirection.Down:
|
|
if (index != -1) index = horiz ? -1 : index + 1;
|
|
break;
|
|
default:
|
|
index = -1;
|
|
break;
|
|
}
|
|
|
|
if (index >= 0 && index < Children.Count)
|
|
{
|
|
return Children[index];
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// General StackPanel layout behavior is to grow unbounded in the "stacking" direction (Size To Content).
|
|
/// Children in this dimension are encouraged to be as large as they like. In the other dimension,
|
|
/// StackPanel will assume the maximum size of its children.
|
|
/// </summary>
|
|
/// <param name="constraint">Constraint</param>
|
|
/// <returns>Desired size</returns>
|
|
protected override Size MeasureOverride(Size constraint)
|
|
{
|
|
Size stackDesiredSize = new Size();
|
|
var children = Children;
|
|
Size layoutSlotSize = constraint;
|
|
bool fHorizontal = (Orientation == Orientation.Horizontal);
|
|
double spacing = Spacing;
|
|
bool hasVisibleChild = false;
|
|
|
|
//
|
|
// Initialize child sizing and iterator data
|
|
// Allow children as much size as they want along the stack.
|
|
//
|
|
if (fHorizontal)
|
|
{
|
|
layoutSlotSize = layoutSlotSize.WithWidth(Double.PositiveInfinity);
|
|
}
|
|
else
|
|
{
|
|
layoutSlotSize = layoutSlotSize.WithHeight(Double.PositiveInfinity);
|
|
}
|
|
|
|
//
|
|
// Iterate through children.
|
|
// While we still supported virtualization, this was hidden in a child iterator (see source history).
|
|
//
|
|
for (int i = 0, count = children.Count; i < count; ++i)
|
|
{
|
|
// Get next child.
|
|
var child = children[i];
|
|
|
|
if (child == null)
|
|
{ continue; }
|
|
|
|
if (child.IsVisible)
|
|
{
|
|
hasVisibleChild = true;
|
|
}
|
|
|
|
// Measure the child.
|
|
child.Measure(layoutSlotSize);
|
|
Size childDesiredSize = child.DesiredSize;
|
|
|
|
// Accumulate child size.
|
|
if (fHorizontal)
|
|
{
|
|
stackDesiredSize = stackDesiredSize.WithWidth(stackDesiredSize.Width + childDesiredSize.Width + (child.IsVisible ? spacing : 0));
|
|
stackDesiredSize = stackDesiredSize.WithHeight(Math.Max(stackDesiredSize.Height, childDesiredSize.Height));
|
|
}
|
|
else
|
|
{
|
|
stackDesiredSize = stackDesiredSize.WithWidth(Math.Max(stackDesiredSize.Width, childDesiredSize.Width));
|
|
stackDesiredSize = stackDesiredSize.WithHeight(stackDesiredSize.Height + childDesiredSize.Height + (child.IsVisible ? spacing : 0));
|
|
}
|
|
}
|
|
|
|
if (Orientation == Orientation.Vertical)
|
|
{
|
|
stackDesiredSize = stackDesiredSize.WithHeight(stackDesiredSize.Height - (hasVisibleChild ? spacing : 0));
|
|
}
|
|
else
|
|
{
|
|
stackDesiredSize = stackDesiredSize.WithWidth(stackDesiredSize.Width - (hasVisibleChild ? spacing : 0));
|
|
}
|
|
|
|
return stackDesiredSize;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Content arrangement.
|
|
/// </summary>
|
|
/// <param name="finalSize">Arrange size</param>
|
|
protected override Size ArrangeOverride(Size finalSize)
|
|
{
|
|
var children = Children;
|
|
bool fHorizontal = (Orientation == Orientation.Horizontal);
|
|
Rect rcChild = new Rect(finalSize);
|
|
double previousChildSize = 0.0;
|
|
var spacing = Spacing;
|
|
|
|
//
|
|
// Arrange and Position Children.
|
|
//
|
|
for (int i = 0, count = children.Count; i < count; ++i)
|
|
{
|
|
var child = children[i];
|
|
|
|
if (child == null)
|
|
{ continue; }
|
|
|
|
if (fHorizontal)
|
|
{
|
|
rcChild = rcChild.WithX(rcChild.X + previousChildSize);
|
|
previousChildSize = child.DesiredSize.Width;
|
|
rcChild = rcChild.WithWidth(previousChildSize);
|
|
rcChild = rcChild.WithHeight(Math.Max(finalSize.Height, child.DesiredSize.Height) + (child.IsVisible ? spacing : 0));
|
|
}
|
|
else
|
|
{
|
|
rcChild = rcChild.WithY(rcChild.Y + previousChildSize);
|
|
previousChildSize = child.DesiredSize.Height;
|
|
rcChild = rcChild.WithHeight(previousChildSize);
|
|
rcChild = rcChild.WithWidth(Math.Max(finalSize.Width, child.DesiredSize.Width) + (child.IsVisible ? spacing : 0));
|
|
}
|
|
|
|
child.Arrange(rcChild);
|
|
}
|
|
|
|
return finalSize;
|
|
}
|
|
|
|
internal virtual void ArrangeChild(
|
|
IControl child,
|
|
Rect rect,
|
|
Size panelSize,
|
|
Orientation orientation)
|
|
{
|
|
child.Arrange(rect);
|
|
}
|
|
}
|
|
}
|
|
|