Browse Source

Fixed StackPanel arrange pass.

Now follow's WPF's behavior better. In order to get the desired outcome, the `.Constrain(availableSize)` call in `Layoutable.Measure` had to be removed. Controls that relied on that (`Image` and `ViewBox`) have to now call `Constrain` themselves.

Fixes #2350
pull/2431/head
Steven Kirk 7 years ago
parent
commit
7ac3556983
  1. 11
      src/Avalonia.Controls/Image.cs
  2. 54
      src/Avalonia.Controls/StackPanel.cs
  3. 2
      src/Avalonia.Controls/Viewbox.cs
  4. 62
      src/Avalonia.Layout/LayoutExtensions.cs
  5. 2
      src/Avalonia.Layout/Layoutable.cs

11
src/Avalonia.Controls/Image.cs

@ -81,23 +81,22 @@ namespace Avalonia.Controls
protected override Size MeasureOverride(Size availableSize)
{
var source = Source;
var result = new Size();
if (source != null)
{
Size sourceSize = new Size(source.PixelSize.Width, source.PixelSize.Height);
if (double.IsInfinity(availableSize.Width) || double.IsInfinity(availableSize.Height))
{
return sourceSize;
result = sourceSize;
}
else
{
return Stretch.CalculateSize(availableSize, sourceSize);
result = Stretch.CalculateSize(availableSize, sourceSize);
}
}
else
{
return new Size();
}
return result.Constrain(availableSize);
}
/// <inheritdoc/>

54
src/Avalonia.Controls/StackPanel.cs

@ -4,6 +4,7 @@
using System;
using System.Linq;
using Avalonia.Input;
using Avalonia.Layout;
namespace Avalonia.Controls
{
@ -219,30 +220,16 @@ namespace Avalonia.Controls
measuredWidth -= (hasVisibleChild ? spacing : 0);
}
return new Size(measuredWidth, measuredHeight);
return new Size(measuredWidth, measuredHeight).Constrain(availableSize);
}
/// <summary>
/// Arranges the control's children.
/// </summary>
/// <param name="finalSize">The size allocated to the control.</param>
/// <returns>The space taken.</returns>
/// <inheritdoc/>
protected override Size ArrangeOverride(Size finalSize)
{
var orientation = Orientation;
double arrangedWidth = finalSize.Width;
double arrangedHeight = finalSize.Height;
double spacing = Spacing;
bool hasVisibleChild = Children.Any(c => c.IsVisible);
if (Orientation == Orientation.Vertical)
{
arrangedHeight = 0;
}
else
{
arrangedWidth = 0;
}
var spacing = Spacing;
var finalRect = new Rect(finalSize);
var pos = 0.0;
foreach (Control child in Children)
{
@ -251,32 +238,21 @@ namespace Avalonia.Controls
if (orientation == Orientation.Vertical)
{
double width = Math.Max(childWidth, arrangedWidth);
Rect childFinal = new Rect(0, arrangedHeight, width, childHeight);
ArrangeChild(child, childFinal, finalSize, orientation);
arrangedWidth = Math.Max(arrangedWidth, childWidth);
arrangedHeight += childHeight + (child.IsVisible ? spacing : 0);
var rect = new Rect(0, pos, childWidth, childHeight)
.Align(finalRect, child.HorizontalAlignment, VerticalAlignment.Top);
ArrangeChild(child, rect, finalSize, orientation);
pos += childHeight + spacing;
}
else
{
double height = Math.Max(childHeight, arrangedHeight);
Rect childFinal = new Rect(arrangedWidth, 0, childWidth, height);
ArrangeChild(child, childFinal, finalSize, orientation);
arrangedWidth += childWidth + (child.IsVisible ? spacing : 0);
arrangedHeight = Math.Max(arrangedHeight, childHeight);
var rect = new Rect(pos, 0, childWidth, childHeight)
.Align(finalRect, HorizontalAlignment.Left, child.VerticalAlignment);
ArrangeChild(child, rect, finalSize, orientation);
pos += childWidth + spacing;
}
}
if (orientation == Orientation.Vertical)
{
arrangedHeight = Math.Max(arrangedHeight - (hasVisibleChild ? spacing : 0), finalSize.Height);
}
else
{
arrangedWidth = Math.Max(arrangedWidth - (hasVisibleChild ? spacing : 0), finalSize.Width);
}
return new Size(arrangedWidth, arrangedHeight);
return finalSize;
}
internal virtual void ArrangeChild(

2
src/Avalonia.Controls/Viewbox.cs

@ -49,7 +49,7 @@ namespace Avalonia.Controls
var scale = GetScale(availableSize, childSize, Stretch);
return childSize * scale;
return (childSize * scale).Constrain(availableSize);
}
return new Size();

62
src/Avalonia.Layout/LayoutExtensions.cs

@ -0,0 +1,62 @@
using System;
namespace Avalonia.Layout
{
/// <summary>
/// Extension methods for layout types.
/// </summary>
public static class LayoutExtensions
{
/// <summary>
/// Aligns a rect in a constraining rect according to horizontal and vertical alignment
/// settings.
/// </summary>
/// <param name="rect">The rect to align.</param>
/// <param name="constraint">The constraining rect.</param>
/// <param name="horizontalAlignment">The horizontal alignment.</param>
/// <param name="verticalAlignment">The vertical alignment.</param>
/// <returns></returns>
public static Rect Align(
this Rect rect,
Rect constraint,
HorizontalAlignment horizontalAlignment,
VerticalAlignment verticalAlignment)
{
switch (horizontalAlignment)
{
case HorizontalAlignment.Center:
rect = rect.WithX((constraint.Width - rect.Width) / 2);
break;
case HorizontalAlignment.Right:
rect = rect.WithX(constraint.Width - rect.Width);
break;
case HorizontalAlignment.Stretch:
rect = new Rect(
0,
rect.Y,
Math.Max(constraint.Width, rect.Width),
rect.Height);
break;
}
switch (verticalAlignment)
{
case VerticalAlignment.Center:
rect = rect.WithY((constraint.Height - rect.Height) / 2);
break;
case VerticalAlignment.Bottom:
rect = rect.WithY(constraint.Height - rect.Height);
break;
case VerticalAlignment.Stretch:
rect = new Rect(
rect.X,
0,
rect.Width,
Math.Max(constraint.Height, rect.Height));
break;
}
return rect;
}
}
}

2
src/Avalonia.Layout/Layoutable.cs

@ -314,7 +314,7 @@ namespace Avalonia.Layout
try
{
_measuring = true;
desiredSize = MeasureCore(availableSize).Constrain(availableSize);
desiredSize = MeasureCore(availableSize);
}
finally
{

Loading…
Cancel
Save