Skip to content

Commit

Permalink
fixed #784 and optimized MeasureChild method
Browse files Browse the repository at this point in the history
  • Loading branch information
NaBian committed Apr 14, 2021
1 parent f7ede33 commit f4d4bdd
Showing 1 changed file with 40 additions and 38 deletions.
78 changes: 40 additions & 38 deletions src/Shared/HandyControl_Shared/Controls/Panel/RelativePanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void SetAlignLeftWithPanel(DependencyObject element, bool value)
=> element.SetValue(AlignLeftWithPanelProperty, ValueBoxes.BooleanBox(value));

public static bool GetAlignLeftWithPanel(DependencyObject element)
=> (bool) element.GetValue(AlignLeftWithPanelProperty);
=> (bool)element.GetValue(AlignLeftWithPanelProperty);

public static readonly DependencyProperty AlignTopWithPanelProperty = DependencyProperty.RegisterAttached(
"AlignTopWithPanel", typeof(bool), typeof(RelativePanel), new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.AffectsRender));
Expand All @@ -36,7 +36,7 @@ public static void SetAlignTopWithPanel(DependencyObject element, bool value)
=> element.SetValue(AlignTopWithPanelProperty, ValueBoxes.BooleanBox(value));

public static bool GetAlignTopWithPanel(DependencyObject element)
=> (bool) element.GetValue(AlignTopWithPanelProperty);
=> (bool)element.GetValue(AlignTopWithPanelProperty);

public static readonly DependencyProperty AlignRightWithPanelProperty = DependencyProperty.RegisterAttached(
"AlignRightWithPanel", typeof(bool), typeof(RelativePanel), new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.AffectsRender));
Expand All @@ -45,7 +45,7 @@ public static void SetAlignRightWithPanel(DependencyObject element, bool value)
=> element.SetValue(AlignRightWithPanelProperty, ValueBoxes.BooleanBox(value));

public static bool GetAlignRightWithPanel(DependencyObject element)
=> (bool) element.GetValue(AlignRightWithPanelProperty);
=> (bool)element.GetValue(AlignRightWithPanelProperty);

public static readonly DependencyProperty AlignBottomWithPanelProperty = DependencyProperty.RegisterAttached(
"AlignBottomWithPanel", typeof(bool), typeof(RelativePanel), new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.AffectsRender));
Expand All @@ -54,7 +54,7 @@ public static void SetAlignBottomWithPanel(DependencyObject element, bool value)
=> element.SetValue(AlignBottomWithPanelProperty, ValueBoxes.BooleanBox(value));

public static bool GetAlignBottomWithPanel(DependencyObject element)
=> (bool) element.GetValue(AlignBottomWithPanelProperty);
=> (bool)element.GetValue(AlignBottomWithPanelProperty);

#endregion

Expand All @@ -68,7 +68,7 @@ public static void SetAlignLeftWith(DependencyObject element, UIElement value)

[TypeConverter(typeof(NameReferenceConverter))]
public static UIElement GetAlignLeftWith(DependencyObject element)
=> (UIElement) element.GetValue(AlignLeftWithProperty);
=> (UIElement)element.GetValue(AlignLeftWithProperty);

public static readonly DependencyProperty AlignTopWithProperty = DependencyProperty.RegisterAttached(
"AlignTopWith", typeof(UIElement), typeof(RelativePanel), new FrameworkPropertyMetadata(default(UIElement), FrameworkPropertyMetadataOptions.AffectsRender));
Expand All @@ -78,7 +78,7 @@ public static void SetAlignTopWith(DependencyObject element, UIElement value)

[TypeConverter(typeof(NameReferenceConverter))]
public static UIElement GetAlignTopWith(DependencyObject element)
=> (UIElement) element.GetValue(AlignTopWithProperty);
=> (UIElement)element.GetValue(AlignTopWithProperty);

public static readonly DependencyProperty AlignRightWithProperty = DependencyProperty.RegisterAttached(
"AlignRightWith", typeof(UIElement), typeof(RelativePanel), new FrameworkPropertyMetadata(default(UIElement), FrameworkPropertyMetadataOptions.AffectsRender));
Expand All @@ -88,7 +88,7 @@ public static void SetAlignRightWith(DependencyObject element, UIElement value)

[TypeConverter(typeof(NameReferenceConverter))]
public static UIElement GetAlignRightWith(DependencyObject element)
=> (UIElement) element.GetValue(AlignRightWithProperty);
=> (UIElement)element.GetValue(AlignRightWithProperty);

public static readonly DependencyProperty AlignBottomWithProperty = DependencyProperty.RegisterAttached(
"AlignBottomWith", typeof(UIElement), typeof(RelativePanel), new FrameworkPropertyMetadata(default(UIElement), FrameworkPropertyMetadataOptions.AffectsRender));
Expand All @@ -98,7 +98,7 @@ public static void SetAlignBottomWith(DependencyObject element, UIElement value)

[TypeConverter(typeof(NameReferenceConverter))]
public static UIElement GetAlignBottomWith(DependencyObject element)
=> (UIElement) element.GetValue(AlignBottomWithProperty);
=> (UIElement)element.GetValue(AlignBottomWithProperty);

#endregion

Expand All @@ -112,7 +112,7 @@ public static void SetLeftOf(DependencyObject element, UIElement value)

[TypeConverter(typeof(NameReferenceConverter))]
public static UIElement GetLeftOf(DependencyObject element)
=> (UIElement) element.GetValue(LeftOfProperty);
=> (UIElement)element.GetValue(LeftOfProperty);

public static readonly DependencyProperty AboveProperty = DependencyProperty.RegisterAttached(
"Above", typeof(UIElement), typeof(RelativePanel), new FrameworkPropertyMetadata(default(UIElement), FrameworkPropertyMetadataOptions.AffectsRender));
Expand All @@ -122,7 +122,7 @@ public static void SetAbove(DependencyObject element, UIElement value)

[TypeConverter(typeof(NameReferenceConverter))]
public static UIElement GetAbove(DependencyObject element)
=> (UIElement) element.GetValue(AboveProperty);
=> (UIElement)element.GetValue(AboveProperty);

public static readonly DependencyProperty RightOfProperty = DependencyProperty.RegisterAttached(
"RightOf", typeof(UIElement), typeof(RelativePanel), new FrameworkPropertyMetadata(default(UIElement), FrameworkPropertyMetadataOptions.AffectsRender));
Expand All @@ -132,7 +132,7 @@ public static void SetRightOf(DependencyObject element, UIElement value)

[TypeConverter(typeof(NameReferenceConverter))]
public static UIElement GetRightOf(DependencyObject element)
=> (UIElement) element.GetValue(RightOfProperty);
=> (UIElement)element.GetValue(RightOfProperty);

public static readonly DependencyProperty BelowProperty = DependencyProperty.RegisterAttached(
"Below", typeof(UIElement), typeof(RelativePanel), new FrameworkPropertyMetadata(default(UIElement), FrameworkPropertyMetadataOptions.AffectsRender));
Expand All @@ -142,7 +142,7 @@ public static void SetBelow(DependencyObject element, UIElement value)

[TypeConverter(typeof(NameReferenceConverter))]
public static UIElement GetBelow(DependencyObject element)
=> (UIElement) element.GetValue(BelowProperty);
=> (UIElement)element.GetValue(BelowProperty);

#endregion

Expand All @@ -155,7 +155,7 @@ public static void SetAlignHorizontalCenterWithPanel(DependencyObject element, b
=> element.SetValue(AlignHorizontalCenterWithPanelProperty, ValueBoxes.BooleanBox(value));

public static bool GetAlignHorizontalCenterWithPanel(DependencyObject element)
=> (bool) element.GetValue(AlignHorizontalCenterWithPanelProperty);
=> (bool)element.GetValue(AlignHorizontalCenterWithPanelProperty);

public static readonly DependencyProperty AlignVerticalCenterWithPanelProperty = DependencyProperty.RegisterAttached(
"AlignVerticalCenterWithPanel", typeof(bool), typeof(RelativePanel), new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.AffectsRender));
Expand All @@ -164,7 +164,7 @@ public static void SetAlignVerticalCenterWithPanel(DependencyObject element, boo
=> element.SetValue(AlignVerticalCenterWithPanelProperty, ValueBoxes.BooleanBox(value));

public static bool GetAlignVerticalCenterWithPanel(DependencyObject element)
=> (bool) element.GetValue(AlignVerticalCenterWithPanelProperty);
=> (bool)element.GetValue(AlignVerticalCenterWithPanelProperty);

public static readonly DependencyProperty AlignHorizontalCenterWithProperty = DependencyProperty.RegisterAttached(
"AlignHorizontalCenterWith", typeof(UIElement), typeof(RelativePanel), new FrameworkPropertyMetadata(default(UIElement), FrameworkPropertyMetadataOptions.AffectsRender));
Expand All @@ -174,7 +174,7 @@ public static void SetAlignHorizontalCenterWith(DependencyObject element, UIElem

[TypeConverter(typeof(NameReferenceConverter))]
public static UIElement GetAlignHorizontalCenterWith(DependencyObject element)
=> (UIElement) element.GetValue(AlignHorizontalCenterWithProperty);
=> (UIElement)element.GetValue(AlignHorizontalCenterWithProperty);

public static readonly DependencyProperty AlignVerticalCenterWithProperty = DependencyProperty.RegisterAttached(
"AlignVerticalCenterWith", typeof(UIElement), typeof(RelativePanel), new FrameworkPropertyMetadata(default(UIElement), FrameworkPropertyMetadataOptions.AffectsRender));
Expand All @@ -184,7 +184,7 @@ public static void SetAlignVerticalCenterWith(DependencyObject element, UIElemen

[TypeConverter(typeof(NameReferenceConverter))]
public static UIElement GetAlignVerticalCenterWith(DependencyObject element)
=> (UIElement) element.GetValue(AlignVerticalCenterWithProperty);
=> (UIElement)element.GetValue(AlignVerticalCenterWithProperty);

#endregion

Expand Down Expand Up @@ -218,7 +218,7 @@ protected override Size MeasureOverride(Size availableSize)
#region Calc AvailableSize

_childGraph.Reset(false);
var boundingSize = _childGraph.GetBoundingSize(Width.IsNaN(), Height.IsNaN());
var boundingSize = _childGraph.GetBoundingSize(Width.IsNaN(), Height.IsNaN(), availableSize);
_childGraph.Reset();
_childGraph.Measure(boundingSize);
return boundingSize;
Expand Down Expand Up @@ -454,6 +454,8 @@ private void Measure(IEnumerable<GraphNode> nodes, HashSet<DependencyObject> set

private void MeasureChild(GraphNode node)
{
var availableSize = AvailableSize;

var child = node.Element;
child.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
node.OriginDesiredSize = child.DesiredSize;
Expand Down Expand Up @@ -502,10 +504,10 @@ private void MeasureChild(GraphNode node)

#region Measure

var availableHeight = AvailableSize.Height - node.Top - node.Bottom;
var availableHeight = availableSize.Height - node.Top - node.Bottom;
if (availableHeight.IsNaN())
{
availableHeight = AvailableSize.Height;
availableHeight = availableSize.Height;

if (!node.Top.IsNaN() && node.Bottom.IsNaN())
{
Expand All @@ -517,10 +519,10 @@ private void MeasureChild(GraphNode node)
}
}

var availableWidth = AvailableSize.Width - node.Left - node.Right;
var availableWidth = availableSize.Width - node.Left - node.Right;
if (availableWidth.IsNaN())
{
availableWidth = AvailableSize.Width;
availableWidth = availableSize.Width;

if (!node.Left.IsNaN() && node.Right.IsNaN())
{
Expand Down Expand Up @@ -558,7 +560,7 @@ private void MeasureChild(GraphNode node)

if (node.Left.IsNaN())
{
node.Left = AvailableSize.Width - node.RightOfNode.Right;
node.Left = availableSize.Width - node.RightOfNode.Right;
}
}

Expand All @@ -571,7 +573,7 @@ private void MeasureChild(GraphNode node)

if (node.Top.IsNaN())
{
node.Top = AvailableSize.Height - node.BelowNode.Bottom;
node.Top = availableSize.Height - node.BelowNode.Bottom;
}
}

Expand All @@ -581,8 +583,8 @@ private void MeasureChild(GraphNode node)

if (node.AlignHorizontalCenterWith != null)
{
var halfWidthLeft = (AvailableSize.Width + node.AlignHorizontalCenterWith.Left - node.AlignHorizontalCenterWith.Right - childSize.Width) * 0.5;
var halfWidthRight = (AvailableSize.Width - node.AlignHorizontalCenterWith.Left + node.AlignHorizontalCenterWith.Right - childSize.Width) * 0.5;
var halfWidthLeft = (availableSize.Width + node.AlignHorizontalCenterWith.Left - node.AlignHorizontalCenterWith.Right - childSize.Width) * 0.5;
var halfWidthRight = (availableSize.Width - node.AlignHorizontalCenterWith.Left + node.AlignHorizontalCenterWith.Right - childSize.Width) * 0.5;

if (node.Left.IsNaN()) node.Left = halfWidthLeft;
else node.Left = (node.Left + halfWidthLeft) * 0.5;
Expand All @@ -593,8 +595,8 @@ private void MeasureChild(GraphNode node)

if (node.AlignVerticalCenterWith != null)
{
var halfHeightTop = (AvailableSize.Height + node.AlignVerticalCenterWith.Top - node.AlignVerticalCenterWith.Bottom - childSize.Height) * 0.5;
var halfHeightBottom = (AvailableSize.Height - node.AlignVerticalCenterWith.Top + node.AlignVerticalCenterWith.Bottom - childSize.Height) * 0.5;
var halfHeightTop = (availableSize.Height + node.AlignVerticalCenterWith.Top - node.AlignVerticalCenterWith.Bottom - childSize.Height) * 0.5;
var halfHeightBottom = (availableSize.Height - node.AlignVerticalCenterWith.Top + node.AlignVerticalCenterWith.Bottom - childSize.Height) * 0.5;

if (node.Top.IsNaN()) node.Top = halfHeightTop;
else node.Top = (node.Top + halfHeightTop) * 0.5;
Expand All @@ -609,7 +611,7 @@ private void MeasureChild(GraphNode node)

if (GetAlignHorizontalCenterWithPanel(child))
{
var halfSubWidth = (AvailableSize.Width - childSize.Width) * 0.5;
var halfSubWidth = (availableSize.Width - childSize.Width) * 0.5;

if (node.Left.IsNaN()) node.Left = halfSubWidth;
else node.Left = (node.Left + halfSubWidth) * 0.5;
Expand All @@ -620,7 +622,7 @@ private void MeasureChild(GraphNode node)

if (GetAlignVerticalCenterWithPanel(child))
{
var halfSubHeight = (AvailableSize.Height - childSize.Height) * 0.5;
var halfSubHeight = (availableSize.Height - childSize.Height) * 0.5;

if (node.Top.IsNaN()) node.Top = halfSubHeight;
else node.Top = (node.Top + halfSubHeight) * 0.5;
Expand All @@ -634,37 +636,37 @@ private void MeasureChild(GraphNode node)
if (node.Left.IsNaN())
{
if (!node.Right.IsNaN())
node.Left = AvailableSize.Width - node.Right - childSize.Width;
node.Left = availableSize.Width - node.Right - childSize.Width;
else
{
node.Left = 0;
node.Right = AvailableSize.Width - childSize.Width;
node.Right = availableSize.Width - childSize.Width;
}
}
else if (!node.Left.IsNaN() && node.Right.IsNaN())
{
node.Right = AvailableSize.Width - node.Left - childSize.Width;
node.Right = availableSize.Width - node.Left - childSize.Width;
}

if (node.Top.IsNaN())
{
if (!node.Bottom.IsNaN())
node.Top = AvailableSize.Height - node.Bottom - childSize.Height;
node.Top = availableSize.Height - node.Bottom - childSize.Height;
else
{
node.Top = 0;
node.Bottom = AvailableSize.Height - childSize.Height;
node.Bottom = availableSize.Height - childSize.Height;
}
}
else if (!node.Top.IsNaN() && node.Bottom.IsNaN())
{
node.Bottom = AvailableSize.Height - node.Top - childSize.Height;
node.Bottom = availableSize.Height - node.Top - childSize.Height;
}

node.Measured = true;
}

public Size GetBoundingSize(bool calcWidth, bool calcHeight)
public Size GetBoundingSize(bool calcWidth, bool calcHeight, Size availableSize)
{
var boundingSize = new Size();

Expand All @@ -675,8 +677,8 @@ public Size GetBoundingSize(bool calcWidth, bool calcHeight)
boundingSize.Height = Math.Max(boundingSize.Height, size.Height);
}

boundingSize.Width = calcWidth ? boundingSize.Width : AvailableSize.Width;
boundingSize.Height = calcHeight ? boundingSize.Height : AvailableSize.Height;
boundingSize.Width = calcWidth ? Math.Max(availableSize.Width, boundingSize.Width) : availableSize.Width;
boundingSize.Height = calcHeight ? Math.Max(availableSize.Height, boundingSize.Height) : availableSize.Height;
return boundingSize;
}
}
Expand Down

0 comments on commit f4d4bdd

Please sign in to comment.